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/dom/Document.h" |
| 11 #include "core/frame/LocalFrame.h" |
| 12 #include "core/page/ChromeClient.h" |
| 13 #include "core/page/Page.h" |
| 14 #include "platform/KeyboardLocker.h" |
| 15 #include "platform/wtf/Assertions.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 NavigatorKeyboardLock::NavigatorKeyboardLock(Navigator& navigator) |
| 20 : Supplement<Navigator>(navigator) {} |
| 21 |
| 22 NavigatorKeyboardLock& NavigatorKeyboardLock::From(Navigator& navigator) { |
| 23 NavigatorKeyboardLock* supplement = static_cast<NavigatorKeyboardLock*>( |
| 24 Supplement<Navigator>::From(navigator, SupplementName())); |
| 25 if (!supplement) { |
| 26 supplement = new NavigatorKeyboardLock(navigator); |
| 27 ProvideTo(navigator, SupplementName(), supplement); |
| 28 } |
| 29 return *supplement; |
| 30 } |
| 31 |
| 32 // static |
| 33 ScriptPromise NavigatorKeyboardLock::requestKeyLock( |
| 34 ScriptState* state, |
| 35 Navigator& navigator, |
| 36 const Vector<String>& keycodes) { |
| 37 return NavigatorKeyboardLock::From(navigator).requestKeyLock(state, keycodes); |
| 38 } |
| 39 |
| 40 ScriptPromise NavigatorKeyboardLock::requestKeyLock( |
| 41 ScriptState* state, |
| 42 const Vector<String>& keycodes) { |
| 43 DCHECK(state); |
| 44 if (request_keylock_resolver_) { |
| 45 return ScriptPromise::Reject( |
| 46 state, V8String(state->GetIsolate(), |
| 47 "Last requestKeyLock() has not finished yet.")); |
| 48 } |
| 49 |
| 50 ChromeClient* client = nullptr; |
| 51 String exception; |
| 52 if (!RetrieveChromeClient(client, exception)) { |
| 53 return ScriptPromise::Reject(state, |
| 54 V8String(state->GetIsolate(), exception)); |
| 55 } |
| 56 |
| 57 request_keylock_resolver_ = ScriptPromiseResolver::Create(state); |
| 58 client->RequestKeyLock(GetSupplementable()->GetFrame(), |
| 59 NewKeyboardLocker(keycodes)); |
| 60 return request_keylock_resolver_->Promise(); |
| 61 } |
| 62 |
| 63 // static |
| 64 void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { |
| 65 NavigatorKeyboardLock::From(navigator).cancelKeyLock(); |
| 66 } |
| 67 |
| 68 void NavigatorKeyboardLock::cancelKeyLock() { |
| 69 ChromeClient* client = nullptr; |
| 70 String exception; |
| 71 if (!RetrieveChromeClient(client, exception)) { |
| 72 return; |
| 73 } |
| 74 |
| 75 client->CancelKeyLock(GetSupplementable()->GetFrame()); |
| 76 } |
| 77 |
| 78 void NavigatorKeyboardLock::LockRequestFinished(bool allowed, |
| 79 const String& reason) { |
| 80 DCHECK(request_keylock_resolver_); |
| 81 if (allowed) { |
| 82 request_keylock_resolver_->Resolve(); |
| 83 } else { |
| 84 request_keylock_resolver_->Reject(reason); |
| 85 } |
| 86 request_keylock_resolver_ = nullptr; |
| 87 } |
| 88 |
| 89 bool NavigatorKeyboardLock::RetrieveChromeClient(ChromeClient*& client, |
| 90 String& exception) const { |
| 91 if (!GetSupplementable()->GetFrame()) { |
| 92 exception = "Invalid frame."; |
| 93 return false; |
| 94 } |
| 95 Page* page = GetSupplementable()->GetFrame()->GetPage(); |
| 96 if (!page) { |
| 97 exception = "Execute when detaching."; |
| 98 return false; |
| 99 } |
| 100 client = &(page->GetChromeClient()); |
| 101 return true; |
| 102 } |
| 103 |
| 104 // static |
| 105 const char* NavigatorKeyboardLock::SupplementName() { |
| 106 return "NavigatorKeyboardLock"; |
| 107 } |
| 108 |
| 109 DEFINE_TRACE(NavigatorKeyboardLock) { |
| 110 visitor->Trace(request_keylock_resolver_); |
| 111 Supplement<Navigator>::Trace(visitor); |
| 112 } |
| 113 |
| 114 } // namespace blink |
OLD | NEW |