-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 1398: Navigator.getBattery should return constant result
Auditors: @bridiver
- Loading branch information
Showing
3 changed files
with
103 additions
and
20 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
chromium_src/third_party/blink/renderer/modules/battery/battery_manager.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
chromium_src/third_party/blink/renderer/modules/battery/navigator_battery.cc
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |