summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/keyboard/keyboard_lock.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/third_party/blink/renderer/modules/keyboard/keyboard_lock.cc')
-rw-r--r--chromium/third_party/blink/renderer/modules/keyboard/keyboard_lock.cc54
1 files changed, 36 insertions, 18 deletions
diff --git a/chromium/third_party/blink/renderer/modules/keyboard/keyboard_lock.cc b/chromium/third_party/blink/renderer/modules/keyboard/keyboard_lock.cc
index 2e738caa98b..274e38e53ea 100644
--- a/chromium/third_party/blink/renderer/modules/keyboard/keyboard_lock.cc
+++ b/chromium/third_party/blink/renderer/modules/keyboard/keyboard_lock.cc
@@ -8,7 +8,6 @@
#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/core/dom/dom_exception.h"
-#include "third_party/blink/renderer/core/dom/exception_code.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
@@ -19,18 +18,19 @@ namespace blink {
namespace {
-constexpr char kFrameDetachedErrorMsg[] = "Current frame is detached.";
+constexpr char kKeyboardLockFrameDetachedErrorMsg[] =
+ "Current frame is detached.";
-constexpr char kPromisePreemptedErrorMsg[] =
+constexpr char kKeyboardLockPromisePreemptedErrorMsg[] =
"This request has been superseded by a subsequent lock() method call.";
-constexpr char kNoValidKeyCodesErrorMsg[] =
+constexpr char kKeyboardLockNoValidKeyCodesErrorMsg[] =
"No valid key codes passed into lock().";
-constexpr char kChildFrameErrorMsg[] =
+constexpr char kKeyboardLockChildFrameErrorMsg[] =
"lock() must be called from a top-level browsing context.";
-constexpr char kRequestFailedErrorMsg[] =
+constexpr char kKeyboardLockRequestFailedErrorMsg[] =
"lock() request could not be registered.";
} // namespace
@@ -44,15 +44,22 @@ ScriptPromise KeyboardLock::lock(ScriptState* state,
const Vector<String>& keycodes) {
DCHECK(state);
+ if (!IsLocalFrameAttached()) {
+ return ScriptPromise::RejectWithDOMException(
+ state, DOMException::Create(DOMExceptionCode::kInvalidStateError,
+ kKeyboardLockFrameDetachedErrorMsg));
+ }
+
if (!CalledFromSupportedContext(ExecutionContext::From(state))) {
return ScriptPromise::RejectWithDOMException(
- state, DOMException::Create(kInvalidStateError, kChildFrameErrorMsg));
+ state, DOMException::Create(DOMExceptionCode::kInvalidStateError,
+ kKeyboardLockChildFrameErrorMsg));
}
if (!EnsureServiceConnected()) {
return ScriptPromise::RejectWithDOMException(
- state,
- DOMException::Create(kInvalidStateError, kFrameDetachedErrorMsg));
+ state, DOMException::Create(DOMExceptionCode::kInvalidStateError,
+ kKeyboardLockRequestFailedErrorMsg));
}
request_keylock_resolver_ = ScriptPromiseResolver::Create(state);
@@ -75,6 +82,12 @@ void KeyboardLock::unlock(ScriptState* state) {
service_->CancelKeyboardLock();
}
+bool KeyboardLock::IsLocalFrameAttached() {
+ if (GetFrame())
+ return true;
+ return false;
+}
+
bool KeyboardLock::EnsureServiceConnected() {
if (!service_) {
LocalFrame* frame = GetFrame();
@@ -82,16 +95,17 @@ bool KeyboardLock::EnsureServiceConnected() {
return false;
}
frame->GetInterfaceProvider().GetInterface(mojo::MakeRequest(&service_));
+ DCHECK(service_);
}
- DCHECK(service_);
return true;
}
bool KeyboardLock::CalledFromSupportedContext(ExecutionContext* context) {
- // KeyboardLock API is only accessible from a top level browsing context.
+ DCHECK(context);
+ // This API is only accessible from a top level, secure browsing context.
LocalFrame* frame = GetFrame();
- return frame && frame->IsMainFrame();
+ return frame && frame->IsMainFrame() && context->IsSecureContext();
}
void KeyboardLock::LockRequestFinished(
@@ -101,8 +115,8 @@ void KeyboardLock::LockRequestFinished(
// If |resolver| is not the current promise, then reject the promise.
if (resolver != request_keylock_resolver_) {
- resolver->Reject(
- DOMException::Create(kAbortError, kPromisePreemptedErrorMsg));
+ resolver->Reject(DOMException::Create(
+ DOMExceptionCode::kAbortError, kKeyboardLockPromisePreemptedErrorMsg));
return;
}
@@ -112,19 +126,23 @@ void KeyboardLock::LockRequestFinished(
break;
case mojom::KeyboardLockRequestResult::kFrameDetachedError:
request_keylock_resolver_->Reject(
- DOMException::Create(kInvalidStateError, kFrameDetachedErrorMsg));
+ DOMException::Create(DOMExceptionCode::kInvalidStateError,
+ kKeyboardLockFrameDetachedErrorMsg));
break;
case mojom::KeyboardLockRequestResult::kNoValidKeyCodesError:
request_keylock_resolver_->Reject(
- DOMException::Create(kInvalidAccessError, kNoValidKeyCodesErrorMsg));
+ DOMException::Create(DOMExceptionCode::kInvalidAccessError,
+ kKeyboardLockNoValidKeyCodesErrorMsg));
break;
case mojom::KeyboardLockRequestResult::kChildFrameError:
request_keylock_resolver_->Reject(
- DOMException::Create(kInvalidStateError, kChildFrameErrorMsg));
+ DOMException::Create(DOMExceptionCode::kInvalidStateError,
+ kKeyboardLockChildFrameErrorMsg));
break;
case mojom::KeyboardLockRequestResult::kRequestFailedError:
request_keylock_resolver_->Reject(
- DOMException::Create(kInvalidStateError, kRequestFailedErrorMsg));
+ DOMException::Create(DOMExceptionCode::kInvalidStateError,
+ kKeyboardLockRequestFailedErrorMsg));
break;
}
request_keylock_resolver_ = nullptr;