OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/clipboard/ClipboardAsync.h" |
| 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "core/clipboard/DataObject.h" |
| 9 #include "core/clipboard/DataTransfer.h" |
| 10 #include "core/clipboard/DataTransferItem.h" |
| 11 #include "core/clipboard/DataTransferItemList.h" |
| 12 #include "core/dom/TaskRunnerHelper.h" |
| 13 #include "platform/CrossThreadFunctional.h" |
| 14 #include "public/platform/Platform.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 ClipboardAsync* ClipboardAsync::create(ScriptState* scriptState) { |
| 19 return new ClipboardAsync(scriptState); |
| 20 } |
| 21 |
| 22 ClipboardAsync::ClipboardAsync(ScriptState* scriptState) |
| 23 : ContextLifecycleObserver(scriptState->getExecutionContext()), |
| 24 m_scriptPromiseResolver(ScriptPromiseResolver::create(scriptState)), |
| 25 m_buffer(WebClipboard::BufferStandard) {} |
| 26 |
| 27 WebTaskRunner* ClipboardAsync::getTaskRunner() { |
| 28 // TODO(garykac): Replace MiscPlatformAPI with TaskType specific to clipboard. |
| 29 return TaskRunnerHelper::get(TaskType::MiscPlatformAPI, getExecutionContext()) |
| 30 .get(); |
| 31 } |
| 32 |
| 33 ScriptPromise ClipboardAsync::scheduleRead() { |
| 34 getTaskRunner()->postTask( |
| 35 BLINK_FROM_HERE, |
| 36 WTF::bind(&ClipboardAsync::handleRead, wrapPersistent(this))); |
| 37 return m_scriptPromiseResolver->promise(); |
| 38 } |
| 39 |
| 40 ScriptPromise ClipboardAsync::scheduleReadText() { |
| 41 getTaskRunner()->postTask( |
| 42 BLINK_FROM_HERE, |
| 43 WTF::bind(&ClipboardAsync::handleReadText, wrapPersistent(this))); |
| 44 return m_scriptPromiseResolver->promise(); |
| 45 } |
| 46 |
| 47 ScriptPromise ClipboardAsync::scheduleWrite(DataTransfer* data) { |
| 48 getTaskRunner()->postTask( |
| 49 BLINK_FROM_HERE, WTF::bind(&ClipboardAsync::handleWrite, |
| 50 wrapPersistent(this), wrapPersistent(data))); |
| 51 return m_scriptPromiseResolver->promise(); |
| 52 } |
| 53 |
| 54 ScriptPromise ClipboardAsync::scheduleWriteText(const String& data) { |
| 55 getTaskRunner()->postTask( |
| 56 BLINK_FROM_HERE, |
| 57 WTF::bind(&ClipboardAsync::handleWriteText, wrapPersistent(this), data)); |
| 58 return m_scriptPromiseResolver->promise(); |
| 59 } |
| 60 |
| 61 // TODO(garykac): This currently only handles plain text. |
| 62 void ClipboardAsync::handleRead() { |
| 63 String plainText = Platform::current()->clipboard()->readPlainText(m_buffer); |
| 64 |
| 65 DataTransfer::DataTransferType type = |
| 66 DataTransfer::DataTransferType::CopyAndPaste; |
| 67 DataTransferAccessPolicy access = |
| 68 DataTransferAccessPolicy::DataTransferReadable; |
| 69 DataObject* data = DataObject::createFromString(plainText); |
| 70 DataTransfer* dt = DataTransfer::create(type, access, data); |
| 71 m_scriptPromiseResolver->resolve(dt); |
| 72 } |
| 73 |
| 74 void ClipboardAsync::handleReadText() { |
| 75 String text = Platform::current()->clipboard()->readPlainText(m_buffer); |
| 76 m_scriptPromiseResolver->resolve(text); |
| 77 } |
| 78 |
| 79 // TODO(garykac): This currently only handles plain text. |
| 80 void ClipboardAsync::handleWrite(DataTransfer* data) { |
| 81 size_t nItems = data->items()->length(); |
| 82 for (unsigned long i = 0; i < nItems; i++) { |
| 83 DataTransferItem* item = data->items()->item(i); |
| 84 DataObjectItem* objectItem = item->getDataObjectItem(); |
| 85 if (objectItem->kind() == DataObjectItem::StringKind && |
| 86 objectItem->type() == "text/plain") { |
| 87 String text = objectItem->getAsString(); |
| 88 Platform::current()->clipboard()->writePlainText(text); |
| 89 m_scriptPromiseResolver->resolve(); |
| 90 return; |
| 91 } |
| 92 } |
| 93 m_scriptPromiseResolver->reject(); |
| 94 } |
| 95 |
| 96 void ClipboardAsync::handleWriteText(const String& data) { |
| 97 Platform::current()->clipboard()->writePlainText(data); |
| 98 m_scriptPromiseResolver->resolve(); |
| 99 } |
| 100 |
| 101 void ClipboardAsync::contextDestroyed(ExecutionContext*) { |
| 102 m_scriptPromiseResolver.clear(); |
| 103 } |
| 104 |
| 105 DEFINE_TRACE(ClipboardAsync) { |
| 106 visitor->trace(m_scriptPromiseResolver); |
| 107 ContextLifecycleObserver::trace(visitor); |
| 108 } |
| 109 |
| 110 } // namespace blink |
OLD | NEW |