Index: ui/events/devices/input_device_manager.cc |
diff --git a/ui/events/devices/input_device_manager.cc b/ui/events/devices/input_device_manager.cc |
index b6ca664e25a4f42bf7f4dcd015cf7583e73560b8..1f53855e3f687214c455e485911f621b318fb914 100644 |
--- a/ui/events/devices/input_device_manager.cc |
+++ b/ui/events/devices/input_device_manager.cc |
@@ -3,31 +3,41 @@ |
// found in the LICENSE file. |
#include "ui/events/devices/input_device_manager.h" |
+#include "base/lazy_instance.h" |
+#include "base/threading/thread_local.h" |
namespace ui { |
+namespace { |
-InputDeviceManager* InputDeviceManager::instance_ = nullptr; |
+// InputDeviceManager singleton is thread-local so that different instances can |
+// be used on different threads (eg. UI Service thread vs. browser UI thread). |
+base::LazyInstance<base::ThreadLocalPointer<InputDeviceManager>>::Leaky |
+ lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
+ |
+} // namespace |
// static |
InputDeviceManager* InputDeviceManager::GetInstance() { |
- DCHECK(instance_); |
- return instance_; |
+ InputDeviceManager* instance = lazy_tls_ptr.Pointer()->Get(); |
+ DCHECK(instance) << "InputDeviceManager::SetInstance must be called before " |
+ "getting the instance of InputDeviceManager."; |
+ return instance; |
} |
// static |
bool InputDeviceManager::HasInstance() { |
- return instance_ != nullptr; |
+ return lazy_tls_ptr.Pointer()->Get() != nullptr; |
} |
// static |
void InputDeviceManager::SetInstance(InputDeviceManager* instance) { |
- DCHECK(!instance_); |
- instance_ = instance; |
+ DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
+ lazy_tls_ptr.Pointer()->Set(instance); |
} |
// static |
void InputDeviceManager::ClearInstance() { |
- instance_ = nullptr; |
+ lazy_tls_ptr.Pointer()->Set(nullptr); |
} |
} // namespace ui |