Chromium Code Reviews| 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; | |
|
Ken Rockot(use gerrit already)
2015/06/15 18:22:55
Probably want to null out g_vr_device_manager here
| |
| 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 ProviderList::iterator iter; | |
| 82 for (iter = providers_.begin(); iter != providers_.end(); ++iter) { | |
|
Ken Rockot(use gerrit already)
2015/06/15 18:22:55
nit: you could simplify a bit with ranged-based lo
| |
| 83 (*iter)->GetDevices(devices); | |
| 84 } | |
| 85 | |
| 86 mojo::Array<VRDeviceInfoPtr> out_devices(0); | |
| 87 std::vector<VRDevice*>::iterator diter; | |
| 88 for (diter = devices.begin(); diter != devices.end(); ++diter) { | |
| 89 VRDevice* device = *diter; | |
| 90 | |
| 91 if (device->id() == VR_DEVICE_LAST_ID) | |
| 92 continue; | |
| 93 | |
| 94 if (devices_.find(device->id()) == devices_.end()) { | |
| 95 devices_[device->id()] = device; | |
| 96 } | |
| 97 | |
| 98 VRDeviceInfoPtr vr_device_info = device->GetVRDevice(); | |
| 99 if (vr_device_info.is_null()) | |
| 100 continue; | |
| 101 | |
| 102 vr_device_info->index = device->id(); | |
| 103 out_devices.push_back(vr_device_info.Pass()); | |
| 104 } | |
| 105 | |
| 106 return out_devices; | |
| 107 } | |
| 108 | |
| 109 VRDevice* VRDeviceManager::GetDevice(unsigned int index) { | |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 111 | |
| 112 DeviceMap::iterator iter = devices_.find(index); | |
| 113 if (iter == devices_.end()) { | |
| 114 return nullptr; | |
| 115 } | |
| 116 return iter->second; | |
| 117 } | |
| 118 | |
| 119 void VRDeviceManager::InitializeProviders() { | |
| 120 if (vr_initialized_) { | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 ProviderList::iterator iter; | |
| 125 for (iter = providers_.begin(); iter != providers_.end(); ++iter) { | |
| 126 (*iter)->Initialize(); | |
| 127 } | |
| 128 | |
| 129 vr_initialized_ = true; | |
| 130 } | |
| 131 | |
| 132 void VRDeviceManager::RegisterProvider(scoped_ptr<VRDeviceProvider> provider) { | |
| 133 providers_.push_back(make_linked_ptr(provider.release())); | |
| 134 } | |
| 135 | |
| 136 void VRDeviceManager::GetDevices(const GetDevicesCallback& callback) { | |
| 137 callback.Run(GetVRDevices()); | |
| 138 } | |
| 139 | |
| 140 void VRDeviceManager::GetSensorState(uint32_t index, | |
| 141 const GetSensorStateCallback& callback) { | |
| 142 VRDevice* device = GetDevice(index); | |
| 143 if (device) { | |
| 144 callback.Run(device->GetSensorState()); | |
| 145 } else { | |
| 146 callback.Run(nullptr); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void VRDeviceManager::ResetSensor(uint32_t index) { | |
| 151 VRDevice* device = GetDevice(index); | |
| 152 if (device) | |
| 153 device->ResetSensor(); | |
| 154 } | |
| 155 | |
| 156 } // namespace content | |
| OLD | NEW |