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..03f59320557d9bd7e11d130841ce0c138a26e4d2 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
@@ -0,0 +1,123 @@ |
+// 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/frame/LocalFrame.h" |
+#include "platform/heap/Persistent.h" |
+#include "platform/wtf/Assertions.h" |
+#include "platform/wtf/Functional.h" |
+#include "public/platform/InterfaceProvider.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_) { |
+ // TODO(zijiehe): Reject with a DOMException once it has been defined in the |
+ // spec. See https://github.com/garykac/system-keyboard-lock/issues/18. |
+ return ScriptPromise::Reject( |
+ state, V8String(state->GetIsolate(), |
+ "Last requestKeyLock() has not finished yet.")); |
+ } |
+ |
+ String error_message; |
+ if (!EnsureServiceConnected(&error_message)) { |
dcheng
2017/04/26 13:27:17
The only possible failure is if the frame is detac
Hzj_jie
2017/04/26 22:05:56
This comment is conflict with what Kentaro suggest
dcheng
2017/04/27 02:53:10
Oh I see; I missed that there's no promise being r
Hzj_jie
2017/04/28 00:10:31
Since this change has been lasted for almost one m
dcheng
2017/04/28 02:24:49
I'm OK with deferring 1 and 3, but 2 is easy enoug
Hzj_jie
2017/04/28 04:53:01
Done.
|
+ // TODO(zijiehe): Retrying instead of rejecting the request if browser is |
+ // not connected. |
+ return ScriptPromise::Reject(state, |
+ V8String(state->GetIsolate(), error_message)); |
+ } |
+ |
+ request_keylock_resolver_ = ScriptPromiseResolver::Create(state); |
+ service_->RequestKeyLock( |
+ keycodes, |
+ ConvertToBaseCallback(WTF::Bind( |
+ &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this)))); |
+ return request_keylock_resolver_->Promise(); |
+} |
+ |
+void NavigatorKeyboardLock::cancelKeyLock() { |
+ if (!EnsureServiceConnected(nullptr)) |
+ return; |
+ |
+ service_->CancelKeyLock(); |
+} |
+ |
+// static |
+void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { |
+ NavigatorKeyboardLock::From(navigator).cancelKeyLock(); |
+} |
+ |
+bool NavigatorKeyboardLock::EnsureServiceConnected(String* error_message) { |
haraken
2017/04/26 06:56:03
I'd inline this method into the caller sites becau
Hzj_jie
2017/04/26 22:05:56
Please see my reply in Daniel's comment. I would k
|
+ if (!service_) { |
+ LocalFrame* frame = GetSupplementable()->GetFrame(); |
+ if (!frame) { |
+ if (error_message) { |
+ *error_message = "Current frame is detached."; |
+ } |
+ return false; |
+ } |
+ frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); |
+ } |
+ |
+ if (!service_.get()) { |
dcheng
2017/04/26 13:27:17
This block is unnecessary.
Hzj_jie
2017/04/26 22:05:56
Since Daniel is OoO, and my understanding is GetIn
dcheng
2017/04/27 02:53:10
As discussed offline, this should work fine assumi
Hzj_jie
2017/04/28 00:10:31
Done.
|
+ if (error_message) { |
+ *error_message = "Browser is not connecting."; |
+ } |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+void NavigatorKeyboardLock::LockRequestFinished(bool allowed, |
+ const String& reason) { |
+ DCHECK(request_keylock_resolver_); |
+ // TODO(zijiehe): Reject with a DOMException once it has been defined in the |
+ // spec. |
+ if (allowed) |
+ request_keylock_resolver_->Resolve(); |
+ else |
+ request_keylock_resolver_->Reject(reason); |
+ request_keylock_resolver_ = nullptr; |
+} |
+ |
+// static |
+const char* NavigatorKeyboardLock::SupplementName() { |
+ return "NavigatorKeyboardLock"; |
+} |
+ |
+DEFINE_TRACE(NavigatorKeyboardLock) { |
+ visitor->Trace(request_keylock_resolver_); |
+ Supplement<Navigator>::Trace(visitor); |
+} |
+ |
+} // namespace blink |