summaryrefslogtreecommitdiffstats
path: root/content/child/power_monitor_broadcast_source.cc
blob: aa6bebfce1d4b5b58efe178d25b9aaf0b6709cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/child/power_monitor_broadcast_source.h"

#include "base/location.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "content/common/power_monitor_messages.h"
#include "ipc/message_filter.h"

namespace content {

class PowerMessageFilter : public IPC::MessageFilter {
 public:
  PowerMessageFilter(PowerMonitorBroadcastSource* source,
                     scoped_refptr<base::SingleThreadTaskRunner> task_runner)
      : source_(source), task_runner_(task_runner) {}

  bool OnMessageReceived(const IPC::Message& message) override {
    bool handled = true;
    IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter, message)
      IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange)
      IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend)
      IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume)
      IPC_MESSAGE_UNHANDLED(handled = false)
    IPC_END_MESSAGE_MAP()
    return handled;
  }

  void RemoveSource() {
    source_ = NULL;
  }

 private:
  friend class base::RefCounted<PowerMessageFilter>;

  ~PowerMessageFilter() override{};

  void OnPowerStateChange(bool on_battery_power) {
    task_runner_->PostTask(
        FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange,
                              this, on_battery_power));
  }
  void OnSuspend() {
    task_runner_->PostTask(
        FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceSuspend, this));
  }
  void OnResume() {
    task_runner_->PostTask(
        FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceResume, this));
  }

  void NotifySourcePowerStateChange(bool on_battery_power) {
    if (source_)
      source_->OnPowerStateChange(on_battery_power);
  }
  void NotifySourceSuspend() {
    if (source_)
      source_->OnSuspend();
  }
  void NotifySourceResume() {
     if (source_)
      source_->OnResume();
  }

  // source_ should only be accessed on the thread associated with
  // message_loop_.
  PowerMonitorBroadcastSource* source_;
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

  DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter);
};

PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
    : last_reported_battery_power_state_(false) {
  message_filter_ =
      new PowerMessageFilter(this, base::ThreadTaskRunnerHandle::Get());
}

PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
  message_filter_->RemoveSource();
}

IPC::MessageFilter* PowerMonitorBroadcastSource::GetMessageFilter() {
  return message_filter_.get();
}

bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() {
  return last_reported_battery_power_state_;
}

void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) {
  last_reported_battery_power_state_ = on_battery_power;
  ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT);
}

void PowerMonitorBroadcastSource::OnSuspend() {
  ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT);
}

void PowerMonitorBroadcastSource::OnResume() {
  ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT);
}

}  // namespace content