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 "mojo/services/html_viewer/webclipboard_impl.h"
8 #include "mojo/services/html_viewer/blink_basic_type_converters.h"
13 void CopyUint64(uint64_t* output
, uint64_t input
) {
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()) {
24 *output
= blink::WebString::fromUTF8(
25 reinterpret_cast<const char*>(&input
.front()),
30 void CopyURL(blink::WebURL
* pageURL
,
31 const mojo::Array
<uint8_t>& input
) {
32 if (input
.is_null()) {
33 *pageURL
= blink::WebURL();
35 *pageURL
= GURL(std::string(reinterpret_cast<const char*>(&input
.front()),
40 void CopyVectorString(std::vector
<std::string
>* output
,
41 const Array
<String
>& input
) {
42 *output
= input
.To
<std::vector
<std::string
> >();
45 template <typename T
, typename U
>
46 bool Contains(const std::vector
<T
>& v
, const U
& item
) {
47 return std::find(v
.begin(), v
.end(), item
) != v
.end();
50 const char kMimeTypeWebkitSmartPaste
[] = "chromium/x-webkit-paste";
54 WebClipboardImpl::WebClipboardImpl(ClipboardPtr clipboard
)
55 : clipboard_(clipboard
.Pass()) {
58 WebClipboardImpl::~WebClipboardImpl() {
61 uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer
) {
62 mojo::Clipboard::Type clipboard_type
= ConvertBufferType(buffer
);
65 clipboard_
->GetSequenceNumber(clipboard_type
,
66 base::Bind(&CopyUint64
, &number
));
68 // Force this to be synchronous.
69 clipboard_
.WaitForIncomingMethodCall();
73 bool WebClipboardImpl::isFormatAvailable(Format format
, Buffer buffer
) {
74 mojo::Clipboard::Type clipboard_type
= ConvertBufferType(buffer
);
76 std::vector
<std::string
> types
;
77 clipboard_
->GetAvailableMimeTypes(
78 clipboard_type
, base::Bind(&CopyVectorString
, &types
));
80 // Force this to be synchronous.
81 clipboard_
.WaitForIncomingMethodCall();
85 return Contains(types
, mojo::Clipboard::MIME_TYPE_TEXT
);
87 return Contains(types
, mojo::Clipboard::MIME_TYPE_HTML
);
88 case FormatSmartPaste
:
89 return Contains(types
, kMimeTypeWebkitSmartPaste
);
91 // This might be difficult.
98 blink::WebVector
<blink::WebString
> WebClipboardImpl::readAvailableTypes(
100 bool* containsFilenames
) {
101 mojo::Clipboard::Type clipboard_type
= ConvertBufferType(buffer
);
103 std::vector
<std::string
> types
;
104 clipboard_
->GetAvailableMimeTypes(
105 clipboard_type
, base::Bind(&CopyVectorString
, &types
));
107 // Force this to be synchronous.
108 clipboard_
.WaitForIncomingMethodCall();
110 // AFAICT, every instance of setting containsFilenames is false.
111 *containsFilenames
= false;
113 blink::WebVector
<blink::WebString
> output(types
.size());
114 for (size_t i
= 0; i
< types
.size(); ++i
) {
115 output
[i
] = blink::WebString::fromUTF8(types
[i
]);
121 blink::WebString
WebClipboardImpl::readPlainText(Buffer buffer
) {
122 mojo::Clipboard::Type type
= ConvertBufferType(buffer
);
124 blink::WebString text
;
125 clipboard_
->ReadMimeType(
126 type
, mojo::Clipboard::MIME_TYPE_TEXT
, base::Bind(&CopyWebString
, &text
));
128 // Force this to be synchronous.
129 clipboard_
.WaitForIncomingMethodCall();
134 blink::WebString
WebClipboardImpl::readHTML(Buffer buffer
,
135 blink::WebURL
* pageURL
,
136 unsigned* fragmentStart
,
137 unsigned* fragmentEnd
) {
138 mojo::Clipboard::Type type
= ConvertBufferType(buffer
);
140 blink::WebString html
;
141 clipboard_
->ReadMimeType(
142 type
, mojo::Clipboard::MIME_TYPE_HTML
, base::Bind(&CopyWebString
, &html
));
143 clipboard_
.WaitForIncomingMethodCall();
146 *fragmentEnd
= static_cast<unsigned>(html
.length());
148 clipboard_
->ReadMimeType(
149 type
, mojo::Clipboard::MIME_TYPE_URL
, base::Bind(&CopyURL
, pageURL
));
150 clipboard_
.WaitForIncomingMethodCall();
155 blink::WebString
WebClipboardImpl::readCustomData(
157 const blink::WebString
& mime_type
) {
158 mojo::Clipboard::Type clipboard_type
= ConvertBufferType(buffer
);
160 blink::WebString data
;
161 clipboard_
->ReadMimeType(
162 clipboard_type
, mime_type
.utf8(), base::Bind(&CopyWebString
, &data
));
164 // Force this to be synchronous.
165 clipboard_
.WaitForIncomingMethodCall();
170 void WebClipboardImpl::writePlainText(const blink::WebString
& text
) {
171 Map
<String
, Array
<uint8_t>> data
;
172 data
[mojo::Clipboard::MIME_TYPE_TEXT
] = Array
<uint8_t>::From(text
);
174 clipboard_
->WriteClipboardData(mojo::Clipboard::TYPE_COPY_PASTE
, data
.Pass());
177 void WebClipboardImpl::writeHTML(const blink::WebString
& htmlText
,
178 const blink::WebURL
& url
,
179 const blink::WebString
& plainText
,
180 bool writeSmartPaste
) {
181 Map
<String
, Array
<uint8_t>> data
;
182 data
[mojo::Clipboard::MIME_TYPE_TEXT
] = Array
<uint8_t>::From(plainText
);
183 data
[mojo::Clipboard::MIME_TYPE_HTML
] = Array
<uint8_t>::From(htmlText
);
184 data
[mojo::Clipboard::MIME_TYPE_URL
] = Array
<uint8_t>::From(url
.string());
187 data
[kMimeTypeWebkitSmartPaste
] = Array
<uint8_t>::From(blink::WebString());
189 clipboard_
->WriteClipboardData(mojo::Clipboard::TYPE_COPY_PASTE
, data
.Pass());
192 mojo::Clipboard::Type
WebClipboardImpl::ConvertBufferType(Buffer buffer
) {
195 return mojo::Clipboard::TYPE_COPY_PASTE
;
196 case BufferSelection
:
197 return mojo::Clipboard::TYPE_SELECTION
;
201 return mojo::Clipboard::TYPE_COPY_PASTE
;