Index: third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
diff --git a/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp b/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..74f574f52d4fbde98ff24ecb6db859e0c5628db1 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
@@ -0,0 +1,114 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "modules/keyboard_lock/NavigatorKeyboardLock.h" |
+ |
+#include "bindings/core/v8/ScriptPromise.h" |
+#include "bindings/core/v8/ScriptPromiseResolver.h" |
+#include "bindings/core/v8/V8Binding.h" |
+#include "core/dom/Document.h" |
+#include "core/frame/LocalFrame.h" |
+#include "core/page/ChromeClient.h" |
+#include "core/page/Page.h" |
+#include "platform/KeyboardLocker.h" |
+#include "platform/wtf/Assertions.h" |
+ |
+namespace blink { |
+ |
+NavigatorKeyboardLock::NavigatorKeyboardLock(Navigator& navigator) |
+ : Supplement<Navigator>(navigator) {} |
+ |
+NavigatorKeyboardLock& NavigatorKeyboardLock::From(Navigator& navigator) { |
+ NavigatorKeyboardLock* supplement = static_cast<NavigatorKeyboardLock*>( |
+ Supplement<Navigator>::From(navigator, SupplementName())); |
+ if (!supplement) { |
+ supplement = new NavigatorKeyboardLock(navigator); |
+ ProvideTo(navigator, SupplementName(), supplement); |
+ } |
+ return *supplement; |
+} |
+ |
+// static |
+ScriptPromise NavigatorKeyboardLock::requestKeyLock( |
+ ScriptState* state, |
+ Navigator& navigator, |
+ const Vector<String>& keycodes) { |
+ return NavigatorKeyboardLock::From(navigator).requestKeyLock(state, keycodes); |
+} |
+ |
+ScriptPromise NavigatorKeyboardLock::requestKeyLock( |
+ ScriptState* state, |
+ const Vector<String>& keycodes) { |
+ DCHECK(state); |
+ if (request_keylock_resolver_) { |
+ return ScriptPromise::Reject( |
+ state, V8String(state->GetIsolate(), |
+ "Last requestKeyLock() has not finished yet.")); |
+ } |
+ |
+ ChromeClient* client = nullptr; |
+ String exception; |
+ if (!RetrieveChromeClient(client, exception)) { |
+ return ScriptPromise::Reject(state, |
+ V8String(state->GetIsolate(), exception)); |
+ } |
+ |
+ request_keylock_resolver_ = ScriptPromiseResolver::Create(state); |
+ client->RequestKeyLock(GetSupplementable()->GetFrame(), |
+ NewKeyboardLocker(keycodes)); |
+ return request_keylock_resolver_->Promise(); |
+} |
+ |
+// static |
+void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { |
+ NavigatorKeyboardLock::From(navigator).cancelKeyLock(); |
+} |
+ |
+void NavigatorKeyboardLock::cancelKeyLock() { |
+ ChromeClient* client = nullptr; |
+ String exception; |
+ if (!RetrieveChromeClient(client, exception)) { |
+ return; |
+ } |
+ |
+ client->CancelKeyLock(GetSupplementable()->GetFrame()); |
+} |
+ |
+void NavigatorKeyboardLock::LockRequestFinished(bool allowed, |
+ const String& reason) { |
+ DCHECK(request_keylock_resolver_); |
+ if (allowed) { |
+ request_keylock_resolver_->Resolve(); |
+ } else { |
+ request_keylock_resolver_->Reject(reason); |
+ } |
+ request_keylock_resolver_ = nullptr; |
+} |
+ |
+bool NavigatorKeyboardLock::RetrieveChromeClient(ChromeClient*& client, |
+ String& exception) const { |
+ if (!GetSupplementable()->GetFrame()) { |
+ exception = "Invalid frame."; |
+ return false; |
+ } |
+ Page* page = GetSupplementable()->GetFrame()->GetPage(); |
+ if (!page) { |
+ exception = "Execute when detaching."; |
+ return false; |
+ } |
+ client = &(page->GetChromeClient()); |
+ return true; |
+} |
+ |
+// static |
+const char* NavigatorKeyboardLock::SupplementName() { |
+ return "NavigatorKeyboardLock"; |
+} |
+ |
+DEFINE_TRACE(NavigatorKeyboardLock) { |
+ visitor->Trace(request_keylock_resolver_); |
+ Supplement<Navigator>::Trace(visitor); |
+} |
+ |
+} // namespace blink |