1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CLIPBOARD_CLIPBOARD_PROMISE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_CLIPBOARD_CLIPBOARD_PROMISE_H_
#include <utility>
#include "base/sequence_checker.h"
#include "base/task/single_thread_task_runner.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_blob_string.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/fileapi/blob.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_item.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_reader.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
namespace blink {
class ClipboardWriter;
class LocalFrame;
class ExceptionState;
class ExecutionContext;
class ClipboardUnsanitizedFormats;
// Represents a promise to execute Async Clipboard API functions off the main
// thread. It handles read and write operations on the clipboard, including
// reading and writing text and blobs for different MIME types. This class also
// interacts with the `PermissionService` to check for read and write
// permissions. It uses a `ClipboardItem` object to read/write supported MIME
// types. Spec: https://w3c.github.io/clipboard-apis/#async-clipboard-api
class MODULES_EXPORT ClipboardPromise final
: public GarbageCollected<ClipboardPromise>,
public ExecutionContextLifecycleObserver {
public:
// Creates a promise for reading clipboard data.
// Spec: https://w3c.github.io/clipboard-apis/#dom-clipboard-read
// `formats`: Unsanitized formats to be read from the clipboard.
// Spec:
// https://w3c.github.io/clipboard-apis/#dom-clipboardunsanitizedformats-unsanitized
static ScriptPromise<IDLSequence<ClipboardItem>> CreateForRead(
ExecutionContext* execution_context,
ScriptState* script_state,
ClipboardUnsanitizedFormats* formats,
ExceptionState& exception_state);
// Creates a promise for reading plain text from the clipboard.
// Spec: https://w3c.github.io/clipboard-apis/#dom-clipboard-readtext
static ScriptPromise<IDLString> CreateForReadText(
ExecutionContext* execution_context,
ScriptState* script_state,
ExceptionState& exception_state);
// Creates a promise for writing supported MIME types to the clipboard.
// Spec: https://w3c.github.io/clipboard-apis/#dom-clipboard-write
static ScriptPromise<IDLUndefined> CreateForWrite(
ExecutionContext* execution_context,
ScriptState* script_state,
const HeapVector<Member<ClipboardItem>>& items,
ExceptionState& exception_state);
// Creates a promise for writing text to the clipboard.
// `text`: The text to be written to the clipboard.
// Spec: https://w3c.github.io/clipboard-apis/#dom-clipboard-writetext
static ScriptPromise<IDLUndefined> CreateForWriteText(
ExecutionContext* execution_context,
ScriptState* script_state,
const String& text,
ExceptionState& exception_state);
// Use one of the above factories to construct. This ctor is public for
// `MakeGarbageCollected<>`.
ClipboardPromise(ExecutionContext* execution_context,
ScriptPromiseResolverBase*,
ExceptionState& exception_state);
ClipboardPromise(const ClipboardPromise&) = delete;
ClipboardPromise& operator=(const ClipboardPromise&) = delete;
~ClipboardPromise() override;
// Finishes writing the current representation and prepares for the next one.
void CompleteWriteRepresentation();
// Handles rejections originating from the ClipboardWriter.
void RejectFromReadOrDecodeFailure();
// Adds the given `blob` to the `clipboard_item_data_`.
void OnRead(Blob* blob);
// Returns the local frame associated with the promise.
LocalFrame* GetLocalFrame() const;
// Returns the script state associated with the promise.
ScriptState* GetScriptState() const;
// ExecutionContextLifecycleObserver
void Trace(Visitor* visitor) const override;
private:
class ClipboardItemDataPromiseFulfill;
class ClipboardItemDataPromiseReject;
void HandlePromiseWrite(
GCedHeapVector<Member<V8UnionBlobOrString>>* clipboard_item_list);
void WriteClipboardItemData(
GCedHeapVector<Member<V8UnionBlobOrString>>* clipboard_item_list);
// Rejects the promise for blobs that have invalid MIME types or got rejected
// with the given exception.
void RejectClipboardItemPromise(ScriptValue);
void WriteNextRepresentation();
// Checks Read/Write permission (interacting with `PermissionService`).
void HandleRead(ClipboardUnsanitizedFormats* formats);
void HandleReadText();
void HandleWrite(const HeapVector<Member<ClipboardItem>>& items);
void HandleWriteText(const String& text);
// Reads/Writes after permission check.
void HandleReadWithPermission(mojom::blink::PermissionStatus permission);
void HandleReadTextWithPermission(mojom::blink::PermissionStatus permission);
void HandleWriteWithPermission(mojom::blink::PermissionStatus permission);
void HandleWriteTextWithPermission(mojom::blink::PermissionStatus permission);
// Callback function called when the available format names for reading are
// received from the clipboard.
// `format_names`: The available format names on the clipboard
void OnReadAvailableFormatNames(const Vector<String>& format_names);
// Reads the next clipboard representation.
void ReadNextRepresentation();
// Resolves the read promise.
void ResolveRead();
// Returns the `PermissionService` associated with the promise, or nullptr if
// the remote connection fails.
mojom::blink::PermissionService* GetPermissionService();
// Validates that the action may proceed, including but not limited to
// requesting permissions from the `PermissionService` as necessary.
// On failure, will reject via `script_promise_resolver_`.
//
// `permission`: The permission to request.
// `will_be_sanitized`: Whether the data will be sanitized.
// `callback`: The callback function to be called with the permission status.
void ValidatePreconditions(
mojom::blink::PermissionName permission,
bool will_be_sanitized,
base::OnceCallback<void(mojom::blink::PermissionStatus)> callback);
scoped_refptr<base::SingleThreadTaskRunner> GetClipboardTaskRunner();
// ExecutionContextLifecycleObserver
void ContextDestroyed() override;
Member<ScriptPromiseResolverBase> script_promise_resolver_;
Member<ClipboardWriter> clipboard_writer_;
HeapMojoRemote<mojom::blink::PermissionService> permission_service_;
// When true, the HTML data read from the clipboard will be unprocessed.
// When false, the HTML data is processed by the fragment parser.
bool will_read_unprocessed_html_ = false;
// Plain text data to be written to the clipboard.
String plain_text_;
// The list of formats read from the clipboard.
HeapVector<std::pair<String, Member<V8UnionBlobOrString>>>
clipboard_item_data_;
// The list of formats with their corresponding promises to the Blob data to
// be written to the clipboard.
HeapVector<std::pair<String, MemberScriptPromise<V8UnionBlobOrString>>>
clipboard_item_data_with_promises_;
wtf_size_t clipboard_representation_index_ = 0;
// List of custom format with "web " prefix.
Vector<String> write_custom_format_types_;
// Stores the types provided by the web authors.
Vector<String> write_clipboard_item_types_;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_CLIPBOARD_CLIPBOARD_PROMISE_H_
|