| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/keyboard_lock/NavigatorKeyboardLock.h" | 5 #include "modules/keyboard_lock/NavigatorKeyboardLock.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
| 10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 NavigatorKeyboardLock* supplement = static_cast<NavigatorKeyboardLock*>( | 22 NavigatorKeyboardLock* supplement = static_cast<NavigatorKeyboardLock*>( |
| 23 Supplement<Navigator>::From(navigator, SupplementName())); | 23 Supplement<Navigator>::From(navigator, SupplementName())); |
| 24 if (!supplement) { | 24 if (!supplement) { |
| 25 supplement = new NavigatorKeyboardLock(navigator); | 25 supplement = new NavigatorKeyboardLock(navigator); |
| 26 ProvideTo(navigator, SupplementName(), supplement); | 26 ProvideTo(navigator, SupplementName(), supplement); |
| 27 } | 27 } |
| 28 return *supplement; | 28 return *supplement; |
| 29 } | 29 } |
| 30 | 30 |
| 31 // static | 31 // static |
| 32 ScriptPromise NavigatorKeyboardLock::requestKeyLock( | 32 ScriptPromise NavigatorKeyboardLock::requestKeyboardLock( |
| 33 ScriptState* state, | 33 ScriptState* state, |
| 34 Navigator& navigator, | 34 Navigator& navigator, |
| 35 const Vector<String>& keycodes) { | 35 const Vector<String>& keycodes) { |
| 36 return NavigatorKeyboardLock::From(navigator).requestKeyLock(state, keycodes); | 36 return NavigatorKeyboardLock::From(navigator).requestKeyboardLock( |
| 37 state, keycodes); |
| 37 } | 38 } |
| 38 | 39 |
| 39 ScriptPromise NavigatorKeyboardLock::requestKeyLock( | 40 ScriptPromise NavigatorKeyboardLock::requestKeyboardLock( |
| 40 ScriptState* state, | 41 ScriptState* state, |
| 41 const Vector<String>& keycodes) { | 42 const Vector<String>& keycodes) { |
| 42 DCHECK(state); | 43 DCHECK(state); |
| 43 if (request_keylock_resolver_) { | 44 if (request_keylock_resolver_) { |
| 44 // TODO(zijiehe): Reject with a DOMException once it has been defined in the | 45 // 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 // spec. See https://github.com/w3c/keyboard-lock/issues/18. |
| 46 return ScriptPromise::Reject( | 47 return ScriptPromise::Reject( |
| 47 state, V8String(state->GetIsolate(), | 48 state, V8String(state->GetIsolate(), |
| 48 "Last requestKeyLock() has not finished yet.")); | 49 "Last requestKeyboardLock() has not finished yet.")); |
| 49 } | 50 } |
| 50 | 51 |
| 51 if (!EnsureServiceConnected()) { | 52 if (!EnsureServiceConnected()) { |
| 52 return ScriptPromise::Reject( | 53 return ScriptPromise::Reject( |
| 53 state, V8String(state->GetIsolate(), "Current frame is detached.")); | 54 state, V8String(state->GetIsolate(), "Current frame is detached.")); |
| 54 } | 55 } |
| 55 | 56 |
| 56 request_keylock_resolver_ = ScriptPromiseResolver::Create(state); | 57 request_keylock_resolver_ = ScriptPromiseResolver::Create(state); |
| 57 service_->RequestKeyLock( | 58 service_->RequestKeyboardLock( |
| 58 keycodes, | 59 keycodes, |
| 59 ConvertToBaseCallback(WTF::Bind( | 60 ConvertToBaseCallback(WTF::Bind( |
| 60 &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this)))); | 61 &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this)))); |
| 61 return request_keylock_resolver_->Promise(); | 62 return request_keylock_resolver_->Promise(); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void NavigatorKeyboardLock::cancelKeyLock() { | 65 void NavigatorKeyboardLock::cancelKeyboardLock() { |
| 65 if (!EnsureServiceConnected()) { | 66 if (!EnsureServiceConnected()) { |
| 66 // Current frame is detached. | 67 // Current frame is detached. |
| 67 return; | 68 return; |
| 68 } | 69 } |
| 69 | 70 |
| 70 service_->CancelKeyLock(); | 71 service_->CancelKeyboardLock(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 // static | 74 // static |
| 74 void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { | 75 void NavigatorKeyboardLock::cancelKeyboardLock(Navigator& navigator) { |
| 75 NavigatorKeyboardLock::From(navigator).cancelKeyLock(); | 76 NavigatorKeyboardLock::From(navigator).cancelKeyboardLock(); |
| 76 } | 77 } |
| 77 | 78 |
| 78 bool NavigatorKeyboardLock::EnsureServiceConnected() { | 79 bool NavigatorKeyboardLock::EnsureServiceConnected() { |
| 79 if (!service_) { | 80 if (!service_) { |
| 80 LocalFrame* frame = GetSupplementable()->GetFrame(); | 81 LocalFrame* frame = GetSupplementable()->GetFrame(); |
| 81 if (!frame) { | 82 if (!frame) { |
| 82 return false; | 83 return false; |
| 83 } | 84 } |
| 84 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); | 85 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_)); |
| 85 } | 86 } |
| 86 | 87 |
| 87 DCHECK(service_); | 88 DCHECK(service_); |
| 88 return true; | 89 return true; |
| 89 } | 90 } |
| 90 | 91 |
| 91 void NavigatorKeyboardLock::LockRequestFinished(bool allowed, | 92 void NavigatorKeyboardLock::LockRequestFinished( |
| 92 const String& reason) { | 93 mojom::KeyboardLockRequestResult result) { |
| 93 DCHECK(request_keylock_resolver_); | 94 DCHECK(request_keylock_resolver_); |
| 94 // TODO(zijiehe): Reject with a DOMException once it has been defined in the | 95 // TODO(zijiehe): Reject with a DOMException once it has been defined in the |
| 95 // spec. | 96 // spec. |
| 96 if (allowed) | 97 if (result == mojom::KeyboardLockRequestResult::SUCCESS) |
| 97 request_keylock_resolver_->Resolve(); | 98 request_keylock_resolver_->Resolve(); |
| 98 else | 99 else |
| 99 request_keylock_resolver_->Reject(reason); | 100 request_keylock_resolver_->Reject(); |
| 100 request_keylock_resolver_ = nullptr; | 101 request_keylock_resolver_ = nullptr; |
| 101 } | 102 } |
| 102 | 103 |
| 103 // static | 104 // static |
| 104 const char* NavigatorKeyboardLock::SupplementName() { | 105 const char* NavigatorKeyboardLock::SupplementName() { |
| 105 return "NavigatorKeyboardLock"; | 106 return "NavigatorKeyboardLock"; |
| 106 } | 107 } |
| 107 | 108 |
| 108 DEFINE_TRACE(NavigatorKeyboardLock) { | 109 DEFINE_TRACE(NavigatorKeyboardLock) { |
| 109 visitor->Trace(request_keylock_resolver_); | 110 visitor->Trace(request_keylock_resolver_); |
| 110 Supplement<Navigator>::Trace(visitor); | 111 Supplement<Navigator>::Trace(visitor); |
| 111 } | 112 } |
| 112 | 113 |
| 113 } // namespace blink | 114 } // namespace blink |
| OLD | NEW |