| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/battery/BatteryDispatcher.h" | 5 #include "modules/battery/BatteryDispatcher.h" |
| 6 | 6 |
| 7 #include "platform/threading/BindForMojo.h" |
| 8 #include "public/platform/Platform.h" |
| 9 #include "wtf/Assertions.h" |
| 7 #include "wtf/PassOwnPtr.h" | 10 #include "wtf/PassOwnPtr.h" |
| 8 | 11 |
| 9 namespace blink { | 12 namespace blink { |
| 10 | 13 |
| 11 BatteryDispatcher& BatteryDispatcher::instance() | 14 BatteryDispatcher& BatteryDispatcher::instance() |
| 12 { | 15 { |
| 13 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B
atteryDispatcher())); | 16 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B
atteryDispatcher())); |
| 14 return *batteryDispatcher; | 17 return *batteryDispatcher; |
| 15 } | 18 } |
| 16 | 19 |
| 17 BatteryDispatcher::BatteryDispatcher() | 20 BatteryDispatcher::BatteryDispatcher() |
| 18 : m_hasLatestData(false) | 21 : m_hasLatestData(false) |
| 19 , m_batteryDispatcherProxy(adoptPtr(new BatteryDispatcherProxy(this))) | |
| 20 { | 22 { |
| 21 } | 23 } |
| 22 | 24 |
| 23 BatteryDispatcher::~BatteryDispatcher() | 25 void BatteryDispatcher::queryNextStatus() |
| 24 { | 26 { |
| 27 m_monitor->QueryNextStatus( |
| 28 sameThreadBindForMojo(&BatteryDispatcher::onDidChange, this)); |
| 25 } | 29 } |
| 26 | 30 |
| 27 void BatteryDispatcher::OnUpdateBatteryStatus(const BatteryStatus& batteryStatus
) | 31 void BatteryDispatcher::onDidChange(device::BatteryStatusPtr batteryStatus) |
| 32 { |
| 33 // m_monitor can be null during testing. |
| 34 if (m_monitor) |
| 35 queryNextStatus(); |
| 36 |
| 37 ASSERT(batteryStatus); |
| 38 |
| 39 updateBatteryStatus(BatteryStatus( |
| 40 batteryStatus->charging, |
| 41 batteryStatus->charging_time, |
| 42 batteryStatus->discharging_time, |
| 43 batteryStatus->level)); |
| 44 } |
| 45 |
| 46 void BatteryDispatcher::updateBatteryStatus(const BatteryStatus& batteryStatus) |
| 28 { | 47 { |
| 29 m_batteryStatus = batteryStatus; | 48 m_batteryStatus = batteryStatus; |
| 30 m_hasLatestData = true; | 49 m_hasLatestData = true; |
| 31 notifyControllers(); | 50 notifyControllers(); |
| 32 } | 51 } |
| 33 | 52 |
| 34 void BatteryDispatcher::startListening() | 53 void BatteryDispatcher::startListening() |
| 35 { | 54 { |
| 36 m_batteryDispatcherProxy->StartListening(); | 55 ASSERT(!m_monitor.is_bound()); |
| 56 Platform::current()->connectToRemoteService(mojo::GetProxy(&m_monitor)); |
| 57 // m_monitor can be null during testing. |
| 58 if (m_monitor) |
| 59 queryNextStatus(); |
| 37 } | 60 } |
| 38 | 61 |
| 39 void BatteryDispatcher::stopListening() | 62 void BatteryDispatcher::stopListening() |
| 40 { | 63 { |
| 41 m_batteryDispatcherProxy->StopListening(); | 64 // m_monitor can be null during testing. |
| 65 if (m_monitor) |
| 66 m_monitor.reset(); |
| 42 m_hasLatestData = false; | 67 m_hasLatestData = false; |
| 43 } | 68 } |
| 44 | 69 |
| 45 } // namespace blink | 70 } // namespace blink |
| OLD | NEW |