Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / power / process_power_collector.h
blob7976e8374779fcd51fe642f7b4ed7cfd485d8fe5
1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_
6 #define CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_
8 #include <map>
10 #include "base/memory/linked_ptr.h"
11 #include "base/process/process_handle.h"
12 #include "base/process/process_metrics.h"
13 #include "base/timer/timer.h"
14 #include "components/power/origin_power_map_factory.h"
15 #include "url/gurl.h"
17 #if defined(OS_CHROMEOS)
18 #include "chromeos/dbus/power_manager_client.h"
19 #endif
21 class Profile;
23 namespace content {
24 class RenderProcessHost;
27 #if defined(OS_CHROMEOS)
28 namespace power_manager {
29 class PowerSupplyProperties;
31 #endif
33 // Manages regular updates of the profile power consumption.
34 class ProcessPowerCollector
35 #if defined(OS_CHROMEOS)
36 : public chromeos::PowerManagerClient::Observer
37 #endif
39 public:
40 class PerProcessData {
41 public:
42 PerProcessData(scoped_ptr<base::ProcessMetrics> metrics,
43 const GURL& origin,
44 Profile* profile);
45 PerProcessData();
46 ~PerProcessData();
48 base::ProcessMetrics* metrics() const { return metrics_.get(); }
49 Profile* profile() const { return profile_; }
50 GURL last_origin() const { return last_origin_; }
51 int last_cpu_percent() const { return last_cpu_percent_; }
52 bool seen_this_cycle() const { return seen_this_cycle_; }
53 void set_last_cpu_percent(double new_cpu) { last_cpu_percent_ = new_cpu; }
54 void set_seen_this_cycle(bool seen) { seen_this_cycle_ = seen; }
56 private:
57 // |metrics_| holds the ProcessMetrics information for the given process.
58 scoped_ptr<base::ProcessMetrics> metrics_;
60 // |profile| is the profile that is visiting the |last_origin_|.
61 // It is not owned by PerProcessData.
62 Profile* profile_;
64 // |last_origin_| is the last origin visited by the process.
65 GURL last_origin_;
67 // |last_cpu_percent_| is the proportion of the CPU used since the last
68 // query.
69 double last_cpu_percent_;
71 // |seen_this_cycle| represents if the process still exists in this cycle.
72 // If it doesn't, we erase the PerProcessData.
73 bool seen_this_cycle_;
75 DISALLOW_COPY_AND_ASSIGN(PerProcessData);
78 // A map from all process handles to a metric.
79 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> >
80 ProcessMetricsMap;
81 // A callback used to define mock CPU usage for testing.
82 typedef base::Callback<double(base::ProcessHandle)> CpuUsageCallback;
84 // On Chrome OS, can only be initialized after the DBusThreadManager has been
85 // initialized.
86 ProcessPowerCollector();
87 #if defined(OS_CHROMEOS)
88 // On Chrome OS, can only be destroyed before DBusThreadManager is.
89 ~ProcessPowerCollector() override;
90 #else
91 virtual ~ProcessPowerCollector();
92 #endif
94 void set_cpu_usage_callback_for_testing(const CpuUsageCallback& callback) {
95 cpu_usage_callback_ = callback;
98 ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; }
100 #if defined(OS_CHROMEOS)
101 // PowerManagerClient::Observer implementation:
102 void PowerChanged(const power_manager::PowerSupplyProperties& prop) override;
103 #endif
105 // Begin periodically updating the power consumption numbers by profile.
106 void Initialize();
108 // Calls UpdatePowerConsumption() and returns the total CPU percent.
109 double UpdatePowerConsumptionForTesting();
111 private:
112 // Starts the timer for updating the power consumption.
113 void StartTimer();
115 // Calls SynchronizerProcesses() and RecordCpuUsageByOrigin() to update the
116 // |metrics_map_| and attribute power consumption. Invoked by |timer_| and as
117 // a helper method for UpdatePowerConsumptionForTesting().
118 double UpdatePowerConsumption();
120 // Calls UpdatePowerConsumption(). Invoked by |timer_|.
121 void HandleUpdateTimeout();
123 // Synchronizes the currently active processes to the |metrics_map_| and
124 // returns the total amount of cpu usage in the cycle.
125 double SynchronizeProcesses();
127 // Attributes the power usage to the profiles and origins using the
128 // information from SynchronizeProcesses() given a total amount
129 // of CPU used in this cycle, |total_cpu_percent|.
130 void RecordCpuUsageByOrigin(double total_cpu_percent);
132 // Adds the information from a given RenderProcessHost to the |metrics_map_|
133 // for a given origin. Called by SynchronizeProcesses().
134 void UpdateProcessInMap(const content::RenderProcessHost* render_process,
135 const GURL& origin);
137 ProcessMetricsMap metrics_map_;
138 base::RepeatingTimer<ProcessPowerCollector> timer_;
140 // Callback to use to get CPU usage if set.
141 CpuUsageCallback cpu_usage_callback_;
143 // The factor to scale the CPU usage by.
144 double scale_factor_;
146 DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector);
149 #endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_