[BackgroundSync] Clean up some tests
[chromium-blink-merge.git] / device / battery / battery_monitor_impl.cc
blobb5383946252573aefe2793e91169d40b7b5604fe
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/battery/battery_monitor_impl.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
10 namespace device {
12 // static
13 void BatteryMonitorImpl::Create(
14 mojo::InterfaceRequest<BatteryMonitor> request) {
15 new BatteryMonitorImpl(request.Pass());
18 BatteryMonitorImpl::BatteryMonitorImpl(
19 mojo::InterfaceRequest<BatteryMonitor> request)
20 : binding_(this, request.Pass()),
21 status_to_report_(false) {
22 // NOTE: DidChange may be called before AddCallback returns. This is done to
23 // report current status.
24 subscription_ = BatteryStatusService::GetInstance()->AddCallback(
25 base::Bind(&BatteryMonitorImpl::DidChange, base::Unretained(this)));
28 BatteryMonitorImpl::~BatteryMonitorImpl() {
31 void BatteryMonitorImpl::QueryNextStatus(
32 const BatteryStatusCallback& callback) {
33 if (!callback_.is_null()) {
34 DVLOG(1) << "Overlapped call to QueryNextStatus!";
35 delete this;
36 return;
38 callback_ = callback;
40 if (status_to_report_)
41 ReportStatus();
44 void BatteryMonitorImpl::RegisterSubscription() {
47 void BatteryMonitorImpl::DidChange(const BatteryStatus& battery_status) {
48 status_ = battery_status;
49 status_to_report_ = true;
51 if (!callback_.is_null())
52 ReportStatus();
55 void BatteryMonitorImpl::ReportStatus() {
56 callback_.Run(status_.Clone());
57 callback_.reset();
59 status_to_report_ = false;
62 } // namespace device