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