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