1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* Copyright (C) 2014 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "NavigatorGamepad.h"
#if ENABLE(GAMEPAD)
#include "Chrome.h"
#include "ChromeClient.h"
#include "Document.h"
#include "Gamepad.h"
#include "GamepadManager.h"
#include "GamepadProvider.h"
#include "LocalDOMWindow.h"
#include "Navigator.h"
#include "Page.h"
#include "PermissionsPolicy.h"
#include "PlatformGamepad.h"
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(NavigatorGamepad);
NavigatorGamepad::NavigatorGamepad(Navigator& navigator)
: m_navigator(navigator)
{
GamepadManager::singleton().registerNavigator(navigator);
}
NavigatorGamepad::~NavigatorGamepad()
{
GamepadManager::singleton().unregisterNavigator(protectedNavigator());
}
Ref<Navigator> NavigatorGamepad::protectedNavigator() const
{
return m_navigator.get();
}
ASCIILiteral NavigatorGamepad::supplementName()
{
return "NavigatorGamepad"_s;
}
NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator)
{
auto* supplement = static_cast<NavigatorGamepad*>(Supplement<Navigator>::from(&navigator, supplementName()));
if (!supplement) {
auto newSupplement = makeUnique<NavigatorGamepad>(navigator);
supplement = newSupplement.get();
provideTo(&navigator, supplementName(), WTFMove(newSupplement));
}
return *supplement;
}
Ref<Gamepad> NavigatorGamepad::gamepadFromPlatformGamepad(PlatformGamepad& platformGamepad)
{
unsigned index = platformGamepad.index();
if (index >= m_gamepads.size() || !m_gamepads[index])
return Gamepad::create(m_navigator->protectedDocument().get(), platformGamepad);
return *m_gamepads[index];
}
ExceptionOr<const Vector<RefPtr<Gamepad>>&> NavigatorGamepad::getGamepads(Navigator& navigator)
{
RefPtr document = navigator.document() ? navigator.document() : nullptr;
if (!document || !document->isFullyActive()) {
static NeverDestroyed<Vector<RefPtr<Gamepad>>> emptyGamepads;
return { emptyGamepads.get() };
}
if (!PermissionsPolicy::isFeatureEnabled(PermissionsPolicy::Feature::Gamepad, *document))
return Exception { ExceptionCode::SecurityError, "Third-party iframes are not allowed to call getGamepads() unless explicitly allowed via Feature-Policy (gamepad)"_s };
return NavigatorGamepad::from(navigator).gamepads();
}
Navigator& NavigatorGamepad::navigator() const
{
return m_navigator.get();
}
// The UIProcess tracks when a WebPage has recently used gamepads to configure certain behaviors on the page.
//
// Once a WebPage notifies the UIProcess that gamepads have been accessed, the UIProcess starts a timer with
// a short delay, after which it assumes that WebPage is no longer actively using gamepads.
//
// This works because of the polling nature of the gamepad API - If a web page is using gamepads it will be
// accessing them multiple times per second.
//
// WebPages need to continuously tell the UIProcess that gamepads have been used recently, but can do so lazily;
// As long as a WebPage continuously using gamepads notifies the UIProcess at least once during the UIProcess's
// timer delay, things will work as expected.
//
// So it follows: Web processes have this value initialized to be compatible with the UIProcess timer threshold.
static Seconds s_gamepadsRecentlyAccessedThreshold;
void NavigatorGamepad::setGamepadsRecentlyAccessedThreshold(Seconds threshold)
{
// This value should only initialized once.
RELEASE_ASSERT(!s_gamepadsRecentlyAccessedThreshold);
s_gamepadsRecentlyAccessedThreshold = threshold;
}
Seconds NavigatorGamepad::gamepadsRecentlyAccessedThreshold()
{
return s_gamepadsRecentlyAccessedThreshold;
}
const Vector<RefPtr<Gamepad>>& NavigatorGamepad::gamepads()
{
if (RefPtr frame = m_navigator->frame()) {
if (RefPtr page = frame->protectedPage())
page->gamepadsRecentlyAccessed();
}
if (m_gamepads.isEmpty())
return m_gamepads;
auto& platformGamepads = GamepadProvider::singleton().platformGamepads();
for (unsigned i = 0; i < platformGamepads.size(); ++i) {
if (!platformGamepads[i]) {
ASSERT(!m_gamepads[i]);
continue;
}
ASSERT(m_gamepads[i]);
m_gamepads[i]->updateFromPlatformGamepad(*platformGamepads[i]);
}
return m_gamepads;
}
void NavigatorGamepad::gamepadsBecameVisible()
{
auto& platformGamepads = GamepadProvider::singleton().platformGamepads();
m_gamepads.resize(platformGamepads.size());
for (unsigned i = 0; i < platformGamepads.size(); ++i) {
if (!platformGamepads[i])
continue;
m_gamepads[i] = Gamepad::create(m_navigator->protectedDocument().get(), *platformGamepads[i]);
}
}
void NavigatorGamepad::gamepadConnected(PlatformGamepad& platformGamepad)
{
// If this is the first gamepad this Navigator object has seen, then all gamepads just became visible.
if (m_gamepads.isEmpty()) {
gamepadsBecameVisible();
return;
}
unsigned index = platformGamepad.index();
ASSERT(GamepadProvider::singleton().platformGamepads()[index] == &platformGamepad);
// The new index should already fit in the existing array, or should be exactly one past-the-end of the existing array.
ASSERT(index <= m_gamepads.size());
if (index < m_gamepads.size())
m_gamepads[index] = Gamepad::create(m_navigator->protectedDocument().get(), platformGamepad);
else if (index == m_gamepads.size())
m_gamepads.append(Gamepad::create(m_navigator->protectedDocument().get(), platformGamepad));
}
void NavigatorGamepad::gamepadDisconnected(PlatformGamepad& platformGamepad)
{
// If this Navigator hasn't seen any gamepads yet its Vector will still be empty.
if (!m_gamepads.size())
return;
ASSERT(platformGamepad.index() < m_gamepads.size());
ASSERT(m_gamepads[platformGamepad.index()]);
m_gamepads[platformGamepad.index()] = nullptr;
}
RefPtr<Page> NavigatorGamepad::protectedPage() const
{
RefPtr frame = m_navigator->frame();
return frame ? frame->protectedPage() : nullptr;
}
} // namespace WebCore
#endif // ENABLE(GAMEPAD)
|