Update V8 to version 4.6.61.
[chromium-blink-merge.git] / components / html_viewer / web_clipboard_impl.cc
blob0641a936696b990515242b6522ca1b06a801d016
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.
5 #include "components/html_viewer/web_clipboard_impl.h"
7 #include "base/bind.h"
8 #include "components/html_viewer/blink_basic_type_converters.h"
10 using mojo::Array;
11 using mojo::Clipboard;
12 using mojo::Map;
13 using mojo::String;
15 namespace html_viewer {
16 namespace {
18 void CopyUint64(uint64_t* output, uint64_t input) {
19 *output = input;
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());
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()));
43 void CopyVectorString(std::vector<std::string>* output,
44 const Array<String>& input) {
45 *output = input.To<std::vector<std::string> >();
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();
53 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste";
55 } // namespace
57 WebClipboardImpl::WebClipboardImpl(mojo::ClipboardPtr clipboard)
58 : clipboard_(clipboard.Pass()) {
61 WebClipboardImpl::~WebClipboardImpl() {
64 uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer) {
65 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);
67 uint64_t number = 0;
68 clipboard_->GetSequenceNumber(clipboard_type,
69 base::Bind(&CopyUint64, &number));
71 // Force this to be synchronous.
72 clipboard_.WaitForIncomingResponse();
73 return number;
76 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
77 Clipboard::Type clipboard_type = ConvertBufferType(buffer);
79 std::vector<std::string> types;
80 clipboard_->GetAvailableMimeTypes(
81 clipboard_type, base::Bind(&CopyVectorString, &types));
83 // Force this to be synchronous.
84 clipboard_.WaitForIncomingResponse();
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;
98 return false;
101 blink::WebVector<blink::WebString> WebClipboardImpl::readAvailableTypes(
102 Buffer buffer,
103 bool* contains_filenames) {
104 Clipboard::Type clipboard_type = ConvertBufferType(buffer);
106 std::vector<std::string> types;
107 clipboard_->GetAvailableMimeTypes(
108 clipboard_type, base::Bind(&CopyVectorString, &types));
110 // Force this to be synchronous.
111 clipboard_.WaitForIncomingResponse();
113 // AFAICT, every instance of setting contains_filenames is false.
114 *contains_filenames = false;
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]);
121 return output;
124 blink::WebString WebClipboardImpl::readPlainText(Buffer buffer) {
125 Clipboard::Type type = ConvertBufferType(buffer);
127 blink::WebString text;
128 clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_TEXT,
129 base::Bind(&CopyWebString, &text));
131 // Force this to be synchronous.
132 clipboard_.WaitForIncomingResponse();
134 return text;
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);
143 blink::WebString html;
144 clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_HTML,
145 base::Bind(&CopyWebString, &html));
146 clipboard_.WaitForIncomingResponse();
148 *fragment_start = 0;
149 *fragment_end = static_cast<unsigned>(html.length());
151 clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_URL,
152 base::Bind(&CopyURL, page_url));
153 clipboard_.WaitForIncomingResponse();
155 return html;
158 blink::WebString WebClipboardImpl::readCustomData(
159 Buffer buffer,
160 const blink::WebString& mime_type) {
161 Clipboard::Type clipboard_type = ConvertBufferType(buffer);
163 blink::WebString data;
164 clipboard_->ReadMimeType(
165 clipboard_type, mime_type.utf8(), base::Bind(&CopyWebString, &data));
167 // Force this to be synchronous.
168 clipboard_.WaitForIncomingResponse();
170 return data;
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);
177 clipboard_->WriteClipboardData(Clipboard::TYPE_COPY_PASTE, data.Pass());
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());
189 if (writeSmartPaste)
190 data[kMimeTypeWebkitSmartPaste] = Array<uint8_t>::From(blink::WebString());
192 clipboard_->WriteClipboardData(Clipboard::TYPE_COPY_PASTE, data.Pass());
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;
203 NOTREACHED();
204 return Clipboard::TYPE_COPY_PASTE;
207 } // namespace html_viewer