DataTransferItem.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/HeapFunction.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/DataTransferItemPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/FileAPI/File.h>
  11. #include <LibWeb/HTML/DataTransfer.h>
  12. #include <LibWeb/HTML/DataTransferItem.h>
  13. #include <LibWeb/WebIDL/AbstractOperations.h>
  14. #include <LibWeb/WebIDL/CallbackType.h>
  15. namespace Web::HTML {
  16. JS_DEFINE_ALLOCATOR(DataTransferItem);
  17. JS::NonnullGCPtr<DataTransferItem> DataTransferItem::create(JS::Realm& realm, JS::NonnullGCPtr<DataTransfer> data_transfer, size_t item_index)
  18. {
  19. return realm.heap().allocate<DataTransferItem>(realm, realm, data_transfer, item_index);
  20. }
  21. DataTransferItem::DataTransferItem(JS::Realm& realm, JS::NonnullGCPtr<DataTransfer> data_transfer, size_t item_index)
  22. : PlatformObject(realm)
  23. , m_data_transfer(data_transfer)
  24. , m_item_index(item_index)
  25. {
  26. }
  27. DataTransferItem::~DataTransferItem() = default;
  28. void DataTransferItem::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. WEB_SET_PROTOTYPE_FOR_INTERFACE(DataTransferItem);
  32. }
  33. void DataTransferItem::visit_edges(JS::Cell::Visitor& visitor)
  34. {
  35. Base::visit_edges(visitor);
  36. visitor.visit(m_data_transfer);
  37. }
  38. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitem-kind
  39. String DataTransferItem::kind() const
  40. {
  41. // The kind attribute must return the empty string if the DataTransferItem object is in the disabled mode; otherwise
  42. // it must return the string given in the cell from the second column of the following table from the row whose cell
  43. // in the first column contains the drag data item kind of the item represented by the DataTransferItem object:
  44. //
  45. // Kind | String
  46. // ---------------
  47. // Text | "string"
  48. // File | "file"
  49. if (!mode().has_value())
  50. return {};
  51. auto const& item = m_data_transfer->drag_data(*m_item_index);
  52. switch (item.kind) {
  53. case DragDataStoreItem::Kind::Text:
  54. return "string"_string;
  55. case DragDataStoreItem::Kind::File:
  56. return "file"_string;
  57. }
  58. VERIFY_NOT_REACHED();
  59. }
  60. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitem-type
  61. String DataTransferItem::type() const
  62. {
  63. // The type attribute must return the empty string if the DataTransferItem object is in the disabled mode; otherwise
  64. // it must return the drag data item type string of the item represented by the DataTransferItem object.
  65. if (!mode().has_value())
  66. return {};
  67. auto const& item = m_data_transfer->drag_data(*m_item_index);
  68. return item.type_string;
  69. }
  70. Optional<DragDataStore::Mode> DataTransferItem::mode() const
  71. {
  72. if (!m_item_index.has_value())
  73. return {};
  74. return m_data_transfer->mode();
  75. }
  76. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitem-getasstring
  77. void DataTransferItem::get_as_string(JS::GCPtr<WebIDL::CallbackType> callback) const
  78. {
  79. auto& realm = this->realm();
  80. auto& vm = realm.vm();
  81. // 1. If the callback is null, return.
  82. if (!callback)
  83. return;
  84. // 2. If the DataTransferItem object is not in the read/write mode or the read-only mode, return. The callback is
  85. // never invoked.
  86. if (mode() != DragDataStore::Mode::ReadWrite && mode() != DragDataStore::Mode::ReadOnly)
  87. return;
  88. auto const& item = m_data_transfer->drag_data(*m_item_index);
  89. // 3. If the drag data item kind is not text, then return. The callback is never invoked.
  90. if (item.kind != DragDataStoreItem::Kind::Text)
  91. return;
  92. // 4. Otherwise, queue a task to invoke callback, passing the actual data of the item represented by the
  93. // DataTransferItem object as the argument.
  94. auto data = JS::PrimitiveString::create(vm, MUST(String::from_utf8({ item.data })));
  95. HTML::queue_a_task(HTML::Task::Source::Unspecified, nullptr, nullptr,
  96. JS::HeapFunction<void()>::create(realm.heap(), [callback, data]() {
  97. (void)WebIDL::invoke_callback(*callback, {}, data);
  98. }));
  99. }
  100. // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransferitem-getasfile
  101. JS::GCPtr<FileAPI::File> DataTransferItem::get_as_file() const
  102. {
  103. auto& realm = this->realm();
  104. // 1. If the DataTransferItem object is not in the read/write mode or the read-only mode, then return null
  105. if (mode() != DragDataStore::Mode::ReadWrite && mode() != DragDataStore::Mode::ReadOnly)
  106. return nullptr;
  107. auto const& item = m_data_transfer->drag_data(*m_item_index);
  108. // 2. If the drag data item kind is not File, then return null.
  109. if (item.kind != DragDataStoreItem::Kind::File)
  110. return nullptr;
  111. // 3. Return a new File object representing the actual data of the item represented by the DataTransferItem object.
  112. auto blob = FileAPI::Blob::create(realm, item.data, item.type_string);
  113. // FIXME: The FileAPI should use ByteString for file names.
  114. auto file_name = MUST(String::from_byte_string(item.file_name));
  115. // FIXME: Fill in other fields (e.g. last_modified).
  116. FileAPI::FilePropertyBag options {};
  117. options.type = item.type_string;
  118. return MUST(FileAPI::File::create(realm, { JS::make_handle(blob) }, file_name, move(options)));
  119. }
  120. }