| 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 "content/renderer/battery_status/battery_status_dispatcher.h" | 5 #include "platform/battery/battery_dispatcher_proxy.h" |
| 6 | 6 |
| 7 #include "content/public/common/service_registry.h" | 7 #include "platform/base/Bind.h" |
| 8 #include "content/public/renderer/render_thread.h" | 8 #include "platform/battery/battery_status.h" |
| 9 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" | 9 #include "platform/battery/battery_status_listener.h" |
| 10 #include "public/platform/Platform.h" |
| 11 #include "wtf/Assertions.h" |
| 10 | 12 |
| 11 namespace content { | 13 namespace blink { |
| 12 | 14 |
| 13 BatteryStatusDispatcher::BatteryStatusDispatcher( | 15 BatteryDispatcherProxy::BatteryDispatcherProxy(BatteryStatusListener* listener) |
| 14 blink::WebBatteryStatusListener* listener) | |
| 15 : listener_(listener) { | 16 : listener_(listener) { |
| 16 DCHECK(listener_); | 17 ASSERT(listener_); |
| 17 | |
| 18 if (ServiceRegistry* registry = RenderThread::Get()->GetServiceRegistry()) { | |
| 19 // registry can be null during testing. | |
| 20 registry->ConnectToRemoteService(mojo::GetProxy(&monitor_)); | |
| 21 QueryNextStatus(); | |
| 22 } | |
| 23 } | 18 } |
| 24 | 19 |
| 25 BatteryStatusDispatcher::~BatteryStatusDispatcher() { | 20 BatteryDispatcherProxy::~BatteryDispatcherProxy() {} |
| 21 |
| 22 void BatteryDispatcherProxy::StartListening() { |
| 23 ASSERT(!monitor_.is_bound()); |
| 24 Platform::current()->connectToRemoteService(mojo::GetProxy(&monitor_)); |
| 25 // monitor_ can be null during testing. |
| 26 if (monitor_) |
| 27 QueryNextStatus(); |
| 26 } | 28 } |
| 27 | 29 |
| 28 void BatteryStatusDispatcher::QueryNextStatus() { | 30 void BatteryDispatcherProxy::StopListening() { |
| 29 monitor_->QueryNextStatus( | 31 // monitor_ can be null during testing. |
| 30 base::Bind(&BatteryStatusDispatcher::DidChange, base::Unretained(this))); | 32 if (monitor_) |
| 33 monitor_.reset(); |
| 31 } | 34 } |
| 32 | 35 |
| 33 void BatteryStatusDispatcher::DidChange( | 36 void BatteryDispatcherProxy::QueryNextStatus() { |
| 37 monitor_->QueryNextStatus( |
| 38 bindInstanceToMethodForMojo(&BatteryDispatcherProxy::OnDidChange, this)); |
| 39 } |
| 40 |
| 41 void BatteryDispatcherProxy::OnDidChange( |
| 34 device::BatteryStatusPtr battery_status) { | 42 device::BatteryStatusPtr battery_status) { |
| 35 // monitor_ can be null during testing. | 43 // monitor_ can be null during testing. |
| 36 if (monitor_) | 44 if (monitor_) |
| 37 QueryNextStatus(); | 45 QueryNextStatus(); |
| 38 | 46 |
| 39 DCHECK(battery_status); | 47 DCHECK(battery_status); |
| 40 | 48 |
| 41 blink::WebBatteryStatus web_battery_status; | 49 BatteryStatus status(battery_status->charging, |
| 42 web_battery_status.charging = battery_status->charging; | 50 battery_status->charging_time, |
| 43 web_battery_status.chargingTime = battery_status->charging_time; | 51 battery_status->discharging_time, |
| 44 web_battery_status.dischargingTime = battery_status->discharging_time; | 52 battery_status->level); |
| 45 web_battery_status.level = battery_status->level; | 53 listener_->OnUpdateBatteryStatus(status); |
| 46 listener_->updateBatteryStatus(web_battery_status); | |
| 47 } | 54 } |
| 48 | 55 |
| 49 } // namespace content | 56 } // namespace blink |
| OLD | NEW |