Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: content/renderer/battery_status/battery_status_dispatcher.cc

Issue 446603002: Refactor code listening to platform events in content/renderer/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webkitplatform_impl_start_stop
Patch Set: rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
8 #include "content/common/battery_status_messages.h" 7 #include "content/common/battery_status_messages.h"
9 #include "content/renderer/render_thread_impl.h" 8 #include "content/renderer/render_thread_impl.h"
10 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" 9 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h"
11 10
12 namespace content { 11 namespace content {
13 12
14 BatteryStatusDispatcher::BatteryStatusDispatcher(RenderThread* thread) 13 BatteryStatusDispatcher::BatteryStatusDispatcher(RenderThread* thread)
15 : listener_(0) { 14 : PlatformEventObserver<blink::WebBatteryStatusListener>(thread) {
16 if (thread)
17 thread->AddObserver(this);
18 } 15 }
19 16
20 BatteryStatusDispatcher::~BatteryStatusDispatcher() { 17 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();
29 } 18 }
30 19
31 bool BatteryStatusDispatcher::OnControlMessageReceived( 20 bool BatteryStatusDispatcher::OnControlMessageReceived(
32 const IPC::Message& message) { 21 const IPC::Message& message) {
33 bool handled = true; 22 bool handled = true;
34 IPC_BEGIN_MESSAGE_MAP(BatteryStatusDispatcher, message) 23 IPC_BEGIN_MESSAGE_MAP(BatteryStatusDispatcher, message)
35 IPC_MESSAGE_HANDLER(BatteryStatusMsg_DidChange, OnDidChange) 24 IPC_MESSAGE_HANDLER(BatteryStatusMsg_DidChange, OnDidChange)
36 IPC_MESSAGE_UNHANDLED(handled = false) 25 IPC_MESSAGE_UNHANDLED(handled = false)
37 IPC_END_MESSAGE_MAP() 26 IPC_END_MESSAGE_MAP()
38 return handled; 27 return handled;
39 } 28 }
40 29
41 bool BatteryStatusDispatcher::Start() { 30 void BatteryStatusDispatcher::SendStartMessage() {
42 return RenderThread::Get()->Send(new BatteryStatusHostMsg_Start()); 31 RenderThread::Get()->Send(new BatteryStatusHostMsg_Start());
43 } 32 }
44 33
45 bool BatteryStatusDispatcher::Stop() { 34 void BatteryStatusDispatcher::SendStopMessage() {
46 return RenderThread::Get()->Send(new BatteryStatusHostMsg_Stop()); 35 RenderThread::Get()->Send(new BatteryStatusHostMsg_Stop());
47 } 36 }
48 37
49 void BatteryStatusDispatcher::OnDidChange( 38 void BatteryStatusDispatcher::OnDidChange(
50 const blink::WebBatteryStatus& status) { 39 const blink::WebBatteryStatus& status) {
51 if (listener_) 40 if (listener())
52 listener_->updateBatteryStatus(status); 41 listener()->updateBatteryStatus(status);
42 }
43
44 void BatteryStatusDispatcher::SendFakeDataForTesting(void* fake_data) {
45 OnDidChange(*static_cast<blink::WebBatteryStatus*>(fake_data));
53 } 46 }
54 47
55 } // namespace content 48 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698