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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return ContainsKey(map_, format); | 90 return ContainsKey(map_, format); |
91 } | 91 } |
92 | 92 |
93 void ClipboardMap::Set(const std::string& format, const std::string& data) { | 93 void ClipboardMap::Set(const std::string& format, const std::string& data) { |
94 JNIEnv* env = AttachCurrentThread(); | 94 JNIEnv* env = AttachCurrentThread(); |
95 base::AutoLock lock(lock_); | 95 base::AutoLock lock(lock_); |
96 SyncWithAndroidClipboard(); | 96 SyncWithAndroidClipboard(); |
97 | 97 |
98 map_[format] = data; | 98 map_[format] = data; |
99 if (format == kPlainTextFormat) { | 99 if (format == kPlainTextFormat) { |
100 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString( | 100 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, data); |
101 env, data.c_str()); | 101 DCHECK(str.obj()); |
102 DCHECK(str.obj() && !ClearException(env)); | 102 |
103 Java_Clipboard_setText(env, clipboard_manager_.obj(), str.obj()); | 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()); |
104 } | 120 } |
105 } | 121 } |
106 | 122 |
107 void ClipboardMap::Clear() { | 123 void ClipboardMap::Clear() { |
108 JNIEnv* env = AttachCurrentThread(); | 124 JNIEnv* env = AttachCurrentThread(); |
109 base::AutoLock lock(lock_); | 125 base::AutoLock lock(lock_); |
110 map_.clear(); | 126 map_.clear(); |
111 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL); | 127 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL); |
112 } | 128 } |
113 | 129 |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 const char* data_data, size_t data_len) { | 424 const char* data_data, size_t data_len) { |
409 g_map.Get().Set(format.data(), std::string(data_data, data_len)); | 425 g_map.Get().Set(format.data(), std::string(data_data, data_len)); |
410 } | 426 } |
411 | 427 |
412 // See clipboard_android_initialization.h for more information. | 428 // See clipboard_android_initialization.h for more information. |
413 bool RegisterClipboardAndroid(JNIEnv* env) { | 429 bool RegisterClipboardAndroid(JNIEnv* env) { |
414 return RegisterNativesImpl(env); | 430 return RegisterNativesImpl(env); |
415 } | 431 } |
416 | 432 |
417 } // namespace ui | 433 } // namespace ui |
OLD | NEW |