blob: b5383946252573aefe2793e91169d40b7b5604fe (
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
59
60
61
62
|
// 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 "device/battery/battery_monitor_impl.h"
#include "base/bind.h"
#include "base/logging.h"
namespace device {
// static
void BatteryMonitorImpl::Create(
mojo::InterfaceRequest<BatteryMonitor> request) {
new BatteryMonitorImpl(request.Pass());
}
BatteryMonitorImpl::BatteryMonitorImpl(
mojo::InterfaceRequest<BatteryMonitor> request)
: binding_(this, request.Pass()),
status_to_report_(false) {
// NOTE: DidChange may be called before AddCallback returns. This is done to
// report current status.
subscription_ = BatteryStatusService::GetInstance()->AddCallback(
base::Bind(&BatteryMonitorImpl::DidChange, base::Unretained(this)));
}
BatteryMonitorImpl::~BatteryMonitorImpl() {
}
void BatteryMonitorImpl::QueryNextStatus(
const BatteryStatusCallback& callback) {
if (!callback_.is_null()) {
DVLOG(1) << "Overlapped call to QueryNextStatus!";
delete this;
return;
}
callback_ = callback;
if (status_to_report_)
ReportStatus();
}
void BatteryMonitorImpl::RegisterSubscription() {
}
void BatteryMonitorImpl::DidChange(const BatteryStatus& battery_status) {
status_ = battery_status;
status_to_report_ = true;
if (!callback_.is_null())
ReportStatus();
}
void BatteryMonitorImpl::ReportStatus() {
callback_.Run(status_.Clone());
callback_.reset();
status_to_report_ = false;
}
} // namespace device
|