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

Side by Side Diff: content/child/power_monitor_broadcast_source.cc

Issue 2433203003: Mojoify PoweMonitorMessageBroadcaster IPC from browser to child process (Closed)
Patch Set: code rebase Created 4 years, 1 month 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/child/power_monitor_broadcast_source.h" 5 #include "content/child/power_monitor_broadcast_source.h"
6 6
7 #include "base/location.h" 7 #include "base/location.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/single_thread_task_runner.h" 9 #include "content/child/child_thread_impl.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "mojo/public/cpp/bindings/binding.h"
11 #include "content/common/power_monitor_messages.h" 11 #include "services/service_manager/public/cpp/interface_provider.h"
12 #include "ipc/message_filter.h"
13 12
14 namespace content { 13 namespace content {
15 14
16 class PowerMessageFilter : public IPC::MessageFilter { 15 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
17 public: 16 : last_reported_battery_power_state_(false), binding_(this) {
18 PowerMessageFilter(PowerMonitorBroadcastSource* source, 17 // May be null during test execution.
19 scoped_refptr<base::SingleThreadTaskRunner> task_runner) 18 if (ChildThreadImpl::current()) {
20 : source_(source), task_runner_(task_runner) {} 19 device::mojom::PowerMonitorPtr power_monitor;
21 20 ChildThreadImpl::current()->GetRemoteInterfaces()->GetInterface(
22 bool OnMessageReceived(const IPC::Message& message) override { 21 mojo::GetProxy(&power_monitor));
23 bool handled = true; 22 power_monitor->AddClient(binding_.CreateInterfacePtrAndBind());
blundell 2016/10/25 16:53:44 There's a threading change here I think: Before th
Ken Rockot(use gerrit already) 2016/10/25 16:56:14 Yeah this is fine. The new dispatch behavior is ef
24 IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter, message)
25 IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange)
26 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend)
27 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume)
28 IPC_MESSAGE_UNHANDLED(handled = false)
29 IPC_END_MESSAGE_MAP()
30 return handled;
31 } 23 }
32
33 void RemoveSource() {
34 source_ = NULL;
35 }
36
37 private:
38 friend class base::RefCounted<PowerMessageFilter>;
39
40 ~PowerMessageFilter() override{};
41
42 void OnPowerStateChange(bool on_battery_power) {
43 task_runner_->PostTask(
44 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange,
45 this, on_battery_power));
46 }
47 void OnSuspend() {
48 task_runner_->PostTask(
49 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceSuspend, this));
50 }
51 void OnResume() {
52 task_runner_->PostTask(
53 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceResume, this));
54 }
55
56 void NotifySourcePowerStateChange(bool on_battery_power) {
57 if (source_)
58 source_->OnPowerStateChange(on_battery_power);
59 }
60 void NotifySourceSuspend() {
61 if (source_)
62 source_->OnSuspend();
63 }
64 void NotifySourceResume() {
65 if (source_)
66 source_->OnResume();
67 }
68
69 // source_ should only be accessed on the thread associated with
70 // message_loop_.
71 PowerMonitorBroadcastSource* source_;
72 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
73
74 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter);
75 };
76
77 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
78 : last_reported_battery_power_state_(false) {
79 message_filter_ =
80 new PowerMessageFilter(this, base::ThreadTaskRunnerHandle::Get());
81 } 24 }
82 25
83 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { 26 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
84 message_filter_->RemoveSource();
85 }
86
87 IPC::MessageFilter* PowerMonitorBroadcastSource::GetMessageFilter() {
88 return message_filter_.get();
89 } 27 }
90 28
91 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { 29 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() {
92 return last_reported_battery_power_state_; 30 return last_reported_battery_power_state_;
93 } 31 }
94 32
95 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { 33 void PowerMonitorBroadcastSource::PowerStateChange(bool on_battery_power) {
96 last_reported_battery_power_state_ = on_battery_power; 34 last_reported_battery_power_state_ = on_battery_power;
97 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); 35 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT);
98 } 36 }
99 37
100 void PowerMonitorBroadcastSource::OnSuspend() { 38 void PowerMonitorBroadcastSource::Suspend() {
101 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); 39 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT);
102 } 40 }
103 41
104 void PowerMonitorBroadcastSource::OnResume() { 42 void PowerMonitorBroadcastSource::Resume() {
105 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); 43 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT);
106 } 44 }
107 45
108 } // namespace content 46 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698