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
|
// 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.
#include "third_party/blink/renderer/modules/clipboard/clipboard.h"
#include <utility>
#include "net/base/mime_util.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/event_target_names.h"
#include "third_party/blink/renderer/core/event_type_names.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_promise.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "ui/base/clipboard/clipboard_constants.h"
#include "ui/base/ui_base_features.h"
namespace blink {
// static
const char Clipboard::kSupplementName[] = "Clipboard";
Clipboard* Clipboard::clipboard(Navigator& navigator) {
Clipboard* clipboard = Supplement<Navigator>::From<Clipboard>(navigator);
if (!clipboard) {
clipboard = MakeGarbageCollected<Clipboard>(navigator);
ProvideTo(navigator, clipboard);
}
return clipboard;
}
Clipboard::Clipboard(Navigator& navigator) : Supplement<Navigator>(navigator) {}
ScriptPromise<IDLSequence<ClipboardItem>> Clipboard::read(
ScriptState* script_state,
ClipboardUnsanitizedFormats* formats,
ExceptionState& exception_state) {
LocalDOMWindow* window = GetSupplementable()->DomWindow();
LocalFrame* local_frame = window ? window->GetFrame() : nullptr;
if (local_frame && local_frame->IsAdScriptInStack()) {
UseCounter::Count(GetExecutionContext(),
WebFeature::kAdScriptInStackOnClipboardRead);
}
return ClipboardPromise::CreateForRead(GetExecutionContext(), script_state,
formats, exception_state);
}
ScriptPromise<IDLString> Clipboard::readText(ScriptState* script_state,
ExceptionState& exception_state) {
LocalDOMWindow* window = GetSupplementable()->DomWindow();
LocalFrame* local_frame = window ? window->GetFrame() : nullptr;
if (local_frame && local_frame->IsAdScriptInStack()) {
UseCounter::Count(GetExecutionContext(),
WebFeature::kAdScriptInStackOnClipboardRead);
}
return ClipboardPromise::CreateForReadText(GetExecutionContext(),
script_state, exception_state);
}
void Clipboard::AddedEventListener(
const AtomicString& event_type,
RegisteredEventListener& registered_listener) {
EventTarget::AddedEventListener(event_type, registered_listener);
if (!base::FeatureList::IsEnabled(features::kClipboardChangeEvent) ||
event_type != event_type_names::kClipboardchange) {
return;
}
UseCounter::Count(GetExecutionContext(),
WebFeature::kClipboardChangeEventAddListener);
if (!clipboard_change_event_controller_) {
Navigator& navigator = *GetSupplementable();
if (navigator.DomWindow()) {
clipboard_change_event_controller_ =
MakeGarbageCollected<ClipboardChangeEventController>(navigator, this);
}
}
if (clipboard_change_event_controller_) {
clipboard_change_event_controller_->RegisterWithDispatcher();
}
}
void Clipboard::RemovedEventListener(
const AtomicString& event_type,
const RegisteredEventListener& registered_listener) {
EventTarget::RemovedEventListener(event_type, registered_listener);
if (!base::FeatureList::IsEnabled(features::kClipboardChangeEvent) ||
event_type != event_type_names::kClipboardchange) {
return;
}
if (clipboard_change_event_controller_ &&
!HasEventListeners(event_type_names::kClipboardchange)) {
clipboard_change_event_controller_->UnregisterWithDispatcher();
}
}
ScriptPromise<IDLUndefined> Clipboard::write(
ScriptState* script_state,
const HeapVector<Member<ClipboardItem>>& data,
ExceptionState& exception_state) {
return ClipboardPromise::CreateForWrite(GetExecutionContext(), script_state,
std::move(data), exception_state);
}
ScriptPromise<IDLUndefined> Clipboard::writeText(
ScriptState* script_state,
const String& data,
ExceptionState& exception_state) {
return ClipboardPromise::CreateForWriteText(
GetExecutionContext(), script_state, data, exception_state);
}
const AtomicString& Clipboard::InterfaceName() const {
return event_target_names::kClipboard;
}
ExecutionContext* Clipboard::GetExecutionContext() const {
return GetSupplementable()->DomWindow();
}
// static
String Clipboard::ParseWebCustomFormat(const String& format) {
if (format.StartsWith(ui::kWebClipboardFormatPrefix)) {
String web_custom_format_suffix = format.Substring(
static_cast<unsigned>(std::strlen(ui::kWebClipboardFormatPrefix)));
std::string web_top_level_mime_type;
std::string web_mime_sub_type;
if (net::ParseMimeTypeWithoutParameter(web_custom_format_suffix.Utf8(),
&web_top_level_mime_type,
&web_mime_sub_type)) {
return String::Format("%s/%s", web_top_level_mime_type.c_str(),
web_mime_sub_type.c_str());
}
}
return g_empty_string;
}
void Clipboard::Trace(Visitor* visitor) const {
EventTarget::Trace(visitor);
Supplement<Navigator>::Trace(visitor);
visitor->Trace(clipboard_change_event_controller_);
}
} // namespace blink
|