Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Unified Diff: ui/base/clipboard/clipboard.cc

Issue 2011833003: Implement ui::ClipboardMus. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove static in test by creating forwarding clipboard subclass. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/base/clipboard/clipboard.h ('k') | ui/base/clipboard/clipboard_android.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/clipboard/clipboard.cc
diff --git a/ui/base/clipboard/clipboard.cc b/ui/base/clipboard/clipboard.cc
index d43954bbc31d39fc7db5bcbc7fe645ae34599f8e..f8125f32cce99e69a5ed52dcd736221a4232812e 100644
--- a/ui/base/clipboard/clipboard.cc
+++ b/ui/base/clipboard/clipboard.cc
@@ -9,6 +9,7 @@
#include <memory>
#include "base/logging.h"
+#include "base/memory/ptr_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
@@ -32,32 +33,32 @@ void Clipboard::SetAllowedThreads(
}
// static
-Clipboard* Clipboard::GetForCurrentThread() {
+void Clipboard::SetClipboardForCurrentThread(
+ std::unique_ptr<Clipboard> platform_clipboard) {
base::AutoLock lock(clipboard_map_lock_.Get());
+ base::PlatformThreadId id = Clipboard::GetAndValidateThreadID();
- base::PlatformThreadId id = base::PlatformThread::CurrentId();
-
- AllowedThreadsVector* allowed_threads = allowed_threads_.Pointer();
- if (!allowed_threads->empty()) {
- bool found = false;
- for (AllowedThreadsVector::const_iterator it = allowed_threads->begin();
- it != allowed_threads->end(); ++it) {
- if (*it == id) {
- found = true;
- break;
- }
- }
-
- DCHECK(found);
+ ClipboardMap* clipboard_map = clipboard_map_.Pointer();
+ ClipboardMap::const_iterator it = clipboard_map->find(id);
+ if (it != clipboard_map->end()) {
+ // This shouldn't happen. The clipboard should not already exist.
+ NOTREACHED();
}
+ clipboard_map->insert(std::make_pair(id, std::move(platform_clipboard)));
+}
+
+// static
+Clipboard* Clipboard::GetForCurrentThread() {
+ base::AutoLock lock(clipboard_map_lock_.Get());
+ base::PlatformThreadId id = GetAndValidateThreadID();
ClipboardMap* clipboard_map = clipboard_map_.Pointer();
ClipboardMap::const_iterator it = clipboard_map->find(id);
if (it != clipboard_map->end())
- return it->second;
+ return it->second.get();
Clipboard* clipboard = Clipboard::Create();
- clipboard_map->insert(std::make_pair(id, clipboard));
+ clipboard_map->insert(std::make_pair(id, base::WrapUnique(clipboard)));
return clipboard;
}
@@ -67,10 +68,8 @@ void Clipboard::DestroyClipboardForCurrentThread() {
ClipboardMap* clipboard_map = clipboard_map_.Pointer();
base::PlatformThreadId id = base::PlatformThread::CurrentId();
ClipboardMap::iterator it = clipboard_map->find(id);
- if (it != clipboard_map->end()) {
- delete it->second;
+ if (it != clipboard_map->end())
clipboard_map->erase(it);
- }
}
void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) {
@@ -132,4 +131,25 @@ void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) {
}
}
+base::PlatformThreadId Clipboard::GetAndValidateThreadID() {
+ base::PlatformThreadId id = base::PlatformThread::CurrentId();
+#ifndef NDEBUG
+ AllowedThreadsVector* allowed_threads = allowed_threads_.Pointer();
+ if (!allowed_threads->empty()) {
+ bool found = false;
+ for (AllowedThreadsVector::const_iterator it = allowed_threads->begin();
+ it != allowed_threads->end(); ++it) {
+ if (*it == id) {
+ found = true;
+ break;
+ }
+ }
+
+ DCHECK(found);
+ }
+#endif
+
+ return id;
+}
+
} // namespace ui
« no previous file with comments | « ui/base/clipboard/clipboard.h ('k') | ui/base/clipboard/clipboard_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698