| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/browser/vr/vr_device_manager.h" | |
| 6 | |
| 7 #include "base/memory/singleton.h" | |
| 8 #include "third_party/WebKit/public/platform/modules/vr/WebVR.h" | |
| 9 | |
| 10 #if defined(OS_ANDROID) | |
| 11 #include "content/browser/vr/android/cardboard/cardboard_vr_device_provider.h" | |
| 12 #endif | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 namespace { | |
| 17 VRDeviceManager* g_vr_device_manager = nullptr; | |
| 18 } | |
| 19 | |
| 20 VRDeviceManager::VRDeviceManager() | |
| 21 : vr_initialized_(false), keep_alive_(false) { | |
| 22 bindings_.set_error_handler(this); | |
| 23 | |
| 24 #if defined(OS_ANDROID) | |
| 25 scoped_ptr<VRDeviceProvider> cardboard_provider( | |
| 26 new CardboardVRDeviceProvider()); | |
| 27 RegisterProvider(cardboard_provider.Pass()); | |
| 28 #endif | |
| 29 } | |
| 30 | |
| 31 VRDeviceManager::VRDeviceManager(scoped_ptr<VRDeviceProvider> provider) | |
| 32 : vr_initialized_(false), keep_alive_(true) { | |
| 33 thread_checker_.DetachFromThread(); | |
| 34 RegisterProvider(provider.Pass()); | |
| 35 SetInstance(this); | |
| 36 } | |
| 37 | |
| 38 VRDeviceManager::~VRDeviceManager() { | |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 40 g_vr_device_manager = nullptr; | |
| 41 } | |
| 42 | |
| 43 void VRDeviceManager::BindRequest(mojo::InterfaceRequest<VRService> request) { | |
| 44 VRDeviceManager* device_manager = GetInstance(); | |
| 45 device_manager->bindings_.AddBinding(device_manager, request.Pass()); | |
| 46 } | |
| 47 | |
| 48 void VRDeviceManager::OnConnectionError() { | |
| 49 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 50 if (bindings_.empty() && !keep_alive_) { | |
| 51 // Delete the device manager when it has no active connections. | |
| 52 delete g_vr_device_manager; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 VRDeviceManager* VRDeviceManager::GetInstance() { | |
| 57 if (!g_vr_device_manager) | |
| 58 g_vr_device_manager = new VRDeviceManager(); | |
| 59 return g_vr_device_manager; | |
| 60 } | |
| 61 | |
| 62 void VRDeviceManager::SetInstance(VRDeviceManager* instance) { | |
| 63 // Unit tests can create multiple instances but only one should exist at any | |
| 64 // given time so g_vr_device_manager should only go from nullptr to | |
| 65 // non-nullptr and vica versa. | |
| 66 CHECK_NE(!!instance, !!g_vr_device_manager); | |
| 67 g_vr_device_manager = instance; | |
| 68 } | |
| 69 | |
| 70 bool VRDeviceManager::HasInstance() { | |
| 71 // For testing. Checks to see if a VRDeviceManager instance is active. | |
| 72 return !!g_vr_device_manager; | |
| 73 } | |
| 74 | |
| 75 mojo::Array<VRDeviceInfoPtr> VRDeviceManager::GetVRDevices() { | |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 77 | |
| 78 InitializeProviders(); | |
| 79 | |
| 80 std::vector<VRDevice*> devices; | |
| 81 for (const auto& provider : providers_) | |
| 82 provider->GetDevices(devices); | |
| 83 | |
| 84 mojo::Array<VRDeviceInfoPtr> out_devices(0); | |
| 85 for (const auto& device : devices) { | |
| 86 if (device->id() == VR_DEVICE_LAST_ID) | |
| 87 continue; | |
| 88 | |
| 89 if (devices_.find(device->id()) == devices_.end()) | |
| 90 devices_[device->id()] = device; | |
| 91 | |
| 92 VRDeviceInfoPtr vr_device_info = device->GetVRDevice(); | |
| 93 if (vr_device_info.is_null()) | |
| 94 continue; | |
| 95 | |
| 96 vr_device_info->index = device->id(); | |
| 97 out_devices.push_back(vr_device_info.Pass()); | |
| 98 } | |
| 99 | |
| 100 return out_devices; | |
| 101 } | |
| 102 | |
| 103 VRDevice* VRDeviceManager::GetDevice(unsigned int index) { | |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 105 | |
| 106 DeviceMap::iterator iter = devices_.find(index); | |
| 107 if (iter == devices_.end()) { | |
| 108 return nullptr; | |
| 109 } | |
| 110 return iter->second; | |
| 111 } | |
| 112 | |
| 113 void VRDeviceManager::InitializeProviders() { | |
| 114 if (vr_initialized_) { | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 for (const auto& provider : providers_) | |
| 119 provider->Initialize(); | |
| 120 | |
| 121 vr_initialized_ = true; | |
| 122 } | |
| 123 | |
| 124 void VRDeviceManager::RegisterProvider(scoped_ptr<VRDeviceProvider> provider) { | |
| 125 providers_.push_back(make_linked_ptr(provider.release())); | |
| 126 } | |
| 127 | |
| 128 void VRDeviceManager::GetDevices(const GetDevicesCallback& callback) { | |
| 129 callback.Run(GetVRDevices()); | |
| 130 } | |
| 131 | |
| 132 void VRDeviceManager::GetSensorState(uint32_t index, | |
| 133 const GetSensorStateCallback& callback) { | |
| 134 VRDevice* device = GetDevice(index); | |
| 135 if (device) { | |
| 136 callback.Run(device->GetSensorState()); | |
| 137 } else { | |
| 138 callback.Run(nullptr); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void VRDeviceManager::ResetSensor(uint32_t index) { | |
| 143 VRDevice* device = GetDevice(index); | |
| 144 if (device) | |
| 145 device->ResetSensor(); | |
| 146 } | |
| 147 | |
| 148 } // namespace content | |
| OLD | NEW |