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

Unified Diff: ui/events/platform/platform_event_source.cc

Issue 2978873002: Singletons changes necessary to run the UI Service inside the browser's process. (Closed)
Patch Set: Removing unneeded dependency. Created 3 years, 5 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/events/platform/platform_event_source.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/events/platform/platform_event_source.cc
diff --git a/ui/events/platform/platform_event_source.cc b/ui/events/platform/platform_event_source.cc
index 9be6630e268abbcb8ce45c7746f3f34cb119e41c..c521b282457fc1ca83c3e2c49dbe0133a66c9984 100644
--- a/ui/events/platform/platform_event_source.cc
+++ b/ui/events/platform/platform_event_source.cc
@@ -6,30 +6,42 @@
#include <algorithm>
+#include "base/lazy_instance.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
+#include "base/threading/thread_local.h"
#include "ui/events/platform/platform_event_dispatcher.h"
#include "ui/events/platform/platform_event_observer.h"
#include "ui/events/platform/scoped_event_dispatcher.h"
namespace ui {
-// static
-PlatformEventSource* PlatformEventSource::instance_ = NULL;
+namespace {
+
+// PlatformEventSource singleton is thread local so that different instances
+// can be used on different threads (e.g. browser thread should be able to
+// access PlatformEventSource owned by the UI Service's thread).
+base::LazyInstance<base::ThreadLocalPointer<PlatformEventSource>>::Leaky
+ lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
PlatformEventSource::PlatformEventSource()
: overridden_dispatcher_(NULL),
overridden_dispatcher_restored_(false) {
- CHECK(!instance_) << "Only one platform event source can be created.";
- instance_ = this;
+ CHECK(!lazy_tls_ptr.Pointer()->Get())
+ << "Only one platform event source can be created.";
+ lazy_tls_ptr.Pointer()->Set(this);
}
PlatformEventSource::~PlatformEventSource() {
- CHECK_EQ(this, instance_);
- instance_ = NULL;
+ CHECK_EQ(this, lazy_tls_ptr.Pointer()->Get());
+ lazy_tls_ptr.Pointer()->Set(nullptr);
}
-PlatformEventSource* PlatformEventSource::GetInstance() { return instance_; }
+PlatformEventSource* PlatformEventSource::GetInstance() {
+ return lazy_tls_ptr.Pointer()->Get();
+}
void PlatformEventSource::AddPlatformEventDispatcher(
PlatformEventDispatcher* dispatcher) {
« no previous file with comments | « ui/events/platform/platform_event_source.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698