Skip to content

Commit

Permalink
wake lock: Implement new proposed API.
Browse files Browse the repository at this point in the history
This is another big update to the implementation, which now follows the
API proposed in https://github.com/w3c/wake-lock/issues/226.

From a Blink/Chromium internals perspective, things have not changed much,
and we still perform the same permission checks and connect to the wake lock
service via WakeLockStateRecord. The biggest change is that WakeLockController
was merged into WakeLock itself.

From an end-user perspective, though, the API now looks like a mix of the
old, navigator-based API and the one we implemented a few months ago:

* The main idea is that WakeLock.request() no longer returns a promise that
  does not resolve while the lock is held, as that is not very intuitive.
  Instead, it returns a new WakeLockSentinel object.
* WakeLockSentinel is a new interface that can release() a lock and receive
  "release" events.
* We do go back to a Navigator-based API, but we also support
  WorkerNavigator.
* There is no WakeLockRequest interface or state tracking in the WakeLock
  interface.
* WakeLock.requestPermission() was removed.
* Consequently, we now have a WakeLockEvent interface.

The changes to the spec still need to land, but there is some rough
consensus on what the API should look like, and we do not want to miss the
M79 branch cut.

Bug: 257511, 1010162
Change-Id: If1baed914ccd9eb7103a1bb71a40529976777ec2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1849683
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Bo <boliu@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Cr-Commit-Position: refs/heads/master@{#705632}
  • Loading branch information
rakuco authored and Commit Bot committed Oct 14, 2019
1 parent 54a6ef2 commit 87be7af
Show file tree
Hide file tree
Showing 60 changed files with 1,281 additions and 1,217 deletions.
25 changes: 16 additions & 9 deletions chrome/browser/wake_lock/wake_lock_browsertest.cc
Expand Up @@ -119,7 +119,8 @@ IN_PROC_BROWSER_TEST_F(WakeLockBrowserTest, RequestScreenLockFromWorker) {
PermissionRequestObserver observer(
browser()->tab_strip_model()->GetActiveWebContents());
const std::string kWorkerScript =
"WakeLock.request('screen').catch(err => self.postMessage(err.name))";
"navigator.wakeLock.request('screen').catch(err => "
" self.postMessage(err.name))";
NavigateToAndRespondWithScript(
"/workers/create_dedicated_worker.html?worker_url=/js-response",
kWorkerScript);
Expand All @@ -136,7 +137,8 @@ IN_PROC_BROWSER_TEST_F(WakeLockBrowserTest, RequestSystemLockFromWorker) {
PermissionRequestObserver observer(
browser()->tab_strip_model()->GetActiveWebContents());
const std::string kWorkerScript =
"WakeLock.request('system').catch(err => self.postMessage(err.name))";
"navigator.wakeLock.request('system').catch(err => "
" self.postMessage(err.name))";
NavigateToAndRespondWithScript(
"/workers/create_dedicated_worker.html?worker_url=/js-response",
kWorkerScript);
Expand All @@ -156,7 +158,8 @@ IN_PROC_BROWSER_TEST_F(WakeLockBrowserTest, RequestPermissionScreen) {
browser()->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ("granted", content::EvalJs(
browser()->tab_strip_model()->GetActiveWebContents(),
"WakeLock.requestPermission('screen')"));
"navigator.wakeLock.request('screen').then(lock => {"
" lock.release(); return 'granted'; });"));
EXPECT_EQ(observer.request_shown(), false);
}

Expand All @@ -171,7 +174,8 @@ IN_PROC_BROWSER_TEST_F(WakeLockBrowserTest,
EXPECT_EQ(
"granted",
content::EvalJs(browser()->tab_strip_model()->GetActiveWebContents(),
"WakeLock.requestPermission('screen')",
"navigator.wakeLock.request('screen').then(lock => {"
" lock.release(); return 'granted'; });",
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ(observer.request_shown(), false);
}
Expand All @@ -183,9 +187,11 @@ IN_PROC_BROWSER_TEST_F(WakeLockBrowserTest, RequestPermissionSystem) {

PermissionRequestObserver observer(
browser()->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ("denied", content::EvalJs(
browser()->tab_strip_model()->GetActiveWebContents(),
"WakeLock.requestPermission('system')"));
EXPECT_EQ(
"NotAllowedError",
content::EvalJs(browser()->tab_strip_model()->GetActiveWebContents(),
"navigator.wakeLock.request('system').catch(err => {"
" return err.name; });"));
EXPECT_EQ(observer.request_shown(), false);
}

Expand All @@ -198,9 +204,10 @@ IN_PROC_BROWSER_TEST_F(WakeLockBrowserTest,
PermissionRequestObserver observer(
browser()->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ(
"denied",
"NotAllowedError",
content::EvalJs(browser()->tab_strip_model()->GetActiveWebContents(),
"WakeLock.requestPermission('system')",
"navigator.wakeLock.request('system').catch(err => {"
" return err.name; });",
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ(observer.request_shown(), false);
}
Expand Up @@ -44,7 +44,7 @@ public void setUp() {
}

private void getWakeLock(String type) throws TimeoutException {
final String code = "WakeLock.request('" + type + "');";
final String code = "navigator.wakeLock.request('" + type + "');";
JavaScriptUtils.executeJavaScriptAndWaitForResult(mActivityTestRule.getWebContents(), code);
}

Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/bindings/modules/BUILD.gn
Expand Up @@ -69,6 +69,7 @@ generate_event_interfaces("modules_bindings_generated_event_interfaces") {
"//third_party/blink/renderer/modules/speech/speech_synthesis_event.idl",
"//third_party/blink/renderer/modules/storage/storage_event.idl",
"//third_party/blink/renderer/modules/vr/vr_display_event.idl",
"//third_party/blink/renderer/modules/wake_lock/wake_lock_event.idl",
"//third_party/blink/renderer/modules/webaudio/audio_processing_event.idl",
"//third_party/blink/renderer/modules/webaudio/offline_audio_completion_event.idl",
"//third_party/blink/renderer/modules/webgl/webgl_context_event.idl",
Expand Down
Expand Up @@ -229,6 +229,7 @@
"reading",
"readystatechange",
"rejectionhandled",
"release",
"removesourcebuffer",
"removestream",
"removetrack",
Expand Down
3 changes: 2 additions & 1 deletion third_party/blink/renderer/modules/BUILD.gn
Expand Up @@ -401,8 +401,9 @@ jumbo_source_set("unit_tests") {
"service_worker/service_worker_timeout_timer_test.cc",
"service_worker/thread_safe_script_container_test.cc",
"service_worker/web_embedded_worker_impl_test.cc",
"wake_lock/wake_lock_controller_test.cc",
"wake_lock/wake_lock_sentinel_test.cc",
"wake_lock/wake_lock_state_record_test.cc",
"wake_lock/wake_lock_test.cc",
"wake_lock/wake_lock_test_utils.cc",
"wake_lock/wake_lock_test_utils.h",
"webaudio/audio_basic_processor_handler_test.cc",
Expand Down
Expand Up @@ -77,5 +77,6 @@
ImplementedAs: "DOMWebSocket",
},
"USB",
"WakeLockSentinel",
],
}
6 changes: 5 additions & 1 deletion third_party/blink/renderer/modules/modules_idl_files.gni
Expand Up @@ -354,6 +354,8 @@ modules_idl_files =
"vr/vr_pose.idl",
"vr/vr_stage_parameters.idl",
"wake_lock/wake_lock.idl",
"wake_lock/wake_lock_event.idl",
"wake_lock/wake_lock_sentinel.idl",
"webaudio/analyser_node.idl",
"webaudio/audio_buffer.idl",
"webaudio/audio_buffer_source_node.idl",
Expand Down Expand Up @@ -801,7 +803,7 @@ modules_dictionary_idl_files =
"storage/storage_event_init.idl",
"vr/vr_display_event_init.idl",
"vr/vr_layer_init.idl",
"wake_lock/wake_lock_request_options.idl",
"wake_lock/wake_lock_event_init.idl",
"webaudio/analyser_options.idl",
"webaudio/audio_buffer_options.idl",
"webaudio/audio_buffer_source_options.idl",
Expand Down Expand Up @@ -1011,6 +1013,8 @@ modules_dependency_idl_files =
"storage/window_storage.idl",
"vibration/navigator_vibration.idl",
"vr/navigator_vr.idl",
"wake_lock/navigator_wake_lock.idl",
"wake_lock/worker_navigator_wake_lock.idl",
"webdatabase/window_web_database.idl",
"webgl/webgl2_rendering_context_base.idl",
"webgl/webgl_rendering_context_base.idl",
Expand Down
10 changes: 8 additions & 2 deletions third_party/blink/renderer/modules/wake_lock/BUILD.gn
Expand Up @@ -6,14 +6,20 @@ import("//third_party/blink/renderer/modules/modules.gni")

blink_modules_sources("wake_lock") {
sources = [
"navigator_wake_lock.cc",
"navigator_wake_lock.h",
"wake_lock.cc",
"wake_lock.h",
"wake_lock_controller.cc",
"wake_lock_controller.h",
"wake_lock_event.cc",
"wake_lock_event.h",
"wake_lock_sentinel.cc",
"wake_lock_sentinel.h",
"wake_lock_state_record.cc",
"wake_lock_state_record.h",
"wake_lock_type.cc",
"wake_lock_type.h",
"worker_navigator_wake_lock.cc",
"worker_navigator_wake_lock.h",
]
deps = [
"//mojo/public/cpp/bindings",
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/modules/wake_lock/DEPS
Expand Up @@ -6,7 +6,7 @@ include_rules = [
]

specific_include_rules = {
"wake_lock_test_utils\.cc": [
"wake_lock_test_utils\.cc|.+_test\.cc": [
"+base/run_loop.h",
],
}
@@ -0,0 +1,52 @@
// Copyright 2019 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 "third_party/blink/renderer/modules/wake_lock/navigator_wake_lock.h"

#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/modules/wake_lock/wake_lock.h"

namespace blink {

NavigatorWakeLock::NavigatorWakeLock(Navigator& navigator)
: Supplement<Navigator>(navigator) {}

WakeLock* NavigatorWakeLock::GetWakeLock() {
if (!wake_lock_) {
auto* frame = GetSupplementable()->GetFrame();
if (frame) {
DCHECK(frame->GetDocument());
wake_lock_ = MakeGarbageCollected<WakeLock>(*frame->GetDocument());
}
}
return wake_lock_;
}

// static
const char NavigatorWakeLock::kSupplementName[] = "NavigatorWakeLock";

// static
NavigatorWakeLock& NavigatorWakeLock::From(Navigator& navigator) {
NavigatorWakeLock* supplement =
Supplement<Navigator>::From<NavigatorWakeLock>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<NavigatorWakeLock>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}

// static
WakeLock* NavigatorWakeLock::wakeLock(Navigator& navigator) {
return NavigatorWakeLock::From(navigator).GetWakeLock();
}

void NavigatorWakeLock::Trace(blink::Visitor* visitor) {
visitor->Trace(wake_lock_);
Supplement<Navigator>::Trace(visitor);
}

} // namespace blink
39 changes: 39 additions & 0 deletions third_party/blink/renderer/modules/wake_lock/navigator_wake_lock.h
@@ -0,0 +1,39 @@
// Copyright 2019 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WAKE_LOCK_NAVIGATOR_WAKE_LOCK_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WAKE_LOCK_NAVIGATOR_WAKE_LOCK_H_

#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"

namespace blink {

class Navigator;
class WakeLock;

class NavigatorWakeLock final : public GarbageCollected<NavigatorWakeLock>,
public Supplement<Navigator> {
USING_GARBAGE_COLLECTED_MIXIN(NavigatorWakeLock);

public:
static const char kSupplementName[];

static NavigatorWakeLock& From(Navigator&);

static WakeLock* wakeLock(Navigator&);

explicit NavigatorWakeLock(Navigator&);

void Trace(blink::Visitor*) override;

private:
WakeLock* GetWakeLock();

Member<WakeLock> wake_lock_;
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WAKE_LOCK_NAVIGATOR_WAKE_LOCK_H_
@@ -0,0 +1,11 @@
// Copyright 2019 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.

[
ImplementedAs=NavigatorWakeLock,
RuntimeEnabled=WakeLock,
SecureContext
] partial interface Navigator {
[SameObject] readonly attribute WakeLock wakeLock;
};

0 comments on commit 87be7af

Please sign in to comment.