Clipboard.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibTextCodec/Decoder.h>
  8. #include <LibWeb/Bindings/HostDefined.h>
  9. #include <LibWeb/Clipboard/Clipboard.h>
  10. #include <LibWeb/FileAPI/Blob.h>
  11. #include <LibWeb/HTML/Scripting/Environments.h>
  12. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  13. #include <LibWeb/HTML/Window.h>
  14. #include <LibWeb/MimeSniff/MimeType.h>
  15. #include <LibWeb/Page/Page.h>
  16. #include <LibWeb/Platform/EventLoopPlugin.h>
  17. #include <LibWeb/WebIDL/Promise.h>
  18. namespace Web::Clipboard {
  19. JS_DEFINE_ALLOCATOR(Clipboard);
  20. WebIDL::ExceptionOr<JS::NonnullGCPtr<Clipboard>> Clipboard::construct_impl(JS::Realm& realm)
  21. {
  22. return realm.heap().allocate<Clipboard>(realm, realm);
  23. }
  24. Clipboard::Clipboard(JS::Realm& realm)
  25. : DOM::EventTarget(realm)
  26. {
  27. }
  28. Clipboard::~Clipboard() = default;
  29. void Clipboard::initialize(JS::Realm& realm)
  30. {
  31. Base::initialize(realm);
  32. WEB_SET_PROTOTYPE_FOR_INTERFACE(Clipboard);
  33. }
  34. // https://w3c.github.io/clipboard-apis/#os-specific-well-known-format
  35. static String os_specific_well_known_format(StringView mime_type_string)
  36. {
  37. // NOTE: Here we always takes the Linux case, and defer to the chrome layer to handle OS specific implementations.
  38. auto mime_type = MUST(MimeSniff::MimeType::parse(mime_type_string));
  39. // 1. Let wellKnownFormat be an empty string.
  40. String well_known_format {};
  41. // 2. If mimeType’s essence is "text/plain", then
  42. if (mime_type->essence() == "text/plain"sv) {
  43. // On Windows, follow the convention described below:
  44. // Assign CF_UNICODETEXT to wellKnownFormat.
  45. // On MacOS, follow the convention described below:
  46. // Assign NSPasteboardTypeString to wellKnownFormat.
  47. // On Linux, ChromeOS, and Android, follow the convention described below:
  48. // Assign "text/plain" to wellKnownFormat.
  49. well_known_format = "text/plain"_string;
  50. }
  51. // 3. Else, if mimeType’s essence is "text/html", then
  52. if (mime_type->essence() == "text/html"sv) {
  53. // On Windows, follow the convention described below:
  54. // Assign CF_HTML to wellKnownFormat.
  55. // On MacOS, follow the convention described below:
  56. // Assign NSHTMLPboardType to wellKnownFormat.
  57. // On Linux, ChromeOS, and Android, follow the convention described below:
  58. // Assign "text/html" to wellKnownFormat.
  59. well_known_format = "text/html"_string;
  60. }
  61. // 4. Else, if mimeType’s essence is "image/png", then
  62. if (mime_type->essence() == "image/png"sv) {
  63. // On Windows, follow the convention described below:
  64. // Assign "PNG" to wellKnownFormat.
  65. // On MacOS, follow the convention described below:
  66. // Assign NSPasteboardTypePNG to wellKnownFormat.
  67. // On Linux, ChromeOS, and Android, follow the convention described below:
  68. // Assign "image/png" to wellKnownFormat.
  69. well_known_format = "image/png"_string;
  70. }
  71. // 5. Return wellKnownFormat.
  72. return well_known_format;
  73. }
  74. // https://w3c.github.io/clipboard-apis/#write-blobs-and-option-to-the-clipboard
  75. static void write_blobs_and_option_to_clipboard(JS::Realm& realm, ReadonlySpan<JS::NonnullGCPtr<FileAPI::Blob>> items, String presentation_style)
  76. {
  77. auto& window = verify_cast<HTML::Window>(realm.global_object());
  78. // FIXME: 1. Let webCustomFormats be a sequence<Blob>.
  79. // 2. For each item in items:
  80. for (auto const& item : items) {
  81. // 1. Let formatString be the result of running os specific well-known format given item’s type.
  82. auto format_string = os_specific_well_known_format(item->type());
  83. // 2. If formatString is empty then follow the below steps:
  84. if (format_string.is_empty()) {
  85. // FIXME: 1. Let webCustomFormatString be the item’s type.
  86. // FIXME: 2. Let webCustomFormat be an empty type.
  87. // FIXME: 3. If webCustomFormatString starts with `"web "` prefix, then remove the `"web "` prefix and store the
  88. // FIXME: remaining string in webMimeTypeString.
  89. // FIXME: 4. Let webMimeType be the result of parsing a MIME type given webMimeTypeString.
  90. // FIXME: 5. If webMimeType is failure, then abort all steps.
  91. // FIXME: 6. Let webCustomFormat’s type's essence equal to webMimeType.
  92. // FIXME: 7. Set item’s type to webCustomFormat.
  93. // FIXME: 8. Append webCustomFormat to webCustomFormats.
  94. }
  95. // 3. Let payload be the result of UTF-8 decoding item’s underlying byte sequence.
  96. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  97. auto payload = MUST(TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, item->bytes()));
  98. // 4. Insert payload and presentationStyle into the system clipboard using formatString as the native clipboard format.
  99. window.page().client().page_did_insert_clipboard_entry(move(payload), move(presentation_style), move(format_string));
  100. }
  101. // FIXME: 3. Write web custom formats given webCustomFormats.
  102. }
  103. // https://w3c.github.io/clipboard-apis/#check-clipboard-write-permission
  104. static bool check_clipboard_write_permission(JS::Realm& realm)
  105. {
  106. // NOTE: The clipboard permission is undergoing a refactor because the clipboard-write permission was removed from
  107. // the Permissions spec. So this partially implements the proposed update:
  108. // https://pr-preview.s3.amazonaws.com/w3c/clipboard-apis/pull/164.html#write-permission
  109. // 1. Let hasGesture be true if the relevant global object of this has transient activation, false otherwise.
  110. auto has_gesture = verify_cast<HTML::Window>(realm.global_object()).has_transient_activation();
  111. // 2. If hasGesture then,
  112. if (has_gesture) {
  113. // FIXME: 1. Return true if the current script is running as a result of user interaction with a "cut" or "copy"
  114. // element created by the user agent or operating system.
  115. return true;
  116. }
  117. // 3. Otherwise, return false.
  118. return false;
  119. }
  120. // https://w3c.github.io/clipboard-apis/#dom-clipboard-writetext
  121. JS::NonnullGCPtr<JS::Promise> Clipboard::write_text(String data)
  122. {
  123. // 1. Let realm be this's relevant realm.
  124. auto& realm = HTML::relevant_realm(*this);
  125. // 2. Let p be a new promise in realm.
  126. auto promise = WebIDL::create_promise(realm);
  127. // 3. Run the following steps in parallel:
  128. Platform::EventLoopPlugin::the().deferred_invoke([&realm, promise, data = move(data)]() mutable {
  129. // 1. Let r be the result of running check clipboard write permission.
  130. auto result = check_clipboard_write_permission(realm);
  131. // 2. If r is false, then:
  132. if (!result) {
  133. // 1. Queue a global task on the permission task source, given realm’s global object, to reject p with
  134. // "NotAllowedError" DOMException in realm.
  135. queue_global_task(HTML::Task::Source::Permissions, realm.global_object(), JS::create_heap_function(realm.heap(), [&realm, promise]() mutable {
  136. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  137. WebIDL::reject_promise(realm, promise, WebIDL::NotAllowedError::create(realm, "Clipboard writing is only allowed through user activation"_fly_string));
  138. }));
  139. // 2. Abort these steps.
  140. return;
  141. }
  142. // 1. Queue a global task on the clipboard task source, given realm’s global object, to perform the below steps:
  143. queue_global_task(HTML::Task::Source::Clipboard, realm.global_object(), JS::create_heap_function(realm.heap(), [&realm, promise, data = move(data)]() mutable {
  144. // 1. Let itemList be an empty sequence<Blob>.
  145. Vector<JS::NonnullGCPtr<FileAPI::Blob>> item_list;
  146. // 2. Let textBlob be a new Blob created with: type attribute set to "text/plain;charset=utf-8", and its
  147. // underlying byte sequence set to the UTF-8 encoding of data.
  148. // Note: On Windows replace `\n` characters with `\r\n` in data before creating textBlob.
  149. auto text_blob = FileAPI::Blob::create(realm, MUST(ByteBuffer::copy(data.bytes())), "text/plain;charset=utf-8"_string);
  150. // 3. Add textBlob to itemList.
  151. item_list.append(text_blob);
  152. // 4. Let option be set to "unspecified".
  153. auto option = "unspecified"_string;
  154. // 5. Write blobs and option to the clipboard with itemList and option.
  155. write_blobs_and_option_to_clipboard(realm, item_list, move(option));
  156. // 6. Resolve p.
  157. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  158. WebIDL::resolve_promise(realm, promise, JS::js_undefined());
  159. }));
  160. });
  161. // 4. Return p.
  162. return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise->promise()) };
  163. }
  164. }