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_android.h" | 5 #include "ui/base/clipboard/clipboard_android.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
12 #include "base/callback.h" | |
12 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "jni/Clipboard_jni.h" | 18 #include "jni/Clipboard_jni.h" |
18 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
19 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/geometry/size.h" |
20 | 21 |
21 // TODO:(andrewhayden) Support additional formats in Android: Bitmap, URI, HTML, | 22 // TODO:(andrewhayden) Support additional formats in Android: Bitmap, URI, HTML, |
(...skipping 23 matching lines...) Expand all Loading... | |
45 const char kPlainTextFormat[] = "text"; | 46 const char kPlainTextFormat[] = "text"; |
46 const char kHTMLFormat[] = "html"; | 47 const char kHTMLFormat[] = "html"; |
47 const char kRTFFormat[] = "rtf"; | 48 const char kRTFFormat[] = "rtf"; |
48 const char kBitmapFormat[] = "bitmap"; | 49 const char kBitmapFormat[] = "bitmap"; |
49 const char kWebKitSmartPasteFormat[] = "webkit_smart"; | 50 const char kWebKitSmartPasteFormat[] = "webkit_smart"; |
50 const char kBookmarkFormat[] = "bookmark"; | 51 const char kBookmarkFormat[] = "bookmark"; |
51 | 52 |
52 class ClipboardMap { | 53 class ClipboardMap { |
53 public: | 54 public: |
54 ClipboardMap(); | 55 ClipboardMap(); |
56 void SetModifiedCallback(ClipboardAndroid::Modified cb); | |
55 std::string Get(const std::string& format); | 57 std::string Get(const std::string& format); |
56 uint64_t GetSequenceNumber() const; | 58 uint64_t GetSequenceNumber() const; |
57 base::Time GetLastModifiedTime() const; | 59 base::Time GetLastModifiedTime() const; |
58 void ClearLastModifiedTime(); | 60 void ClearLastModifiedTime(); |
59 bool HasFormat(const std::string& format); | 61 bool HasFormat(const std::string& format); |
60 void OnPrimaryClipboardChanged(); | 62 void OnPrimaryClipboardChanged(); |
61 void Set(const std::string& format, const std::string& data); | 63 void Set(const std::string& format, const std::string& data); |
62 void CommitToAndroidClipboard(); | 64 void CommitToAndroidClipboard(); |
63 void Clear(); | 65 void Clear(); |
64 | 66 |
67 // Unlike the functions above, does not call |modified_cb_|. | |
68 void SetLastModifiedTimeWithoutRunningCallback(base::Time time); | |
69 | |
65 private: | 70 private: |
66 enum class MapState { | 71 enum class MapState { |
67 kOutOfDate, | 72 kOutOfDate, |
68 kUpToDate, | 73 kUpToDate, |
69 kPreparingCommit, | 74 kPreparingCommit, |
70 }; | 75 }; |
71 | 76 |
77 // Updates |last_modified_time_| to |time| and writes it to |local_state_|. | |
78 void UpdateLastModifiedTime(base::Time time); | |
79 | |
80 // Updates |map_| and |map_state_| if necessary by fetching data from Java. | |
72 void UpdateFromAndroidClipboard(); | 81 void UpdateFromAndroidClipboard(); |
82 | |
73 std::map<std::string, std::string> map_; | 83 std::map<std::string, std::string> map_; |
74 MapState map_state_; | 84 MapState map_state_; |
75 base::Lock lock_; | 85 base::Lock lock_; |
76 | 86 |
77 uint64_t sequence_number_; | 87 uint64_t sequence_number_; |
78 base::Time last_modified_time_; | 88 base::Time last_modified_time_; |
79 | 89 |
90 ClipboardAndroid::Modified modified_cb_; | |
91 | |
80 // Java class and methods for the Android ClipboardManager. | 92 // Java class and methods for the Android ClipboardManager. |
81 ScopedJavaGlobalRef<jobject> clipboard_manager_; | 93 ScopedJavaGlobalRef<jobject> clipboard_manager_; |
82 }; | 94 }; |
83 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER; | 95 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER; |
84 | 96 |
85 ClipboardMap::ClipboardMap() : map_state_(MapState::kOutOfDate) { | 97 ClipboardMap::ClipboardMap() : map_state_(MapState::kOutOfDate) { |
86 clipboard_manager_.Reset(Java_Clipboard_getInstance(AttachCurrentThread())); | 98 clipboard_manager_.Reset(Java_Clipboard_getInstance(AttachCurrentThread())); |
87 DCHECK(clipboard_manager_.obj()); | 99 DCHECK(clipboard_manager_.obj()); |
88 } | 100 } |
89 | 101 |
102 void ClipboardMap::SetModifiedCallback(ClipboardAndroid::Modified cb) { | |
103 modified_cb_ = std::move(cb); | |
dcheng
2017/04/27 15:25:39
Nit: #include <utility>
Mark P
2017/04/27 19:25:59
Done.
| |
104 } | |
105 | |
90 std::string ClipboardMap::Get(const std::string& format) { | 106 std::string ClipboardMap::Get(const std::string& format) { |
91 base::AutoLock lock(lock_); | 107 base::AutoLock lock(lock_); |
92 UpdateFromAndroidClipboard(); | 108 UpdateFromAndroidClipboard(); |
93 std::map<std::string, std::string>::const_iterator it = map_.find(format); | 109 std::map<std::string, std::string>::const_iterator it = map_.find(format); |
94 return it == map_.end() ? std::string() : it->second; | 110 return it == map_.end() ? std::string() : it->second; |
95 } | 111 } |
96 | 112 |
97 uint64_t ClipboardMap::GetSequenceNumber() const { | 113 uint64_t ClipboardMap::GetSequenceNumber() const { |
98 return sequence_number_; | 114 return sequence_number_; |
99 } | 115 } |
100 | 116 |
101 base::Time ClipboardMap::GetLastModifiedTime() const { | 117 base::Time ClipboardMap::GetLastModifiedTime() const { |
102 return last_modified_time_; | 118 return last_modified_time_; |
103 } | 119 } |
104 | 120 |
105 void ClipboardMap::ClearLastModifiedTime() { | 121 void ClipboardMap::ClearLastModifiedTime() { |
106 last_modified_time_ = base::Time(); | 122 UpdateLastModifiedTime(base::Time()); |
107 } | 123 } |
108 | 124 |
109 bool ClipboardMap::HasFormat(const std::string& format) { | 125 bool ClipboardMap::HasFormat(const std::string& format) { |
110 base::AutoLock lock(lock_); | 126 base::AutoLock lock(lock_); |
111 UpdateFromAndroidClipboard(); | 127 UpdateFromAndroidClipboard(); |
112 return base::ContainsKey(map_, format); | 128 return base::ContainsKey(map_, format); |
113 } | 129 } |
114 | 130 |
115 void ClipboardMap::OnPrimaryClipboardChanged() { | 131 void ClipboardMap::OnPrimaryClipboardChanged() { |
116 sequence_number_++; | 132 sequence_number_++; |
117 last_modified_time_ = base::Time::Now(); | 133 UpdateLastModifiedTime(base::Time::Now()); |
118 map_state_ = MapState::kOutOfDate; | 134 map_state_ = MapState::kOutOfDate; |
119 } | 135 } |
120 | 136 |
121 void ClipboardMap::Set(const std::string& format, const std::string& data) { | 137 void ClipboardMap::Set(const std::string& format, const std::string& data) { |
122 base::AutoLock lock(lock_); | 138 base::AutoLock lock(lock_); |
123 map_[format] = data; | 139 map_[format] = data; |
124 map_state_ = MapState::kPreparingCommit; | 140 map_state_ = MapState::kPreparingCommit; |
125 } | 141 } |
126 | 142 |
127 void ClipboardMap::CommitToAndroidClipboard() { | 143 void ClipboardMap::CommitToAndroidClipboard() { |
(...skipping 16 matching lines...) Expand all Loading... | |
144 ScopedJavaLocalRef<jstring> str = | 160 ScopedJavaLocalRef<jstring> str = |
145 ConvertUTF8ToJavaString(env, map_[kPlainTextFormat]); | 161 ConvertUTF8ToJavaString(env, map_[kPlainTextFormat]); |
146 DCHECK(str.obj()); | 162 DCHECK(str.obj()); |
147 Java_Clipboard_setText(env, clipboard_manager_, str); | 163 Java_Clipboard_setText(env, clipboard_manager_, str); |
148 } else { | 164 } else { |
149 Java_Clipboard_clear(env, clipboard_manager_); | 165 Java_Clipboard_clear(env, clipboard_manager_); |
150 NOTIMPLEMENTED(); | 166 NOTIMPLEMENTED(); |
151 } | 167 } |
152 map_state_ = MapState::kUpToDate; | 168 map_state_ = MapState::kUpToDate; |
153 sequence_number_++; | 169 sequence_number_++; |
154 last_modified_time_ = base::Time::Now(); | 170 UpdateLastModifiedTime(base::Time::Now()); |
155 } | 171 } |
156 | 172 |
157 void ClipboardMap::Clear() { | 173 void ClipboardMap::Clear() { |
158 JNIEnv* env = AttachCurrentThread(); | 174 JNIEnv* env = AttachCurrentThread(); |
159 base::AutoLock lock(lock_); | 175 base::AutoLock lock(lock_); |
160 map_.clear(); | 176 map_.clear(); |
161 Java_Clipboard_clear(env, clipboard_manager_); | 177 Java_Clipboard_clear(env, clipboard_manager_); |
162 map_state_ = MapState::kUpToDate; | 178 map_state_ = MapState::kUpToDate; |
163 sequence_number_++; | 179 sequence_number_++; |
164 last_modified_time_ = base::Time::Now(); | 180 UpdateLastModifiedTime(base::Time::Now()); |
181 } | |
182 | |
183 void ClipboardMap::SetLastModifiedTimeWithoutRunningCallback(base::Time time) { | |
184 last_modified_time_ = time; | |
165 } | 185 } |
166 | 186 |
167 // Add a key:jstr pair to map, but only if jstr is not null, and also | 187 // Add a key:jstr pair to map, but only if jstr is not null, and also |
168 // not empty. | 188 // not empty. |
169 void AddMapEntry(JNIEnv* env, | 189 void AddMapEntry(JNIEnv* env, |
170 std::map<std::string, std::string>* map, | 190 std::map<std::string, std::string>* map, |
171 const char* key, | 191 const char* key, |
172 const ScopedJavaLocalRef<jstring>& jstr) { | 192 const ScopedJavaLocalRef<jstring>& jstr) { |
173 if (!jstr.is_null()) { | 193 if (!jstr.is_null()) { |
174 std::string str = ConvertJavaStringToUTF8(env, jstr.obj()); | 194 std::string str = ConvertJavaStringToUTF8(env, jstr.obj()); |
175 if (!str.empty()) | 195 if (!str.empty()) |
176 (*map)[key] = str; | 196 (*map)[key] = str; |
177 } | 197 } |
178 } | 198 } |
179 | 199 |
200 void ClipboardMap::UpdateLastModifiedTime(base::Time time) { | |
201 last_modified_time_ = time; | |
202 // |modified_callback_| may be null in tests. | |
203 if (modified_cb_) | |
204 modified_cb_.Run(time); | |
205 } | |
206 | |
180 void ClipboardMap::UpdateFromAndroidClipboard() { | 207 void ClipboardMap::UpdateFromAndroidClipboard() { |
181 DCHECK_NE(MapState::kPreparingCommit, map_state_); | 208 DCHECK_NE(MapState::kPreparingCommit, map_state_); |
182 if (map_state_ == MapState::kUpToDate) | 209 if (map_state_ == MapState::kUpToDate) |
183 return; | 210 return; |
184 | 211 |
185 // Fetch the current Android clipboard state. | 212 // Fetch the current Android clipboard state. |
186 lock_.AssertAcquired(); | 213 lock_.AssertAcquired(); |
187 JNIEnv* env = AttachCurrentThread(); | 214 JNIEnv* env = AttachCurrentThread(); |
188 | 215 |
189 ScopedJavaLocalRef<jstring> jtext = | 216 ScopedJavaLocalRef<jstring> jtext = |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 } | 323 } |
297 | 324 |
298 // ClipboardAndroid implementation. | 325 // ClipboardAndroid implementation. |
299 | 326 |
300 void ClipboardAndroid::OnPrimaryClipChanged( | 327 void ClipboardAndroid::OnPrimaryClipChanged( |
301 JNIEnv* env, | 328 JNIEnv* env, |
302 const base::android::JavaParamRef<jobject>& obj) { | 329 const base::android::JavaParamRef<jobject>& obj) { |
303 g_map.Get().OnPrimaryClipboardChanged(); | 330 g_map.Get().OnPrimaryClipboardChanged(); |
304 } | 331 } |
305 | 332 |
333 void ClipboardAndroid::SetModifiedCallback(Modified cb) { | |
334 g_map.Get().SetModifiedCallback(std::move(cb)); | |
335 } | |
336 | |
337 void ClipboardAndroid::SetLastModifiedTimeWithoutRunningCallback( | |
338 base::Time time) { | |
339 g_map.Get().SetLastModifiedTimeWithoutRunningCallback(time); | |
340 } | |
341 | |
306 ClipboardAndroid::ClipboardAndroid() { | 342 ClipboardAndroid::ClipboardAndroid() { |
307 DCHECK(CalledOnValidThread()); | 343 DCHECK(CalledOnValidThread()); |
308 } | 344 } |
309 | 345 |
310 ClipboardAndroid::~ClipboardAndroid() { | 346 ClipboardAndroid::~ClipboardAndroid() { |
311 DCHECK(CalledOnValidThread()); | 347 DCHECK(CalledOnValidThread()); |
312 } | 348 } |
313 | 349 |
314 void ClipboardAndroid::OnPreShutdown() {} | 350 void ClipboardAndroid::OnPreShutdown() {} |
315 | 351 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 return RegisterNativesImpl(env); | 549 return RegisterNativesImpl(env); |
514 } | 550 } |
515 | 551 |
516 // Returns a pointer to the current ClipboardAndroid object. | 552 // Returns a pointer to the current ClipboardAndroid object. |
517 static jlong Init(JNIEnv* env, | 553 static jlong Init(JNIEnv* env, |
518 const base::android::JavaParamRef<jobject>& obj) { | 554 const base::android::JavaParamRef<jobject>& obj) { |
519 return reinterpret_cast<intptr_t>(Clipboard::GetForCurrentThread()); | 555 return reinterpret_cast<intptr_t>(Clipboard::GetForCurrentThread()); |
520 } | 556 } |
521 | 557 |
522 } // namespace ui | 558 } // namespace ui |
OLD | NEW |