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

Side by Side Diff: mojo/services/html_viewer/webclipboard_impl.cc

Issue 562483002: mojo: Create a basic clipboard. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ;_; Created 6 years, 3 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 "mojo/services/html_viewer/webclipboard_impl.h"
6
7 #include "base/bind.h"
8
9 namespace mojo {
10 namespace {
11
12 void CopyUint64(uint64_t* output, uint64_t input) {
13 *output = input;
14 }
15
16 void CopyString(std::string* output, const mojo::String& input) {
17 *output = input.To<std::string>();
18 }
19
20 void CopyVectorString(std::vector<std::string>* output,
21 const Array<String>& input) {
22 *output = input.To<std::vector<std::string> >();
23 }
24
25 // Work around having an odd arrity in base/bind.h.pump.
26 struct HTMLOutput {
27 std::string* text_output;
28 blink::WebURL* url_output;
29 unsigned* start_output;
30 unsigned* end_output;
31 };
32 void CopyHTMLReturnValues(HTMLOutput* output,
33 const mojo::String& text_input,
34 const mojo::String& url_input,
35 uint32_t start_input,
36 uint32_t end_input) {
37 *output->text_output = text_input.To<std::string>();
38 *output->url_output = GURL(url_input.To<std::string>());
39 *output->start_output = start_input;
40 *output->end_output = end_input;
41 }
42
43 template <typename T, typename U>
44 bool Contains(const std::vector<T>& v, const U& item) {
45 return std::find(v.begin(), v.end(), item) != v.end();
46 }
47
48 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste";
49
50 } // namespace
51
52 WebClipboardImpl::WebClipboardImpl(ClipboardPtr clipboard)
53 : clipboard_(clipboard.Pass()) {
54 }
55
56 WebClipboardImpl::~WebClipboardImpl() {
57 }
58
59 uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer) {
60 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
61
62 uint64_t number = 0;
63 clipboard_->GetSequenceNumber(clipboard_type,
64 base::Bind(&CopyUint64, &number));
65
66 // Force this to be synchronous.
67 clipboard_.WaitForIncomingMethodCall();
68 return number;
69 }
70
71 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
72 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
73
74 std::vector<std::string> types;
75 clipboard_->GetAvailableFormatMimeTypes(
76 clipboard_type, base::Bind(&CopyVectorString, &types));
77
78 // Force this to be synchronous.
79 clipboard_.WaitForIncomingMethodCall();
80
81 switch (format) {
82 case FormatPlainText:
83 return Contains(types, mojo::Clipboard::MIME_TYPE_TEXT);
84 case FormatHTML:
85 return Contains(types, mojo::Clipboard::MIME_TYPE_HTML);
86 case FormatSmartPaste:
87 return Contains(types, kMimeTypeWebkitSmartPaste);
88 case FormatBookmark:
89 // This might be difficult.
90 return false;
91 }
92
93 return false;
94 }
95
96 blink::WebVector<blink::WebString> WebClipboardImpl::readAvailableTypes(
97 Buffer buffer,
98 bool* containsFilenames) {
99 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
100
101 std::vector<std::string> types;
102 clipboard_->GetAvailableFormatMimeTypes(
103 clipboard_type, base::Bind(&CopyVectorString, &types));
104
105 // Force this to be synchronous.
106 clipboard_.WaitForIncomingMethodCall();
107
108 // AFAICT, every instance of setting containsFilenames is false.
109 *containsFilenames = false;
110
111 blink::WebVector<blink::WebString> output(types.size());
112 for (size_t i = 0; i < types.size(); ++i) {
113 output[i] = blink::WebString::fromUTF8(types[i]);
114 }
115
116 return output;
117 }
118
119 blink::WebString WebClipboardImpl::readPlainText(Buffer buffer) {
120 mojo::Clipboard::Type type = ConvertBufferType(buffer);
121
122 std::string text;
123 clipboard_->ReadPlainText(type, base::Bind(&CopyString, &text));
124
125 // Force this to be synchronous.
126 clipboard_.WaitForIncomingMethodCall();
127
128 return blink::WebString::fromUTF8(text);
129 }
130
131 blink::WebString WebClipboardImpl::readHTML(Buffer buffer,
132 blink::WebURL* pageURL,
133 unsigned* fragmentStart,
134 unsigned* fragmentEnd) {
135 mojo::Clipboard::Type type = ConvertBufferType(buffer);
136
137 std::string text;
138 struct HTMLOutput output = {&text, pageURL, fragmentStart, fragmentEnd};
139 clipboard_->ReadHTML(type, base::Bind(&CopyHTMLReturnValues, &output));
140
141 // Force this to be synchronous.
142 clipboard_.WaitForIncomingMethodCall();
143
144 return blink::WebString::fromUTF8(text);
145 }
146
147 blink::WebString WebClipboardImpl::readCustomData(
148 Buffer buffer,
149 const blink::WebString& mime_type) {
150 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
151
152 std::string data;
153 clipboard_->ReadMIMEType(
154 clipboard_type, mime_type.utf8(), base::Bind(&CopyString, &data));
155
156 // Force this to be synchronous.
157 clipboard_.WaitForIncomingMethodCall();
158
159 return blink::WebString::fromUTF8(data);
160 }
161
162 void WebClipboardImpl::writePlainText(const blink::WebString& text) {
163 // TODO(erg): What about the target buffer here!?
164 clipboard_->QueueWritePlainText(mojo::Clipboard::TYPE_COPY_PASTE,
165 text.utf8());
166
167 clipboard_->WriteQueuedData(mojo::Clipboard::TYPE_COPY_PASTE);
168 }
169
170 void WebClipboardImpl::writeHTML(const blink::WebString& htmlText,
171 const blink::WebURL& url,
172 const blink::WebString& plainText,
173 bool writeSmartPaste) {
174 clipboard_->QueueWriteHTML(
175 mojo::Clipboard::TYPE_COPY_PASTE, htmlText.utf8(), url.string().utf8());
176 clipboard_->QueueWritePlainText(mojo::Clipboard::TYPE_COPY_PASTE,
177 plainText.utf8());
178
179 if (writeSmartPaste) {
180 clipboard_->QueueWriteMIMEType(mojo::Clipboard::TYPE_COPY_PASTE,
181 kMimeTypeWebkitSmartPaste,
182 std::string());
183 }
184
185 clipboard_->WriteQueuedData(mojo::Clipboard::TYPE_COPY_PASTE);
186 }
187
188 mojo::Clipboard::Type WebClipboardImpl::ConvertBufferType(Buffer buffer) {
189 switch (buffer) {
190 case BufferStandard:
191 return mojo::Clipboard::TYPE_COPY_PASTE;
192 case BufferSelection:
193 return mojo::Clipboard::TYPE_SELECTION;
194 }
195
196 NOTREACHED();
197 return mojo::Clipboard::TYPE_COPY_PASTE;
198 }
199
200 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698