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..4069544f2637e09c026d6980fbd3e4d1d6201e7e |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
@@ -0,0 +1,110 @@ |
+// 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_) { |
+ return ScriptPromise::Reject( |
+ state, V8String(state->GetIsolate(), |
foolip
2017/04/17 08:46:04
To reject with a string is somewhat unusual I thin
Hzj_jie
2017/04/18 02:26:08
I am working with Gary to update the spec.
|
+ "Last requestKeyLock() has not finished yet.")); |
+ } |
+ |
+ if (!service_) { |
+ LocalFrame* frame = GetSupplementable()->GetFrame(); |
+ if (!frame) { |
+ return ScriptPromise::Reject( |
+ state, V8String(state->GetIsolate(), "Current frame is detached.")); |
+ } |
+ frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); |
+ } |
+ |
+ if (!service_.get()) { |
+ return ScriptPromise::Reject( |
+ state, V8String(state->GetIsolate(), "Browser is not connecting.")); |
foolip
2017/04/17 08:46:05
Ditto about this rejection. You will still need a
Hzj_jie
2017/04/18 02:26:08
I believe the spec won't cover the detail of excep
foolip
2017/04/18 05:10:57
When would this exception happen? It's possible it
Hzj_jie
2017/04/19 00:45:54
Sorry, I mean the browser process does not provide
foolip
2017/04/21 06:08:32
I'd still like to understand exactly when this can
Hzj_jie
2017/04/22 01:20:24
After discussion with Jamie, we believe here we sh
|
+ } |
+ |
+ 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 (!service_) { |
foolip
2017/04/17 08:46:04
Can you break out the shared code here into a priv
Hzj_jie
2017/04/18 02:26:08
Done.
|
+ LocalFrame* frame = GetSupplementable()->GetFrame(); |
+ if (!frame) |
+ return; |
+ frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); |
+ } |
+ |
+ if (!service_.get()) |
+ return; |
+ |
+ service_->CancelKeyLock(ConvertToBaseCallback(WTF::Bind([]() {}))); |
foolip
2017/04/17 08:46:05
Why does service_->CancelKeyLock take a callback a
Hzj_jie
2017/04/18 02:26:08
We have also discussed whether the cancelKeyLock()
foolip
2017/04/18 05:10:57
Doesn't mojo support fire-and-forget operations, w
Hzj_jie
2017/04/19 00:45:54
Yes, the new change uses no-callback operation. Bu
|
+} |
+ |
+// static |
+void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { |
+ NavigatorKeyboardLock::From(navigator).cancelKeyLock(); |
+} |
+ |
+void NavigatorKeyboardLock::LockRequestFinished(bool allowed, |
+ const String& reason) { |
+ DCHECK(request_keylock_resolver_); |
+ if (allowed) |
+ request_keylock_resolver_->Resolve(); |
+ else |
+ request_keylock_resolver_->Reject(reason); |
foolip
2017/04/17 08:46:04
Ditto about rejecting with a string.
Hzj_jie
2017/04/18 02:26:08
TODO has been added.
|
+ 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 |