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
3 // found in the LICENSE file.
5 #include "ui/base/clipboard/clipboard.h"
7 #include "base/android/jni_string.h"
8 #include "base/lazy_instance.h"
9 #include "base/stl_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/synchronization/lock.h"
12 #include "jni/Clipboard_jni.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/base/clipboard/clipboard_android_initialization.h"
15 #include "ui/gfx/size.h"
17 // TODO:(andrewhayden) Support additional formats in Android: Bitmap, URI, HTML,
18 // HTML+text now that Android's clipboard system supports them, then nuke the
19 // legacy implementation note below.
21 // Legacy implementation note:
22 // The Android clipboard system used to only support text format. So we used the
23 // Android system when some text was added or retrieved from the system. For
24 // anything else, we STILL store the value in some process wide static
25 // variable protected by a lock. So the (non-text) clipboard will only work
26 // within the same process.
28 using base::android::AttachCurrentThread;
29 using base::android::ClearException;
30 using base::android::ConvertJavaStringToUTF8;
31 using base::android::ConvertUTF8ToJavaString;
32 using base::android::ScopedJavaGlobalRef;
33 using base::android::ScopedJavaLocalRef;
38 // Various formats we support.
39 const char kPlainTextFormat[] = "text";
40 const char kHTMLFormat[] = "html";
41 const char kRTFFormat[] = "rtf";
42 const char kBitmapFormat[] = "bitmap";
43 const char kWebKitSmartPasteFormat[] = "webkit_smart";
44 const char kBookmarkFormat[] = "bookmark";
45 const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data";
46 const char kMimeTypeWebCustomData[] = "chromium/x-web-custom-data";
51 std::string Get(const std::string& format);
52 bool HasFormat(const std::string& format);
53 void Set(const std::string& format, const std::string& data);
57 void SyncWithAndroidClipboard();
58 std::map<std::string, std::string> map_;
61 // Java class and methods for the Android ClipboardManager.
62 ScopedJavaGlobalRef<jobject> clipboard_manager_;
64 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER;
66 ClipboardMap::ClipboardMap() {
67 JNIEnv* env = AttachCurrentThread();
71 jobject context = base::android::GetApplicationContext();
74 ScopedJavaLocalRef<jobject> local_ref =
75 Java_Clipboard_create(env, context);
76 DCHECK(local_ref.obj());
77 clipboard_manager_.Reset(env, local_ref.Release());
80 std::string ClipboardMap::Get(const std::string& format) {
81 base::AutoLock lock(lock_);
82 SyncWithAndroidClipboard();
83 std::map<std::string, std::string>::const_iterator it = map_.find(format);
84 return it == map_.end() ? std::string() : it->second;
87 bool ClipboardMap::HasFormat(const std::string& format) {
88 base::AutoLock lock(lock_);
89 SyncWithAndroidClipboard();
90 return ContainsKey(map_, format);
93 void ClipboardMap::Set(const std::string& format, const std::string& data) {
94 JNIEnv* env = AttachCurrentThread();
95 base::AutoLock lock(lock_);
96 SyncWithAndroidClipboard();
99 if (format == kPlainTextFormat) {
100 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, data);
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))
113 ScopedJavaLocalRef<jstring> html = ConvertUTF8ToJavaString(env, data);
114 ScopedJavaLocalRef<jstring> text = ConvertUTF8ToJavaString(
115 env, map_[kPlainTextFormat].c_str());
117 DCHECK(html.obj() && text.obj());
118 Java_Clipboard_setHTMLText(
119 env, clipboard_manager_.obj(), html.obj(), text.obj());
123 void ClipboardMap::Clear() {
124 JNIEnv* env = AttachCurrentThread();
125 base::AutoLock lock(lock_);
127 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL);
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.
132 void ClipboardMap::SyncWithAndroidClipboard() {
133 lock_.AssertAcquired();
134 JNIEnv* env = AttachCurrentThread();
136 std::map<std::string, std::string>::const_iterator it =
137 map_.find(kPlainTextFormat);
139 if (!Java_Clipboard_hasPlainText(env, clipboard_manager_.obj())) {
140 if (it != map_.end())
141 // We have plain text on this side, but Android doesn't. Nuke ours.
146 ScopedJavaLocalRef<jstring> java_string =
147 Java_Clipboard_getCoercedText(env, clipboard_manager_.obj());
149 if (!java_string.obj()) {
150 // Tolerate a null value from the Java side, even though that should not
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.
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) {
164 map_[kPlainTextFormat] = android_string;
170 Clipboard::FormatType::FormatType() {
173 Clipboard::FormatType::FormatType(const std::string& native_format)
174 : data_(native_format) {
177 Clipboard::FormatType::~FormatType() {
180 std::string Clipboard::FormatType::Serialize() const {
185 Clipboard::FormatType Clipboard::FormatType::Deserialize(
186 const std::string& serialization) {
187 return FormatType(serialization);
190 bool Clipboard::FormatType::Equals(const FormatType& other) const {
191 return data_ == other.data_;
194 Clipboard::Clipboard() {
195 DCHECK(CalledOnValidThread());
198 Clipboard::~Clipboard() {
199 DCHECK(CalledOnValidThread());
202 // Main entry point used to write several values in the clipboard.
203 void Clipboard::WriteObjects(ClipboardType type, const ObjectMap& objects) {
204 DCHECK(CalledOnValidThread());
205 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
207 for (ObjectMap::const_iterator iter = objects.begin();
208 iter != objects.end(); ++iter) {
209 DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
213 uint64 Clipboard::GetSequenceNumber(ClipboardType /* type */) {
214 DCHECK(CalledOnValidThread());
215 // TODO: implement this. For now this interface will advertise
216 // that the clipboard never changes. That's fine as long as we
217 // don't rely on this signal.
221 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format,
222 ClipboardType type) const {
223 DCHECK(CalledOnValidThread());
224 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
225 return g_map.Get().HasFormat(format.data());
228 void Clipboard::Clear(ClipboardType type) {
229 DCHECK(CalledOnValidThread());
230 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
234 void Clipboard::ReadAvailableTypes(ClipboardType type,
235 std::vector<string16>* types,
236 bool* contains_filenames) const {
237 DCHECK(CalledOnValidThread());
238 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
240 if (!types || !contains_filenames) {
248 *contains_filenames = false;
251 void Clipboard::ReadText(ClipboardType type, string16* result) const {
252 DCHECK(CalledOnValidThread());
253 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
255 ReadAsciiText(type, &utf8);
256 *result = UTF8ToUTF16(utf8);
259 void Clipboard::ReadAsciiText(ClipboardType type, std::string* result) const {
260 DCHECK(CalledOnValidThread());
261 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
262 *result = g_map.Get().Get(kPlainTextFormat);
265 // Note: |src_url| isn't really used. It is only implemented in Windows
266 void Clipboard::ReadHTML(ClipboardType type,
268 std::string* src_url,
269 uint32* fragment_start,
270 uint32* fragment_end) const {
271 DCHECK(CalledOnValidThread());
272 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
276 std::string input = g_map.Get().Get(kHTMLFormat);
277 *markup = UTF8ToUTF16(input);
280 *fragment_end = static_cast<uint32>(markup->length());
283 void Clipboard::ReadRTF(ClipboardType type, std::string* result) const {
284 DCHECK(CalledOnValidThread());
288 SkBitmap Clipboard::ReadImage(ClipboardType type) const {
289 DCHECK(CalledOnValidThread());
290 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
291 std::string input = g_map.Get().Get(kBitmapFormat);
294 if (!input.empty()) {
295 DCHECK_LE(sizeof(gfx::Size), input.size());
296 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(input.data());
299 SkBitmap::kARGB_8888_Config, size->width(), size->height(), 0);
302 int bm_size = size->width() * size->height() * 4;
303 DCHECK_EQ(sizeof(gfx::Size) + bm_size, input.size());
305 memcpy(bmp.getPixels(), input.data() + sizeof(gfx::Size), bm_size);
310 void Clipboard::ReadCustomData(ClipboardType clipboard_type,
311 const string16& type,
312 string16* result) const {
313 DCHECK(CalledOnValidThread());
317 void Clipboard::ReadBookmark(string16* title, std::string* url) const {
318 DCHECK(CalledOnValidThread());
322 void Clipboard::ReadData(const Clipboard::FormatType& format,
323 std::string* result) const {
324 DCHECK(CalledOnValidThread());
325 *result = g_map.Get().Get(format.data());
329 Clipboard::FormatType Clipboard::GetFormatType(
330 const std::string& format_string) {
331 return FormatType::Deserialize(format_string);
335 const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() {
336 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPlainTextFormat));
341 const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() {
342 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPlainTextFormat));
347 const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() {
348 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebKitSmartPasteFormat));
353 const Clipboard::FormatType& Clipboard::GetHtmlFormatType() {
354 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kHTMLFormat));
359 const Clipboard::FormatType& Clipboard::GetRtfFormatType() {
360 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kRTFFormat));
365 const Clipboard::FormatType& Clipboard::GetBitmapFormatType() {
366 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kBitmapFormat));
371 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() {
372 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeWebCustomData));
377 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() {
378 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypePepperCustomData));
382 void Clipboard::WriteText(const char* text_data, size_t text_len) {
383 g_map.Get().Set(kPlainTextFormat, std::string(text_data, text_len));
386 void Clipboard::WriteHTML(const char* markup_data,
388 const char* url_data,
390 g_map.Get().Set(kHTMLFormat, std::string(markup_data, markup_len));
393 void Clipboard::WriteRTF(const char* rtf_data, size_t data_len) {
397 // Note: according to other platforms implementations, this really writes the
399 void Clipboard::WriteBookmark(const char* title_data, size_t title_len,
400 const char* url_data, size_t url_len) {
401 g_map.Get().Set(kBookmarkFormat, std::string(url_data, url_len));
404 // Write an extra flavor that signifies WebKit was the last to modify the
405 // pasteboard. This flavor has no data.
406 void Clipboard::WriteWebSmartPaste() {
407 g_map.Get().Set(kWebKitSmartPasteFormat, std::string());
410 // All platforms use gfx::Size for size data but it is passed as a const char*
411 // Further, pixel_data is expected to be 32 bits per pixel
412 // Note: we implement this to pass all unit tests but it is currently unclear
413 // how some code would consume this.
414 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) {
415 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data);
416 int bm_size = size->width() * size->height() * 4;
418 std::string packed(size_data, sizeof(gfx::Size));
419 packed += std::string(pixel_data, bm_size);
420 g_map.Get().Set(kBitmapFormat, packed);
423 void Clipboard::WriteData(const Clipboard::FormatType& format,
424 const char* data_data, size_t data_len) {
425 g_map.Get().Set(format.data(), std::string(data_data, data_len));
428 // See clipboard_android_initialization.h for more information.
429 bool RegisterClipboardAndroid(JNIEnv* env) {
430 return RegisterNativesImpl(env);