1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
// Copyright 2014 The Chromium Authors
// 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/battery_manager.h"
#include "third_party/blink/public/mojom/frame/lifecycle.mojom-blink.h"
#include "third_party/blink/renderer/core/dom/dom_exception.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/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/frame/web_feature.h"
#include "third_party/blink/renderer/modules/battery/battery_dispatcher.h"
namespace blink {
const char BatteryManager::kSupplementName[] = "BatteryManager";
// static
ScriptPromise<BatteryManager> BatteryManager::getBattery(
ScriptState* script_state,
Navigator& navigator) {
if (!navigator.DomWindow())
return EmptyPromise();
// Check to see if this request would be blocked according to the Battery
// Status API specification.
LocalDOMWindow* window = navigator.DomWindow();
// TODO(crbug.com/1007264, crbug.com/1290231): remove fenced frame specific
// code when permission policy implements the battery status API support.
if (window->GetFrame()->IsInFencedFrameTree()) {
return ScriptPromise<BatteryManager>::RejectWithDOMException(
script_state,
DOMException::Create(
"getBattery is not allowed in a fenced frame tree.",
DOMException::GetErrorName(DOMExceptionCode::kNotAllowedError)));
}
window->GetFrame()->CountUseIfFeatureWouldBeBlockedByPermissionsPolicy(
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(Navigator& navigator)
: ActiveScriptWrappable<BatteryManager>({}),
Supplement<Navigator>(navigator),
ExecutionContextLifecycleStateObserver(navigator.DomWindow()),
PlatformEventController(*navigator.DomWindow()),
battery_dispatcher_(
MakeGarbageCollected<BatteryDispatcher>(navigator.DomWindow())) {
UpdateStateIfNeeded();
}
ScriptPromise<BatteryManager> BatteryManager::StartRequest(
ScriptState* script_state) {
if (!battery_property_) {
battery_property_ = MakeGarbageCollected<BatteryProperty>(
ExecutionContext::From(script_state));
// If the context is in a stopped state already, do not start updating.
if (!GetExecutionContext() || GetExecutionContext()->IsContextDestroyed()) {
battery_property_->Resolve(this);
} else {
has_event_listener_ = true;
StartUpdating();
}
}
return battery_property_->Promise(script_state->World());
}
bool BatteryManager::charging() {
return battery_status_.Charging();
}
double BatteryManager::chargingTime() {
return battery_status_.charging_time().InSecondsF();
}
double BatteryManager::dischargingTime() {
return battery_status_.discharging_time().InSecondsF();
}
double BatteryManager::level() {
return battery_status_.Level();
}
void BatteryManager::DidUpdateData() {
DCHECK(battery_property_);
BatteryStatus old_status = battery_status_;
battery_status_ = *battery_dispatcher_->LatestData();
if (battery_property_->GetState() == BatteryProperty::kPending) {
battery_property_->Resolve(this);
return;
}
DCHECK(GetExecutionContext());
if (GetExecutionContext()->IsContextPaused() ||
GetExecutionContext()->IsContextDestroyed()) {
return;
}
if (battery_status_.Charging() != old_status.Charging())
DispatchEvent(*Event::Create(event_type_names::kChargingchange));
if (battery_status_.charging_time() != old_status.charging_time())
DispatchEvent(*Event::Create(event_type_names::kChargingtimechange));
if (battery_status_.discharging_time() != old_status.discharging_time())
DispatchEvent(*Event::Create(event_type_names::kDischargingtimechange));
if (battery_status_.Level() != old_status.Level())
DispatchEvent(*Event::Create(event_type_names::kLevelchange));
}
void BatteryManager::RegisterWithDispatcher() {
battery_dispatcher_->AddController(this, DomWindow());
}
void BatteryManager::UnregisterWithDispatcher() {
battery_dispatcher_->RemoveController(this);
}
bool BatteryManager::HasLastData() {
return battery_dispatcher_->LatestData();
}
void BatteryManager::ContextLifecycleStateChanged(
mojom::FrameLifecycleState state) {
if (state == mojom::FrameLifecycleState::kRunning) {
has_event_listener_ = true;
StartUpdating();
} else {
has_event_listener_ = false;
StopUpdating();
}
}
void BatteryManager::ContextDestroyed() {
has_event_listener_ = false;
battery_property_ = nullptr;
StopUpdating();
}
bool BatteryManager::HasPendingActivity() const {
// Prevent V8 from garbage collecting the wrapper object if there are
// event listeners or pending promises attached to it.
return HasEventListeners() ||
(battery_property_ &&
battery_property_->GetState() == BatteryProperty::kPending);
}
void BatteryManager::Trace(Visitor* visitor) const {
visitor->Trace(battery_property_);
visitor->Trace(battery_dispatcher_);
Supplement<Navigator>::Trace(visitor);
PlatformEventController::Trace(visitor);
EventTarget::Trace(visitor);
ExecutionContextLifecycleStateObserver::Trace(visitor);
}
} // namespace blink
|