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"
8 #include "components/html_viewer/blink_basic_type_converters.h"
11 using mojo::Clipboard
;
15 namespace html_viewer
{
18 void CopyUint64(uint64_t* output
, uint64_t 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()) {
28 *output
= blink::WebString::fromUTF8(
29 reinterpret_cast<const char*>(&input
.front()),
34 void CopyURL(blink::WebURL
* pageURL
, const Array
<uint8_t>& input
) {
35 if (input
.is_null()) {
36 *pageURL
= blink::WebURL();
38 *pageURL
= GURL(std::string(reinterpret_cast<const char*>(&input
.front()),
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";
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
);
68 clipboard_
->GetSequenceNumber(clipboard_type
,
69 base::Bind(&CopyUint64
, &number
));
71 // Force this to be synchronous.
72 clipboard_
.WaitForIncomingResponse();
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();
88 return Contains(types
, Clipboard::MIME_TYPE_TEXT
);
90 return Contains(types
, Clipboard::MIME_TYPE_HTML
);
91 case FormatSmartPaste
:
92 return Contains(types
, kMimeTypeWebkitSmartPaste
);
94 // This might be difficult.
101 blink::WebVector
<blink::WebString
> WebClipboardImpl::readAvailableTypes(
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
]);
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();
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();
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();
158 blink::WebString
WebClipboardImpl::readCustomData(
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();
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());
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
) {
198 return Clipboard::TYPE_COPY_PASTE
;
199 case BufferSelection
:
200 return Clipboard::TYPE_SELECTION
;
204 return Clipboard::TYPE_COPY_PASTE
;
207 } // namespace html_viewer