Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Unified Diff: third_party/WebKit/Source/core/clipboard/ClipboardAsync.cpp

Issue 2695593006: Initial stub version of Async Clipboard API (Closed)
Patch Set: Add WPTs; Update naming conventions; Review comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/clipboard/ClipboardAsync.cpp
diff --git a/third_party/WebKit/Source/core/clipboard/ClipboardAsync.cpp b/third_party/WebKit/Source/core/clipboard/ClipboardAsync.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..3ae1f56f149f20b7d7d58da26e729f97d9641e08
--- /dev/null
+++ b/third_party/WebKit/Source/core/clipboard/ClipboardAsync.cpp
@@ -0,0 +1,111 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/clipboard/ClipboardAsync.h"
+
+#include "bindings/core/v8/ScriptPromiseResolver.h"
+#include "core/clipboard/DataObject.h"
+#include "core/clipboard/DataTransfer.h"
+#include "core/clipboard/DataTransferItem.h"
+#include "core/clipboard/DataTransferItemList.h"
+#include "core/dom/TaskRunnerHelper.h"
+#include "platform/CrossThreadFunctional.h"
+#include "public/platform/Platform.h"
+
+namespace blink {
+
+ClipboardAsync* ClipboardAsync::create(ScriptState* scriptState) {
+ return new ClipboardAsync(scriptState);
+}
+
+ClipboardAsync::ClipboardAsync(ScriptState* scriptState)
+ : ContextLifecycleObserver(blink::ExecutionContext::From(scriptState)),
+ m_scriptPromiseResolver(ScriptPromiseResolver::Create(scriptState)),
+ m_buffer(WebClipboard::kBufferStandard) {}
dcheng 2017/05/13 22:18:32 Out of curiosity, do you see this being used for n
garykac 2017/05/17 23:51:14 Probably not. So I removed CORE_EXPORT from the .h
+
+WebTaskRunner* ClipboardAsync::getTaskRunner() {
+ // TODO(garykac): Replace MiscPlatformAPI with TaskType specific to clipboard.
+ return TaskRunnerHelper::Get(TaskType::kMiscPlatformAPI,
+ GetExecutionContext())
+ .Get();
+}
+
+ScriptPromise ClipboardAsync::scheduleRead() {
+ getTaskRunner()->PostTask(
+ BLINK_FROM_HERE,
+ WTF::Bind(&ClipboardAsync::handleRead, WrapPersistent(this)));
+ return m_scriptPromiseResolver->Promise();
+}
+
+ScriptPromise ClipboardAsync::scheduleReadText() {
+ getTaskRunner()->PostTask(
+ BLINK_FROM_HERE,
+ WTF::Bind(&ClipboardAsync::handleReadText, WrapPersistent(this)));
+ return m_scriptPromiseResolver->Promise();
+}
+
+ScriptPromise ClipboardAsync::scheduleWrite(DataTransfer* data) {
+ getTaskRunner()->PostTask(
+ BLINK_FROM_HERE, WTF::Bind(&ClipboardAsync::handleWrite,
+ WrapPersistent(this), WrapPersistent(data)));
+ return m_scriptPromiseResolver->Promise();
+}
+
+ScriptPromise ClipboardAsync::scheduleWriteText(const String& data) {
+ getTaskRunner()->PostTask(
+ BLINK_FROM_HERE,
+ WTF::Bind(&ClipboardAsync::handleWriteText, WrapPersistent(this), data));
+ return m_scriptPromiseResolver->Promise();
+}
+
+// TODO(garykac): This currently only handles plain text.
+void ClipboardAsync::handleRead() {
+ String plainText = Platform::Current()->Clipboard()->ReadPlainText(m_buffer);
+
+ DataTransfer::DataTransferType type =
+ DataTransfer::DataTransferType::kCopyAndPaste;
+ DataTransferAccessPolicy access =
+ DataTransferAccessPolicy::kDataTransferReadable;
+ DataObject* data = DataObject::CreateFromString(plainText);
+ DataTransfer* dt = DataTransfer::Create(type, access, data);
+ m_scriptPromiseResolver->Resolve(dt);
+}
+
+void ClipboardAsync::handleReadText() {
+ String text = Platform::Current()->Clipboard()->ReadPlainText(m_buffer);
+ m_scriptPromiseResolver->Resolve(text);
+}
+
+// TODO(garykac): This currently only handles plain text.
+void ClipboardAsync::handleWrite(DataTransfer* data) {
+ size_t nItems = data->items()->length();
+ for (unsigned long i = 0; i < nItems; i++) {
+ DataTransferItem* item = data->items()->item(i);
+ DataObjectItem* objectItem = item->GetDataObjectItem();
+ if (objectItem->Kind() == DataObjectItem::kStringKind &&
+ objectItem->GetType() == "text/plain") {
+ String text = objectItem->GetAsString();
+ Platform::Current()->Clipboard()->WritePlainText(text);
+ m_scriptPromiseResolver->Resolve();
+ return;
+ }
+ }
+ m_scriptPromiseResolver->Reject();
+}
+
+void ClipboardAsync::handleWriteText(const String& data) {
+ Platform::Current()->Clipboard()->WritePlainText(data);
+ m_scriptPromiseResolver->Resolve();
+}
+
+void ClipboardAsync::ContextDestroyed(ExecutionContext*) {
+ m_scriptPromiseResolver.Clear();
dcheng 2017/05/13 22:18:32 As far as I can tell, we don't check that the m_sc
garykac 2017/05/17 23:51:14 AIUI, the resolver sticks around until garbage col
dcheng 2017/06/06 23:33:38 Right--but if we call clear() here, Member<ScriptP
garykac 2017/06/09 20:38:59 OK. I've removed the entire ContextDestroyed metho
+}
+
+DEFINE_TRACE(ClipboardAsync) {
+ visitor->Trace(m_scriptPromiseResolver);
+ ContextLifecycleObserver::Trace(visitor);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698