DataTransferItem.cpp 3.9 KB

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