OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/devices/input_device_manager.h" | 5 #include "ui/events/devices/input_device_manager.h" |
| 6 #include "base/lazy_instance.h" |
| 7 #include "base/threading/thread_local.h" |
6 | 8 |
7 namespace ui { | 9 namespace ui { |
| 10 namespace { |
8 | 11 |
9 InputDeviceManager* InputDeviceManager::instance_ = nullptr; | 12 // InputDeviceManager singleton is thread-local so that different instances can |
| 13 // be used on different threads (eg. UI Service thread vs. browser UI thread). |
| 14 base::LazyInstance<base::ThreadLocalPointer<InputDeviceManager>>::Leaky |
| 15 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
| 16 |
| 17 } // namespace |
10 | 18 |
11 // static | 19 // static |
12 InputDeviceManager* InputDeviceManager::GetInstance() { | 20 InputDeviceManager* InputDeviceManager::GetInstance() { |
13 DCHECK(instance_); | 21 InputDeviceManager* instance = lazy_tls_ptr.Pointer()->Get(); |
14 return instance_; | 22 DCHECK(instance) << "InputDeviceManager::SetInstance must be called before " |
| 23 "getting the instance of InputDeviceManager."; |
| 24 return instance; |
15 } | 25 } |
16 | 26 |
17 // static | 27 // static |
18 bool InputDeviceManager::HasInstance() { | 28 bool InputDeviceManager::HasInstance() { |
19 return instance_ != nullptr; | 29 return lazy_tls_ptr.Pointer()->Get() != nullptr; |
20 } | 30 } |
21 | 31 |
22 // static | 32 // static |
23 void InputDeviceManager::SetInstance(InputDeviceManager* instance) { | 33 void InputDeviceManager::SetInstance(InputDeviceManager* instance) { |
24 DCHECK(!instance_); | 34 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
25 instance_ = instance; | 35 lazy_tls_ptr.Pointer()->Set(instance); |
26 } | 36 } |
27 | 37 |
28 // static | 38 // static |
29 void InputDeviceManager::ClearInstance() { | 39 void InputDeviceManager::ClearInstance() { |
30 instance_ = nullptr; | 40 lazy_tls_ptr.Pointer()->Set(nullptr); |
31 } | 41 } |
32 | 42 |
33 } // namespace ui | 43 } // namespace ui |
OLD | NEW |