| 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.h" | 5 #include "ui/base/clipboard/clipboard.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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 void ClipboardMap::Clear() { | 123 void ClipboardMap::Clear() { |
| 124 JNIEnv* env = AttachCurrentThread(); | 124 JNIEnv* env = AttachCurrentThread(); |
| 125 base::AutoLock lock(lock_); | 125 base::AutoLock lock(lock_); |
| 126 map_.clear(); | 126 map_.clear(); |
| 127 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL); | 127 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // If the internal map contains a plain-text entry and it does not match that | 130 // If the internal map contains a plain-text entry and it does not match that |
| 131 // in the Android clipboard, clear the map and insert the Android text into it. | 131 // in the Android clipboard, clear the map and insert the Android text into it. |
| 132 // If there is an HTML entry in the Android clipboard it gets inserted in the |
| 133 // map. |
| 132 void ClipboardMap::SyncWithAndroidClipboard() { | 134 void ClipboardMap::SyncWithAndroidClipboard() { |
| 133 lock_.AssertAcquired(); | 135 lock_.AssertAcquired(); |
| 134 JNIEnv* env = AttachCurrentThread(); | 136 JNIEnv* env = AttachCurrentThread(); |
| 135 | 137 |
| 138 // Update the plain text clipboard entry |
| 136 std::map<std::string, std::string>::const_iterator it = | 139 std::map<std::string, std::string>::const_iterator it = |
| 137 map_.find(kPlainTextFormat); | 140 map_.find(kPlainTextFormat); |
| 138 | 141 ScopedJavaLocalRef<jstring> java_string_text = |
| 139 if (!Java_Clipboard_hasPlainText(env, clipboard_manager_.obj())) { | 142 Java_Clipboard_getCoercedText(env, clipboard_manager_.obj()); |
| 140 if (it != map_.end()) | 143 if (java_string_text.obj()) { |
| 144 std::string android_string = ConvertJavaStringToUTF8(java_string_text); |
| 145 if (!android_string.empty() && |
| 146 (it == map_.end() || it->second != android_string)) { |
| 147 // There is a different string in the Android clipboard than we have. |
| 148 // Clear the map on our side. |
| 149 map_.clear(); |
| 150 map_[kPlainTextFormat] = android_string; |
| 151 } |
| 152 } else { |
| 153 if (it != map_.end()) { |
| 141 // We have plain text on this side, but Android doesn't. Nuke ours. | 154 // We have plain text on this side, but Android doesn't. Nuke ours. |
| 142 map_.clear(); | 155 map_.clear(); |
| 156 } |
| 157 } |
| 158 |
| 159 if (!Java_Clipboard_isHTMLClipboardSupported(env)) { |
| 143 return; | 160 return; |
| 144 } | 161 } |
| 145 | 162 |
| 146 ScopedJavaLocalRef<jstring> java_string = | 163 // Update the html clipboard entry |
| 147 Java_Clipboard_getCoercedText(env, clipboard_manager_.obj()); | 164 ScopedJavaLocalRef<jstring> java_string_html = |
| 148 | 165 Java_Clipboard_getHTMLText(env, clipboard_manager_.obj()); |
| 149 if (!java_string.obj()) { | 166 if (java_string_html.obj()) { |
| 150 // Tolerate a null value from the Java side, even though that should not | 167 std::string android_string = ConvertJavaStringToUTF8(java_string_html); |
| 151 // happen since hasPlainText has already returned true. | 168 if (!android_string.empty()) { |
| 152 // Should only happen if someone is using the clipboard on multiple | 169 map_[kHTMLFormat] = android_string; |
| 153 // threads and clears it out after hasPlainText but before we get here... | 170 return; |
| 154 if (it != map_.end()) | 171 } |
| 155 // We have plain text on this side, but Android doesn't. Nuke ours. | |
| 156 map_.clear(); | |
| 157 return; | |
| 158 } | 172 } |
| 159 | 173 it = map_.find(kHTMLFormat); |
| 160 // If Android text differs from ours (or we have none), then copy Android's. | 174 if (it != map_.end()) { |
| 161 std::string android_string = ConvertJavaStringToUTF8(java_string); | 175 map_.erase(kHTMLFormat); |
| 162 if (it == map_.end() || it->second != android_string) { | |
| 163 map_.clear(); | |
| 164 map_[kPlainTextFormat] = android_string; | |
| 165 } | 176 } |
| 166 } | 177 } |
| 167 | 178 |
| 168 } // namespace | 179 } // namespace |
| 169 | 180 |
| 170 Clipboard::FormatType::FormatType() { | 181 Clipboard::FormatType::FormatType() { |
| 171 } | 182 } |
| 172 | 183 |
| 173 Clipboard::FormatType::FormatType(const std::string& native_format) | 184 Clipboard::FormatType::FormatType(const std::string& native_format) |
| 174 : data_(native_format) { | 185 : data_(native_format) { |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 const char* data_data, size_t data_len) { | 434 const char* data_data, size_t data_len) { |
| 424 g_map.Get().Set(format.data(), std::string(data_data, data_len)); | 435 g_map.Get().Set(format.data(), std::string(data_data, data_len)); |
| 425 } | 436 } |
| 426 | 437 |
| 427 // See clipboard_android_initialization.h for more information. | 438 // See clipboard_android_initialization.h for more information. |
| 428 bool RegisterClipboardAndroid(JNIEnv* env) { | 439 bool RegisterClipboardAndroid(JNIEnv* env) { |
| 429 return RegisterNativesImpl(env); | 440 return RegisterNativesImpl(env); |
| 430 } | 441 } |
| 431 | 442 |
| 432 } // namespace ui | 443 } // namespace ui |
| OLD | NEW |