OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/base/clipboard/clipboard_android.h" | 5 #include "ui/base/clipboard/clipboard_android.h" |
6 | 6 |
7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data"; | 45 const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data"; |
46 const char kMimeTypeWebCustomData[] = "chromium/x-web-custom-data"; | 46 const char kMimeTypeWebCustomData[] = "chromium/x-web-custom-data"; |
47 | 47 |
48 class ClipboardMap { | 48 class ClipboardMap { |
49 public: | 49 public: |
50 ClipboardMap(); | 50 ClipboardMap(); |
51 std::string Get(const std::string& format); | 51 std::string Get(const std::string& format); |
52 bool HasFormat(const std::string& format); | 52 bool HasFormat(const std::string& format); |
53 void Set(const std::string& format, const std::string& data); | 53 void Set(const std::string& format, const std::string& data); |
54 void Clear(); | 54 void Clear(); |
| 55 void CommitToAndroidClipboard(); |
55 | 56 |
56 private: | 57 private: |
57 void SyncWithAndroidClipboard(); | 58 void SyncWithAndroidClipboard(); |
58 std::map<std::string, std::string> map_; | 59 std::map<std::string, std::string> map_; |
59 base::Lock lock_; | 60 base::Lock lock_; |
60 | 61 |
61 // Java class and methods for the Android ClipboardManager. | 62 // Java class and methods for the Android ClipboardManager. |
62 ScopedJavaGlobalRef<jobject> clipboard_manager_; | 63 ScopedJavaGlobalRef<jobject> clipboard_manager_; |
63 }; | 64 }; |
64 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER; | 65 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER; |
(...skipping 19 matching lines...) Expand all Loading... |
84 return it == map_.end() ? std::string() : it->second; | 85 return it == map_.end() ? std::string() : it->second; |
85 } | 86 } |
86 | 87 |
87 bool ClipboardMap::HasFormat(const std::string& format) { | 88 bool ClipboardMap::HasFormat(const std::string& format) { |
88 base::AutoLock lock(lock_); | 89 base::AutoLock lock(lock_); |
89 SyncWithAndroidClipboard(); | 90 SyncWithAndroidClipboard(); |
90 return ContainsKey(map_, format); | 91 return ContainsKey(map_, format); |
91 } | 92 } |
92 | 93 |
93 void ClipboardMap::Set(const std::string& format, const std::string& data) { | 94 void ClipboardMap::Set(const std::string& format, const std::string& data) { |
94 JNIEnv* env = AttachCurrentThread(); | |
95 base::AutoLock lock(lock_); | 95 base::AutoLock lock(lock_); |
96 SyncWithAndroidClipboard(); | |
97 | |
98 map_[format] = data; | 96 map_[format] = data; |
99 if (format == kPlainTextFormat) { | |
100 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, data); | |
101 DCHECK(str.obj()); | |
102 | |
103 Java_Clipboard_setText(env, clipboard_manager_.obj(), str.obj()); | |
104 } else if (format == kHTMLFormat) { | |
105 // Android's API for storing HTML content on the clipboard requires a plain- | |
106 // text representation to be available as well. ScopedClipboardWriter has a | |
107 // stable order for setting clipboard data, ensuring that plain-text data | |
108 // is available first. Do not write to the clipboard when only HTML data is | |
109 // available, because otherwise others apps may not be able to paste it. | |
110 if (!ContainsKey(map_, kPlainTextFormat)) | |
111 return; | |
112 | |
113 ScopedJavaLocalRef<jstring> html = ConvertUTF8ToJavaString(env, data); | |
114 ScopedJavaLocalRef<jstring> text = ConvertUTF8ToJavaString( | |
115 env, map_[kPlainTextFormat].c_str()); | |
116 | |
117 DCHECK(html.obj() && text.obj()); | |
118 Java_Clipboard_setHTMLText( | |
119 env, clipboard_manager_.obj(), html.obj(), text.obj()); | |
120 } | |
121 } | 97 } |
122 | 98 |
123 void ClipboardMap::Clear() { | 99 void ClipboardMap::Clear() { |
124 JNIEnv* env = AttachCurrentThread(); | 100 JNIEnv* env = AttachCurrentThread(); |
125 base::AutoLock lock(lock_); | 101 base::AutoLock lock(lock_); |
126 map_.clear(); | 102 map_.clear(); |
127 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL); | 103 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL); |
128 } | 104 } |
129 | 105 |
130 // If the internal map contains a plain-text entry and it does not match that | 106 // If the internal map contains a plain-text entry and it does not match that |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 map_[kHTMLFormat] = android_string; | 145 map_[kHTMLFormat] = android_string; |
170 return; | 146 return; |
171 } | 147 } |
172 } | 148 } |
173 it = map_.find(kHTMLFormat); | 149 it = map_.find(kHTMLFormat); |
174 if (it != map_.end()) { | 150 if (it != map_.end()) { |
175 map_.erase(kHTMLFormat); | 151 map_.erase(kHTMLFormat); |
176 } | 152 } |
177 } | 153 } |
178 | 154 |
| 155 void ClipboardMap::CommitToAndroidClipboard() { |
| 156 JNIEnv* env = AttachCurrentThread(); |
| 157 base::AutoLock lock(lock_); |
| 158 // Android's API for storing HTML content on the clipboard requires a plain- |
| 159 // text representation to be available as well. ScopedClipboardWriter has a |
| 160 // stable order for setting clipboard data, ensuring that plain-text data |
| 161 // is available first. Do not write to the clipboard when only HTML data is |
| 162 // available, because otherwise others apps may not be able to paste it. |
| 163 if (ContainsKey(map_, kHTMLFormat) && |
| 164 ContainsKey(map_, kPlainTextFormat) && |
| 165 Java_Clipboard_isHTMLClipboardSupported(env)) { |
| 166 ScopedJavaLocalRef<jstring> text = ConvertUTF8ToJavaString( |
| 167 env, map_[kPlainTextFormat]); |
| 168 DCHECK(text.obj()); |
| 169 |
| 170 ScopedJavaLocalRef<jstring> html = ConvertUTF8ToJavaString( |
| 171 env, map_[kHTMLFormat]); |
| 172 DCHECK(html.obj()); |
| 173 Java_Clipboard_setHTMLText( |
| 174 env, clipboard_manager_.obj(), html.obj(), text.obj()); |
| 175 |
| 176 } else if (ContainsKey(map_, kPlainTextFormat)) { |
| 177 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString( |
| 178 env, map_[kPlainTextFormat]); |
| 179 DCHECK(str.obj()); |
| 180 Java_Clipboard_setText(env, clipboard_manager_.obj(), str.obj()); |
| 181 } |
| 182 } |
| 183 |
179 } // namespace | 184 } // namespace |
180 | 185 |
181 // Clipboard::FormatType implementation. | 186 // Clipboard::FormatType implementation. |
182 Clipboard::FormatType::FormatType() { | 187 Clipboard::FormatType::FormatType() { |
183 } | 188 } |
184 | 189 |
185 Clipboard::FormatType::FormatType(const std::string& native_format) | 190 Clipboard::FormatType::FormatType(const std::string& native_format) |
186 : data_(native_format) { | 191 : data_(native_format) { |
187 } | 192 } |
188 | 193 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 // Main entry point used to write several values in the clipboard. | 406 // Main entry point used to write several values in the clipboard. |
402 void ClipboardAndroid::WriteObjects(ClipboardType type, | 407 void ClipboardAndroid::WriteObjects(ClipboardType type, |
403 const ObjectMap& objects) { | 408 const ObjectMap& objects) { |
404 DCHECK(CalledOnValidThread()); | 409 DCHECK(CalledOnValidThread()); |
405 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 410 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
406 g_map.Get().Clear(); | 411 g_map.Get().Clear(); |
407 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); | 412 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); |
408 ++iter) { | 413 ++iter) { |
409 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 414 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
410 } | 415 } |
| 416 g_map.Get().CommitToAndroidClipboard(); |
411 } | 417 } |
412 | 418 |
413 void ClipboardAndroid::WriteText(const char* text_data, size_t text_len) { | 419 void ClipboardAndroid::WriteText(const char* text_data, size_t text_len) { |
414 g_map.Get().Set(kPlainTextFormat, std::string(text_data, text_len)); | 420 g_map.Get().Set(kPlainTextFormat, std::string(text_data, text_len)); |
415 } | 421 } |
416 | 422 |
417 void ClipboardAndroid::WriteHTML(const char* markup_data, | 423 void ClipboardAndroid::WriteHTML(const char* markup_data, |
418 size_t markup_len, | 424 size_t markup_len, |
419 const char* url_data, | 425 const char* url_data, |
420 size_t url_len) { | 426 size_t url_len) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 const char* data_data, | 464 const char* data_data, |
459 size_t data_len) { | 465 size_t data_len) { |
460 g_map.Get().Set(format.ToString(), std::string(data_data, data_len)); | 466 g_map.Get().Set(format.ToString(), std::string(data_data, data_len)); |
461 } | 467 } |
462 | 468 |
463 bool RegisterClipboardAndroid(JNIEnv* env) { | 469 bool RegisterClipboardAndroid(JNIEnv* env) { |
464 return RegisterNativesImpl(env); | 470 return RegisterNativesImpl(env); |
465 } | 471 } |
466 | 472 |
467 } // namespace ui | 473 } // namespace ui |
OLD | NEW |