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(blink::ExecutionContext::From(scriptState)), | |
24 m_scriptPromiseResolver(ScriptPromiseResolver::Create(scriptState)), | |
25 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
| |
26 | |
27 WebTaskRunner* ClipboardAsync::getTaskRunner() { | |
28 // TODO(garykac): Replace MiscPlatformAPI with TaskType specific to clipboard. | |
29 return TaskRunnerHelper::Get(TaskType::kMiscPlatformAPI, | |
30 GetExecutionContext()) | |
31 .Get(); | |
32 } | |
33 | |
34 ScriptPromise ClipboardAsync::scheduleRead() { | |
35 getTaskRunner()->PostTask( | |
36 BLINK_FROM_HERE, | |
37 WTF::Bind(&ClipboardAsync::handleRead, WrapPersistent(this))); | |
38 return m_scriptPromiseResolver->Promise(); | |
39 } | |
40 | |
41 ScriptPromise ClipboardAsync::scheduleReadText() { | |
42 getTaskRunner()->PostTask( | |
43 BLINK_FROM_HERE, | |
44 WTF::Bind(&ClipboardAsync::handleReadText, WrapPersistent(this))); | |
45 return m_scriptPromiseResolver->Promise(); | |
46 } | |
47 | |
48 ScriptPromise ClipboardAsync::scheduleWrite(DataTransfer* data) { | |
49 getTaskRunner()->PostTask( | |
50 BLINK_FROM_HERE, WTF::Bind(&ClipboardAsync::handleWrite, | |
51 WrapPersistent(this), WrapPersistent(data))); | |
52 return m_scriptPromiseResolver->Promise(); | |
53 } | |
54 | |
55 ScriptPromise ClipboardAsync::scheduleWriteText(const String& data) { | |
56 getTaskRunner()->PostTask( | |
57 BLINK_FROM_HERE, | |
58 WTF::Bind(&ClipboardAsync::handleWriteText, WrapPersistent(this), data)); | |
59 return m_scriptPromiseResolver->Promise(); | |
60 } | |
61 | |
62 // TODO(garykac): This currently only handles plain text. | |
63 void ClipboardAsync::handleRead() { | |
64 String plainText = Platform::Current()->Clipboard()->ReadPlainText(m_buffer); | |
65 | |
66 DataTransfer::DataTransferType type = | |
67 DataTransfer::DataTransferType::kCopyAndPaste; | |
68 DataTransferAccessPolicy access = | |
69 DataTransferAccessPolicy::kDataTransferReadable; | |
70 DataObject* data = DataObject::CreateFromString(plainText); | |
71 DataTransfer* dt = DataTransfer::Create(type, access, data); | |
72 m_scriptPromiseResolver->Resolve(dt); | |
73 } | |
74 | |
75 void ClipboardAsync::handleReadText() { | |
76 String text = Platform::Current()->Clipboard()->ReadPlainText(m_buffer); | |
77 m_scriptPromiseResolver->Resolve(text); | |
78 } | |
79 | |
80 // TODO(garykac): This currently only handles plain text. | |
81 void ClipboardAsync::handleWrite(DataTransfer* data) { | |
82 size_t nItems = data->items()->length(); | |
83 for (unsigned long i = 0; i < nItems; i++) { | |
84 DataTransferItem* item = data->items()->item(i); | |
85 DataObjectItem* objectItem = item->GetDataObjectItem(); | |
86 if (objectItem->Kind() == DataObjectItem::kStringKind && | |
87 objectItem->GetType() == "text/plain") { | |
88 String text = objectItem->GetAsString(); | |
89 Platform::Current()->Clipboard()->WritePlainText(text); | |
90 m_scriptPromiseResolver->Resolve(); | |
91 return; | |
92 } | |
93 } | |
94 m_scriptPromiseResolver->Reject(); | |
95 } | |
96 | |
97 void ClipboardAsync::handleWriteText(const String& data) { | |
98 Platform::Current()->Clipboard()->WritePlainText(data); | |
99 m_scriptPromiseResolver->Resolve(); | |
100 } | |
101 | |
102 void ClipboardAsync::ContextDestroyed(ExecutionContext*) { | |
103 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
| |
104 } | |
105 | |
106 DEFINE_TRACE(ClipboardAsync) { | |
107 visitor->Trace(m_scriptPromiseResolver); | |
108 ContextLifecycleObserver::Trace(visitor); | |
109 } | |
110 | |
111 } // namespace blink | |
OLD | NEW |