1/*
2 * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "NavigatorWebVR.h"
28
29#include "Document.h"
30#include "JSVRDisplay.h"
31#include "Navigator.h"
32#include "RuntimeEnabledFeatures.h"
33#include "VRDisplay.h"
34#include "VRManager.h"
35#include "VRPlatformDisplay.h"
36
37namespace WebCore {
38
39void NavigatorWebVR::getVRDisplays(Navigator& navigator, Document& document, GetVRDisplaysPromise&& promise)
40{
41 if (!RuntimeEnabledFeatures::sharedFeatures().webVREnabled()) {
42 promise.reject();
43 return;
44 }
45 NavigatorWebVR::from(&navigator)->getVRDisplays(document, WTFMove(promise));
46}
47
48void NavigatorWebVR::getVRDisplays(Document& document, GetVRDisplaysPromise&& promise)
49{
50 document.postTask([this, promise = WTFMove(promise)] (ScriptExecutionContext& context) mutable {
51 Optional<VRManager::VRDisplaysVector> platformDisplays = VRManager::singleton().getVRDisplays();
52 if (!platformDisplays) {
53 promise.reject();
54 m_displays.clear();
55 return;
56 }
57
58 Vector<Ref<VRDisplay>> oldDisplays = WTFMove(m_displays);
59
60 // Update the vector of displays. Note that although this is O(n^2) the amount of expected
61 // VRDisplays is so reduced that it'll run fast enough.
62 for (auto& platformDisplay : platformDisplays.value()) {
63 bool newDisplay = true;
64 for (auto& oldDisplay : oldDisplays) {
65 if (platformDisplay->getDisplayInfo().displayIdentifier() == oldDisplay->displayId()) {
66 newDisplay = false;
67 m_displays.append(oldDisplay.copyRef());
68 break;
69 }
70 }
71 if (newDisplay) {
72 m_displays.append(VRDisplay::create(context, WTFMove(platformDisplay)));
73 m_displays.last()->platformDisplayConnected();
74 }
75 }
76 promise.resolve(WTF::map(m_displays, [](const Ref<VRDisplay>& display) {
77 return display.copyRef();
78 }));
79 });
80}
81
82const Vector<RefPtr<VRDisplay>>& NavigatorWebVR::activeVRDisplays(Navigator&)
83{
84 static auto mockVector = makeNeverDestroyed(Vector<RefPtr<VRDisplay>> { });
85 return mockVector;
86}
87
88const char* NavigatorWebVR::supplementName()
89{
90 return "NavigatorWebVR";
91}
92
93NavigatorWebVR* NavigatorWebVR::from(Navigator* navigator)
94{
95 NavigatorWebVR* supplement = static_cast<NavigatorWebVR*>(Supplement<Navigator>::from(navigator, supplementName()));
96 if (!supplement) {
97 auto newSupplement = std::make_unique<NavigatorWebVR>();
98 supplement = newSupplement.get();
99 provideTo(navigator, supplementName(), WTFMove(newSupplement));
100 }
101 return supplement;
102}
103
104} // namespace WebCore
105