Skip to content

Commit

Permalink
Merge pull request #567 from brave/battery_status
Browse files Browse the repository at this point in the history
Navigator.getBattery should return constant result
  • Loading branch information
bsclifton committed Oct 13, 2018
1 parent 7c32399 commit 6158801
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "third_party/blink/renderer/modules/battery/battery_manager.h"

#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/dom/events/event.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/battery/battery_dispatcher.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"

namespace blink {

BatteryManager::~BatteryManager() = default;

BatteryManager::BatteryManager(ExecutionContext* context)
: PausableObject(context), PlatformEventController(ToDocument(context)) {}

BatteryManager* BatteryManager::Create(ExecutionContext* context) {
BatteryManager* battery_manager = new BatteryManager(context);
battery_manager->PauseIfNeeded();
return battery_manager;
}

ScriptPromise BatteryManager::StartRequest(ScriptState* script_state) {
if (!battery_property_) {
battery_property_ = new BatteryProperty(
ExecutionContext::From(script_state), this, BatteryProperty::kReady);
battery_property_->Resolve(this);
}
return battery_property_->Promise(script_state->World());
}

bool BatteryManager::charging() {
return true;
}

double BatteryManager::chargingTime() {
return 0;
}

double BatteryManager::dischargingTime() {
return std::numeric_limits<double>::infinity();
}

double BatteryManager::level() {
return 1.0;
}

void BatteryManager::Trace(blink::Visitor* visitor) {
visitor->Trace(battery_property_);
PlatformEventController::Trace(visitor);
EventTargetWithInlineData::Trace(visitor);
PausableObject::Trace(visitor);
}

bool BatteryManager::HasPendingActivity() const {
return false;
}

void BatteryManager::Pause() {
return;
}

void BatteryManager::Unpause() {
return;
}

void BatteryManager::UnregisterWithDispatcher() {
return;
}

void BatteryManager::RegisterWithDispatcher() {
return;
}

void BatteryManager::ContextDestroyed(ExecutionContext*) {
battery_property_ = nullptr;
return;
}

void BatteryManager::DidUpdateData() {
return;
}

bool BatteryManager::HasLastData() {
return false;
}
}

This file was deleted.

18 changes: 12 additions & 6 deletions test/data/battery.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<script>
var result = false;
try {
navigator.getBattery().then(function(battery) {
result = (battery.charging == true &&
battery.chargingTime == 0 &&
battery.dischargingTime == Infinity &&
battery.level == 1);
});
} catch (err) {
result = false;
}
function getBatteryBlocked() {
try {
navigator.get_battery();
} catch (err) {
return true;
}
return false;
return result;
}
</script>

0 comments on commit 6158801

Please sign in to comment.