clipboard_android.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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. 4 5#include "ui/base/clipboard/clipboard.h" 6 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" 16 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. 20 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. 27 28using base::android::AttachCurrentThread; 29using base::android::ClearException; 30using base::android::ConvertJavaStringToUTF8; 31using base::android::ConvertUTF8ToJavaString; 32using base::android::ScopedJavaGlobalRef; 33using base::android::ScopedJavaLocalRef; 34 35namespace ui { 36 37namespace { 38// Various formats we support. 39const char kPlainTextFormat[] = "text"; 40const char kHTMLFormat[] = "html"; 41const char kRTFFormat[] = "rtf"; 42const char kBitmapFormat[] = "bitmap"; 43const char kWebKitSmartPasteFormat[] = "webkit_smart"; 44const char kBookmarkFormat[] = "bookmark"; 45const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data"; 46const char kMimeTypeWebCustomData[] = "chromium/x-web-custom-data"; 47 48class ClipboardMap { 49 public: 50 ClipboardMap(); 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); 54 void Clear(); 55 56 private: 57 void SyncWithAndroidClipboard(); 58 std::map<std::string, std::string> map_; 59 base::Lock lock_; 60 61 // Java class and methods for the Android ClipboardManager. 62 ScopedJavaGlobalRef<jobject> clipboard_manager_; 63}; 64base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER; 65 66ClipboardMap::ClipboardMap() { 67 JNIEnv* env = AttachCurrentThread(); 68 DCHECK(env); 69 70 // Get the context. 71 jobject context = base::android::GetApplicationContext(); 72 DCHECK(context); 73 74 ScopedJavaLocalRef<jobject> local_ref = 75 Java_Clipboard_create(env, context); 76 DCHECK(local_ref.obj()); 77 clipboard_manager_.Reset(env, local_ref.Release()); 78} 79 80std::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; 85} 86 87bool ClipboardMap::HasFormat(const std::string& format) { 88 base::AutoLock lock(lock_); 89 SyncWithAndroidClipboard(); 90 return ContainsKey(map_, format); 91} 92 93void ClipboardMap::Set(const std::string& format, const std::string& data) { 94 JNIEnv* env = AttachCurrentThread(); 95 base::AutoLock lock(lock_); 96 SyncWithAndroidClipboard(); 97 98 map_[format] = data; 99 if (format == kPlainTextFormat) { 100 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, data); 101 DCHECK(str.obj()); 102 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()); 120 } 121} 122 123void ClipboardMap::Clear() { 124 JNIEnv* env = AttachCurrentThread(); 125 base::AutoLock lock(lock_); 126 map_.clear(); 127 Java_Clipboard_setText(env, clipboard_manager_.obj(), NULL); 128} 129 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// If there is an HTML entry in the Android clipboard it gets inserted in the 133// map. 134void ClipboardMap::SyncWithAndroidClipboard() { 135 lock_.AssertAcquired(); 136 JNIEnv* env = AttachCurrentThread(); 137 138 // Update the plain text clipboard entry 139 std::map<std::string, std::string>::const_iterator it = 140 map_.find(kPlainTextFormat); 141 ScopedJavaLocalRef<jstring> java_string_text = 142 Java_Clipboard_getCoercedText(env, clipboard_manager_.obj()); 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()) { 154 // We have plain text on this side, but Android doesn't. Nuke ours. 155 map_.clear(); 156 } 157 } 158 159 if (!Java_Clipboard_isHTMLClipboardSupported(env)) { 160 return; 161 } 162 163 // Update the html clipboard entry 164 ScopedJavaLocalRef<jstring> java_string_html = 165 Java_Clipboard_getHTMLText(env, clipboard_manager_.obj()); 166 if (java_string_html.obj()) { 167 std::string android_string = ConvertJavaStringToUTF8(java_string_html); 168 if (!android_string.empty()) { 169 map_[kHTMLFormat] = android_string; 170 return; 171 } 172 } 173 it = map_.find(kHTMLFormat); 174 if (it != map_.end()) { 175 map_.erase(kHTMLFormat); 176 } 177} 178 179} // namespace 180 181Clipboard::FormatType::FormatType() { 182} 183 184Clipboard::FormatType::FormatType(const std::string& native_format) 185 : data_(native_format) { 186} 187 188Clipboard::FormatType::~FormatType() { 189} 190 191std::string Clipboard::FormatType::Serialize() const { 192 return data_; 193} 194 195// static 196Clipboard::FormatType Clipboard::FormatType::Deserialize( 197 const std::string& serialization) { 198 return FormatType(serialization); 199} 200 201bool Clipboard::FormatType::Equals(const FormatType& other) const { 202 return data_ == other.data_; 203} 204 205Clipboard::Clipboard() { 206 DCHECK(CalledOnValidThread()); 207} 208 209Clipboard::~Clipboard() { 210 DCHECK(CalledOnValidThread()); 211} 212 213// Main entry point used to write several values in the clipboard. 214void Clipboard::WriteObjects(ClipboardType type, const ObjectMap& objects) { 215 DCHECK(CalledOnValidThread()); 216 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 217 g_map.Get().Clear(); 218 for (ObjectMap::const_iterator iter = objects.begin(); 219 iter != objects.end(); ++iter) { 220 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); 221 } 222} 223 224uint64 Clipboard::GetSequenceNumber(ClipboardType /* type */) { 225 DCHECK(CalledOnValidThread()); 226 // TODO: implement this. For now this interface will advertise 227 // that the clipboard never changes. That's fine as long as we 228 // don't rely on this signal. 229 return 0; 230} 231 232bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, 233 ClipboardType type) const { 234 DCHECK(CalledOnValidThread()); 235 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 236 return g_map.Get().HasFormat(format.data()); 237} 238 239void Clipboard::Clear(ClipboardType type) { 240 DCHECK(CalledOnValidThread()); 241 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 242 g_map.Get().Clear(); 243} 244 245void Clipboard::ReadAvailableTypes(ClipboardType type, 246 std::vector<string16>* types, 247 bool* contains_filenames) const { 248 DCHECK(CalledOnValidThread()); 249 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 250 251 if (!types || !contains_filenames) { 252 NOTREACHED(); 253 return; 254 } 255 256 NOTIMPLEMENTED(); 257 258 types->clear(); 259 *contains_filenames = false; 260} 261 262void Clipboard::ReadText(ClipboardType type, string16* result) const { 263 DCHECK(CalledOnValidThread()); 264 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 265 std::string utf8; 266 ReadAsciiText(type, &utf8); 267 *result = UTF8ToUTF16(utf8); 268} 269 270void Clipboard::ReadAsciiText(ClipboardType type, std::string* result) const { 271 DCHECK(CalledOnValidThread()); 272 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 273 *result = g_map.Get().Get(kPlainTextFormat); 274} 275 276// Note: |src_url| isn't really used. It is only implemented in Windows 277void Clipboard::ReadHTML(ClipboardType type, 278 string16* markup, 279 std::string* src_url, 280 uint32* fragment_start, 281 uint32* fragment_end) const { 282 DCHECK(CalledOnValidThread()); 283 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 284 if (src_url) 285 src_url->clear(); 286 287 std::string input = g_map.Get().Get(kHTMLFormat); 288 *markup = UTF8ToUTF16(input); 289 290 *fragment_start = 0; 291 *fragment_end = static_cast<uint32>(markup->length()); 292} 293 294void Clipboard::ReadRTF(ClipboardType type, std::string* result) const { 295 DCHECK(CalledOnValidThread()); 296 NOTIMPLEMENTED(); 297} 298 299SkBitmap Clipboard::ReadImage(ClipboardType type) const { 300 DCHECK(CalledOnValidThread()); 301 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); 302 std::string input = g_map.Get().Get(kBitmapFormat); 303 304 SkBitmap bmp; 305 if (!input.empty()) { 306 DCHECK_LE(sizeof(gfx::Size), input.size()); 307 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(input.data()); 308 309 bmp.setConfig(SkBitmap::kARGB_8888_Config, size->width(), size->height()); 310 bmp.allocPixels(); 311 312 DCHECK_EQ(sizeof(gfx::Size) + bmp.getSize(), input.size()); 313 314 memcpy(bmp.getPixels(), input.data() + sizeof(gfx::Size), bmp.getSize()); 315 } 316 return bmp; 317} 318 319void Clipboard::ReadCustomData(ClipboardType clipboard_type, 320 const string16& type, 321 string16* result) const { 322 DCHECK(CalledOnValidThread()); 323 NOTIMPLEMENTED(); 324} 325 326void Clipboard::ReadBookmark(string16* title, std::string* url) const { 327 DCHECK(CalledOnValidThread()); 328 NOTIMPLEMENTED(); 329} 330 331void Clipboard::ReadData(const Clipboard::FormatType& format, 332 std::string* result) const { 333 DCHECK(CalledOnValidThread()); 334 *result = g_map.Get().Get(format.data()); 335} 336 337// static 338Clipboard::FormatType Clipboard::GetFormatType( 339 const std::string& format_string) { 340 return FormatType::Deserialize(format_string); 341} 342 343// static 344const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() { 345 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPlainTextFormat)); 346 return type; 347} 348 349// static 350const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() { 351 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPlainTextFormat)); 352 return type; 353} 354 355// static 356const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() { 357 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebKitSmartPasteFormat)); 358 return type; 359} 360 361// static 362const Clipboard::FormatType& Clipboard::GetHtmlFormatType() { 363 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kHTMLFormat)); 364 return type; 365} 366 367// static 368const Clipboard::FormatType& Clipboard::GetRtfFormatType() { 369 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kRTFFormat)); 370 return type; 371} 372 373// static 374const Clipboard::FormatType& Clipboard::GetBitmapFormatType() { 375 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kBitmapFormat)); 376 return type; 377} 378 379// static 380const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() { 381 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeWebCustomData)); 382 return type; 383} 384 385// static 386const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { 387 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypePepperCustomData)); 388 return type; 389} 390 391void Clipboard::WriteText(const char* text_data, size_t text_len) { 392 g_map.Get().Set(kPlainTextFormat, std::string(text_data, text_len)); 393} 394 395void Clipboard::WriteHTML(const char* markup_data, 396 size_t markup_len, 397 const char* url_data, 398 size_t url_len) { 399 g_map.Get().Set(kHTMLFormat, std::string(markup_data, markup_len)); 400} 401 402void Clipboard::WriteRTF(const char* rtf_data, size_t data_len) { 403 NOTIMPLEMENTED(); 404} 405 406// Note: according to other platforms implementations, this really writes the 407// URL spec. 408void Clipboard::WriteBookmark(const char* title_data, size_t title_len, 409 const char* url_data, size_t url_len) { 410 g_map.Get().Set(kBookmarkFormat, std::string(url_data, url_len)); 411} 412 413// Write an extra flavor that signifies WebKit was the last to modify the 414// pasteboard. This flavor has no data. 415void Clipboard::WriteWebSmartPaste() { 416 g_map.Get().Set(kWebKitSmartPasteFormat, std::string()); 417} 418 419// Note: we implement this to pass all unit tests but it is currently unclear 420// how some code would consume this. 421void Clipboard::WriteBitmap(const SkBitmap& bitmap) { 422 gfx::Size size(bitmap.width(), bitmap.height()); 423 424 std::string packed(reinterpret_cast<const char*>(&size), sizeof(size)); 425 { 426 SkAutoLockPixels bitmap_lock(bitmap); 427 packed += std::string(static_cast<const char*>(bitmap.getPixels()), 428 bitmap.getSize()); 429 } 430 g_map.Get().Set(kBitmapFormat, packed); 431} 432 433void Clipboard::WriteData(const Clipboard::FormatType& format, 434 const char* data_data, size_t data_len) { 435 g_map.Get().Set(format.data(), std::string(data_data, data_len)); 436} 437 438// See clipboard_android_initialization.h for more information. 439bool RegisterClipboardAndroid(JNIEnv* env) { 440 return RegisterNativesImpl(env); 441} 442 443} // namespace ui 444