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