blob: e2075b994f1fd08a783fdd8e4cdc02cd2cd33560 (
plain)
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
|
// 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/battery_dispatcher.h"
#include "services/device/public/mojom/constants.mojom-blink.h"
#include "third_party/blink/public/platform/interface_provider.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/mojo/mojo_helper.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
namespace blink {
BatteryDispatcher& BatteryDispatcher::Instance() {
DEFINE_STATIC_LOCAL(BatteryDispatcher, battery_dispatcher,
(new BatteryDispatcher));
return battery_dispatcher;
}
BatteryDispatcher::BatteryDispatcher() : has_latest_data_(false) {}
void BatteryDispatcher::QueryNextStatus() {
monitor_->QueryNextStatus(
WTF::Bind(&BatteryDispatcher::OnDidChange, WrapPersistent(this)));
}
void BatteryDispatcher::OnDidChange(
device::mojom::blink::BatteryStatusPtr battery_status) {
QueryNextStatus();
DCHECK(battery_status);
UpdateBatteryStatus(
BatteryStatus(battery_status->charging, battery_status->charging_time,
battery_status->discharging_time, battery_status->level));
}
void BatteryDispatcher::UpdateBatteryStatus(
const BatteryStatus& battery_status) {
battery_status_ = battery_status;
has_latest_data_ = true;
NotifyControllers();
}
void BatteryDispatcher::StartListening() {
DCHECK(!monitor_.is_bound());
Platform::Current()->GetInterfaceProvider()->GetInterface(
mojo::MakeRequest(&monitor_));
QueryNextStatus();
}
void BatteryDispatcher::StopListening() {
monitor_.reset();
has_latest_data_ = false;
}
} // namespace blink
|