Chromium Code Reviews| Index: Source/modules/vr/NavigatorVRDevice.cpp |
| diff --git a/Source/modules/vr/NavigatorVRDevice.cpp b/Source/modules/vr/NavigatorVRDevice.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..17a9a99e295dff2c8fe72da9a8dbdefaad2fd287 |
| --- /dev/null |
| +++ b/Source/modules/vr/NavigatorVRDevice.cpp |
| @@ -0,0 +1,113 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "modules/vr/NavigatorVRDevice.h" |
| + |
| +#include "bindings/core/v8/ScriptPromiseResolver.h" |
| +#include "core/dom/Document.h" |
| +#include "core/frame/LocalDOMWindow.h" |
| +#include "core/frame/LocalFrame.h" |
| +#include "core/frame/Navigator.h" |
| +#include "core/page/Page.h" |
| +#include "modules/vr/HMDVRDevice.h" |
| +#include "modules/vr/PositionSensorVRDevice.h" |
| +#include "modules/vr/VRHardwareUnit.h" |
| +#include "modules/vr/VRPositionState.h" |
| +#include "platform/RuntimeEnabledFeatures.h" |
| +#include "public/platform/Platform.h" |
| + |
| +namespace blink { |
| + |
| +NavigatorVRDevice* NavigatorVRDevice::from(Document& document) |
| +{ |
| + if (!document.frame() || !document.frame()->domWindow()) |
| + return 0; |
| + Navigator& navigator = *document.frame()->domWindow()->navigator(); |
| + return &from(navigator); |
| +} |
| + |
| +NavigatorVRDevice& NavigatorVRDevice::from(Navigator& navigator) |
| +{ |
| + NavigatorVRDevice* supplement = static_cast<NavigatorVRDevice*>(WillBeHeapSupplement<Navigator>::from(navigator, supplementName())); |
| + if (!supplement) { |
| + supplement = new NavigatorVRDevice(); |
| + provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement)); |
| + } |
| + return *supplement; |
| +} |
| + |
| +ScriptPromise NavigatorVRDevice::getVRDevices(ScriptState* scriptState, Navigator& navigator) |
| +{ |
| + return NavigatorVRDevice::from(navigator).getVRDevices(scriptState); |
| +} |
| + |
| +ScriptPromise NavigatorVRDevice::getVRDevices(ScriptState* scriptState) |
| +{ |
| + RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + resolver->resolve(updateVRHardwareUnits()); |
| + |
| + return promise; |
| +} |
| + |
| +HeapVector<Member<VRDevice>> NavigatorVRDevice::updateVRHardwareUnits() |
|
sof
2015/02/24 12:35:29
Hmm, naming doesn't suggest that it returns a resu
|
| +{ |
| + std::vector<blink::WebVRDevice> devices; |
| + blink::Platform::current()->getVRDevices(&devices); |
| + |
| + VRDeviceVector vrDevices; |
| + |
| + std::vector<blink::WebVRDevice>::iterator deviceIterator; |
| + for (deviceIterator = devices.begin(); deviceIterator != devices.end(); deviceIterator++) { |
| + const blink::WebVRDevice& device = *(deviceIterator); |
| + |
| + VRHardwareUnit* hardwareUnit = getHardwareUnitForIndex(device.index); |
| + if (!hardwareUnit) { |
| + hardwareUnit = new VRHardwareUnit(); |
| + m_hardwareUnits.append(hardwareUnit); |
| + } |
| + |
| + hardwareUnit->updateFromWebVRDevice(device); |
| + hardwareUnit->addDevicesToVector(vrDevices); |
| + } |
| + |
| + return vrDevices; |
| +} |
| + |
| +VRHardwareUnit* NavigatorVRDevice::getHardwareUnitForIndex(unsigned index) |
| +{ |
| + VRHardwareUnit* hardwareUnit; |
| + HeapVector<Member<VRHardwareUnit>>::iterator iter; |
| + for (iter = m_hardwareUnits.begin(); iter != m_hardwareUnits.end(); ++iter) { |
|
sof
2015/02/24 12:35:29
Can we use a range-based for loop here?
|
| + hardwareUnit = *iter; |
| + if (hardwareUnit->index() == index) { |
| + return hardwareUnit; |
| + } |
| + } |
| + |
| + return 0; |
| +} |
| + |
| +void NavigatorVRDevice::trace(Visitor* visitor) |
| +{ |
| + visitor->trace(m_hardwareUnits); |
| + |
| + WillBeHeapSupplement<Navigator>::trace(visitor); |
| +} |
| + |
| +NavigatorVRDevice::NavigatorVRDevice() |
| +{ |
| +} |
| + |
| +NavigatorVRDevice::~NavigatorVRDevice() |
| +{ |
| +} |
| + |
| +const char* NavigatorVRDevice::supplementName() |
| +{ |
| + return "NavigatorVRDevice"; |
| +} |
| + |
| +} // namespace blink |