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

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

Issue 2695593006: Initial stub version of Async Clipboard API (Closed)
Patch Set: Created 3 years, 10 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..a7cc8633f491f6b541efe735bcf440b8e6c823f1
--- /dev/null
+++ b/third_party/WebKit/Source/core/clipboard/ClipboardAsync.cpp
@@ -0,0 +1,110 @@
+// 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(scriptState->getExecutionContext()),
+ m_scriptPromiseResolver(ScriptPromiseResolver::create(scriptState)),
+ m_buffer(WebClipboard::BufferStandard) {}
+
+WebTaskRunner* ClipboardAsync::getTaskRunner() {
+ // TODO(garykac): Replace MiscPlatformAPI with TaskType specific to clipboard.
+ return TaskRunnerHelper::get(TaskType::MiscPlatformAPI, 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::CopyAndPaste;
+ DataTransferAccessPolicy access =
+ DataTransferAccessPolicy::DataTransferReadable;
+ 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::StringKind &&
+ objectItem->type() == "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();
+}
+
+DEFINE_TRACE(ClipboardAsync) {
+ visitor->trace(m_scriptPromiseResolver);
+ ContextLifecycleObserver::trace(visitor);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698