Chromium Code Reviews| 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 CommitToClipboard(); | |
|
dcheng
2015/03/18 03:42:46
Probably should be "CommitToAndroidClipboard()". I
| |
| 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::CommitToClipboard() { | |
| 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 ScopedJavaLocalRef<jstring> text = ConvertUTF8ToJavaString( | |
| 166 env, map_[kPlainTextFormat].c_str()); | |
|
dcheng
2015/03/18 03:42:46
The c_str() here (and below) is superfluous.
| |
| 167 DCHECK(text.obj()); | |
| 168 | |
| 169 if (!Java_Clipboard_isHTMLClipboardSupported(env)) { | |
|
dcheng
2015/03/18 03:42:46
Why wasn't this check previously needed?
dcheng
2015/03/18 03:42:46
Why wasn't this check previously needed?
| |
| 170 Java_Clipboard_setText(env, clipboard_manager_.obj(), text.obj()); | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 ScopedJavaLocalRef<jstring> html = ConvertUTF8ToJavaString( | |
| 175 env, map_[kHTMLFormat].c_str()); | |
| 176 | |
| 177 DCHECK(html.obj()); | |
| 178 Java_Clipboard_setHTMLText( | |
| 179 env, clipboard_manager_.obj(), html.obj(), text.obj()); | |
| 180 | |
| 181 } else if (ContainsKey(map_, kPlainTextFormat)) { | |
| 182 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString( | |
| 183 env, map_[kPlainTextFormat].c_str()); | |
| 184 DCHECK(str.obj()); | |
| 185 Java_Clipboard_setText(env, clipboard_manager_.obj(), str.obj()); | |
| 186 } | |
| 187 } | |
| 188 | |
| 179 } // namespace | 189 } // namespace |
| 180 | 190 |
| 181 // Clipboard::FormatType implementation. | 191 // Clipboard::FormatType implementation. |
| 182 Clipboard::FormatType::FormatType() { | 192 Clipboard::FormatType::FormatType() { |
| 183 } | 193 } |
| 184 | 194 |
| 185 Clipboard::FormatType::FormatType(const std::string& native_format) | 195 Clipboard::FormatType::FormatType(const std::string& native_format) |
| 186 : data_(native_format) { | 196 : data_(native_format) { |
| 187 } | 197 } |
| 188 | 198 |
| (...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. | 411 // Main entry point used to write several values in the clipboard. |
| 402 void ClipboardAndroid::WriteObjects(ClipboardType type, | 412 void ClipboardAndroid::WriteObjects(ClipboardType type, |
| 403 const ObjectMap& objects) { | 413 const ObjectMap& objects) { |
| 404 DCHECK(CalledOnValidThread()); | 414 DCHECK(CalledOnValidThread()); |
| 405 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 415 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
| 406 g_map.Get().Clear(); | 416 g_map.Get().Clear(); |
| 407 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); | 417 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); |
| 408 ++iter) { | 418 ++iter) { |
| 409 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 419 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| 410 } | 420 } |
| 421 g_map.Get().CommitToClipboard(); | |
| 411 } | 422 } |
| 412 | 423 |
| 413 void ClipboardAndroid::WriteText(const char* text_data, size_t text_len) { | 424 void ClipboardAndroid::WriteText(const char* text_data, size_t text_len) { |
| 414 g_map.Get().Set(kPlainTextFormat, std::string(text_data, text_len)); | 425 g_map.Get().Set(kPlainTextFormat, std::string(text_data, text_len)); |
| 415 } | 426 } |
| 416 | 427 |
| 417 void ClipboardAndroid::WriteHTML(const char* markup_data, | 428 void ClipboardAndroid::WriteHTML(const char* markup_data, |
| 418 size_t markup_len, | 429 size_t markup_len, |
| 419 const char* url_data, | 430 const char* url_data, |
| 420 size_t url_len) { | 431 size_t url_len) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 const char* data_data, | 469 const char* data_data, |
| 459 size_t data_len) { | 470 size_t data_len) { |
| 460 g_map.Get().Set(format.ToString(), std::string(data_data, data_len)); | 471 g_map.Get().Set(format.ToString(), std::string(data_data, data_len)); |
| 461 } | 472 } |
| 462 | 473 |
| 463 bool RegisterClipboardAndroid(JNIEnv* env) { | 474 bool RegisterClipboardAndroid(JNIEnv* env) { |
| 464 return RegisterNativesImpl(env); | 475 return RegisterNativesImpl(env); |
| 465 } | 476 } |
| 466 | 477 |
| 467 } // namespace ui | 478 } // namespace ui |
| OLD | NEW |