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/w3c/keyboard-lock/issues/18. | |
| 46 return ScriptPromise::Reject( | |
| 47 state, V8String(state->GetIsolate(), | |
| 48 "Last requestKeyLock() has not finished yet.")); | |
| 49 } | |
| 50 | |
| 51 if (!service_) { | |
| 52 LocalFrame* frame = GetSupplementable()->GetFrame(); | |
| 53 if (!frame) { | |
| 54 // TODO(zijiehe): Retrying instead of rejecting the request if browser is | |
| 55 // not connected. | |
|
dcheng
2017/04/28 15:33:45
This isn't a recoverable error -- once frame is nu
Hzj_jie
2017/04/28 19:16:02
I do not really understand the insight: why would
| |
| 56 return ScriptPromise::Reject( | |
| 57 state, V8String(state->GetIsolate(), "Current frame is detached.")); | |
| 58 } | |
| 59 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); | |
| 60 } | |
| 61 DCHECK(service_); | |
| 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_) { | |
| 73 LocalFrame* frame = GetSupplementable()->GetFrame(); | |
| 74 if (!frame) { | |
| 75 // Current frame is detached. | |
| 76 return; | |
| 77 } | |
| 78 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); | |
| 79 } | |
| 80 DCHECK(service_); | |
| 81 | |
| 82 service_->CancelKeyLock(); | |
| 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 // TODO(zijiehe): Reject with a DOMException once it has been defined in the | |
| 94 // spec. | |
| 95 if (allowed) | |
| 96 request_keylock_resolver_->Resolve(); | |
| 97 else | |
| 98 request_keylock_resolver_->Reject(reason); | |
| 99 request_keylock_resolver_ = nullptr; | |
| 100 } | |
| 101 | |
| 102 // static | |
| 103 const char* NavigatorKeyboardLock::SupplementName() { | |
| 104 return "NavigatorKeyboardLock"; | |
| 105 } | |
| 106 | |
| 107 DEFINE_TRACE(NavigatorKeyboardLock) { | |
| 108 visitor->Trace(request_keylock_resolver_); | |
| 109 Supplement<Navigator>::Trace(visitor); | |
| 110 } | |
| 111 | |
| 112 } // namespace blink | |
| OLD | NEW |