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 { |
16 | 18 |
17 // static | 19 namespace { |
18 PlatformEventSource* PlatformEventSource::instance_ = NULL; | 20 |
| 21 // PlatformEventSource singleton is thread local so that different instances |
| 22 // can be used on different threads (e.g. browser thread should be able to |
| 23 // access PlatformEventSource owned by the UI Service's thread). |
| 24 base::LazyInstance<base::ThreadLocalPointer<PlatformEventSource>>::Leaky |
| 25 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
| 26 |
| 27 } // namespace |
19 | 28 |
20 PlatformEventSource::PlatformEventSource() | 29 PlatformEventSource::PlatformEventSource() |
21 : overridden_dispatcher_(NULL), | 30 : overridden_dispatcher_(NULL), |
22 overridden_dispatcher_restored_(false) { | 31 overridden_dispatcher_restored_(false) { |
23 CHECK(!instance_) << "Only one platform event source can be created."; | 32 CHECK(!lazy_tls_ptr.Pointer()->Get()) |
24 instance_ = this; | 33 << "Only one platform event source can be created."; |
| 34 lazy_tls_ptr.Pointer()->Set(this); |
25 } | 35 } |
26 | 36 |
27 PlatformEventSource::~PlatformEventSource() { | 37 PlatformEventSource::~PlatformEventSource() { |
28 CHECK_EQ(this, instance_); | 38 CHECK_EQ(this, lazy_tls_ptr.Pointer()->Get()); |
29 instance_ = NULL; | 39 lazy_tls_ptr.Pointer()->Set(nullptr); |
30 } | 40 } |
31 | 41 |
32 PlatformEventSource* PlatformEventSource::GetInstance() { return instance_; } | 42 PlatformEventSource* PlatformEventSource::GetInstance() { |
| 43 return lazy_tls_ptr.Pointer()->Get(); |
| 44 } |
33 | 45 |
34 void PlatformEventSource::AddPlatformEventDispatcher( | 46 void PlatformEventSource::AddPlatformEventDispatcher( |
35 PlatformEventDispatcher* dispatcher) { | 47 PlatformEventDispatcher* dispatcher) { |
36 CHECK(dispatcher); | 48 CHECK(dispatcher); |
37 dispatchers_.AddObserver(dispatcher); | 49 dispatchers_.AddObserver(dispatcher); |
38 OnDispatcherListChanged(); | 50 OnDispatcherListChanged(); |
39 } | 51 } |
40 | 52 |
41 void PlatformEventSource::RemovePlatformEventDispatcher( | 53 void PlatformEventSource::RemovePlatformEventDispatcher( |
42 PlatformEventDispatcher* dispatcher) { | 54 PlatformEventDispatcher* dispatcher) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 113 |
102 void PlatformEventSource::OnDispatcherListChanged() { | 114 void PlatformEventSource::OnDispatcherListChanged() { |
103 } | 115 } |
104 | 116 |
105 void PlatformEventSource::OnOverriddenDispatcherRestored() { | 117 void PlatformEventSource::OnOverriddenDispatcherRestored() { |
106 CHECK(overridden_dispatcher_); | 118 CHECK(overridden_dispatcher_); |
107 overridden_dispatcher_restored_ = true; | 119 overridden_dispatcher_restored_ = true; |
108 } | 120 } |
109 | 121 |
110 } // namespace ui | 122 } // namespace ui |
OLD | NEW |