Commit 0a5c2755 authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Merge NavigatorBattery and BatteryManager

BatteryManager can adequately be its own Supplement<Navigator>,
reducing boilerplate.

Bug: 1147612
Change-Id: I15cfaa7b2778f34a1bc754a8ca54a5361b075ddd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2523362
Commit-Queue: Nate Chapin <japhet@chromium.org>
Reviewed-by: default avatarTim Volodine <timvolodine@chromium.org>
Auto-Submit: Nate Chapin <japhet@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828299}
parent dba106ea
...@@ -11,8 +11,6 @@ blink_modules_sources("battery") { ...@@ -11,8 +11,6 @@ blink_modules_sources("battery") {
"battery_manager.cc", "battery_manager.cc",
"battery_manager.h", "battery_manager.h",
"battery_status.h", "battery_status.h",
"navigator_battery.cc",
"navigator_battery.h",
] ]
deps = [ "//services/service_manager/public/cpp" ] deps = [ "//services/service_manager/public/cpp" ]
......
...@@ -9,23 +9,45 @@ ...@@ -9,23 +9,45 @@
#include "third_party/blink/renderer/core/dom/events/event.h" #include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.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/battery/battery_dispatcher.h" #include "third_party/blink/renderer/modules/battery/battery_dispatcher.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h" #include "third_party/blink/renderer/platform/wtf/assertions.h"
namespace blink { namespace blink {
BatteryManager* BatteryManager::Create(ExecutionContext* context) { const char BatteryManager::kSupplementName[] = "BatteryManager";
BatteryManager* battery_manager =
MakeGarbageCollected<BatteryManager>(context); // static
battery_manager->UpdateStateIfNeeded(); ScriptPromise BatteryManager::getBattery(ScriptState* script_state,
return battery_manager; Navigator& navigator) {
if (!navigator.DomWindow())
return ScriptPromise();
// Check to see if this request would be blocked according to the Battery
// Status API specification.
LocalDOMWindow* window = navigator.DomWindow();
if (!window->IsSecureContext())
UseCounter::Count(window, WebFeature::kBatteryStatusInsecureOrigin);
window->GetFrame()->CountUseIfFeatureWouldBeBlockedByFeaturePolicy(
WebFeature::kBatteryStatusCrossOrigin,
WebFeature::kBatteryStatusSameOriginABA);
auto* supplement = Supplement<Navigator>::From<BatteryManager>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<BatteryManager>(navigator);
ProvideTo(navigator, supplement);
}
return supplement->StartRequest(script_state);
} }
BatteryManager::~BatteryManager() = default; BatteryManager::~BatteryManager() = default;
BatteryManager::BatteryManager(ExecutionContext* context) BatteryManager::BatteryManager(Navigator& navigator)
: ExecutionContextLifecycleStateObserver(context), : Supplement<Navigator>(navigator),
PlatformEventController(*To<LocalDOMWindow>(context)) {} ExecutionContextLifecycleStateObserver(navigator.DomWindow()),
PlatformEventController(*navigator.DomWindow()) {
UpdateStateIfNeeded();
}
ScriptPromise BatteryManager::StartRequest(ScriptState* script_state) { ScriptPromise BatteryManager::StartRequest(ScriptState* script_state) {
if (!battery_property_) { if (!battery_property_) {
...@@ -126,6 +148,7 @@ bool BatteryManager::HasPendingActivity() const { ...@@ -126,6 +148,7 @@ bool BatteryManager::HasPendingActivity() const {
void BatteryManager::Trace(Visitor* visitor) const { void BatteryManager::Trace(Visitor* visitor) const {
visitor->Trace(battery_property_); visitor->Trace(battery_property_);
Supplement<Navigator>::Trace(visitor);
PlatformEventController::Trace(visitor); PlatformEventController::Trace(visitor);
EventTargetWithInlineData::Trace(visitor); EventTargetWithInlineData::Trace(visitor);
ExecutionContextLifecycleStateObserver::Trace(visitor); ExecutionContextLifecycleStateObserver::Trace(visitor);
......
...@@ -13,19 +13,23 @@ ...@@ -13,19 +13,23 @@
#include "third_party/blink/renderer/modules/battery/battery_status.h" #include "third_party/blink/renderer/modules/battery/battery_status.h"
#include "third_party/blink/renderer/modules/event_target_modules.h" #include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink { namespace blink {
class Navigator;
class BatteryManager final : public EventTargetWithInlineData, class BatteryManager final : public EventTargetWithInlineData,
public ActiveScriptWrappable<BatteryManager>, public ActiveScriptWrappable<BatteryManager>,
public Supplement<Navigator>,
public ExecutionContextLifecycleStateObserver, public ExecutionContextLifecycleStateObserver,
public PlatformEventController { public PlatformEventController {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
static BatteryManager* Create(ExecutionContext*); static const char kSupplementName[];
static ScriptPromise getBattery(ScriptState*, Navigator&);
explicit BatteryManager(ExecutionContext*); explicit BatteryManager(Navigator&);
~BatteryManager() override; ~BatteryManager() override;
// Returns a promise object that will be resolved with this BatteryManager. // Returns a promise object that will be resolved with this BatteryManager.
......
// Copyright 2014 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/battery/navigator_battery.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.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/modules/battery/battery_manager.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
namespace blink {
NavigatorBattery::NavigatorBattery(Navigator& navigator)
: Supplement<Navigator>(navigator) {}
ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state,
Navigator& navigator) {
return NavigatorBattery::From(navigator).getBattery(script_state);
}
ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state) {
if (!script_state->ContextIsValid())
return ScriptPromise();
LocalDOMWindow* window = LocalDOMWindow::From(script_state);
// Check to see if this request would be blocked according to the Battery
// Status API specification.
if (window) {
if (LocalFrame* frame = window->GetFrame()) {
if (!window->IsSecureContext())
UseCounter::Count(window, WebFeature::kBatteryStatusInsecureOrigin);
frame->CountUseIfFeatureWouldBeBlockedByFeaturePolicy(
WebFeature::kBatteryStatusCrossOrigin,
WebFeature::kBatteryStatusSameOriginABA);
}
}
if (!battery_manager_)
battery_manager_ = BatteryManager::Create(window);
return battery_manager_->StartRequest(script_state);
}
const char NavigatorBattery::kSupplementName[] = "NavigatorBattery";
NavigatorBattery& NavigatorBattery::From(Navigator& navigator) {
NavigatorBattery* supplement =
Supplement<Navigator>::From<NavigatorBattery>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<NavigatorBattery>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
void NavigatorBattery::Trace(Visitor* visitor) const {
visitor->Trace(battery_manager_);
Supplement<Navigator>::Trace(visitor);
}
} // namespace blink
// Copyright 2014 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_BATTERY_NAVIGATOR_BATTERY_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_BATTERY_NAVIGATOR_BATTERY_H_
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class BatteryManager;
class Navigator;
class NavigatorBattery final : public GarbageCollected<NavigatorBattery>,
public Supplement<Navigator> {
public:
static const char kSupplementName[];
static NavigatorBattery& From(Navigator&);
static ScriptPromise getBattery(ScriptState*, Navigator&);
ScriptPromise getBattery(ScriptState*);
explicit NavigatorBattery(Navigator&);
void Trace(Visitor*) const override;
private:
Member<BatteryManager> battery_manager_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_BATTERY_NAVIGATOR_BATTERY_H_
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
// https://w3c.github.io/battery/#the-navigator-interface // https://w3c.github.io/battery/#the-navigator-interface
[ [
ImplementedAs=NavigatorBattery ImplementedAs=BatteryManager
] partial interface Navigator { ] partial interface Navigator {
[CallWith=ScriptState, MeasureAs=BatteryStatusGetBattery] Promise<BatteryManager> getBattery(); [CallWith=ScriptState, MeasureAs=BatteryStatusGetBattery] Promise<BatteryManager> getBattery();
}; };
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