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

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: Maybe fix windows and gn builds. 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 #include "mojo/services/html_viewer/blink_basic_type_converters.h"
9
10 namespace mojo {
11 namespace {
12
13 void CopyUint64(uint64_t* output, uint64_t input) {
14 *output = input;
15 }
16
17 void CopyWebString(blink::WebString* output,
18 const mojo::Array<uint8_t>& input) {
19 // blink does not differentiate between the requested data type not existing
20 // and the empty string.
21 if (input.is_null()) {
22 output->reset();
23 } else {
24 output->assign(reinterpret_cast<const blink::WebUChar*>(&input.front()),
25 input.size());
26 }
27 }
28
29 void CopyURL(blink::WebURL* pageURL,
30 const mojo::Array<uint8_t>& input) {
31 if (input.is_null()) {
32 *pageURL = blink::WebURL();
33 } else {
34 *pageURL = GURL(std::string(reinterpret_cast<const char*>(&input.front()),
35 input.size()));
36 }
37 }
38
39 void CopyVectorString(std::vector<std::string>* output,
40 const Array<String>& input) {
41 *output = input.To<std::vector<std::string> >();
42 }
43
44 template <typename T, typename U>
45 bool Contains(const std::vector<T>& v, const U& item) {
46 return std::find(v.begin(), v.end(), item) != v.end();
47 }
48
49 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste";
50
51 } // namespace
52
53 WebClipboardImpl::WebClipboardImpl(ClipboardPtr clipboard)
54 : clipboard_(clipboard.Pass()) {
55 }
56
57 WebClipboardImpl::~WebClipboardImpl() {
58 }
59
60 uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer) {
61 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
62
63 uint64_t number = 0;
64 clipboard_->GetSequenceNumber(clipboard_type,
65 base::Bind(&CopyUint64, &number));
66
67 // Force this to be synchronous.
68 clipboard_.WaitForIncomingMethodCall();
69 return number;
70 }
71
72 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
73 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
74
75 std::vector<std::string> types;
76 clipboard_->GetAvailableMimeTypes(
77 clipboard_type, base::Bind(&CopyVectorString, &types));
78
79 // Force this to be synchronous.
80 clipboard_.WaitForIncomingMethodCall();
81
82 switch (format) {
83 case FormatPlainText:
84 return Contains(types, mojo::Clipboard::MIME_TYPE_TEXT);
85 case FormatHTML:
86 return Contains(types, mojo::Clipboard::MIME_TYPE_HTML);
87 case FormatSmartPaste:
88 return Contains(types, kMimeTypeWebkitSmartPaste);
89 case FormatBookmark:
90 // This might be difficult.
91 return false;
92 }
93
94 return false;
95 }
96
97 blink::WebVector<blink::WebString> WebClipboardImpl::readAvailableTypes(
98 Buffer buffer,
99 bool* containsFilenames) {
100 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
101
102 std::vector<std::string> types;
103 clipboard_->GetAvailableMimeTypes(
104 clipboard_type, base::Bind(&CopyVectorString, &types));
105
106 // Force this to be synchronous.
107 clipboard_.WaitForIncomingMethodCall();
108
109 // AFAICT, every instance of setting containsFilenames is false.
110 *containsFilenames = false;
111
112 blink::WebVector<blink::WebString> output(types.size());
113 for (size_t i = 0; i < types.size(); ++i) {
114 output[i] = blink::WebString::fromUTF8(types[i]);
115 }
116
117 return output;
118 }
119
120 blink::WebString WebClipboardImpl::readPlainText(Buffer buffer) {
121 mojo::Clipboard::Type type = ConvertBufferType(buffer);
122
123 blink::WebString text;
124 clipboard_->ReadMimeType(
125 type, mojo::Clipboard::MIME_TYPE_TEXT, base::Bind(&CopyWebString, &text));
126
127 // Force this to be synchronous.
128 clipboard_.WaitForIncomingMethodCall();
129
130 return text;
131 }
132
133 blink::WebString WebClipboardImpl::readHTML(Buffer buffer,
134 blink::WebURL* pageURL,
135 unsigned* fragmentStart,
136 unsigned* fragmentEnd) {
137 mojo::Clipboard::Type type = ConvertBufferType(buffer);
138
139 blink::WebString html;
140 clipboard_->ReadMimeType(
141 type, mojo::Clipboard::MIME_TYPE_HTML, base::Bind(&CopyWebString, &html));
142 clipboard_.WaitForIncomingMethodCall();
143
144 *fragmentStart = 0;
145 *fragmentEnd = static_cast<unsigned>(html.length());
146
147 clipboard_->ReadMimeType(
148 type, mojo::Clipboard::MIME_TYPE_URL, base::Bind(&CopyURL, pageURL));
149 clipboard_.WaitForIncomingMethodCall();
150
151 return html;
152 }
153
154 blink::WebString WebClipboardImpl::readCustomData(
155 Buffer buffer,
156 const blink::WebString& mime_type) {
157 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
158
159 blink::WebString data;
160 clipboard_->ReadMimeType(
161 clipboard_type, mime_type.utf8(), base::Bind(&CopyWebString, &data));
162
163 // Force this to be synchronous.
164 clipboard_.WaitForIncomingMethodCall();
165
166 return data;
167 }
168
169 void WebClipboardImpl::writePlainText(const blink::WebString& text) {
170 Array<MimeTypePairPtr> data;
171 MimeTypePairPtr text_data(MimeTypePair::New());
172 text_data->mime_type = mojo::Clipboard::MIME_TYPE_TEXT;
173 text_data->data = Array<uint8_t>::From(text).Pass();
174 data.push_back(text_data.Pass());
175
176 clipboard_->WriteClipboardData(mojo::Clipboard::TYPE_COPY_PASTE, data.Pass());
177 }
178
179 void WebClipboardImpl::writeHTML(const blink::WebString& htmlText,
180 const blink::WebURL& url,
181 const blink::WebString& plainText,
182 bool writeSmartPaste) {
183 Array<MimeTypePairPtr> data;
184 MimeTypePairPtr text_data(MimeTypePair::New());
185 text_data->mime_type = mojo::Clipboard::MIME_TYPE_TEXT;
186 text_data->data = Array<uint8_t>::From(plainText).Pass();
187 data.push_back(text_data.Pass());
188
189 MimeTypePairPtr html_data(MimeTypePair::New());
190 text_data->mime_type = mojo::Clipboard::MIME_TYPE_HTML;
191 text_data->data = Array<uint8_t>::From(htmlText).Pass();
192 data.push_back(html_data.Pass());
193
194 MimeTypePairPtr url_data(MimeTypePair::New());
195 url_data->mime_type = mojo::Clipboard::MIME_TYPE_URL;
196 url_data->data = Array<uint8_t>::From(url.string()).Pass();
197 data.push_back(url_data.Pass());
198
199 if (writeSmartPaste) {
200 MimeTypePairPtr smart_paste(MimeTypePair::New());
201 url_data->mime_type = kMimeTypeWebkitSmartPaste;
202 url_data->data = Array<uint8_t>::From(blink::WebString()).Pass();
203 data.push_back(smart_paste.Pass());
204 }
205
206 clipboard_->WriteClipboardData(mojo::Clipboard::TYPE_COPY_PASTE, data.Pass());
207 }
208
209 mojo::Clipboard::Type WebClipboardImpl::ConvertBufferType(Buffer buffer) {
210 switch (buffer) {
211 case BufferStandard:
212 return mojo::Clipboard::TYPE_COPY_PASTE;
213 case BufferSelection:
214 return mojo::Clipboard::TYPE_SELECTION;
215 }
216
217 NOTREACHED();
218 return mojo::Clipboard::TYPE_COPY_PASTE;
219 }
220
221 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698