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 // TODO(zijiehe): Reject with a DOMException once it has been defined in the | |
| 45 // spec. See https://github.com/garykac/system-keyboard-lock/issues/18. | |
| 46 return ScriptPromise::Reject( | |
| 47 state, V8String(state->GetIsolate(), | |
| 48 "Last requestKeyLock() has not finished yet.")); | |
| 49 } | |
| 50 | |
| 51 String error_message; | |
| 52 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.
| |
| 53 // TODO(zijiehe): Retrying instead of rejecting the request if browser is | |
| 54 // not connected. | |
| 55 return ScriptPromise::Reject(state, | |
| 56 V8String(state->GetIsolate(), error_message)); | |
| 57 } | |
| 58 | |
| 59 request_keylock_resolver_ = ScriptPromiseResolver::Create(state); | |
| 60 service_->RequestKeyLock( | |
| 61 keycodes, | |
| 62 ConvertToBaseCallback(WTF::Bind( | |
| 63 &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this)))); | |
| 64 return request_keylock_resolver_->Promise(); | |
| 65 } | |
| 66 | |
| 67 void NavigatorKeyboardLock::cancelKeyLock() { | |
| 68 if (!EnsureServiceConnected(nullptr)) | |
| 69 return; | |
| 70 | |
| 71 service_->CancelKeyLock(); | |
| 72 } | |
| 73 | |
| 74 // static | |
| 75 void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { | |
| 76 NavigatorKeyboardLock::From(navigator).cancelKeyLock(); | |
| 77 } | |
| 78 | |
| 79 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
| |
| 80 if (!service_) { | |
| 81 LocalFrame* frame = GetSupplementable()->GetFrame(); | |
| 82 if (!frame) { | |
| 83 if (error_message) { | |
| 84 *error_message = "Current frame is detached."; | |
| 85 } | |
| 86 return false; | |
| 87 } | |
| 88 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); | |
| 89 } | |
| 90 | |
| 91 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.
| |
| 92 if (error_message) { | |
| 93 *error_message = "Browser is not connecting."; | |
| 94 } | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 void NavigatorKeyboardLock::LockRequestFinished(bool allowed, | |
| 102 const String& reason) { | |
| 103 DCHECK(request_keylock_resolver_); | |
| 104 // TODO(zijiehe): Reject with a DOMException once it has been defined in the | |
| 105 // spec. | |
| 106 if (allowed) | |
| 107 request_keylock_resolver_->Resolve(); | |
| 108 else | |
| 109 request_keylock_resolver_->Reject(reason); | |
| 110 request_keylock_resolver_ = nullptr; | |
| 111 } | |
| 112 | |
| 113 // static | |
| 114 const char* NavigatorKeyboardLock::SupplementName() { | |
| 115 return "NavigatorKeyboardLock"; | |
| 116 } | |
| 117 | |
| 118 DEFINE_TRACE(NavigatorKeyboardLock) { | |
| 119 visitor->Trace(request_keylock_resolver_); | |
| 120 Supplement<Navigator>::Trace(visitor); | |
| 121 } | |
| 122 | |
| 123 } // namespace blink | |
| OLD | NEW |