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 VRService::VRService() : | |
17 vr_initialized_(false) { | |
18 #if defined(OS_ANDROID) | |
19 RegisterProvider(new CardboardVRDeviceProvider()); | |
20 #endif | |
21 } | |
22 | |
23 VRService::VRService(VRDeviceProvider* provider) : | |
24 vr_initialized_(false) { | |
25 thread_checker_.DetachFromThread(); | |
26 RegisterProvider(provider); | |
27 } | |
28 | |
29 VRService::~VRService() { | |
no sievers
2015/03/26 19:55:15
Should this also DCHECK(thread_checker_.CalledOnVa
| |
30 std::for_each(providers_.begin(), providers_.end(), | |
31 [](VRDeviceProvider* p){ delete p; }); | |
32 } | |
33 | |
34 VRService* VRService::GetInstance() { | |
35 return Singleton<VRService, | |
36 LeakySingletonTraits<VRService> >::get(); | |
no sievers
2015/03/26 19:55:15
So you are still leaking this whole class, so the
| |
37 } | |
38 | |
39 void VRService::GetVRDevices( | |
40 std::vector<blink::WebVRDevice>* webvr_devices) { | |
41 DCHECK(thread_checker_.CalledOnValidThread()); | |
42 | |
43 if(webvr_devices == NULL) | |
44 return; | |
45 | |
46 InitializeProviders(); | |
47 | |
48 webvr_devices->clear(); | |
49 | |
50 ProviderList::iterator iter; | |
51 | |
52 std::vector<VRDevice*> devices; | |
53 | |
54 for (iter = providers_.begin(); iter != providers_.end(); ++iter) { | |
55 (*iter)->GetDevices(devices); | |
56 } | |
57 | |
58 std::vector<VRDevice*>::iterator diter; | |
59 for (diter = devices.begin(); diter != devices.end(); ++diter) { | |
60 VRDevice* device = *diter; | |
61 if (devices_.find(device->id()) == devices_.end()) { | |
62 devices_[device->id()] = device; | |
63 } | |
64 | |
65 blink::WebVRDevice webvr_device; | |
66 device->GetVRDevice(&webvr_device); | |
67 webvr_device.index = device->id(); | |
68 webvr_devices->push_back(webvr_device); | |
69 } | |
70 } | |
71 | |
72 VRDevice* VRService::GetDevice(unsigned int index) { | |
73 DCHECK(thread_checker_.CalledOnValidThread()); | |
74 | |
75 DeviceMap::iterator iter = devices_.find(index); | |
76 if (iter == devices_.end()) { | |
77 return NULL; | |
78 } | |
79 return iter->second; | |
80 } | |
81 | |
82 void VRService::AddConsumer(VRServiceConsumer* consumer) { | |
83 DCHECK(thread_checker_.CalledOnValidThread()); | |
84 | |
85 // Explicitly does not initialize the providers. That's done lazily the first | |
86 // time GetVRDevices is called. | |
87 consumers_.insert(consumer); | |
88 } | |
89 | |
90 void VRService::RemoveConsumer(VRServiceConsumer* consumer) { | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 | |
93 ConsumerSet::iterator it = consumers_.find(consumer); | |
94 if (it != consumers_.end()) { | |
no sievers
2015/03/26 19:55:15
Should this DCHECK() or log an error if not found?
| |
95 consumers_.erase(it); | |
96 if (consumers_.size() == 0) { | |
97 ReleaseProviderDevices(); | |
98 } | |
99 } | |
100 } | |
101 | |
102 void VRService::InitializeProviders() { | |
103 if(vr_initialized_ == true) { | |
no sievers
2015/03/26 19:55:15
nit: space after 'if'
| |
104 return; | |
105 } | |
106 | |
107 ProviderList::iterator iter; | |
108 for (iter = providers_.begin(); iter != providers_.end(); ++iter) { | |
109 (*iter)->Initialize(); | |
110 } | |
111 | |
112 vr_initialized_ = true; | |
113 } | |
114 | |
115 void VRService::ReleaseProviderDevices() { | |
116 if(vr_initialized_ == false) { | |
no sievers
2015/03/26 19:55:15
nit: space after 'if' (also !vr_initalized?)
| |
117 return; | |
118 } | |
119 | |
120 devices_.clear(); | |
121 | |
122 ProviderList::iterator iter; | |
123 for (iter = providers_.begin(); iter != providers_.end(); ++iter) { | |
124 (*iter)->ReleaseDevices(); | |
125 } | |
126 | |
127 vr_initialized_ = false; | |
128 } | |
129 | |
130 void VRService::RegisterProvider(VRDeviceProvider* provider) { | |
no sievers
2015/03/26 19:55:15
If |this| takes ownership, should it be passing a
| |
131 providers_.push_back(provider); | |
132 } | |
133 | |
134 } // namespace content | |
OLD | NEW |