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 "config.h" |
| 6 #include "modules/vr/NavigatorVRDevice.h" |
| 7 |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/frame/LocalDOMWindow.h" |
| 11 #include "core/frame/LocalFrame.h" |
| 12 #include "core/frame/Navigator.h" |
| 13 #include "core/page/Page.h" |
| 14 #include "modules/vr/HMDVRDevice.h" |
| 15 #include "modules/vr/PositionSensorVRDevice.h" |
| 16 #include "modules/vr/VRHardwareUnit.h" |
| 17 #include "modules/vr/VRPositionState.h" |
| 18 #include "platform/RuntimeEnabledFeatures.h" |
| 19 #include "public/platform/Platform.h" |
| 20 |
| 21 namespace blink { |
| 22 |
| 23 NavigatorVRDevice* NavigatorVRDevice::from(Document& document) |
| 24 { |
| 25 if (!document.frame() || !document.frame()->domWindow()) |
| 26 return 0; |
| 27 Navigator& navigator = *document.frame()->domWindow()->navigator(); |
| 28 return &from(navigator); |
| 29 } |
| 30 |
| 31 NavigatorVRDevice& NavigatorVRDevice::from(Navigator& navigator) |
| 32 { |
| 33 NavigatorVRDevice* supplement = static_cast<NavigatorVRDevice*>(WillBeHeapSu
pplement<Navigator>::from(navigator, supplementName())); |
| 34 if (!supplement) { |
| 35 supplement = new NavigatorVRDevice(); |
| 36 provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement)); |
| 37 } |
| 38 return *supplement; |
| 39 } |
| 40 |
| 41 ScriptPromise NavigatorVRDevice::getVRDevices(ScriptState* scriptState, Navigato
r& navigator) |
| 42 { |
| 43 return NavigatorVRDevice::from(navigator).getVRDevices(scriptState); |
| 44 } |
| 45 |
| 46 ScriptPromise NavigatorVRDevice::getVRDevices(ScriptState* scriptState) |
| 47 { |
| 48 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 49 ScriptPromise promise = resolver->promise(); |
| 50 resolver->resolve(getUpdatedVRHardwareUnits()); |
| 51 |
| 52 return promise; |
| 53 } |
| 54 |
| 55 HeapVector<Member<VRDevice>> NavigatorVRDevice::getUpdatedVRHardwareUnits() |
| 56 { |
| 57 WebVector<blink::WebVRDevice> devices; |
| 58 blink::Platform::current()->getVRDevices(&devices); |
| 59 |
| 60 VRDeviceVector vrDevices; |
| 61 for (size_t i = 0; i < devices.size(); ++i) { |
| 62 const blink::WebVRDevice& device = devices[i]; |
| 63 |
| 64 VRHardwareUnit* hardwareUnit = getHardwareUnitForIndex(device.index); |
| 65 if (!hardwareUnit) { |
| 66 hardwareUnit = new VRHardwareUnit(); |
| 67 m_hardwareUnits.append(hardwareUnit); |
| 68 } |
| 69 |
| 70 hardwareUnit->updateFromWebVRDevice(device); |
| 71 hardwareUnit->addDevicesToVector(vrDevices); |
| 72 } |
| 73 |
| 74 return vrDevices; |
| 75 } |
| 76 |
| 77 VRHardwareUnit* NavigatorVRDevice::getHardwareUnitForIndex(unsigned index) |
| 78 { |
| 79 VRHardwareUnit* hardwareUnit; |
| 80 for (size_t i = 0; i < m_hardwareUnits.size(); ++i) { |
| 81 hardwareUnit = m_hardwareUnits[i]; |
| 82 if (hardwareUnit->index() == index) { |
| 83 return hardwareUnit; |
| 84 } |
| 85 } |
| 86 |
| 87 return 0; |
| 88 } |
| 89 |
| 90 void NavigatorVRDevice::trace(Visitor* visitor) |
| 91 { |
| 92 visitor->trace(m_hardwareUnits); |
| 93 |
| 94 WillBeHeapSupplement<Navigator>::trace(visitor); |
| 95 } |
| 96 |
| 97 NavigatorVRDevice::NavigatorVRDevice() |
| 98 { |
| 99 } |
| 100 |
| 101 NavigatorVRDevice::~NavigatorVRDevice() |
| 102 { |
| 103 } |
| 104 |
| 105 const char* NavigatorVRDevice::supplementName() |
| 106 { |
| 107 return "NavigatorVRDevice"; |
| 108 } |
| 109 |
| 110 } // namespace blink |
OLD | NEW |