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

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

Issue 10916214: Try 2 - Change how ui::Clipboard is accessed so there's only one per thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moved to BrowserThreadsStarted Created 8 years, 3 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_unittest.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 d20d85414ea620649a91e45cd368b27a79fe3e3b..2473723e35939c99a4f1190be70a24b5df851f44 100644
--- a/ui/base/clipboard/clipboard.cc
+++ b/ui/base/clipboard/clipboard.cc
@@ -4,8 +4,12 @@
#include "ui/base/clipboard/clipboard.h"
+#include <iterator>
+
+#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
+#include "base/synchronization/lock.h"
#include "ui/gfx/size.h"
namespace ui {
@@ -72,6 +76,22 @@ bool ValidateAndMapSharedBitmap(const Clipboard::ObjectMapParams& params,
return true;
}
+// A list of allowed threads. By default, this is empty and no thread checking
+// is done (in the unit test case), but a user (like content) can set which
+// threads are allowed to call this method.
+typedef std::vector<base::PlatformThreadId> AllowedThreadsVector;
+static base::LazyInstance<AllowedThreadsVector> g_allowed_threads =
+ LAZY_INSTANCE_INITIALIZER;
+
+// Mapping from threads to clipboard objects.
+typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap;
+static base::LazyInstance<ClipboardMap> g_clipboard_map =
+ LAZY_INSTANCE_INITIALIZER;
+
+// Mutex that controls access to |g_clipboard_map|.
+static base::LazyInstance<base::Lock>::Leaky
+ g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER;
+
} // namespace
const char Clipboard::kMimeTypeText[] = "text/plain";
@@ -81,6 +101,58 @@ const char Clipboard::kMimeTypeHTML[] = "text/html";
const char Clipboard::kMimeTypeRTF[] = "text/rtf";
const char Clipboard::kMimeTypePNG[] = "image/png";
+// static
+void Clipboard::SetAllowedThreads(
+ const std::vector<base::PlatformThreadId>& allowed_threads) {
+ base::AutoLock lock(g_clipboard_map_lock.Get());
+
+ g_allowed_threads.Get().clear();
+ std::copy(allowed_threads.begin(), allowed_threads.end(),
+ std::back_inserter(g_allowed_threads.Get()));
+}
+
+// static
+Clipboard* Clipboard::GetForCurrentThread() {
+ base::AutoLock lock(g_clipboard_map_lock.Get());
+
+ base::PlatformThreadId id = base::PlatformThread::CurrentId();
+
+ AllowedThreadsVector* allowed_threads = g_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 = g_clipboard_map.Pointer();
+ ClipboardMap::iterator it = clipboard_map->find(id);
+ if (it != clipboard_map->end())
+ return it->second;
+
+ Clipboard* clipboard = new ui::Clipboard;
+ clipboard_map->insert(std::make_pair(id, clipboard));
+ return clipboard;
+}
+
+void Clipboard::DestroyClipboardForCurrentThread() {
+ base::AutoLock lock(g_clipboard_map_lock.Get());
+
+ ClipboardMap* clipboard_map = g_clipboard_map.Pointer();
+ base::PlatformThreadId id = base::PlatformThread::CurrentId();
+ ClipboardMap::iterator it = clipboard_map->find(id);
+ if (it != clipboard_map->end()) {
+ delete it->second;
+ clipboard_map->erase(it);
+ }
+}
+
void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) {
// All types apart from CBF_WEBKIT need at least 1 non-empty param.
if (type != CBF_WEBKIT && (params.empty() || params[0].empty()))
« no previous file with comments | « ui/base/clipboard/clipboard.h ('k') | ui/base/clipboard/clipboard_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698