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_service.h" | |
6 | |
7 #include "base/memory/singleton.h" | |
8 #include "third_party/WebKit/public/platform/WebVR.h" | |
9 | |
10 #if defined(OS_ANDROID) | |
11 #include "content/browser/vr/cardboard/cardboard_vr_device_provider.h" | |
12 #endif | |
13 | |
14 namespace content { | |
15 | |
16 namespace { | |
17 VRService* g_vr_service = NULL; | |
18 } | |
19 | |
20 VRService::VRService() : | |
21 vr_initialized_(false), | |
22 keep_alive_(false) { | |
23 #if defined(OS_ANDROID) | |
24 scoped_ptr<VRDeviceProvider> cardboard_provider( | |
25 new CardboardVRDeviceProvider()); | |
26 RegisterProvider(cardboard_provider.Pass()); | |
27 #endif | |
28 } | |
29 | |
30 VRService::VRService(scoped_ptr<VRDeviceProvider> provider) : | |
31 vr_initialized_(false), | |
32 keep_alive_(true) { | |
33 thread_checker_.DetachFromThread(); | |
34 RegisterProvider(provider.Pass()); | |
35 SetInstance(this); | |
36 } | |
37 | |
38 VRService::~VRService() { | |
39 DCHECK(thread_checker_.CalledOnValidThread()); | |
40 g_vr_service = NULL; | |
41 } | |
42 | |
43 VRService* VRService::GetInstance() { | |
44 if (!g_vr_service) | |
45 g_vr_service = new VRService(); | |
46 return g_vr_service; | |
47 } | |
48 | |
49 void VRService::SetInstance(VRService* instance) { | |
50 // Unit tests can create multiple instances but only one should exist at any | |
51 // given time so g_vr_service should only go from NULL to non-NULL and | |
52 // vica versa. | |
53 CHECK_NE(!!instance, !!g_vr_service); | |
54 g_vr_service = instance; | |
55 } | |
56 | |
57 bool VRService::HasInstance() { | |
58 // For testing. Checks to see if a VRService instance is active. | |
59 return !!g_vr_service; | |
60 } | |
61 | |
62 void VRService::GetVRDevices( | |
63 std::vector<blink::WebVRDevice>* webvr_devices) { | |
64 DCHECK(thread_checker_.CalledOnValidThread()); | |
65 | |
66 if (!webvr_devices) | |
67 return; | |
68 | |
69 InitializeProviders(); | |
70 | |
71 webvr_devices->clear(); | |
72 | |
73 ProviderList::iterator iter; | |
74 | |
75 std::vector<VRDevice*> devices; | |
76 | |
77 for (iter = providers_.begin(); iter != providers_.end(); ++iter) { | |
78 (*iter)->GetDevices(devices); | |
79 } | |
80 | |
81 std::vector<VRDevice*>::iterator diter; | |
82 for (diter = devices.begin(); diter != devices.end(); ++diter) { | |
83 VRDevice* device = *diter; | |
84 if (devices_.find(device->id()) == devices_.end()) { | |
85 devices_[device->id()] = device; | |
86 } | |
87 | |
88 blink::WebVRDevice webvr_device; | |
89 device->GetVRDevice(&webvr_device); | |
90 webvr_device.index = device->id(); | |
91 webvr_devices->push_back(webvr_device); | |
92 } | |
93 } | |
94 | |
95 VRDevice* VRService::GetDevice(unsigned int index) { | |
96 DCHECK(thread_checker_.CalledOnValidThread()); | |
97 | |
98 DeviceMap::iterator iter = devices_.find(index); | |
99 if (iter == devices_.end()) { | |
100 return NULL; | |
Ted C
2015/04/24 01:06:07
nullptr (and other places instead of NULL)
| |
101 } | |
102 return iter->second; | |
103 } | |
104 | |
105 void VRService::AddConsumer(VRServiceConsumer* consumer) { | |
106 VRService* service = GetInstance(); | |
107 DCHECK(service->thread_checker_.CalledOnValidThread()); | |
108 | |
109 // Explicitly does not initialize the providers. That's done lazily the first | |
110 // time GetVRDevices is called. | |
111 service->consumers_.insert(consumer); | |
112 } | |
113 | |
114 void VRService::RemoveConsumer(VRServiceConsumer* consumer) { | |
115 CHECK(g_vr_service); | |
116 DCHECK(g_vr_service->thread_checker_.CalledOnValidThread()); | |
117 | |
118 ConsumerSet::iterator it = g_vr_service->consumers_.find(consumer); | |
119 DCHECK(it != g_vr_service->consumers_.end()); | |
120 | |
121 if (it != g_vr_service->consumers_.end()) { | |
122 g_vr_service->consumers_.erase(it); | |
123 if (g_vr_service->consumers_.size() == 0 && !g_vr_service->keep_alive_) { | |
124 // Delete the service when it has no consumers left. | |
125 delete g_vr_service; | |
126 } | |
127 } | |
128 } | |
129 | |
130 void VRService::InitializeProviders() { | |
131 if (vr_initialized_ == true) { | |
Ted C
2015/04/24 01:06:07
no need for == true
| |
132 return; | |
133 } | |
134 | |
135 ProviderList::iterator iter; | |
136 for (iter = providers_.begin(); iter != providers_.end(); ++iter) { | |
137 (*iter)->Initialize(); | |
138 } | |
139 | |
140 vr_initialized_ = true; | |
141 } | |
142 | |
143 void VRService::RegisterProvider(scoped_ptr<VRDeviceProvider> provider) { | |
144 providers_.push_back(make_linked_ptr(provider.release())); | |
145 } | |
146 | |
147 } // namespace content | |
OLD | NEW |