Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "modules/keyboard_lock/NavigatorKeyboardLock.h" | |
| 6 | |
| 7 #include "bindings/core/v8/ScriptPromise.h" | |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
| 9 #include "bindings/core/v8/V8Binding.h" | |
| 10 #include "core/frame/LocalFrame.h" | |
| 11 #include "platform/heap/Persistent.h" | |
| 12 #include "platform/wtf/Assertions.h" | |
| 13 #include "platform/wtf/Functional.h" | |
| 14 #include "public/platform/InterfaceProvider.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 NavigatorKeyboardLock::NavigatorKeyboardLock(Navigator& navigator) | |
| 19 : Supplement<Navigator>(navigator) {} | |
| 20 | |
| 21 NavigatorKeyboardLock& NavigatorKeyboardLock::From(Navigator& navigator) { | |
| 22 NavigatorKeyboardLock* supplement = static_cast<NavigatorKeyboardLock*>( | |
| 23 Supplement<Navigator>::From(navigator, SupplementName())); | |
| 24 if (!supplement) { | |
| 25 supplement = new NavigatorKeyboardLock(navigator); | |
| 26 ProvideTo(navigator, SupplementName(), supplement); | |
| 27 } | |
| 28 return *supplement; | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 ScriptPromise NavigatorKeyboardLock::requestKeyLock( | |
| 33 ScriptState* state, | |
| 34 Navigator& navigator, | |
| 35 const Vector<String>& keycodes) { | |
| 36 return NavigatorKeyboardLock::From(navigator).requestKeyLock(state, keycodes); | |
| 37 } | |
| 38 | |
| 39 ScriptPromise NavigatorKeyboardLock::requestKeyLock( | |
| 40 ScriptState* state, | |
| 41 const Vector<String>& keycodes) { | |
| 42 DCHECK(state); | |
| 43 if (request_keylock_resolver_) { | |
| 44 return ScriptPromise::Reject( | |
| 45 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.
| |
| 46 "Last requestKeyLock() has not finished yet.")); | |
| 47 } | |
| 48 | |
| 49 if (!service_) { | |
| 50 LocalFrame* frame = GetSupplementable()->GetFrame(); | |
| 51 if (!frame) { | |
| 52 return ScriptPromise::Reject( | |
| 53 state, V8String(state->GetIsolate(), "Current frame is detached.")); | |
| 54 } | |
| 55 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); | |
| 56 } | |
| 57 | |
| 58 if (!service_.get()) { | |
| 59 return ScriptPromise::Reject( | |
| 60 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
| |
| 61 } | |
| 62 | |
| 63 request_keylock_resolver_ = ScriptPromiseResolver::Create(state); | |
| 64 service_->RequestKeyLock( | |
| 65 keycodes, | |
| 66 ConvertToBaseCallback(WTF::Bind( | |
| 67 &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this)))); | |
| 68 return request_keylock_resolver_->Promise(); | |
| 69 } | |
| 70 | |
| 71 void NavigatorKeyboardLock::cancelKeyLock() { | |
| 72 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.
| |
| 73 LocalFrame* frame = GetSupplementable()->GetFrame(); | |
| 74 if (!frame) | |
| 75 return; | |
| 76 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); | |
| 77 } | |
| 78 | |
| 79 if (!service_.get()) | |
| 80 return; | |
| 81 | |
| 82 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
| |
| 83 } | |
| 84 | |
| 85 // static | |
| 86 void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { | |
| 87 NavigatorKeyboardLock::From(navigator).cancelKeyLock(); | |
| 88 } | |
| 89 | |
| 90 void NavigatorKeyboardLock::LockRequestFinished(bool allowed, | |
| 91 const String& reason) { | |
| 92 DCHECK(request_keylock_resolver_); | |
| 93 if (allowed) | |
| 94 request_keylock_resolver_->Resolve(); | |
| 95 else | |
| 96 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.
| |
| 97 request_keylock_resolver_ = nullptr; | |
| 98 } | |
| 99 | |
| 100 // static | |
| 101 const char* NavigatorKeyboardLock::SupplementName() { | |
| 102 return "NavigatorKeyboardLock"; | |
| 103 } | |
| 104 | |
| 105 DEFINE_TRACE(NavigatorKeyboardLock) { | |
| 106 visitor->Trace(request_keylock_resolver_); | |
| 107 Supplement<Navigator>::Trace(visitor); | |
| 108 } | |
| 109 | |
| 110 } // namespace blink | |
| OLD | NEW |