Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/vr/VRController.h" | 5 #include "modules/vr/VRController.h" |
| 6 | 6 |
| 7 #include "core/dom/DOMException.h" | |
| 8 #include "core/dom/Document.h" | |
| 7 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "modules/vr/NavigatorVR.h" | |
| 8 #include "modules/vr/VRGetDevicesCallback.h" | 11 #include "modules/vr/VRGetDevicesCallback.h" |
| 9 #include "platform/RuntimeEnabledFeatures.h" | |
| 10 #include "platform/mojo/MojoHelper.h" | |
| 11 #include "public/platform/ServiceRegistry.h" | 12 #include "public/platform/ServiceRegistry.h" |
| 12 | 13 |
|
Mike West
2016/07/21 07:36:38
Nit: Drop the blank line.
| |
| 14 #include "wtf/Assertions.h" | |
| 15 | |
| 13 namespace blink { | 16 namespace blink { |
| 14 | 17 |
| 18 VRController::VRController(NavigatorVR* navigatorVR) | |
| 19 : ContextLifecycleObserver(navigatorVR->document()) | |
| 20 , m_navigatorVR(navigatorVR) | |
| 21 , m_binding(this) | |
| 22 { | |
| 23 navigatorVR->document()->frame()->serviceRegistry()->connectToRemoteService( mojo::GetProxy(&m_service)); | |
| 24 m_service->SetClient(m_binding.CreateInterfacePtrAndBind()); | |
| 25 } | |
| 26 | |
| 15 VRController::~VRController() | 27 VRController::~VRController() |
| 16 { | 28 { |
| 17 } | 29 } |
| 18 | 30 |
| 19 void VRController::provideTo(LocalFrame& frame, ServiceRegistry* registry) | 31 void VRController::getDisplays(ScriptPromiseResolver* resolver) |
| 20 { | |
| 21 ASSERT(RuntimeEnabledFeatures::webVREnabled()); | |
| 22 Supplement<LocalFrame>::provideTo(frame, supplementName(), registry ? new VR Controller(frame, registry) : nullptr); | |
| 23 } | |
| 24 | |
| 25 VRController* VRController::from(LocalFrame& frame) | |
| 26 { | |
| 27 return static_cast<VRController*>(Supplement<LocalFrame>::from(frame, supple mentName())); | |
| 28 } | |
| 29 | |
| 30 VRController::VRController(LocalFrame& frame, ServiceRegistry* registry) | |
| 31 { | |
| 32 ASSERT(!m_service.is_bound()); | |
| 33 registry->connectToRemoteService(mojo::GetProxy(&m_service)); | |
| 34 } | |
| 35 | |
| 36 const char* VRController::supplementName() | |
| 37 { | |
| 38 return "VRController"; | |
| 39 } | |
| 40 | |
| 41 void VRController::getDisplays(std::unique_ptr<VRGetDevicesCallback> callback) | |
| 42 { | 32 { |
| 43 if (!m_service) { | 33 if (!m_service) { |
| 44 callback->onError(); | 34 DOMException* exception = DOMException::create(InvalidStateError, "The s ervice is no longer active."); |
| 35 resolver->reject(exception); | |
| 45 return; | 36 return; |
| 46 } | 37 } |
| 47 | 38 |
| 48 m_pendingGetDevicesCallbacks.append(std::move(callback)); | 39 m_pendingGetDevicesCallbacks.append(WTF::wrapUnique(new VRGetDevicesCallback (resolver))); |
| 49 m_service->GetDisplays(convertToBaseCallback(WTF::bind(&VRController::onGetD isplays, wrapPersistent(this)))); | 40 m_service->GetDisplays(convertToBaseCallback(WTF::bind(&VRController::onGetD isplays, wrapPersistent(this)))); |
| 50 } | 41 } |
| 51 | 42 |
| 52 device::blink::VRPosePtr VRController::getPose(unsigned index) | 43 device::blink::VRPosePtr VRController::getPose(unsigned index) |
| 53 { | 44 { |
| 54 if (!m_service) | 45 if (!m_service) |
| 55 return nullptr; | 46 return nullptr; |
| 56 | 47 |
| 57 device::blink::VRPosePtr pose; | 48 device::blink::VRPosePtr pose; |
| 58 m_service->GetPose(index, &pose); | 49 m_service->GetPose(index, &pose); |
| 59 return pose; | 50 return pose; |
| 60 } | 51 } |
| 61 | 52 |
| 62 void VRController::resetPose(unsigned index) | 53 void VRController::resetPose(unsigned index) |
| 63 { | 54 { |
| 64 if (!m_service) | 55 if (!m_service) |
| 65 return; | 56 return; |
| 57 | |
| 66 m_service->ResetPose(index); | 58 m_service->ResetPose(index); |
| 67 } | 59 } |
| 68 | 60 |
| 61 VRDisplay* VRController::createOrUpdateDisplay(const device::blink::VRDisplayPtr & display) | |
| 62 { | |
| 63 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | |
| 64 if (!vrDisplay) { | |
| 65 vrDisplay = new VRDisplay(m_navigatorVR); | |
| 66 m_displays.append(vrDisplay); | |
| 67 } | |
| 68 | |
| 69 vrDisplay->update(display); | |
| 70 return vrDisplay; | |
| 71 } | |
| 72 | |
| 73 VRDisplayVector VRController::updateDisplays(mojo::WTFArray<device::blink::VRDis playPtr> displays) | |
| 74 { | |
| 75 VRDisplayVector vrDisplays; | |
| 76 | |
| 77 for (const auto& display : displays.PassStorage()) { | |
| 78 VRDisplay* vrDisplay = createOrUpdateDisplay(display); | |
| 79 vrDisplays.append(vrDisplay); | |
| 80 } | |
| 81 | |
| 82 return vrDisplays; | |
| 83 } | |
| 84 | |
| 85 VRDisplay* VRController::getDisplayForIndex(unsigned index) | |
| 86 { | |
| 87 VRDisplay* display; | |
| 88 for (size_t i = 0; i < m_displays.size(); ++i) { | |
| 89 display = m_displays[i]; | |
| 90 if (display->displayId() == index) { | |
| 91 return display; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 return 0; | |
| 96 } | |
| 97 | |
| 69 void VRController::onGetDisplays(mojo::WTFArray<device::blink::VRDisplayPtr> dis plays) | 98 void VRController::onGetDisplays(mojo::WTFArray<device::blink::VRDisplayPtr> dis plays) |
| 70 { | 99 { |
| 100 VRDisplayVector outDisplays = updateDisplays(std::move(displays)); | |
| 101 | |
| 71 std::unique_ptr<VRGetDevicesCallback> callback = m_pendingGetDevicesCallback s.takeFirst(); | 102 std::unique_ptr<VRGetDevicesCallback> callback = m_pendingGetDevicesCallback s.takeFirst(); |
| 72 if (!callback) | 103 if (!callback) |
| 73 return; | 104 return; |
| 74 | 105 |
| 75 callback->onSuccess(std::move(displays)); | 106 callback->onSuccess(outDisplays); |
| 107 } | |
| 108 | |
| 109 void VRController::OnDisplayChanged(device::blink::VRDisplayPtr display) | |
| 110 { | |
| 111 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | |
| 112 if (!vrDisplay) | |
| 113 return; | |
| 114 | |
| 115 vrDisplay->update(display); | |
| 116 } | |
| 117 | |
| 118 void VRController::contextDestroyed() | |
| 119 { | |
| 120 // If the document context was destroyed, shut down the client connection | |
| 121 // and never call the mojo service again. | |
| 122 m_binding.Close(); | |
| 123 m_service.reset(); | |
| 124 | |
| 125 // The context is not automatically cleared, so do it manually. | |
| 126 ContextLifecycleObserver::clearContext(); | |
| 76 } | 127 } |
| 77 | 128 |
| 78 DEFINE_TRACE(VRController) | 129 DEFINE_TRACE(VRController) |
| 79 { | 130 { |
| 80 Supplement<LocalFrame>::trace(visitor); | 131 visitor->trace(m_navigatorVR); |
| 132 visitor->trace(m_displays); | |
| 133 | |
| 134 ContextLifecycleObserver::trace(visitor); | |
| 81 } | 135 } |
| 82 | 136 |
| 83 } // namespace blink | 137 } // namespace blink |
| OLD | NEW |