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

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

Issue 59023007: Enable pasting HTML content from the Android clipboard (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « ui/android/java/src/org/chromium/ui/base/Clipboard.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 map_[kHTMLFormat] = ConvertJavaStringToUTF8(java_string_html);
newt (away) 2013/11/26 18:36:14 what if map_ contains an HTML clipboard entry, but
Kristian Monsen 2013/11/26 22:47:39 Updated to do that, and check if the HTML string i
Kristian Monsen 2013/11/27 00:34:04 For slightly complicated reasons we can't do both
151 // happen since hasPlainText has already returned true.
152 // Should only happen if someone is using the clipboard on multiple
153 // threads and clears it out after hasPlainText but before we get here...
154 if (it != map_.end())
155 // We have plain text on this side, but Android doesn't. Nuke ours.
156 map_.clear();
Kristian Monsen 2013/11/27 00:34:04 This should probably just erase the text entry?
157 return;
158 }
159
160 // If Android text differs from ours (or we have none), then copy Android's.
161 std::string android_string = ConvertJavaStringToUTF8(java_string);
162 if (it == map_.end() || it->second != android_string) {
163 map_.clear();
164 map_[kPlainTextFormat] = android_string;
165 } 164 }
166 } 165 }
167 166
168 } // namespace 167 } // namespace
169 168
170 Clipboard::FormatType::FormatType() { 169 Clipboard::FormatType::FormatType() {
171 } 170 }
172 171
173 Clipboard::FormatType::FormatType(const std::string& native_format) 172 Clipboard::FormatType::FormatType(const std::string& native_format)
174 : data_(native_format) { 173 : data_(native_format) {
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 const char* data_data, size_t data_len) { 422 const char* data_data, size_t data_len) {
424 g_map.Get().Set(format.data(), std::string(data_data, data_len)); 423 g_map.Get().Set(format.data(), std::string(data_data, data_len));
425 } 424 }
426 425
427 // See clipboard_android_initialization.h for more information. 426 // See clipboard_android_initialization.h for more information.
428 bool RegisterClipboardAndroid(JNIEnv* env) { 427 bool RegisterClipboardAndroid(JNIEnv* env) {
429 return RegisterNativesImpl(env); 428 return RegisterNativesImpl(env);
430 } 429 }
431 430
432 } // namespace ui 431 } // namespace ui
OLDNEW
« no previous file with comments | « ui/android/java/src/org/chromium/ui/base/Clipboard.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698