| Index: content/browser/battery_status/battery_status_message_filter.cc
|
| diff --git a/content/browser/battery_status/battery_status_message_filter.cc b/content/browser/battery_status/battery_status_message_filter.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..601cfaf8d90d991f0cff710c7dc40f8c4a305bc9
|
| --- /dev/null
|
| +++ b/content/browser/battery_status/battery_status_message_filter.cc
|
| @@ -0,0 +1,81 @@
|
| +// Copyright (c) 2012 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/browser/battery_status/battery_status_message_filter.h"
|
| +
|
| +#include "content/browser/battery_status/battery_status_provider_impl.h"
|
| +#include "content/common/battery_status_messages.h"
|
| +#include "content/public/browser/power_supply_status.h"
|
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebBatteryStatus.h"
|
| +
|
| +namespace content {
|
| +
|
| +class BatteryStatusMessageFilter::PowerManagerObserver :
|
| + public BatteryStatusProviderImpl::Observer {
|
| + public:
|
| + explicit PowerManagerObserver(BatteryStatusMessageFilter* filter)
|
| + : filter_(filter) {
|
| + BatteryStatusProviderImpl::GetInstance()->AddObserver(this);
|
| + }
|
| +
|
| + virtual ~PowerManagerObserver() {
|
| + BatteryStatusProviderImpl::GetInstance()->RemoveObserver(this);
|
| + }
|
| +
|
| + private:
|
| + virtual void PowerChanged(
|
| + const PowerSupplyStatus& power_status) OVERRIDE {
|
| + filter_->SendUpdates(power_status);
|
| + }
|
| +
|
| + BatteryStatusMessageFilter* filter_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PowerManagerObserver);
|
| +};
|
| +
|
| +BatteryStatusMessageFilter::BatteryStatusMessageFilter() {
|
| +}
|
| +
|
| +void BatteryStatusMessageFilter::SendUpdates(
|
| + const PowerSupplyStatus& power_status) {
|
| + WebKit::WebBatteryStatus web_status;
|
| + web_status.charging = power_status.line_power_on;
|
| + web_status.chargingTime = power_status.battery_seconds_to_full;
|
| + web_status.dischargingTime = power_status.battery_seconds_to_empty;
|
| + web_status.level = power_status.battery_percentage / 100.;
|
| +
|
| + for (std::list<int>::iterator i = notify_updates_.begin();
|
| + i != notify_updates_.end();
|
| + ++i) {
|
| + Send(new BatteryStatusMsg_Updated(*i, web_status));
|
| + }
|
| +}
|
| +
|
| +BatteryStatusMessageFilter::~BatteryStatusMessageFilter() {
|
| +}
|
| +
|
| +void BatteryStatusMessageFilter::OnStartUpdating(int render_view_id) {
|
| + if (!observer_.get())
|
| + observer_.reset(new PowerManagerObserver(this));
|
| + notify_updates_.push_back(render_view_id);
|
| +}
|
| +
|
| +void BatteryStatusMessageFilter::OnStopUpdating(int render_view_id) {
|
| + notify_updates_.remove(render_view_id);
|
| + if (notify_updates_.size() == 0)
|
| + observer_.reset();
|
| +}
|
| +
|
| +bool BatteryStatusMessageFilter::OnMessageReceived(const IPC::Message& message,
|
| + bool* message_was_ok) {
|
| + bool handled = true;
|
| + IPC_BEGIN_MESSAGE_MAP_EX(BatteryStatusMessageFilter, message, *message_was_ok)
|
| + IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_StartUpdating, OnStartUpdating)
|
| + IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_StopUpdating, OnStopUpdating)
|
| + IPC_MESSAGE_UNHANDLED(handled = false)
|
| + IPC_END_MESSAGE_MAP_EX()
|
| + return handled;
|
| +}
|
| +
|
| +} // namespace content
|
|
|