| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "device/battery/battery_monitor_impl.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 13 | |
| 14 namespace device { | |
| 15 | |
| 16 // static | |
| 17 void BatteryMonitorImpl::Create(mojom::BatteryMonitorRequest request) { | |
| 18 auto* impl = new BatteryMonitorImpl; | |
| 19 auto binding = | |
| 20 mojo::MakeStrongBinding(base::WrapUnique(impl), std::move(request)); | |
| 21 impl->binding_ = binding; | |
| 22 } | |
| 23 | |
| 24 BatteryMonitorImpl::BatteryMonitorImpl() : status_to_report_(false) { | |
| 25 // NOTE: DidChange may be called before AddCallback returns. This is done to | |
| 26 // report current status. | |
| 27 subscription_ = BatteryStatusService::GetInstance()->AddCallback( | |
| 28 base::Bind(&BatteryMonitorImpl::DidChange, base::Unretained(this))); | |
| 29 } | |
| 30 | |
| 31 BatteryMonitorImpl::~BatteryMonitorImpl() { | |
| 32 } | |
| 33 | |
| 34 void BatteryMonitorImpl::QueryNextStatus( | |
| 35 const QueryNextStatusCallback& callback) { | |
| 36 if (!callback_.is_null()) { | |
| 37 DVLOG(1) << "Overlapped call to QueryNextStatus!"; | |
| 38 binding_->Close(); | |
| 39 return; | |
| 40 } | |
| 41 callback_ = callback; | |
| 42 | |
| 43 if (status_to_report_) | |
| 44 ReportStatus(); | |
| 45 } | |
| 46 | |
| 47 void BatteryMonitorImpl::RegisterSubscription() { | |
| 48 } | |
| 49 | |
| 50 void BatteryMonitorImpl::DidChange(const mojom::BatteryStatus& battery_status) { | |
| 51 status_ = battery_status; | |
| 52 status_to_report_ = true; | |
| 53 | |
| 54 if (!callback_.is_null()) | |
| 55 ReportStatus(); | |
| 56 } | |
| 57 | |
| 58 void BatteryMonitorImpl::ReportStatus() { | |
| 59 callback_.Run(status_.Clone()); | |
| 60 callback_.Reset(); | |
| 61 | |
| 62 status_to_report_ = false; | |
| 63 } | |
| 64 | |
| 65 } // namespace device | |
| OLD | NEW |