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 "battery_status_dispatcher.h" | 5 #include "battery_status_dispatcher.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "content/common/battery_status_messages.h" | 8 #include "content/common/battery_status_messages.h" |
8 #include "content/renderer/render_thread_impl.h" | 9 #include "content/renderer/render_thread_impl.h" |
9 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" | 10 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" |
10 | 11 |
11 namespace content { | 12 namespace content { |
12 | 13 |
13 BatteryStatusDispatcher::BatteryStatusDispatcher(RenderThread* thread) | 14 BatteryStatusDispatcher::BatteryStatusDispatcher(RenderThread* thread) |
14 : PlatformEventObserver<blink::WebBatteryStatusListener>(thread) { | 15 : listener_(0) { |
| 16 if (thread) |
| 17 thread->AddObserver(this); |
15 } | 18 } |
16 | 19 |
17 BatteryStatusDispatcher::~BatteryStatusDispatcher() { | 20 BatteryStatusDispatcher::~BatteryStatusDispatcher() { |
| 21 if (listener_) |
| 22 Stop(); |
| 23 } |
| 24 |
| 25 bool BatteryStatusDispatcher::SetListener( |
| 26 blink::WebBatteryStatusListener* listener) { |
| 27 listener_ = listener; |
| 28 return listener ? Start() : Stop(); |
18 } | 29 } |
19 | 30 |
20 bool BatteryStatusDispatcher::OnControlMessageReceived( | 31 bool BatteryStatusDispatcher::OnControlMessageReceived( |
21 const IPC::Message& message) { | 32 const IPC::Message& message) { |
22 bool handled = true; | 33 bool handled = true; |
23 IPC_BEGIN_MESSAGE_MAP(BatteryStatusDispatcher, message) | 34 IPC_BEGIN_MESSAGE_MAP(BatteryStatusDispatcher, message) |
24 IPC_MESSAGE_HANDLER(BatteryStatusMsg_DidChange, OnDidChange) | 35 IPC_MESSAGE_HANDLER(BatteryStatusMsg_DidChange, OnDidChange) |
25 IPC_MESSAGE_UNHANDLED(handled = false) | 36 IPC_MESSAGE_UNHANDLED(handled = false) |
26 IPC_END_MESSAGE_MAP() | 37 IPC_END_MESSAGE_MAP() |
27 return handled; | 38 return handled; |
28 } | 39 } |
29 | 40 |
30 void BatteryStatusDispatcher::SendStartMessage() { | 41 bool BatteryStatusDispatcher::Start() { |
31 RenderThread::Get()->Send(new BatteryStatusHostMsg_Start()); | 42 return RenderThread::Get()->Send(new BatteryStatusHostMsg_Start()); |
32 } | 43 } |
33 | 44 |
34 void BatteryStatusDispatcher::SendStopMessage() { | 45 bool BatteryStatusDispatcher::Stop() { |
35 RenderThread::Get()->Send(new BatteryStatusHostMsg_Stop()); | 46 return RenderThread::Get()->Send(new BatteryStatusHostMsg_Stop()); |
36 } | 47 } |
37 | 48 |
38 void BatteryStatusDispatcher::OnDidChange( | 49 void BatteryStatusDispatcher::OnDidChange( |
39 const blink::WebBatteryStatus& status) { | 50 const blink::WebBatteryStatus& status) { |
40 if (listener()) | 51 if (listener_) |
41 listener()->updateBatteryStatus(status); | 52 listener_->updateBatteryStatus(status); |
42 } | |
43 | |
44 void BatteryStatusDispatcher::SendFakeDataForTesting(void* fake_data) { | |
45 OnDidChange(*static_cast<blink::WebBatteryStatus*>(fake_data)); | |
46 } | 53 } |
47 | 54 |
48 } // namespace content | 55 } // namespace content |
OLD | NEW |