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

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

Powered by Google App Engine
This is Rietveld 408576698