DataTransferItem.cpp 6.2 KB

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