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