OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/events/platform/platform_event_source.h" | 5 #include "ui/events/platform/platform_event_source.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/lazy_instance.h" |
9 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/threading/thread_local.h" |
11 #include "ui/events/platform/platform_event_dispatcher.h" | 13 #include "ui/events/platform/platform_event_dispatcher.h" |
12 #include "ui/events/platform/platform_event_observer.h" | 14 #include "ui/events/platform/platform_event_observer.h" |
13 #include "ui/events/platform/scoped_event_dispatcher.h" | 15 #include "ui/events/platform/scoped_event_dispatcher.h" |
14 | 16 |
15 namespace ui { | 17 namespace ui { |
| 18 namespace { |
16 | 19 |
17 // static | 20 // PlatformEventSource is thread local so that different instances can be used |
18 PlatformEventSource* PlatformEventSource::instance_ = NULL; | 21 // on different threads (e.g. WindowServer thread vs. Browser UI thread). |
| 22 base::LazyInstance<base::ThreadLocalPointer<PlatformEventSource>>::Leaky |
| 23 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
| 24 |
| 25 } // namespace |
19 | 26 |
20 PlatformEventSource::PlatformEventSource() | 27 PlatformEventSource::PlatformEventSource() |
21 : overridden_dispatcher_(NULL), | 28 : overridden_dispatcher_(NULL), |
22 overridden_dispatcher_restored_(false) { | 29 overridden_dispatcher_restored_(false) { |
23 CHECK(!instance_) << "Only one platform event source can be created."; | 30 CHECK(!lazy_tls_ptr.Pointer()->Get()) |
24 instance_ = this; | 31 << "Only one platform event source can be created per thread."; |
| 32 lazy_tls_ptr.Pointer()->Set(this); |
25 } | 33 } |
26 | 34 |
27 PlatformEventSource::~PlatformEventSource() { | 35 PlatformEventSource::~PlatformEventSource() { |
28 CHECK_EQ(this, instance_); | 36 CHECK_EQ(this, lazy_tls_ptr.Pointer()->Get()); |
29 instance_ = NULL; | 37 lazy_tls_ptr.Pointer()->Set(nullptr); |
30 } | 38 } |
31 | 39 |
32 PlatformEventSource* PlatformEventSource::GetInstance() { return instance_; } | 40 PlatformEventSource* PlatformEventSource::GetInstance() { |
| 41 return lazy_tls_ptr.Pointer()->Get(); |
| 42 } |
33 | 43 |
34 void PlatformEventSource::AddPlatformEventDispatcher( | 44 void PlatformEventSource::AddPlatformEventDispatcher( |
35 PlatformEventDispatcher* dispatcher) { | 45 PlatformEventDispatcher* dispatcher) { |
36 CHECK(dispatcher); | 46 CHECK(dispatcher); |
37 dispatchers_.AddObserver(dispatcher); | 47 dispatchers_.AddObserver(dispatcher); |
38 OnDispatcherListChanged(); | 48 OnDispatcherListChanged(); |
39 } | 49 } |
40 | 50 |
41 void PlatformEventSource::RemovePlatformEventDispatcher( | 51 void PlatformEventSource::RemovePlatformEventDispatcher( |
42 PlatformEventDispatcher* dispatcher) { | 52 PlatformEventDispatcher* dispatcher) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 111 |
102 void PlatformEventSource::OnDispatcherListChanged() { | 112 void PlatformEventSource::OnDispatcherListChanged() { |
103 } | 113 } |
104 | 114 |
105 void PlatformEventSource::OnOverriddenDispatcherRestored() { | 115 void PlatformEventSource::OnOverriddenDispatcherRestored() { |
106 CHECK(overridden_dispatcher_); | 116 CHECK(overridden_dispatcher_); |
107 overridden_dispatcher_restored_ = true; | 117 overridden_dispatcher_restored_ = true; |
108 } | 118 } |
109 | 119 |
110 } // namespace ui | 120 } // namespace ui |
OLD | NEW |