Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp

Issue 2805763004: [System-Keyboard-Lock] Forward navigator functions to RenderFrameHost (Closed)
Patch Set: Resolve review comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // static
40 ScriptPromise NavigatorKeyboardLock::requestKeyLock(ScriptState* state,
41 Navigator& navigator) {
42 return NavigatorKeyboardLock::requestKeyLock(state, navigator,
43 Vector<String>());
44 }
45
46 ScriptPromise NavigatorKeyboardLock::requestKeyLock(
47 ScriptState* state,
48 const Vector<String>& keycodes) {
49 DCHECK(state);
50 if (request_keylock_resolver_) {
51 return ScriptPromise::Reject(
52 state, V8String(state->GetIsolate(),
53 "Last requestKeyLock() has not finished yet."));
54 }
55
56 String error_message;
57 if (!EnsureServiceConnected(&error_message)) {
58 // TODO(zijiehe): Reject with a DOMException once it has been defined in the
59 // spec.
60 return ScriptPromise::Reject(state,
61 V8String(state->GetIsolate(), error_message));
62 }
63
64 request_keylock_resolver_ = ScriptPromiseResolver::Create(state);
65 service_->RequestKeyLock(
66 keycodes,
67 ConvertToBaseCallback(WTF::Bind(
68 &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this))));
69 return request_keylock_resolver_->Promise();
70 }
71
72 void NavigatorKeyboardLock::cancelKeyLock() {
73 if (!EnsureServiceConnected(nullptr))
74 return;
75
76 service_->CancelKeyLock();
77 }
78
79 // static
80 void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) {
81 NavigatorKeyboardLock::From(navigator).cancelKeyLock();
82 }
83
84 bool NavigatorKeyboardLock::EnsureServiceConnected(String* error_message) {
85 if (!service_) {
86 LocalFrame* frame = GetSupplementable()->GetFrame();
87 if (!frame) {
88 if (error_message) {
89 *error_message = String::FromUTF8("Current frame is detached.");
dcheng 2017/04/17 23:07:52 Nit: I think it's relatively rare to see FromUTF8(
Hzj_jie 2017/04/18 02:26:08 Done.
90 }
91 return false;
92 }
93 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&service_));
94 }
95
96 if (!service_.get()) {
97 if (error_message) {
98 *error_message = String::FromUTF8("Browser is not connecting.");
99 }
100 return false;
101 }
102
103 return true;
104 }
105
106 void NavigatorKeyboardLock::LockRequestFinished(bool allowed,
107 const String& reason) {
108 DCHECK(request_keylock_resolver_);
109 // TODO(zijiehe): Reject with a DOMException once it has been defined in the
110 // spec.
111 if (allowed)
112 request_keylock_resolver_->Resolve();
113 else
114 request_keylock_resolver_->Reject(reason);
115 request_keylock_resolver_ = nullptr;
116 }
117
118 // static
119 const char* NavigatorKeyboardLock::SupplementName() {
120 return "NavigatorKeyboardLock";
121 }
122
123 DEFINE_TRACE(NavigatorKeyboardLock) {
124 visitor->Trace(request_keylock_resolver_);
125 Supplement<Navigator>::Trace(visitor);
126 }
127
128 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698