Chromium Code Reviews| Index: third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
| diff --git a/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp b/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ead4ab3bc9c125123acd3dfd245c470c0afcdd1b |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/keyboard_lock/NavigatorKeyboardLock.cpp |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/keyboard_lock/NavigatorKeyboardLock.h" |
| + |
| +#include "bindings/core/v8/ScriptPromise.h" |
| +#include "bindings/core/v8/ScriptPromiseResolver.h" |
| +#include "bindings/core/v8/V8Binding.h" |
| +#include "core/frame/LocalFrame.h" |
| +#include "platform/heap/Persistent.h" |
| +#include "platform/wtf/Assertions.h" |
| +#include "platform/wtf/Functional.h" |
| +#include "public/platform/InterfaceProvider.h" |
| + |
| +namespace blink { |
| + |
| +NavigatorKeyboardLock::NavigatorKeyboardLock(Navigator& navigator) |
| + : Supplement<Navigator>(navigator) {} |
| + |
| +NavigatorKeyboardLock& NavigatorKeyboardLock::From(Navigator& navigator) { |
| + NavigatorKeyboardLock* supplement = static_cast<NavigatorKeyboardLock*>( |
| + Supplement<Navigator>::From(navigator, SupplementName())); |
| + if (!supplement) { |
| + supplement = new NavigatorKeyboardLock(navigator); |
| + ProvideTo(navigator, SupplementName(), supplement); |
| + } |
| + return *supplement; |
| +} |
| + |
| +// static |
| +ScriptPromise NavigatorKeyboardLock::requestKeyLock( |
| + ScriptState* state, |
| + Navigator& navigator, |
| + const Vector<String>& keycodes) { |
| + return NavigatorKeyboardLock::From(navigator).requestKeyLock(state, keycodes); |
| +} |
| + |
| +ScriptPromise NavigatorKeyboardLock::requestKeyLock( |
| + ScriptState* state, |
| + const Vector<String>& keycodes) { |
| + DCHECK(state); |
| + if (request_keylock_resolver_) { |
| + return ScriptPromise::Reject( |
| + state, V8String(state->GetIsolate(), |
| + "Last requestKeyLock() has not finished yet.")); |
| + } |
| + |
| + if (!service_) { |
| + GetSupplementable()->GetFrame()->GetInterfaceProvider()->GetInterface( |
| + mojo::MakeRequest(&service_)); |
| + } |
| + |
| + if (!service_.get()) { |
| + return ScriptPromise::Reject( |
| + state, V8String(state->GetIsolate(), "Browser is not connecting.")); |
| + } |
| + |
| + request_keylock_resolver_ = ScriptPromiseResolver::Create(state); |
| + service_->RequestKeyLock( |
| + keycodes, |
| + ConvertToBaseCallback(WTF::Bind( |
| + &NavigatorKeyboardLock::LockRequestFinished, WrapPersistent(this)))); |
| + return request_keylock_resolver_->Promise(); |
| +} |
| + |
| +void NavigatorKeyboardLock::cancelKeyLock() { |
| + if (!service_) { |
| + GetSupplementable()->GetFrame()->GetInterfaceProvider()->GetInterface( |
| + mojo::MakeRequest(&service_)); |
| + } |
| + |
| + if (!service_.get()) { |
|
whywhat
2017/04/12 16:02:13
nit: one line blocks don't require {}
Hzj_jie
2017/04/13 00:38:54
Done.
|
| + return; |
| + } |
| + |
| + service_->CancelKeyLock(ConvertToBaseCallback(WTF::Bind( |
| + &NavigatorKeyboardLock::CancelRequestFinished, WrapPersistent(this)))); |
| +} |
| + |
| +// static |
| +void NavigatorKeyboardLock::cancelKeyLock(Navigator& navigator) { |
| + NavigatorKeyboardLock::From(navigator).cancelKeyLock(); |
| +} |
| + |
| +void NavigatorKeyboardLock::LockRequestFinished(bool allowed, |
| + const String& reason) { |
| + DCHECK(request_keylock_resolver_); |
| + if (allowed) { |
|
whywhat
2017/04/12 16:02:13
nit: ditto
Hzj_jie
2017/04/13 00:38:54
Done.
|
| + request_keylock_resolver_->Resolve(); |
| + } else { |
| + request_keylock_resolver_->Reject(reason); |
| + } |
| + request_keylock_resolver_ = nullptr; |
| +} |
| + |
| +void NavigatorKeyboardLock::CancelRequestFinished() { |
| + // TODO(zijiehe): The response of CancelKeyLock may need to be forwarded to |
|
whywhat
2017/04/12 16:02:13
nit: you could not submit the unused code path unt
Hzj_jie
2017/04/13 00:38:54
Done.
|
| + // the web page. |
| +} |
| + |
| +// static |
| +const char* NavigatorKeyboardLock::SupplementName() { |
| + return "NavigatorKeyboardLock"; |
| +} |
| + |
| +DEFINE_TRACE(NavigatorKeyboardLock) { |
| + visitor->Trace(request_keylock_resolver_); |
| + Supplement<Navigator>::Trace(visitor); |
| +} |
| + |
| +} // namespace blink |