Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / vr / vr_device_manager.cc
blob07e420da38b7c5c2cd560cfa558ba7e1f59a4dc2
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/vr/vr_device_manager.h"
7 #include "base/bind.h"
8 #include "base/memory/singleton.h"
9 #include "third_party/WebKit/public/platform/modules/vr/WebVR.h"
11 #if defined(OS_ANDROID)
12 #include "content/browser/vr/android/cardboard/cardboard_vr_device_provider.h"
13 #endif
15 namespace content {
17 namespace {
18 VRDeviceManager* g_vr_device_manager = nullptr;
21 VRDeviceManager::VRDeviceManager()
22 : vr_initialized_(false), keep_alive_(false) {
23 bindings_.set_connection_error_handler(
24 base::Bind(&VRDeviceManager::OnConnectionError, base::Unretained(this)));
25 // Register VRDeviceProviders for the current platform
26 #if defined(OS_ANDROID)
27 scoped_ptr<VRDeviceProvider> cardboard_provider(
28 new CardboardVRDeviceProvider());
29 RegisterProvider(cardboard_provider.Pass());
30 #endif
33 VRDeviceManager::VRDeviceManager(scoped_ptr<VRDeviceProvider> provider)
34 : vr_initialized_(false), keep_alive_(true) {
35 thread_checker_.DetachFromThread();
36 RegisterProvider(provider.Pass());
37 SetInstance(this);
40 VRDeviceManager::~VRDeviceManager() {
41 DCHECK(thread_checker_.CalledOnValidThread());
42 g_vr_device_manager = nullptr;
45 void VRDeviceManager::BindRequest(mojo::InterfaceRequest<VRService> request) {
46 VRDeviceManager* device_manager = GetInstance();
47 device_manager->bindings_.AddBinding(device_manager, request.Pass());
50 void VRDeviceManager::OnConnectionError() {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 if (bindings_.empty() && !keep_alive_) {
53 // Delete the device manager when it has no active connections.
54 delete g_vr_device_manager;
58 VRDeviceManager* VRDeviceManager::GetInstance() {
59 if (!g_vr_device_manager)
60 g_vr_device_manager = new VRDeviceManager();
61 return g_vr_device_manager;
64 void VRDeviceManager::SetInstance(VRDeviceManager* instance) {
65 // Unit tests can create multiple instances but only one should exist at any
66 // given time so g_vr_device_manager should only go from nullptr to
67 // non-nullptr and vica versa.
68 CHECK_NE(!!instance, !!g_vr_device_manager);
69 g_vr_device_manager = instance;
72 bool VRDeviceManager::HasInstance() {
73 // For testing. Checks to see if a VRDeviceManager instance is active.
74 return !!g_vr_device_manager;
77 mojo::Array<VRDeviceInfoPtr> VRDeviceManager::GetVRDevices() {
78 DCHECK(thread_checker_.CalledOnValidThread());
80 InitializeProviders();
82 std::vector<VRDevice*> devices;
83 for (const auto& provider : providers_)
84 provider->GetDevices(&devices);
86 mojo::Array<VRDeviceInfoPtr> out_devices(0);
87 for (const auto& device : devices) {
88 if (device->id() == VR_DEVICE_LAST_ID)
89 continue;
91 if (devices_.find(device->id()) == devices_.end())
92 devices_[device->id()] = device;
94 VRDeviceInfoPtr vr_device_info = device->GetVRDevice();
95 if (vr_device_info.is_null())
96 continue;
98 vr_device_info->index = device->id();
99 out_devices.push_back(vr_device_info.Pass());
102 return out_devices;
105 VRDevice* VRDeviceManager::GetDevice(unsigned int index) {
106 DCHECK(thread_checker_.CalledOnValidThread());
108 DeviceMap::iterator iter = devices_.find(index);
109 if (iter == devices_.end()) {
110 return nullptr;
112 return iter->second;
115 void VRDeviceManager::InitializeProviders() {
116 if (vr_initialized_) {
117 return;
120 for (const auto& provider : providers_)
121 provider->Initialize();
123 vr_initialized_ = true;
126 void VRDeviceManager::RegisterProvider(scoped_ptr<VRDeviceProvider> provider) {
127 providers_.push_back(make_linked_ptr(provider.release()));
130 void VRDeviceManager::GetDevices(const GetDevicesCallback& callback) {
131 callback.Run(GetVRDevices());
134 void VRDeviceManager::GetSensorState(uint32_t index,
135 const GetSensorStateCallback& callback) {
136 VRDevice* device = GetDevice(index);
137 if (device) {
138 callback.Run(device->GetSensorState());
139 } else {
140 callback.Run(nullptr);
144 void VRDeviceManager::ResetSensor(uint32_t index) {
145 VRDevice* device = GetDevice(index);
146 if (device)
147 device->ResetSensor();
150 } // namespace content