Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: ui/base/clipboard/clipboard_android.cc

Issue 851853002: It is time. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Trying to reup because the last upload failed. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/clipboard/clipboard_android.h ('k') | ui/base/clipboard/clipboard_aura.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "ui/base/clipboard/clipboard_android.h"
6
7 #include "base/android/jni_string.h"
8 #include "base/lazy_instance.h"
9 #include "base/stl_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/synchronization/lock.h"
12 #include "jni/Clipboard_jni.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/gfx/size.h"
15
16 // TODO:(andrewhayden) Support additional formats in Android: Bitmap, URI, HTML,
17 // HTML+text now that Android's clipboard system supports them, then nuke the
18 // legacy implementation note below.
19
20 // Legacy implementation note:
21 // The Android clipboard system used to only support text format. So we used the
22 // Android system when some text was added or retrieved from the system. For
23 // anything else, we STILL store the value in some process wide static
24 // variable protected by a lock. So the (non-text) clipboard will only work
25 // within the same process.
26
27 using base::android::AttachCurrentThread;
28 using base::android::ClearException;
29 using base::android::ConvertJavaStringToUTF8;
30 using base::android::ConvertUTF8ToJavaString;
31 using base::android::ScopedJavaGlobalRef;
32 using base::android::ScopedJavaLocalRef;
33
34 namespace ui {
35
36 namespace {
37 // Various formats we support.
38 const char kPlainTextFormat[] = "text";
39 const char kHTMLFormat[] = "html";
40 const char kRTFFormat[] = "rtf";
41 const char kBitmapFormat[] = "bitmap";
42 const char kWebKitSmartPasteFormat[] = "webkit_smart";
43 const char kBookmarkFormat[] = "bookmark";
44 const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data";
45 const char kMimeTypeWebCustomData[] = "chromium/x-web-custom-data";
46
47 class ClipboardMap {
48 public:
49 ClipboardMap();
50 std::string Get(const std::string& format);
51 bool HasFormat(const std::string& format);
52 void Set(const std::string& format, const std::string& data);
53 void Clear();
54
55 private:
56 void SyncWithAndroidClipboard();
57 std::map<std::string, std::string> map_;
58 base::Lock lock_;
59
60 // Java class and methods for the Android ClipboardManager.
61 ScopedJavaGlobalRef<jobject> clipboard_manager_;
62 };
63 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER;
64
65 ClipboardMap::ClipboardMap() {
66 JNIEnv* env = AttachCurrentThread();
67 DCHECK(env);
68
69 // Get the context.
70 jobject context = base::android::GetApplicationContext();
71 DCHECK(context);
72
73 ScopedJavaLocalRef<jobject> local_ref =
74 Java_Clipboard_create(env, context);
75 DCHECK(local_ref.obj());
76 clipboard_manager_.Reset(env, local_ref.Release());
77 }
78
79 std::string ClipboardMap::Get(const std::string& format) {
80 base::AutoLock lock(lock_);
81 SyncWithAndroidClipboard();
82 std::map<std::string, std::string>::const_iterator it = map_.find(format);
83 return it == map_.end() ? std::string() : it->second;
84 }
85
86 bool ClipboardMap::HasFormat(const std::string& format) {
87 base::AutoLock lock(lock_);
88 SyncWithAndroidClipboard();
89 return ContainsKey(map_, format);
90 }
91
92 void ClipboardMap::Set(const std::string& format, const std::string& data) {
93 JNIEnv* env = AttachCurrentThread();
94 base::AutoLock lock(lock_);
95 SyncWithAndroidClipboard();
96
97 map_[format] = data;
98 if (format == kPlainTextFormat) {
99 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, data);
100 DCHECK(str.obj());
101
102 Java_Clipboard_setText(env, clipboard_manager_.obj(), str.obj());
103 } else if (format == kHTMLFormat) {
104 // Android's API for storing HTML content on the clipboard requires a plain-
105 // text representation to be available as well. ScopedClipboardWriter has a
106 // stable order for setting clipboard data, ensuring that plain-text data
107 // is available first. Do not write to the clipboard when only HTML data is
108 // available, because otherwise others apps may not be able to paste it.
109 if (!ContainsKey(map_, kPlainTextFormat))
110 return;
111
112 ScopedJavaLocalRef<jstring> html = ConvertUTF8ToJavaString(env, data);
113 ScopedJavaLocalRef<jstring> text = ConvertUTF8ToJavaString(
114 env, map_[kPlainTextFormat].c_str());
115
116 DCHECK(html.obj() && text.obj());
117 Java_Clipboard_setHTMLText(
118 env, clipboard_manager_.obj(), html.obj(), text.obj());
119 }
120 }
121
122 void ClipboardMap::Clear() {
123 JNIEnv* env = AttachCurrentThread();
124 base::AutoLock lock(lock_);
125 map_.clear();
126 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL);
127 }
128
129 // If the internal map contains a plain-text entry and it does not match that
130 // in the Android clipboard, clear the map and insert the Android text into it.
131 // If there is an HTML entry in the Android clipboard it gets inserted in the
132 // map.
133 void ClipboardMap::SyncWithAndroidClipboard() {
134 lock_.AssertAcquired();
135 JNIEnv* env = AttachCurrentThread();
136
137 // Update the plain text clipboard entry
138 std::map<std::string, std::string>::const_iterator it =
139 map_.find(kPlainTextFormat);
140 ScopedJavaLocalRef<jstring> java_string_text =
141 Java_Clipboard_getCoercedText(env, clipboard_manager_.obj());
142 if (java_string_text.obj()) {
143 std::string android_string = ConvertJavaStringToUTF8(java_string_text);
144 if (!android_string.empty() &&
145 (it == map_.end() || it->second != android_string)) {
146 // There is a different string in the Android clipboard than we have.
147 // Clear the map on our side.
148 map_.clear();
149 map_[kPlainTextFormat] = android_string;
150 }
151 } else {
152 if (it != map_.end()) {
153 // We have plain text on this side, but Android doesn't. Nuke ours.
154 map_.clear();
155 }
156 }
157
158 if (!Java_Clipboard_isHTMLClipboardSupported(env)) {
159 return;
160 }
161
162 // Update the html clipboard entry
163 ScopedJavaLocalRef<jstring> java_string_html =
164 Java_Clipboard_getHTMLText(env, clipboard_manager_.obj());
165 if (java_string_html.obj()) {
166 std::string android_string = ConvertJavaStringToUTF8(java_string_html);
167 if (!android_string.empty()) {
168 map_[kHTMLFormat] = android_string;
169 return;
170 }
171 }
172 it = map_.find(kHTMLFormat);
173 if (it != map_.end()) {
174 map_.erase(kHTMLFormat);
175 }
176 }
177
178 } // namespace
179
180 // Clipboard::FormatType implementation.
181 Clipboard::FormatType::FormatType() {
182 }
183
184 Clipboard::FormatType::FormatType(const std::string& native_format)
185 : data_(native_format) {
186 }
187
188 Clipboard::FormatType::~FormatType() {
189 }
190
191 std::string Clipboard::FormatType::Serialize() const {
192 return data_;
193 }
194
195 // static
196 Clipboard::FormatType Clipboard::FormatType::Deserialize(
197 const std::string& serialization) {
198 return FormatType(serialization);
199 }
200
201 bool Clipboard::FormatType::Equals(const FormatType& other) const {
202 return data_ == other.data_;
203 }
204
205 // Various predefined FormatTypes.
206 // static
207 Clipboard::FormatType Clipboard::GetFormatType(
208 const std::string& format_string) {
209 return FormatType::Deserialize(format_string);
210 }
211
212 // static
213 const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() {
214 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPlainTextFormat));
215 return type;
216 }
217
218 // static
219 const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() {
220 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPlainTextFormat));
221 return type;
222 }
223
224 // static
225 const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() {
226 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebKitSmartPasteFormat));
227 return type;
228 }
229
230 // static
231 const Clipboard::FormatType& Clipboard::GetHtmlFormatType() {
232 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kHTMLFormat));
233 return type;
234 }
235
236 // static
237 const Clipboard::FormatType& Clipboard::GetRtfFormatType() {
238 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kRTFFormat));
239 return type;
240 }
241
242 // static
243 const Clipboard::FormatType& Clipboard::GetBitmapFormatType() {
244 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kBitmapFormat));
245 return type;
246 }
247
248 // static
249 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() {
250 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeWebCustomData));
251 return type;
252 }
253
254 // static
255 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() {
256 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypePepperCustomData));
257 return type;
258 }
259
260 // Clipboard factory method.
261 // static
262 Clipboard* Clipboard::Create() {
263 return new ClipboardAndroid;
264 }
265
266 // ClipboardAndroid implementation.
267 ClipboardAndroid::ClipboardAndroid() {
268 DCHECK(CalledOnValidThread());
269 }
270
271 ClipboardAndroid::~ClipboardAndroid() {
272 DCHECK(CalledOnValidThread());
273 }
274
275 uint64 ClipboardAndroid::GetSequenceNumber(ClipboardType /* type */) {
276 DCHECK(CalledOnValidThread());
277 // TODO: implement this. For now this interface will advertise
278 // that the clipboard never changes. That's fine as long as we
279 // don't rely on this signal.
280 return 0;
281 }
282
283 bool ClipboardAndroid::IsFormatAvailable(const Clipboard::FormatType& format,
284 ClipboardType type) const {
285 DCHECK(CalledOnValidThread());
286 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
287 return g_map.Get().HasFormat(format.ToString());
288 }
289
290 void ClipboardAndroid::Clear(ClipboardType type) {
291 DCHECK(CalledOnValidThread());
292 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
293 g_map.Get().Clear();
294 }
295
296 void ClipboardAndroid::ReadAvailableTypes(ClipboardType type,
297 std::vector<base::string16>* types,
298 bool* contains_filenames) const {
299 DCHECK(CalledOnValidThread());
300 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
301
302 if (!types || !contains_filenames) {
303 NOTREACHED();
304 return;
305 }
306
307 NOTIMPLEMENTED();
308
309 types->clear();
310 *contains_filenames = false;
311 }
312
313 void ClipboardAndroid::ReadText(ClipboardType type,
314 base::string16* result) const {
315 DCHECK(CalledOnValidThread());
316 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
317 std::string utf8;
318 ReadAsciiText(type, &utf8);
319 *result = base::UTF8ToUTF16(utf8);
320 }
321
322 void ClipboardAndroid::ReadAsciiText(ClipboardType type,
323 std::string* result) const {
324 DCHECK(CalledOnValidThread());
325 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
326 *result = g_map.Get().Get(kPlainTextFormat);
327 }
328
329 // Note: |src_url| isn't really used. It is only implemented in Windows
330 void ClipboardAndroid::ReadHTML(ClipboardType type,
331 base::string16* markup,
332 std::string* src_url,
333 uint32* fragment_start,
334 uint32* fragment_end) const {
335 DCHECK(CalledOnValidThread());
336 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
337 if (src_url)
338 src_url->clear();
339
340 std::string input = g_map.Get().Get(kHTMLFormat);
341 *markup = base::UTF8ToUTF16(input);
342
343 *fragment_start = 0;
344 *fragment_end = static_cast<uint32>(markup->length());
345 }
346
347 void ClipboardAndroid::ReadRTF(ClipboardType type, std::string* result) const {
348 DCHECK(CalledOnValidThread());
349 NOTIMPLEMENTED();
350 }
351
352 SkBitmap ClipboardAndroid::ReadImage(ClipboardType type) const {
353 DCHECK(CalledOnValidThread());
354 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
355 std::string input = g_map.Get().Get(kBitmapFormat);
356
357 SkBitmap bmp;
358 if (!input.empty()) {
359 DCHECK_LE(sizeof(gfx::Size), input.size());
360 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(input.data());
361
362 bmp.allocN32Pixels(size->width(), size->height());
363
364 DCHECK_EQ(sizeof(gfx::Size) + bmp.getSize(), input.size());
365
366 memcpy(bmp.getPixels(), input.data() + sizeof(gfx::Size), bmp.getSize());
367 }
368 return bmp;
369 }
370
371 void ClipboardAndroid::ReadCustomData(ClipboardType clipboard_type,
372 const base::string16& type,
373 base::string16* result) const {
374 DCHECK(CalledOnValidThread());
375 NOTIMPLEMENTED();
376 }
377
378 void ClipboardAndroid::ReadBookmark(base::string16* title,
379 std::string* url) const {
380 DCHECK(CalledOnValidThread());
381 NOTIMPLEMENTED();
382 }
383
384 void ClipboardAndroid::ReadData(const Clipboard::FormatType& format,
385 std::string* result) const {
386 DCHECK(CalledOnValidThread());
387 *result = g_map.Get().Get(format.ToString());
388 }
389
390 // Main entry point used to write several values in the clipboard.
391 void ClipboardAndroid::WriteObjects(ClipboardType type,
392 const ObjectMap& objects) {
393 DCHECK(CalledOnValidThread());
394 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
395 g_map.Get().Clear();
396 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end();
397 ++iter) {
398 DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
399 }
400 }
401
402 void ClipboardAndroid::WriteText(const char* text_data, size_t text_len) {
403 g_map.Get().Set(kPlainTextFormat, std::string(text_data, text_len));
404 }
405
406 void ClipboardAndroid::WriteHTML(const char* markup_data,
407 size_t markup_len,
408 const char* url_data,
409 size_t url_len) {
410 g_map.Get().Set(kHTMLFormat, std::string(markup_data, markup_len));
411 }
412
413 void ClipboardAndroid::WriteRTF(const char* rtf_data, size_t data_len) {
414 NOTIMPLEMENTED();
415 }
416
417 // Note: according to other platforms implementations, this really writes the
418 // URL spec.
419 void ClipboardAndroid::WriteBookmark(const char* title_data,
420 size_t title_len,
421 const char* url_data,
422 size_t url_len) {
423 g_map.Get().Set(kBookmarkFormat, std::string(url_data, url_len));
424 }
425
426 // Write an extra flavor that signifies WebKit was the last to modify the
427 // pasteboard. This flavor has no data.
428 void ClipboardAndroid::WriteWebSmartPaste() {
429 g_map.Get().Set(kWebKitSmartPasteFormat, std::string());
430 }
431
432 // Note: we implement this to pass all unit tests but it is currently unclear
433 // how some code would consume this.
434 void ClipboardAndroid::WriteBitmap(const SkBitmap& bitmap) {
435 gfx::Size size(bitmap.width(), bitmap.height());
436
437 std::string packed(reinterpret_cast<const char*>(&size), sizeof(size));
438 {
439 SkAutoLockPixels bitmap_lock(bitmap);
440 packed += std::string(static_cast<const char*>(bitmap.getPixels()),
441 bitmap.getSize());
442 }
443 g_map.Get().Set(kBitmapFormat, packed);
444 }
445
446 void ClipboardAndroid::WriteData(const Clipboard::FormatType& format,
447 const char* data_data,
448 size_t data_len) {
449 g_map.Get().Set(format.ToString(), std::string(data_data, data_len));
450 }
451
452 bool RegisterClipboardAndroid(JNIEnv* env) {
453 return RegisterNativesImpl(env);
454 }
455
456 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/clipboard/clipboard_android.h ('k') | ui/base/clipboard/clipboard_aura.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698