Chromium Code Reviews| Index: content/browser/power_usage_monitor_impl.cc |
| diff --git a/content/browser/power_usage_monitor_impl.cc b/content/browser/power_usage_monitor_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..88d236b10959c62e0d4ce68018fdfea5e2abd30d |
| --- /dev/null |
| +++ b/content/browser/power_usage_monitor_impl.cc |
| @@ -0,0 +1,235 @@ |
| +// Copyright 2014 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/power_usage_monitor_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/metrics/histogram.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/time/time.h" |
| +#include "content/common/battery_status_messages.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "content/public/browser/power_usage_monitor.h" |
| +#include "content/public/browser/render_process_host.h" |
| + |
| +namespace content { |
| + |
| +void StartPowerUsageMonitor() { |
| + PowerUsageMonitor::GetInstance()->Initialize(); |
| +} |
| + |
| +PowerUsageMonitor::HistogramRecorder::HistogramRecorder() : |
| + weak_ptr_factory_(this) { |
| +} |
| + |
| +PowerUsageMonitor::HistogramRecorder::~HistogramRecorder() { |
| +} |
| + |
| +void PowerUsageMonitor::HistogramRecorder::CancelPendingHistgramReports() { |
| + weak_ptr_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +void PowerUsageMonitor::HistogramRecorder::ReportBatteryLevelHistogram( |
| + base::TimeDelta discharge_time) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + const std::string histogram_name = |
| + base::StringPrintf("Power.BatteryDischarge_%d", |
| + discharge_time.InMinutes()); |
| + base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| + histogram_name, |
| + 0, |
| + 100, |
| + 102, |
| + base::Histogram::kUmaTargetedHistogramFlag); |
| + double discharge_amount = |
| + PowerUsageMonitor::GetInstance()->DischargeAmount(); |
| + histogram->Add(discharge_amount * 100); |
| + |
| + PowerUsageMonitor::GetInstance()->BatteryLevelReported(); |
| +} |
| + |
| + |
| +PowerUsageMonitor::PowerUsageMonitor() : |
|
Daniel Erat
2014/09/30 23:55:24
fix style here (please read the chromium and googl
|
| + histogram_recorder_(new HistogramRecorder()), |
| + was_on_battery_power_(false), |
| + initial_battery_level_(0), |
| + current_battery_level_(0) { |
| + |
| + // PowerUsageMonitor gets battery level information. |
| + callback_ = base::Bind(&PowerUsageMonitor::OnBatteryStatusUpdate, |
| + base::Unretained(this)); |
| + |
| + // Power Monitor used to get suspend/resume notifications. |
| + base::PowerMonitor::Get()->AddObserver(this); |
| + |
| + registrar_.Add(this, |
| + NOTIFICATION_RENDERER_PROCESS_CREATED, |
| + NotificationService::AllBrowserContextsAndSources()); |
| + |
| + registrar_.Add(this, |
| + NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| + NotificationService::AllBrowserContextsAndSources()); |
| + registrar_.Add(this, |
| + NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| + NotificationService::AllBrowserContextsAndSources()); |
| +} |
| + |
| +PowerUsageMonitor::~PowerUsageMonitor() { |
| + base::PowerMonitor::Get()->RemoveObserver(this); |
| +} |
| + |
| +PowerUsageMonitor* PowerUsageMonitor::GetInstance() { |
| + return Singleton<PowerUsageMonitor, |
| + LeakySingletonTraits<PowerUsageMonitor> >::get(); |
| +} |
| + |
| +void PowerUsageMonitor::Initialize() { |
| + const base::TimeDelta delay = base::TimeDelta::FromSeconds(1); |
| + |
| + BrowserThread::PostDelayedTask(BrowserThread::IO, |
| + FROM_HERE, base::Bind(&PowerUsageMonitor::StartOnIOThread, |
| + base::Unretained(this)), delay); |
| +} |
| + |
| +void PowerUsageMonitor::StartOnIOThread() { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + subscription_ = |
| + BatteryStatusService::GetInstance()->AddCallback(callback_); |
| +} |
| + |
| +void PowerUsageMonitor::DischargeStarted(double battery_level) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + // Cancel any in-progress ReportBatteryLevelHistogram() calls. |
| + histogram_recorder_->CancelPendingHistgramReports(); |
| + |
| + // Rate-limit power recording to once every 24 hours. |
| + if (!first_histogram_report_timestamp_.is_null()) { |
| + base::TimeDelta time_since_last_report = |
| + base::Time::Now() - first_histogram_report_timestamp_; |
| + if (time_since_last_report.InHours() < 24) |
| + return; |
| + |
| + // More than 24 hours have passed, reset timestamp. |
| + first_histogram_report_timestamp_ = base::Time(); |
| + } |
| + |
| + was_on_battery_power_ = true; |
| + initial_battery_level_ = battery_level; |
| + current_battery_level_ = battery_level; |
| + start_discharge_time_ = base::Time::Now(); |
| + |
| + const int kBatteryReportingIntervalMinutes[] = {5, 15, 30}; |
| + |
| + for (size_t i = 0; i < arraysize(kBatteryReportingIntervalMinutes); ++i) { |
| + base::TimeDelta delay = |
| + base::TimeDelta::FromMinutes(kBatteryReportingIntervalMinutes[i]); |
| + BrowserThread::PostDelayedTask(BrowserThread::IO, |
|
Daniel Erat
2014/09/30 23:55:24
can you move this into HistogramRecorder so it doe
|
| + FROM_HERE, base::Bind( |
| + &PowerUsageMonitor::HistogramRecorder::ReportBatteryLevelHistogram, |
| + histogram_recorder_->GetWeakPtr(), delay), delay); |
| + } |
| +} |
| + |
| +void PowerUsageMonitor::WallPowerConnected(double battery_level) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + // Cancel any in-progress ReportBatteryLevelHistogram() calls. |
| + histogram_recorder_->CancelPendingHistgramReports(); |
| + |
| + if (!start_discharge_time_.is_null()) { |
| + base::TimeDelta discharge_time = base::Time::Now() - start_discharge_time_; |
| + double discharge_percent = initial_battery_level_ - battery_level; |
| + double discharge_rate = discharge_percent / discharge_time.InMinutes(); |
| + |
| + if (discharge_time.InMinutes() > 30) |
|
Daniel Erat
2014/09/30 23:55:24
need curly brackets here
|
| + UMA_HISTOGRAM_PERCENTAGE("Power.BatteryDischargeRateWhenUnplugged", |
| + discharge_rate * 100.0); |
| + } |
| + |
| + was_on_battery_power_ = false; |
| + initial_battery_level_ = 0; |
| + current_battery_level_ = 0; |
| + start_discharge_time_ = base::Time(); |
| +} |
| + |
| +void PowerUsageMonitor::OnBatteryStatusUpdate( |
| + const blink::WebBatteryStatus& status) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + bool now_on_battery_power = (status.charging == 0); |
| + double battery_level = status.level; |
| + |
| + if (now_on_battery_power == was_on_battery_power_) { |
| + if (now_on_battery_power) |
| + current_battery_level_ = battery_level; |
| + return; |
| + } else if (now_on_battery_power) { // Wall power disconnected. |
| + DischargeStarted(battery_level); |
| + } else { // Wall power connected. |
| + WallPowerConnected(battery_level); |
| + } |
| +} |
| + |
| +void PowerUsageMonitor::BatteryLevelReported() { |
| + if (first_histogram_report_timestamp_.is_null()) { |
| + // Record timestamp of first histogram report. |
| + first_histogram_report_timestamp_ = base::Time::Now(); |
| + } |
| +} |
| + |
| +void PowerUsageMonitor::SetHistogramRecorderForTest( |
| + scoped_ptr<HistogramRecorder> recorder) { |
| + histogram_recorder_ = recorder.Pass(); |
| +} |
| + |
| +void PowerUsageMonitor::OnSuspend() { |
| + BrowserThread::PostTask(BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind( |
|
Daniel Erat
2014/09/30 23:55:24
fix weird indenting -- i recommend clang-format
|
| + &PowerUsageMonitor::CancelPendingHistogramRecordingOnIOThread, |
| + base::Unretained(this))); |
| +} |
| + |
| +void PowerUsageMonitor::Observe(int type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + RenderProcessHost* rph = |
| + Source<RenderProcessHost>(source).ptr(); |
| + |
| + size_t previous_num_live_renderers = live_renderers_.size(); |
| + |
| + if(type == NOTIFICATION_RENDERER_PROCESS_CREATED) { |
|
Daniel Erat
2014/09/30 23:55:24
nit: add a space between 'if' and '(' here and eve
|
| + live_renderers_.insert(rph->GetID()); |
| + } else if(type == NOTIFICATION_RENDERER_PROCESS_TERMINATED || |
| + type == NOTIFICATION_RENDERER_PROCESS_CLOSED) { |
| + live_renderers_.erase(rph->GetID()); |
| + } |
| + |
| + if (live_renderers_.size() == 0 && previous_num_live_renderers != 0) { |
| + // All render processes have died. |
| + BrowserThread::PostTask(BrowserThread::IO, |
| + FROM_HERE, |
|
Daniel Erat
2014/09/30 23:55:24
fix indenting
|
| + base::Bind( |
| + &PowerUsageMonitor::CancelPendingHistogramRecordingOnIOThread, |
| + base::Unretained(this))); |
| + } |
| +} |
| + |
| +void PowerUsageMonitor::CancelPendingHistogramRecordingOnIOThread() { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + // Cancel any in-progress histogram reports and reporting of discharge UMA. |
| + histogram_recorder_->CancelPendingHistgramReports(); |
| + start_discharge_time_ = base::Time(); |
| +} |
| + |
| +} // namespace content |