| 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/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
| 14 | 14 |
| 15 // Important note: | 15 // Important note: |
| 16 // The Android clipboard system only supports text format. So we use the | 16 // The Android clipboard system only supports text format. So we use the |
| 17 // Android system when some text is added or retrieved from the system. For | 17 // Android system when some text is added or retrieved from the system. For |
| 18 // anything else, we currently store the value in some process wide static | 18 // anything else, we currently store the value in some process wide static |
| 19 // variable protected by a lock. So the (non-text) clipboard will only work | 19 // variable protected by a lock. So the (non-text) clipboard will only work |
| 20 // within the same process. | 20 // within the same process. |
| 21 | 21 |
| 22 using base::android::AttachCurrentThread; | 22 using base::android::AttachCurrentThread; |
| 23 using base::android::CheckException; | 23 using base::android::CheckException; |
| 24 using base::android::ClearException; | 24 using base::android::ClearException; |
| 25 using base::android::ConvertJavaStringToUTF16; | 25 using base::android::ConvertJavaStringToUTF16; |
| 26 using base::android::ConvertJavaStringToUTF8; | 26 using base::android::ConvertJavaStringToUTF8; |
| 27 using base::android::GetClass; | 27 using base::android::GetClass; |
| 28 using base::android::GetMethodID; | 28 using base::android::MethodID; |
| 29 using base::android::ScopedJavaLocalRef; | 29 using base::android::ScopedJavaLocalRef; |
| 30 | 30 |
| 31 namespace ui { | 31 namespace ui { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 // Various formats we support. | 34 // Various formats we support. |
| 35 const char kPlainTextFormat[] = "text"; | 35 const char kPlainTextFormat[] = "text"; |
| 36 const char kHTMLFormat[] = "html"; | 36 const char kHTMLFormat[] = "html"; |
| 37 const char kRTFFormat[] = "rtf"; | 37 const char kRTFFormat[] = "rtf"; |
| 38 const char kBitmapFormat[] = "bitmap"; | 38 const char kBitmapFormat[] = "bitmap"; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 68 | 68 |
| 69 // Get the context. | 69 // Get the context. |
| 70 jobject context = base::android::GetApplicationContext(); | 70 jobject context = base::android::GetApplicationContext(); |
| 71 DCHECK(context); | 71 DCHECK(context); |
| 72 | 72 |
| 73 // Get the context class. | 73 // Get the context class. |
| 74 ScopedJavaLocalRef<jclass> context_class = | 74 ScopedJavaLocalRef<jclass> context_class = |
| 75 GetClass(env, "android/content/Context"); | 75 GetClass(env, "android/content/Context"); |
| 76 | 76 |
| 77 // Get the system service method. | 77 // Get the system service method. |
| 78 jmethodID get_system_service = GetMethodID(env, context_class, | 78 jmethodID get_system_service = MethodID::Get< |
| 79 "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); | 79 MethodID::METHODTYPE_NORMAL, MethodID::EXCEPTIONCHECK_YES>( |
| 80 env, context_class.obj(), "getSystemService", |
| 81 "(Ljava/lang/String;)Ljava/lang/Object;"); |
| 80 | 82 |
| 81 // Retrieve the system service. | 83 // Retrieve the system service. |
| 82 ScopedJavaLocalRef<jstring> service_name(env, env->NewStringUTF("clipboard")); | 84 ScopedJavaLocalRef<jstring> service_name(env, env->NewStringUTF("clipboard")); |
| 83 clipboard_manager_.Reset(env, env->CallObjectMethod(context, | 85 clipboard_manager_.Reset(env, env->CallObjectMethod(context, |
| 84 get_system_service, service_name.obj())); | 86 get_system_service, service_name.obj())); |
| 85 DCHECK(clipboard_manager_.obj() && !ClearException(env)); | 87 DCHECK(clipboard_manager_.obj() && !ClearException(env)); |
| 86 | 88 |
| 87 // Retain a few methods we'll keep using. | 89 // Retain a few methods we'll keep using. |
| 88 ScopedJavaLocalRef<jclass> clipboard_class = | 90 ScopedJavaLocalRef<jclass> clipboard_class = |
| 89 GetClass(env, "android/text/ClipboardManager"); | 91 GetClass(env, "android/text/ClipboardManager"); |
| 90 set_text_ = GetMethodID(env, clipboard_class, | 92 set_text_ = MethodID::Get< |
| 91 "setText", "(Ljava/lang/CharSequence;)V"); | 93 MethodID::METHODTYPE_NORMAL, MethodID::EXCEPTIONCHECK_YES>( |
| 92 get_text_ = GetMethodID(env, clipboard_class, | 94 env, clipboard_class.obj(), "setText", "(Ljava/lang/CharSequence;)V"); |
| 93 "getText", "()Ljava/lang/CharSequence;"); | 95 get_text_ = MethodID::Get< |
| 94 has_text_ = GetMethodID(env, clipboard_class, | 96 MethodID::METHODTYPE_NORMAL, MethodID::EXCEPTIONCHECK_YES>( |
| 95 "hasText", "()Z"); | 97 env, clipboard_class.obj(), "getText", "()Ljava/lang/CharSequence;"); |
| 98 has_text_ = MethodID::Get< |
| 99 MethodID::METHODTYPE_NORMAL, MethodID::EXCEPTIONCHECK_YES>( |
| 100 env, clipboard_class.obj(), "hasText", "()Z"); |
| 96 | 101 |
| 97 // Will need to call toString as CharSequence is not always a String. | 102 // Will need to call toString as CharSequence is not always a String. |
| 98 ScopedJavaLocalRef<jclass> charsequence_class = | 103 ScopedJavaLocalRef<jclass> charsequence_class = |
| 99 GetClass(env, "java/lang/CharSequence"); | 104 GetClass(env, "java/lang/CharSequence"); |
| 100 to_string_ = GetMethodID(env, charsequence_class, | 105 to_string_ = MethodID::Get< |
| 101 "toString", "()Ljava/lang/String;"); | 106 MethodID::METHODTYPE_NORMAL, MethodID::EXCEPTIONCHECK_YES>( |
| 107 env, charsequence_class.obj(), "toString", "()Ljava/lang/String;"); |
| 102 } | 108 } |
| 103 | 109 |
| 104 std::string ClipboardMap::Get(const std::string& format) { | 110 std::string ClipboardMap::Get(const std::string& format) { |
| 105 base::AutoLock lock(lock_); | 111 base::AutoLock lock(lock_); |
| 106 SyncWithAndroidClipboard(); | 112 SyncWithAndroidClipboard(); |
| 107 std::map<std::string, std::string>::const_iterator it = map_.find(format); | 113 std::map<std::string, std::string>::const_iterator it = map_.find(format); |
| 108 return it == map_.end() ? std::string() : it->second; | 114 return it == map_.end() ? std::string() : it->second; |
| 109 } | 115 } |
| 110 | 116 |
| 111 bool ClipboardMap::HasFormat(const std::string& format) { | 117 bool ClipboardMap::HasFormat(const std::string& format) { |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 packed += std::string(pixel_data, bm_size); | 402 packed += std::string(pixel_data, bm_size); |
| 397 g_map.Get().Set(kBitmapFormat, packed); | 403 g_map.Get().Set(kBitmapFormat, packed); |
| 398 } | 404 } |
| 399 | 405 |
| 400 void Clipboard::WriteData(const Clipboard::FormatType& format, | 406 void Clipboard::WriteData(const Clipboard::FormatType& format, |
| 401 const char* data_data, size_t data_len) { | 407 const char* data_data, size_t data_len) { |
| 402 g_map.Get().Set(format.data(), std::string(data_data, data_len)); | 408 g_map.Get().Set(format.data(), std::string(data_data, data_len)); |
| 403 } | 409 } |
| 404 | 410 |
| 405 } // namespace ui | 411 } // namespace ui |
| OLD | NEW |