Commit dcabd508 authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Make WakeLock a Supplement<NavigatorBase>

NavigatorBase is a newly-added base class for Navigator and
WorkerNavigator. Making WakeLock a Supplement of NavigatorBase
allows a single class to handle all of the work of exposing the
navigator.wakeLock getter for both windows and workers, and
greatly reduces boilerplate.

Bug: 1147612
Change-Id: I7a56f1e71d6c8ec3e45bf163d726f8035b8cc70b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2537919
Commit-Queue: Nate Chapin <japhet@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Auto-Submit: Nate Chapin <japhet@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827536}
parent 0dd38b8f
......@@ -6,8 +6,6 @@ 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_manager.cc",
......@@ -16,8 +14,6 @@ blink_modules_sources("wake_lock") {
"wake_lock_sentinel.h",
"wake_lock_type.cc",
"wake_lock_type.h",
"worker_navigator_wake_lock.cc",
"worker_navigator_wake_lock.h",
]
deps = [
"//mojo/public/cpp/bindings",
......
// 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/frame/local_dom_window.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_) {
if (auto* window = GetSupplementable()->DomWindow())
wake_lock_ = MakeGarbageCollected<WakeLock>(*window);
}
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(Visitor* visitor) const {
visitor->Trace(wake_lock_);
Supplement<Navigator>::Trace(visitor);
}
} // namespace blink
// 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> {
public:
static const char kSupplementName[];
static NavigatorWakeLock& From(Navigator&);
static WakeLock* wakeLock(Navigator&);
explicit NavigatorWakeLock(Navigator&);
void Trace(Visitor*) const override;
private:
WakeLock* GetWakeLock();
Member<WakeLock> wake_lock_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WAKE_LOCK_NAVIGATOR_WAKE_LOCK_H_
......@@ -5,7 +5,7 @@
// https://w3c.github.io/screen-wake-lock/#extensions-to-the-navigator-interface
[
ImplementedAs=NavigatorWakeLock,
ImplementedAs=WakeLock,
RuntimeEnabled=WakeLock,
SecureContext
] partial interface Navigator {
......
......@@ -9,6 +9,7 @@
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/execution_context/navigator_base.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
......@@ -26,24 +27,40 @@ namespace blink {
using mojom::blink::PermissionService;
using mojom::blink::PermissionStatus;
WakeLock::WakeLock(LocalDOMWindow& window)
: ExecutionContextLifecycleObserver(&window),
PageVisibilityObserver(window.GetFrame()->GetPage()),
permission_service_(&window),
// static
const char WakeLock::kSupplementName[] = "WakeLock";
// static
WakeLock* WakeLock::wakeLock(NavigatorBase& navigator) {
ExecutionContext* context = navigator.GetExecutionContext();
if (!context ||
(!context->IsWindow() && !context->IsDedicatedWorkerGlobalScope())) {
// TODO(https://crbug.com/839117): Remove this check once the Exposed
// attribute is fixed to only expose this property in dedicated workers.
return nullptr;
}
WakeLock* supplement = Supplement<NavigatorBase>::From<WakeLock>(navigator);
if (!supplement && navigator.GetExecutionContext()) {
supplement = MakeGarbageCollected<WakeLock>(navigator);
ProvideTo(navigator, supplement);
}
return supplement;
}
WakeLock::WakeLock(NavigatorBase& navigator)
: Supplement<NavigatorBase>(navigator),
ExecutionContextLifecycleObserver(navigator.GetExecutionContext()),
PageVisibilityObserver(navigator.DomWindow()
? navigator.DomWindow()->GetFrame()->GetPage()
: nullptr),
permission_service_(navigator.GetExecutionContext()),
managers_{
MakeGarbageCollected<WakeLockManager>(&window, WakeLockType::kScreen),
MakeGarbageCollected<WakeLockManager>(&window,
MakeGarbageCollected<WakeLockManager>(navigator.GetExecutionContext(),
WakeLockType::kScreen),
MakeGarbageCollected<WakeLockManager>(navigator.GetExecutionContext(),
WakeLockType::kSystem)} {}
WakeLock::WakeLock(DedicatedWorkerGlobalScope& worker_scope)
: ExecutionContextLifecycleObserver(&worker_scope),
PageVisibilityObserver(nullptr),
permission_service_(&worker_scope),
managers_{MakeGarbageCollected<WakeLockManager>(&worker_scope,
WakeLockType::kScreen),
MakeGarbageCollected<WakeLockManager>(&worker_scope,
WakeLockType::kSystem)} {}
ScriptPromise WakeLock::request(ScriptState* script_state,
const String& type,
ExceptionState& exception_state) {
......@@ -278,6 +295,7 @@ void WakeLock::Trace(Visitor* visitor) const {
for (const WakeLockManager* manager : managers_)
visitor->Trace(manager);
visitor->Trace(permission_service_);
Supplement<NavigatorBase>::Trace(visitor);
PageVisibilityObserver::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor);
ScriptWrappable::Trace(visitor);
......
......@@ -18,6 +18,7 @@
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace WTF {
......@@ -28,18 +29,23 @@ class String;
namespace blink {
class ExceptionState;
class LocalDOMWindow;
class NavigatorBase;
class ScriptState;
class WakeLockManager;
class MODULES_EXPORT WakeLock final : public ScriptWrappable,
public Supplement<NavigatorBase>,
public ExecutionContextLifecycleObserver,
public PageVisibilityObserver {
DEFINE_WRAPPERTYPEINFO();
public:
explicit WakeLock(LocalDOMWindow&);
explicit WakeLock(DedicatedWorkerGlobalScope&);
static const char kSupplementName[];
// Getter for navigator.wakelock
static WakeLock* wakeLock(NavigatorBase&);
explicit WakeLock(NavigatorBase&);
ScriptPromise request(ScriptState*,
const WTF::String& type,
......
......@@ -12,6 +12,7 @@
#include "third_party/blink/renderer/core/dom/events/native_event_listener.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.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/event_target_modules_names.h"
#include "third_party/blink/renderer/modules/wake_lock/wake_lock.h"
#include "third_party/blink/renderer/modules/wake_lock/wake_lock_manager.h"
......@@ -114,7 +115,7 @@ TEST(WakeLockSentinelTest, ContextDestruction) {
MakeGarbageCollected<ScriptPromiseResolver>(context.GetScriptState());
ScriptPromise screen_promise = screen_resolver->Promise();
auto* wake_lock = MakeGarbageCollected<WakeLock>(*context.DomWindow());
auto* wake_lock = WakeLock::wakeLock(*context.DomWindow()->navigator());
wake_lock->DoRequest(WakeLockType::kScreen, screen_resolver);
WakeLockManager* manager =
......
......@@ -10,6 +10,7 @@
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.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/core/page/page.h"
#include "third_party/blink/renderer/modules/wake_lock/wake_lock_test_utils.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
......@@ -28,7 +29,7 @@ TEST(WakeLockTest, RequestWakeLockGranted) {
MakeGarbageCollected<ScriptPromiseResolver>(context.GetScriptState());
ScriptPromise screen_promise = screen_resolver->Promise();
auto* wake_lock = MakeGarbageCollected<WakeLock>(*context.DomWindow());
auto* wake_lock = WakeLock::wakeLock(*context.DomWindow()->navigator());
wake_lock->DoRequest(WakeLockType::kScreen, screen_resolver);
MockWakeLock& screen_lock =
......@@ -55,7 +56,7 @@ TEST(WakeLockTest, RequestWakeLockDenied) {
MakeGarbageCollected<ScriptPromiseResolver>(context.GetScriptState());
ScriptPromise system_promise = system_resolver->Promise();
auto* wake_lock = MakeGarbageCollected<WakeLock>(*context.DomWindow());
auto* wake_lock = WakeLock::wakeLock(*context.DomWindow()->navigator());
wake_lock->DoRequest(WakeLockType::kSystem, system_resolver);
MockWakeLock& system_lock =
......@@ -102,7 +103,7 @@ TEST(WakeLockTest, LossOfDocumentActivity) {
MakeGarbageCollected<ScriptPromiseResolver>(context.GetScriptState());
system_resolver1->Promise();
auto* wake_lock = MakeGarbageCollected<WakeLock>(*context.DomWindow());
auto* wake_lock = WakeLock::wakeLock(*context.DomWindow()->navigator());
wake_lock->DoRequest(WakeLockType::kScreen, screen_resolver1);
wake_lock->DoRequest(WakeLockType::kScreen, screen_resolver2);
screen_lock.WaitForRequest();
......@@ -143,7 +144,7 @@ TEST(WakeLockTest, PageVisibilityHidden) {
MakeGarbageCollected<ScriptPromiseResolver>(context.GetScriptState());
ScriptPromise system_promise = system_resolver->Promise();
auto* wake_lock = MakeGarbageCollected<WakeLock>(*context.DomWindow());
auto* wake_lock = WakeLock::wakeLock(*context.DomWindow()->navigator());
wake_lock->DoRequest(WakeLockType::kScreen, screen_resolver);
screen_lock.WaitForRequest();
wake_lock->DoRequest(WakeLockType::kSystem, system_resolver);
......@@ -194,7 +195,7 @@ TEST(WakeLockTest, PageVisibilityHiddenBeforeLockAcquisition) {
MakeGarbageCollected<ScriptPromiseResolver>(context.GetScriptState());
ScriptPromise system_promise = system_resolver->Promise();
auto* wake_lock = MakeGarbageCollected<WakeLock>(*context.DomWindow());
auto* wake_lock = WakeLock::wakeLock(*context.DomWindow()->navigator());
wake_lock->DoRequest(WakeLockType::kScreen, screen_resolver);
wake_lock->DoRequest(WakeLockType::kSystem, system_resolver);
context.Frame()->GetPage()->SetVisibilityState(
......
// 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/worker_navigator_wake_lock.h"
#include "third_party/blink/renderer/core/workers/worker_navigator.h"
#include "third_party/blink/renderer/modules/wake_lock/wake_lock.h"
namespace blink {
WorkerNavigatorWakeLock::WorkerNavigatorWakeLock(WorkerNavigator& navigator)
: Supplement<WorkerNavigator>(navigator) {}
// static
const char WorkerNavigatorWakeLock::kSupplementName[] =
"WorkerNavigatorWakeLock";
// static
WorkerNavigatorWakeLock& WorkerNavigatorWakeLock::From(
WorkerNavigator& navigator) {
WorkerNavigatorWakeLock* supplement =
Supplement<WorkerNavigator>::From<WorkerNavigatorWakeLock>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<WorkerNavigatorWakeLock>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
// static
WakeLock* WorkerNavigatorWakeLock::wakeLock(ScriptState* script_state,
WorkerNavigator& navigator) {
return WorkerNavigatorWakeLock::From(navigator).GetWakeLock(script_state);
}
WakeLock* WorkerNavigatorWakeLock::GetWakeLock(ScriptState* script_state) {
if (!wake_lock_) {
auto* execution_context = ExecutionContext::From(script_state);
DCHECK(execution_context);
// TODO(https://crbug.com/839117): Remove this check once the Exposed
// attribute is fixed to only expose this property in dedicated workers.
if (execution_context->IsDedicatedWorkerGlobalScope()) {
wake_lock_ = MakeGarbageCollected<WakeLock>(
*To<DedicatedWorkerGlobalScope>(execution_context));
}
}
return wake_lock_;
}
void WorkerNavigatorWakeLock::Trace(Visitor* visitor) const {
visitor->Trace(wake_lock_);
Supplement<WorkerNavigator>::Trace(visitor);
}
} // namespace blink
// 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_WORKER_NAVIGATOR_WAKE_LOCK_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WAKE_LOCK_WORKER_NAVIGATOR_WAKE_LOCK_H_
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class ScriptState;
class WakeLock;
class WorkerNavigator;
class WorkerNavigatorWakeLock final
: public GarbageCollected<WorkerNavigatorWakeLock>,
public Supplement<WorkerNavigator> {
public:
static const char kSupplementName[];
static WorkerNavigatorWakeLock& From(WorkerNavigator&);
static WakeLock* wakeLock(ScriptState*, WorkerNavigator&);
explicit WorkerNavigatorWakeLock(WorkerNavigator&);
void Trace(Visitor*) const override;
private:
WakeLock* GetWakeLock(ScriptState*);
Member<WakeLock> wake_lock_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WAKE_LOCK_WORKER_NAVIGATOR_WAKE_LOCK_H_
......@@ -7,9 +7,9 @@
// until System Wake Lock API was split from the Screen Wake Lock API.
[
ImplementedAs=WorkerNavigatorWakeLock,
ImplementedAs=WakeLock,
RuntimeEnabled=SystemWakeLock,
SecureContext
] partial interface WorkerNavigator {
[CallWith=ScriptState, SameObject] readonly attribute WakeLock wakeLock;
[SameObject] readonly attribute WakeLock wakeLock;
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment