OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/battery_status/battery_status_message_filter.h" |
| 6 |
| 7 #include "content/browser/battery_status/battery_status_provider_impl.h" |
| 8 #include "content/common/battery_status_messages.h" |
| 9 #include "content/public/browser/power_supply_status.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBatteryStatus.h" |
| 11 |
| 12 class BatteryStatusMessageFilter::PowerManagerObserver : |
| 13 public BatteryStatusProviderImpl::Observer { |
| 14 public: |
| 15 explicit PowerManagerObserver(BatteryStatusMessageFilter* filter) |
| 16 : filter_(filter) { |
| 17 BatteryStatusProviderImpl::GetInstance()->AddObserver(this); |
| 18 } |
| 19 |
| 20 virtual ~PowerManagerObserver() { |
| 21 BatteryStatusProviderImpl::GetInstance()->RemoveObserver(this); |
| 22 } |
| 23 |
| 24 private: |
| 25 virtual void PowerChanged( |
| 26 const content::PowerSupplyStatus& power_status) OVERRIDE { |
| 27 filter_->SendUpdates(power_status); |
| 28 } |
| 29 |
| 30 BatteryStatusMessageFilter* filter_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(PowerManagerObserver); |
| 33 }; |
| 34 |
| 35 BatteryStatusMessageFilter::BatteryStatusMessageFilter() { |
| 36 } |
| 37 |
| 38 void BatteryStatusMessageFilter::SendUpdates( |
| 39 const content::PowerSupplyStatus& power_status) { |
| 40 WebKit::WebBatteryStatus web_status; |
| 41 web_status.charging = power_status.line_power_on; |
| 42 web_status.chargingTime = power_status.battery_seconds_to_full; |
| 43 web_status.dischargingTime = power_status.battery_seconds_to_empty; |
| 44 web_status.level = power_status.battery_percentage / 100.; |
| 45 |
| 46 for (std::list<int>::iterator i = notify_updates_.begin(); |
| 47 i != notify_updates_.end(); |
| 48 ++i) { |
| 49 Send(new BatteryStatusMsg_Updated(*i, web_status)); |
| 50 } |
| 51 } |
| 52 |
| 53 BatteryStatusMessageFilter::~BatteryStatusMessageFilter() { |
| 54 } |
| 55 |
| 56 void BatteryStatusMessageFilter::OnStartUpdating(int render_view_id) { |
| 57 if (!observer_.get()) |
| 58 observer_.reset(new PowerManagerObserver(this)); |
| 59 notify_updates_.push_back(render_view_id); |
| 60 } |
| 61 |
| 62 void BatteryStatusMessageFilter::OnStopUpdating(int render_view_id) { |
| 63 notify_updates_.remove(render_view_id); |
| 64 if (notify_updates_.size() == 0) |
| 65 observer_.reset(); |
| 66 } |
| 67 |
| 68 bool BatteryStatusMessageFilter::OnMessageReceived(const IPC::Message& message, |
| 69 bool* message_was_ok) { |
| 70 bool handled = true; |
| 71 IPC_BEGIN_MESSAGE_MAP_EX(BatteryStatusMessageFilter, message, *message_was_ok) |
| 72 IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_StartUpdating, OnStartUpdating) |
| 73 IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_StopUpdating, OnStopUpdating) |
| 74 IPC_MESSAGE_UNHANDLED(handled = false) |
| 75 IPC_END_MESSAGE_MAP_EX() |
| 76 return handled; |
| 77 } |
OLD | NEW |