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 (!EnsureServiceConnected()) { |
| 52 return ScriptPromise::Reject( |
| 53 state, V8String(state->GetIsolate(), "Current frame is detached.")); |
| 54 } |
| 55 |
| 56 request_keylock_resolver_ = ScriptPromiseResolver::Create(state); |
| 57 service_->RequestKeyLock( |
| 58 keycodes, |
| 59 ConvertToBaseCallback(WTF::Bind( |
| 60 &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this)))); |
| 61 return request_keylock_resolver_->Promise(); |
| 62 } |
| 63 |
| 64 void NavigatorKeyboardLock::cancelKeyLock() { |
| 65 if (!EnsureServiceConnected()) { |
| 66 // Current frame is detached. |
| 67 return; |
| 68 } |
| 69 |
| 70 service_->CancelKeyLock(); |
| 71 } |
| 72 |
| 73 // static |
| 74 void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { |
| 75 NavigatorKeyboardLock::From(navigator).cancelKeyLock(); |
| 76 } |
| 77 |
| 78 bool NavigatorKeyboardLock::EnsureServiceConnected() { |
| 79 if (!service_) { |
| 80 LocalFrame* frame = GetSupplementable()->GetFrame(); |
| 81 if (!frame) { |
| 82 return false; |
| 83 } |
| 84 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); |
| 85 } |
| 86 |
| 87 DCHECK(service_); |
| 88 return true; |
| 89 } |
| 90 |
| 91 void NavigatorKeyboardLock::LockRequestFinished(bool allowed, |
| 92 const String& reason) { |
| 93 DCHECK(request_keylock_resolver_); |
| 94 // TODO(zijiehe): Reject with a DOMException once it has been defined in the |
| 95 // spec. |
| 96 if (allowed) |
| 97 request_keylock_resolver_->Resolve(); |
| 98 else |
| 99 request_keylock_resolver_->Reject(reason); |
| 100 request_keylock_resolver_ = nullptr; |
| 101 } |
| 102 |
| 103 // static |
| 104 const char* NavigatorKeyboardLock::SupplementName() { |
| 105 return "NavigatorKeyboardLock"; |
| 106 } |
| 107 |
| 108 DEFINE_TRACE(NavigatorKeyboardLock) { |
| 109 visitor->Trace(request_keylock_resolver_); |
| 110 Supplement<Navigator>::Trace(visitor); |
| 111 } |
| 112 |
| 113 } // namespace blink |
OLD | NEW |