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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/keyboard/keyboard_lock.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink {
namespace {
constexpr char kKeyboardLockFrameDetachedErrorMsg[] =
"Current frame is detached.";
constexpr char kKeyboardLockPromisePreemptedErrorMsg[] =
"This request has been superseded by a subsequent lock() method call.";
constexpr char kKeyboardLockNoValidKeyCodesErrorMsg[] =
"No valid key codes passed into lock().";
constexpr char kKeyboardLockChildFrameErrorMsg[] =
"lock() must be called from a primary top-level browsing context.";
constexpr char kKeyboardLockRequestFailedErrorMsg[] =
"lock() request could not be registered.";
} // namespace
KeyboardLock::KeyboardLock(ExecutionContext* context)
: ExecutionContextClient(context), service_(context) {}
KeyboardLock::~KeyboardLock() = default;
ScriptPromise<IDLUndefined> KeyboardLock::lock(
ScriptState* state,
const Vector<String>& keycodes,
ExceptionState& exception_state) {
DCHECK(state);
if (!IsLocalFrameAttached()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
kKeyboardLockFrameDetachedErrorMsg);
return EmptyPromise();
}
if (!CalledFromSupportedContext(ExecutionContext::From(state))) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
kKeyboardLockChildFrameErrorMsg);
return EmptyPromise();
}
if (!EnsureServiceConnected()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
kKeyboardLockRequestFailedErrorMsg);
return EmptyPromise();
}
request_keylock_resolver_ =
MakeGarbageCollected<ScriptPromiseResolver<IDLUndefined>>(state);
service_->RequestKeyboardLock(
keycodes,
WTF::BindOnce(&KeyboardLock::LockRequestFinished, WrapPersistent(this),
WrapPersistent(request_keylock_resolver_.Get())));
return request_keylock_resolver_->Promise();
}
void KeyboardLock::unlock(ScriptState* state) {
DCHECK(state);
if (!CalledFromSupportedContext(ExecutionContext::From(state)))
return;
if (!EnsureServiceConnected())
return;
service_->CancelKeyboardLock();
}
bool KeyboardLock::IsLocalFrameAttached() {
return DomWindow();
}
bool KeyboardLock::EnsureServiceConnected() {
if (!service_.is_bound()) {
if (!DomWindow())
return false;
// See https://bit.ly/2S0zRAS for task types.
DomWindow()->GetBrowserInterfaceBroker().GetInterface(
service_.BindNewPipeAndPassReceiver(
DomWindow()->GetTaskRunner(TaskType::kMiscPlatformAPI)));
DCHECK(service_.is_bound());
}
return true;
}
bool KeyboardLock::CalledFromSupportedContext(ExecutionContext* context) {
DCHECK(context);
// This API is only accessible from an outermost main frame, secure browsing
// context.
return DomWindow() && DomWindow()->GetFrame()->IsOutermostMainFrame() &&
context->IsSecureContext();
}
void KeyboardLock::LockRequestFinished(
ScriptPromiseResolver<IDLUndefined>* resolver,
mojom::KeyboardLockRequestResult result) {
DCHECK(request_keylock_resolver_);
// If |resolver| is not the current promise, then reject the promise.
if (resolver != request_keylock_resolver_) {
resolver->RejectWithDOMException(DOMExceptionCode::kAbortError,
kKeyboardLockPromisePreemptedErrorMsg);
return;
}
switch (result) {
case mojom::blink::KeyboardLockRequestResult::kSuccess:
resolver->Resolve();
break;
case mojom::blink::KeyboardLockRequestResult::kFrameDetachedError:
resolver->RejectWithDOMException(DOMExceptionCode::kInvalidStateError,
kKeyboardLockFrameDetachedErrorMsg);
break;
case mojom::blink::KeyboardLockRequestResult::kNoValidKeyCodesError:
resolver->RejectWithDOMException(DOMExceptionCode::kInvalidAccessError,
kKeyboardLockNoValidKeyCodesErrorMsg);
break;
case mojom::blink::KeyboardLockRequestResult::kChildFrameError:
resolver->RejectWithDOMException(DOMExceptionCode::kInvalidStateError,
kKeyboardLockChildFrameErrorMsg);
break;
case mojom::blink::KeyboardLockRequestResult::kRequestFailedError:
resolver->RejectWithDOMException(DOMExceptionCode::kInvalidStateError,
kKeyboardLockRequestFailedErrorMsg);
break;
}
request_keylock_resolver_ = nullptr;
}
void KeyboardLock::Trace(Visitor* visitor) const {
visitor->Trace(service_);
visitor->Trace(request_keylock_resolver_);
ExecutionContextClient::Trace(visitor);
}
} // namespace blink
|