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

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

Powered by Google App Engine
This is Rietveld 408576698