Chromium Code Reviews| 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> | |
| 8 | |
| 9 #include "base/android/context_utils.h" | |
| 7 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 8 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 9 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 12 #include "jni/Clipboard_jni.h" | 15 #include "jni/Clipboard_jni.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 15 | 18 |
| 16 // TODO:(andrewhayden) Support additional formats in Android: Bitmap, URI, HTML, | 19 // TODO:(andrewhayden) Support additional formats in Android: Bitmap, URI, HTML, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 40 const char kHTMLFormat[] = "html"; | 43 const char kHTMLFormat[] = "html"; |
| 41 const char kRTFFormat[] = "rtf"; | 44 const char kRTFFormat[] = "rtf"; |
| 42 const char kBitmapFormat[] = "bitmap"; | 45 const char kBitmapFormat[] = "bitmap"; |
| 43 const char kWebKitSmartPasteFormat[] = "webkit_smart"; | 46 const char kWebKitSmartPasteFormat[] = "webkit_smart"; |
| 44 const char kBookmarkFormat[] = "bookmark"; | 47 const char kBookmarkFormat[] = "bookmark"; |
| 45 | 48 |
| 46 class ClipboardMap { | 49 class ClipboardMap { |
| 47 public: | 50 public: |
| 48 ClipboardMap(); | 51 ClipboardMap(); |
| 49 std::string Get(const std::string& format); | 52 std::string Get(const std::string& format); |
| 53 int64_t GetLastClipboardChangeTimeInMillis(); | |
| 50 bool HasFormat(const std::string& format); | 54 bool HasFormat(const std::string& format); |
| 51 void Set(const std::string& format, const std::string& data); | 55 void Set(const std::string& format, const std::string& data); |
| 52 void CommitToAndroidClipboard(); | 56 void CommitToAndroidClipboard(); |
| 53 void Clear(); | 57 void Clear(); |
| 54 | 58 |
| 55 private: | 59 private: |
| 56 void UpdateFromAndroidClipboard(); | 60 void UpdateFromAndroidClipboard(); |
| 57 std::map<std::string, std::string> map_; | 61 std::map<std::string, std::string> map_; |
| 58 base::Lock lock_; | 62 base::Lock lock_; |
| 59 | 63 |
| 64 int64_t last_clipboard_change_time_ms_; | |
| 65 | |
| 60 // Java class and methods for the Android ClipboardManager. | 66 // Java class and methods for the Android ClipboardManager. |
| 61 ScopedJavaGlobalRef<jobject> clipboard_manager_; | 67 ScopedJavaGlobalRef<jobject> clipboard_manager_; |
| 62 }; | 68 }; |
| 63 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER; | 69 base::LazyInstance<ClipboardMap>::Leaky g_map = LAZY_INSTANCE_INITIALIZER; |
| 64 | 70 |
| 65 ClipboardMap::ClipboardMap() { | 71 ClipboardMap::ClipboardMap() { |
| 66 clipboard_manager_.Reset(Java_Clipboard_getInstance(AttachCurrentThread())); | 72 clipboard_manager_.Reset(Java_Clipboard_getInstance(AttachCurrentThread())); |
| 67 DCHECK(clipboard_manager_.obj()); | 73 DCHECK(clipboard_manager_.obj()); |
| 68 } | 74 } |
| 69 | 75 |
| 70 std::string ClipboardMap::Get(const std::string& format) { | 76 std::string ClipboardMap::Get(const std::string& format) { |
| 71 base::AutoLock lock(lock_); | 77 base::AutoLock lock(lock_); |
| 72 UpdateFromAndroidClipboard(); | 78 UpdateFromAndroidClipboard(); |
| 73 std::map<std::string, std::string>::const_iterator it = map_.find(format); | 79 std::map<std::string, std::string>::const_iterator it = map_.find(format); |
| 74 return it == map_.end() ? std::string() : it->second; | 80 return it == map_.end() ? std::string() : it->second; |
| 75 } | 81 } |
| 76 | 82 |
| 83 int64_t ClipboardMap::GetLastClipboardChangeTimeInMillis() { | |
| 84 base::AutoLock lock(lock_); | |
| 85 UpdateFromAndroidClipboard(); | |
| 86 return last_clipboard_change_time_ms_; | |
| 87 } | |
| 88 | |
| 77 bool ClipboardMap::HasFormat(const std::string& format) { | 89 bool ClipboardMap::HasFormat(const std::string& format) { |
| 78 base::AutoLock lock(lock_); | 90 base::AutoLock lock(lock_); |
| 79 UpdateFromAndroidClipboard(); | 91 UpdateFromAndroidClipboard(); |
| 80 return base::ContainsKey(map_, format); | 92 return base::ContainsKey(map_, format); |
| 81 } | 93 } |
| 82 | 94 |
| 83 void ClipboardMap::Set(const std::string& format, const std::string& data) { | 95 void ClipboardMap::Set(const std::string& format, const std::string& data) { |
| 84 base::AutoLock lock(lock_); | 96 base::AutoLock lock(lock_); |
| 85 map_[format] = data; | 97 map_[format] = data; |
| 86 } | 98 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 | 163 |
| 152 std::map<std::string, std::string> android_clipboard_state; | 164 std::map<std::string, std::string> android_clipboard_state; |
| 153 | 165 |
| 154 ScopedJavaLocalRef<jstring> jtext = | 166 ScopedJavaLocalRef<jstring> jtext = |
| 155 Java_Clipboard_getCoercedText(env, clipboard_manager_); | 167 Java_Clipboard_getCoercedText(env, clipboard_manager_); |
| 156 ScopedJavaLocalRef<jstring> jhtml = | 168 ScopedJavaLocalRef<jstring> jhtml = |
| 157 Java_Clipboard_getHTMLText(env, clipboard_manager_); | 169 Java_Clipboard_getHTMLText(env, clipboard_manager_); |
| 158 | 170 |
| 159 AddMapEntry(env, &android_clipboard_state, kPlainTextFormat, jtext); | 171 AddMapEntry(env, &android_clipboard_state, kPlainTextFormat, jtext); |
| 160 AddMapEntry(env, &android_clipboard_state, kHTMLFormat, jhtml); | 172 AddMapEntry(env, &android_clipboard_state, kHTMLFormat, jhtml); |
| 173 last_clipboard_change_time_ms_ = | |
|
Ted C
2017/03/30 16:55:21
How often are we going to be asking for the clipbo
Mark P
2017/03/30 16:59:26
Not per keystroke. The current plan only involves
Ted C
2017/03/30 17:02:06
I would personally probably inline the JNI into th
Mark P
2017/03/30 17:33:36
I don't understand the current structure either.
dcheng
2017/03/30 19:01:11
Either option seems reasonable here. The original
Mark P
2017/03/30 19:15:03
Ah, that makes sense (as an ambition).
| |
| 174 Java_Clipboard_getClipboardContentChangeTimeInMillis(env, | |
| 175 clipboard_manager_); | |
| 161 | 176 |
| 162 if (!MapIsSubset(android_clipboard_state, map_)) | 177 if (!MapIsSubset(android_clipboard_state, map_)) |
| 163 android_clipboard_state.swap(map_); | 178 android_clipboard_state.swap(map_); |
| 164 } | 179 } |
| 165 | 180 |
| 166 } // namespace | 181 } // namespace |
| 167 | 182 |
| 168 // Clipboard::FormatType implementation. | 183 // Clipboard::FormatType implementation. |
| 169 Clipboard::FormatType::FormatType() { | 184 Clipboard::FormatType::FormatType() { |
| 170 } | 185 } |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 DCHECK(CalledOnValidThread()); | 407 DCHECK(CalledOnValidThread()); |
| 393 NOTIMPLEMENTED(); | 408 NOTIMPLEMENTED(); |
| 394 } | 409 } |
| 395 | 410 |
| 396 void ClipboardAndroid::ReadData(const Clipboard::FormatType& format, | 411 void ClipboardAndroid::ReadData(const Clipboard::FormatType& format, |
| 397 std::string* result) const { | 412 std::string* result) const { |
| 398 DCHECK(CalledOnValidThread()); | 413 DCHECK(CalledOnValidThread()); |
| 399 *result = g_map.Get().Get(format.ToString()); | 414 *result = g_map.Get().Get(format.ToString()); |
| 400 } | 415 } |
| 401 | 416 |
| 417 base::Time ClipboardAndroid::GetClipboardLastModifiedTime() const { | |
| 418 DCHECK(CalledOnValidThread()); | |
| 419 return base::Time::FromJavaTime( | |
| 420 g_map.Get().GetLastClipboardChangeTimeInMillis()); | |
| 421 } | |
| 422 | |
| 402 // Main entry point used to write several values in the clipboard. | 423 // Main entry point used to write several values in the clipboard. |
| 403 void ClipboardAndroid::WriteObjects(ClipboardType type, | 424 void ClipboardAndroid::WriteObjects(ClipboardType type, |
| 404 const ObjectMap& objects) { | 425 const ObjectMap& objects) { |
| 405 DCHECK(CalledOnValidThread()); | 426 DCHECK(CalledOnValidThread()); |
| 406 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 427 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
| 407 g_map.Get().Clear(); | 428 g_map.Get().Clear(); |
| 408 | 429 |
| 409 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); | 430 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); |
| 410 ++iter) { | 431 ++iter) { |
| 411 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 432 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 g_map.Get().Set(kBitmapFormat, packed); | 479 g_map.Get().Set(kBitmapFormat, packed); |
| 459 } | 480 } |
| 460 | 481 |
| 461 void ClipboardAndroid::WriteData(const Clipboard::FormatType& format, | 482 void ClipboardAndroid::WriteData(const Clipboard::FormatType& format, |
| 462 const char* data_data, | 483 const char* data_data, |
| 463 size_t data_len) { | 484 size_t data_len) { |
| 464 g_map.Get().Set(format.ToString(), std::string(data_data, data_len)); | 485 g_map.Get().Set(format.ToString(), std::string(data_data, data_len)); |
| 465 } | 486 } |
| 466 | 487 |
| 467 } // namespace ui | 488 } // namespace ui |
| OLD | NEW |