Skip to content

Commit

Permalink
WIP keyboard-lock mock implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
philn committed May 17, 2023
1 parent b17b33d commit 0d7f83d
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Source/WebCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(WebCore_PRIVATE_INCLUDE_DIRECTORIES
"${WEBCORE_DIR}/Modules/indexeddb/client"
"${WEBCORE_DIR}/Modules/indexeddb/server"
"${WEBCORE_DIR}/Modules/indexeddb/shared"
"${WEBCORE_DIR}/Modules/keyboard-lock"
"${WEBCORE_DIR}/Modules/mediacapabilities"
"${WEBCORE_DIR}/Modules/mediacontrols"
"${WEBCORE_DIR}/Modules/mediarecorder"
Expand Down Expand Up @@ -351,6 +352,9 @@ set(WebCore_NON_SVG_IDL_FILES
Modules/indexeddb/IDBVersionChangeEvent.idl
Modules/indexeddb/WindowOrWorkerGlobalScope+IndexedDatabase.idl

Modules/keyboard-lock/Navigator+Keyboard.idl
Modules/keyboard-lock/Keyboard.idl

Modules/mediacapabilities/AudioConfiguration.idl
Modules/mediacapabilities/ColorGamut.idl
Modules/mediacapabilities/HdrMetadataType.idl
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/Headers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
Modules/indexeddb/shared/IDBResultData.h
Modules/indexeddb/shared/IDBTransactionInfo.h

Modules/keyboard-lock/Keyboard.h

Modules/mediarecorder/MediaRecorderProvider.h

Modules/mediasource/SampleMap.h
Expand Down
66 changes: 66 additions & 0 deletions Source/WebCore/Modules/keyboard-lock/Keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2021 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 "Keyboard.h"

#include "JSDOMPromiseDeferred.h"
#include "NotImplemented.h"
#include <wtf/IsoMallocInlines.h>

namespace WebCore {

WTF_MAKE_ISO_ALLOCATED_IMPL(Keyboard);

Ref<Keyboard> Keyboard::create(NavigatorBase& navigator)
{
return adoptRef(*new Keyboard(navigator));
}

Keyboard::Keyboard(NavigatorBase& navigator)
: m_navigator(navigator)
{
}

NavigatorBase* Keyboard::navigator()
{
return m_navigator.get();
}

Keyboard::~Keyboard() = default;

void Keyboard::lock(const std::optional<Vector<String>>& keyCodes, DOMPromiseDeferred<void>&& promise)
{
UNUSED_PARAM(keyCodes);
notImplemented();
promise.resolve();
}

void Keyboard::unlock()
{
notImplemented();
}

} // namespace WebCore
58 changes: 58 additions & 0 deletions Source/WebCore/Modules/keyboard-lock/Keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2021 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.
*/

#pragma once

#include "IDLTypes.h"
#include <wtf/IsoMalloc.h>
#include <wtf/WeakPtr.h>

namespace WTF {
class String;
}

namespace WebCore {

class NavigatorBase;

template<typename IDLType> class DOMPromiseDeferred;

class Keyboard : public RefCounted<Keyboard> {
WTF_MAKE_ISO_ALLOCATED(Keyboard);
public:
static Ref<Keyboard> create(NavigatorBase&);
~Keyboard();

NavigatorBase* navigator();
void lock(const std::optional<Vector<String>>& keyCodes, DOMPromiseDeferred<void>&& promise);
void unlock();

private:
explicit Keyboard(NavigatorBase&);

WeakPtr<NavigatorBase> m_navigator;
};

} // namespace WebCore
35 changes: 35 additions & 0 deletions Source/WebCore/Modules/keyboard-lock/Keyboard.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 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.
*/

// https://wicg.github.io/keyboard-lock/#keyboard-interface

[
EnabledBySetting=PermissionsAPIEnabled,
GenerateIsReachable=ReachableFromNavigator,
Exposed=Window
] interface Keyboard {
Promise<undefined> lock(optional sequence<DOMString> keyCodes = []);
undefined unlock();
};
8 changes: 8 additions & 0 deletions Source/WebCore/Modules/keyboard-lock/Navigator+Keyboard.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

// https://wicg.github.io/keyboard-lock/#navigator-interface

[
ImplementedBy=NavigatorKeyboard
] partial interface Navigator {
[SecureContext, SameObject] readonly attribute Keyboard keyboard;
};
69 changes: 69 additions & 0 deletions Source/WebCore/Modules/keyboard-lock/NavigatorKeyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2021 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 "NavigatorKeyboard.h"

#include "Navigator.h"
#include "Keyboard.h"

namespace WebCore {

NavigatorKeyboard::NavigatorKeyboard(Navigator& navigator)
: m_navigator(navigator)
{
}

Keyboard& NavigatorKeyboard::keyboard(Navigator& navigator)
{
return NavigatorKeyboard::from(navigator).keyboard();
}

Keyboard& NavigatorKeyboard::keyboard()
{
if (!m_keyboard)
m_keyboard = Keyboard::create(m_navigator);

return *m_keyboard;
}

NavigatorKeyboard& NavigatorKeyboard::from(Navigator& navigator)
{
auto* supplement = static_cast<NavigatorKeyboard*>(Supplement<Navigator>::from(&navigator, supplementName()));
if (!supplement) {
auto newSupplement = makeUnique<NavigatorKeyboard>(navigator);
supplement = newSupplement.get();
provideTo(&navigator, supplementName(), WTFMove(newSupplement));
}

return *supplement;
}

const char* NavigatorKeyboard::supplementName()
{
return "NavigatorKeyboard";
}

} // namespace WebCore
51 changes: 51 additions & 0 deletions Source/WebCore/Modules/keyboard-lock/NavigatorKeyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2021 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.
*/

#pragma once

#include "Supplementable.h"

namespace WebCore {

class Navigator;
class Keyboard;

class NavigatorKeyboard final : public Supplement<Navigator> {
WTF_MAKE_FAST_ALLOCATED;
public:
explicit NavigatorKeyboard(Navigator&);

static Keyboard& keyboard(Navigator&);
Keyboard& keyboard();

private:
static NavigatorKeyboard& from(Navigator&);
static const char* supplementName();

RefPtr<Keyboard> m_keyboard;
Navigator& m_navigator;
};

}
2 changes: 2 additions & 0 deletions Source/WebCore/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ Modules/indexeddb/shared/IDBResourceIdentifier.cpp
Modules/indexeddb/shared/IDBResultData.cpp
Modules/indexeddb/shared/IDBTransactionInfo.cpp
Modules/indexeddb/shared/IndexKey.cpp
Modules/keyboard-lock/Keyboard.cpp
Modules/keyboard-lock/NavigatorKeyboard.cpp
Modules/mediacapabilities/MediaCapabilities.cpp
Modules/mediacapabilities/NavigatorMediaCapabilities.cpp
Modules/mediacapabilities/WorkerNavigatorMediaCapabilities.cpp
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/bindings/js/WebCoreBuiltinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ namespace WebCore {
macro(InputEvent) \
macro(IntersectionObserver) \
macro(IntersectionObserverEntry) \
macro(Keyboard) \
macro(KeyframeEffect) \
macro(Lock) \
macro(LockManager) \
Expand Down Expand Up @@ -269,6 +270,7 @@ namespace WebCore {
macro(MockRTCRtpTransform) \
macro(NavigationPreloadManager) \
macro(NavigatorCredentials) \
macro(NavigatorKeyboard) \
macro(NavigatorMediaDevices) \
macro(NavigatorPermissions) \
macro(NavigatorUserMedia) \
Expand Down

1 comment on commit 0d7f83d

@xingri
Copy link

@xingri xingri commented on 0d7f83d Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upstream work on WebKit: WebKit/WebKit#20823

Please sign in to comment.