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(); |
143 return; | 156 } |
144 } | 157 } |
145 | 158 |
146 ScopedJavaLocalRef<jstring> java_string = | 159 // Update the html clipboard entry |
147 Java_Clipboard_getCoercedText(env, clipboard_manager_.obj()); | 160 ScopedJavaLocalRef<jstring> java_string_html = |
148 | 161 Java_Clipboard_getHtmlText(env, clipboard_manager_.obj()); |
149 if (!java_string.obj()) { | 162 if (java_string_html.obj()) { |
150 // Tolerate a null value from the Java side, even though that should not | 163 std::string android_string = ConvertJavaStringToUTF8(java_string_html); |
151 // happen since hasPlainText has already returned true. | 164 if (!android_string.empty()) { |
152 // Should only happen if someone is using the clipboard on multiple | 165 map_[kHTMLFormat] = ConvertJavaStringToUTF8(java_string_html); |
bulach
2013/11/27 16:26:38
nit: s/ConvertJavaStringToUTF8(java_string_html)/a
Kristian Monsen
2013/12/03 17:11:17
Done.
| |
153 // threads and clears it out after hasPlainText but before we get here... | 166 return; |
154 if (it != map_.end()) | 167 } |
155 // We have plain text on this side, but Android doesn't. Nuke ours. | |
156 map_.clear(); | |
157 return; | |
158 } | 168 } |
159 | 169 it = map_.find(kHTMLFormat); |
160 // If Android text differs from ours (or we have none), then copy Android's. | 170 if (it != map_.end()) { |
161 std::string android_string = ConvertJavaStringToUTF8(java_string); | 171 map_.erase(kHTMLFormat); |
162 if (it == map_.end() || it->second != android_string) { | |
163 map_.clear(); | |
164 map_[kPlainTextFormat] = android_string; | |
165 } | 172 } |
166 } | 173 } |
167 | 174 |
168 } // namespace | 175 } // namespace |
169 | 176 |
170 Clipboard::FormatType::FormatType() { | 177 Clipboard::FormatType::FormatType() { |
171 } | 178 } |
172 | 179 |
173 Clipboard::FormatType::FormatType(const std::string& native_format) | 180 Clipboard::FormatType::FormatType(const std::string& native_format) |
174 : data_(native_format) { | 181 : 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) { | 430 const char* data_data, size_t data_len) { |
424 g_map.Get().Set(format.data(), std::string(data_data, data_len)); | 431 g_map.Get().Set(format.data(), std::string(data_data, data_len)); |
425 } | 432 } |
426 | 433 |
427 // See clipboard_android_initialization.h for more information. | 434 // See clipboard_android_initialization.h for more information. |
428 bool RegisterClipboardAndroid(JNIEnv* env) { | 435 bool RegisterClipboardAndroid(JNIEnv* env) { |
429 return RegisterNativesImpl(env); | 436 return RegisterNativesImpl(env); |
430 } | 437 } |
431 | 438 |
432 } // namespace ui | 439 } // namespace ui |
OLD | NEW |