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