Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / vr / vr_device_manager.cc
blob476149c27be14ced93cc52a018761a17a9e92a70
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/memory/singleton.h"
8 #include "third_party/WebKit/public/platform/modules/vr/WebVR.h"
10 namespace content {
12 namespace {
13 VRDeviceManager* g_vr_device_manager = nullptr;
16 VRDeviceManager::VRDeviceManager()
17 : vr_initialized_(false), keep_alive_(false) {
18 bindings_.set_error_handler(this);
19 // Register VRDeviceProviders for the current platform
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);
29 VRDeviceManager::~VRDeviceManager() {
30 DCHECK(thread_checker_.CalledOnValidThread());
31 g_vr_device_manager = nullptr;
34 void VRDeviceManager::BindRequest(mojo::InterfaceRequest<VRService> request) {
35 VRDeviceManager* device_manager = GetInstance();
36 device_manager->bindings_.AddBinding(device_manager, request.Pass());
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;
47 VRDeviceManager* VRDeviceManager::GetInstance() {
48 if (!g_vr_device_manager)
49 g_vr_device_manager = new VRDeviceManager();
50 return g_vr_device_manager;
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;
61 bool VRDeviceManager::HasInstance() {
62 // For testing. Checks to see if a VRDeviceManager instance is active.
63 return !!g_vr_device_manager;
66 mojo::Array<VRDeviceInfoPtr> VRDeviceManager::GetVRDevices() {
67 DCHECK(thread_checker_.CalledOnValidThread());
69 InitializeProviders();
71 std::vector<VRDevice*> devices;
72 for (const auto& provider : providers_)
73 provider->GetDevices(devices);
75 mojo::Array<VRDeviceInfoPtr> out_devices(0);
76 for (const auto& device : devices) {
77 if (device->id() == VR_DEVICE_LAST_ID)
78 continue;
80 if (devices_.find(device->id()) == devices_.end())
81 devices_[device->id()] = device;
83 VRDeviceInfoPtr vr_device_info = device->GetVRDevice();
84 if (vr_device_info.is_null())
85 continue;
87 vr_device_info->index = device->id();
88 out_devices.push_back(vr_device_info.Pass());
91 return out_devices;
94 VRDevice* VRDeviceManager::GetDevice(unsigned int index) {
95 DCHECK(thread_checker_.CalledOnValidThread());
97 DeviceMap::iterator iter = devices_.find(index);
98 if (iter == devices_.end()) {
99 return nullptr;
101 return iter->second;
104 void VRDeviceManager::InitializeProviders() {
105 if (vr_initialized_) {
106 return;
109 for (const auto& provider : providers_)
110 provider->Initialize();
112 vr_initialized_ = true;
115 void VRDeviceManager::RegisterProvider(scoped_ptr<VRDeviceProvider> provider) {
116 providers_.push_back(make_linked_ptr(provider.release()));
119 void VRDeviceManager::GetDevices(const GetDevicesCallback& callback) {
120 callback.Run(GetVRDevices());
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);
133 void VRDeviceManager::ResetSensor(uint32_t index) {
134 VRDevice* device = GetDevice(index);
135 if (device)
136 device->ResetSensor();
139 } // namespace content