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<MethodID::TYPE_INSTANCE>( |
79 "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); | 79 env, context_class.obj(), "getSystemService", |
| 80 "(Ljava/lang/String;)Ljava/lang/Object;"); |
80 | 81 |
81 // Retrieve the system service. | 82 // Retrieve the system service. |
82 ScopedJavaLocalRef<jstring> service_name(env, env->NewStringUTF("clipboard")); | 83 ScopedJavaLocalRef<jstring> service_name(env, env->NewStringUTF("clipboard")); |
83 clipboard_manager_.Reset(env, env->CallObjectMethod(context, | 84 clipboard_manager_.Reset(env, env->CallObjectMethod(context, |
84 get_system_service, service_name.obj())); | 85 get_system_service, service_name.obj())); |
85 DCHECK(clipboard_manager_.obj() && !ClearException(env)); | 86 DCHECK(clipboard_manager_.obj() && !ClearException(env)); |
86 | 87 |
87 // Retain a few methods we'll keep using. | 88 // Retain a few methods we'll keep using. |
88 ScopedJavaLocalRef<jclass> clipboard_class = | 89 ScopedJavaLocalRef<jclass> clipboard_class = |
89 GetClass(env, "android/text/ClipboardManager"); | 90 GetClass(env, "android/text/ClipboardManager"); |
90 set_text_ = GetMethodID(env, clipboard_class, | 91 set_text_ = MethodID::Get<MethodID::TYPE_INSTANCE>( |
91 "setText", "(Ljava/lang/CharSequence;)V"); | 92 env, clipboard_class.obj(), "setText", "(Ljava/lang/CharSequence;)V"); |
92 get_text_ = GetMethodID(env, clipboard_class, | 93 get_text_ = MethodID::Get<MethodID::TYPE_INSTANCE>( |
93 "getText", "()Ljava/lang/CharSequence;"); | 94 env, clipboard_class.obj(), "getText", "()Ljava/lang/CharSequence;"); |
94 has_text_ = GetMethodID(env, clipboard_class, | 95 has_text_ = MethodID::Get<MethodID::TYPE_INSTANCE>( |
95 "hasText", "()Z"); | 96 env, clipboard_class.obj(), "hasText", "()Z"); |
96 | 97 |
97 // Will need to call toString as CharSequence is not always a String. | 98 // Will need to call toString as CharSequence is not always a String. |
98 ScopedJavaLocalRef<jclass> charsequence_class = | 99 ScopedJavaLocalRef<jclass> charsequence_class = |
99 GetClass(env, "java/lang/CharSequence"); | 100 GetClass(env, "java/lang/CharSequence"); |
100 to_string_ = GetMethodID(env, charsequence_class, | 101 to_string_ = MethodID::Get<MethodID::TYPE_INSTANCE>( |
101 "toString", "()Ljava/lang/String;"); | 102 env, charsequence_class.obj(), "toString", "()Ljava/lang/String;"); |
102 } | 103 } |
103 | 104 |
104 std::string ClipboardMap::Get(const std::string& format) { | 105 std::string ClipboardMap::Get(const std::string& format) { |
105 base::AutoLock lock(lock_); | 106 base::AutoLock lock(lock_); |
106 SyncWithAndroidClipboard(); | 107 SyncWithAndroidClipboard(); |
107 std::map<std::string, std::string>::const_iterator it = map_.find(format); | 108 std::map<std::string, std::string>::const_iterator it = map_.find(format); |
108 return it == map_.end() ? std::string() : it->second; | 109 return it == map_.end() ? std::string() : it->second; |
109 } | 110 } |
110 | 111 |
111 bool ClipboardMap::HasFormat(const std::string& format) { | 112 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); | 397 packed += std::string(pixel_data, bm_size); |
397 g_map.Get().Set(kBitmapFormat, packed); | 398 g_map.Get().Set(kBitmapFormat, packed); |
398 } | 399 } |
399 | 400 |
400 void Clipboard::WriteData(const Clipboard::FormatType& format, | 401 void Clipboard::WriteData(const Clipboard::FormatType& format, |
401 const char* data_data, size_t data_len) { | 402 const char* data_data, size_t data_len) { |
402 g_map.Get().Set(format.data(), std::string(data_data, data_len)); | 403 g_map.Get().Set(format.data(), std::string(data_data, data_len)); |
403 } | 404 } |
404 | 405 |
405 } // namespace ui | 406 } // namespace ui |
OLD | NEW |