Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(933)

Side by Side Diff: chrome/browser/chromeos/policy/device_status_collector.h

Issue 2314813002: Refactored DeviceStatusCollector to enable truely asynchronous status queries (Closed)
Patch Set: Initialize *status_ in the C++11'y way Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_ 5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_ 6 #define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <deque> 10 #include <deque>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback_forward.h" 15 #include "base/callback_forward.h"
16 #include "base/callback_list.h" 16 #include "base/callback_list.h"
17 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/weak_ptr.h" 19 #include "base/memory/weak_ptr.h"
20 #include "base/sequenced_task_runner.h"
21 #include "base/task/cancelable_task_tracker.h" 21 #include "base/task/cancelable_task_tracker.h"
22 #include "base/threading/thread_checker.h"
22 #include "base/time/time.h" 23 #include "base/time/time.h"
23 #include "base/timer/timer.h" 24 #include "base/timer/timer.h"
25
24 #include "chrome/browser/chromeos/settings/cros_settings.h" 26 #include "chrome/browser/chromeos/settings/cros_settings.h"
25 #include "chromeos/system/version_loader.h" 27 #include "chromeos/system/version_loader.h"
26 #include "components/policy/proto/device_management_backend.pb.h" 28 #include "components/policy/proto/device_management_backend.pb.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "device/geolocation/geolocation_provider.h"
29 #include "device/geolocation/geoposition.h"
30 #include "ui/base/idle/idle.h" 29 #include "ui/base/idle/idle.h"
31 30
32 namespace chromeos { 31 namespace chromeos {
33 class CrosSettings; 32 class CrosSettings;
34 namespace system { 33 namespace system {
35 class StatisticsProvider; 34 class StatisticsProvider;
36 } 35 }
37 } 36 }
38 37
39 namespace content { 38 namespace content {
40 class NotificationDetails; 39 class NotificationDetails;
41 class NotificationSource; 40 class NotificationSource;
42 } 41 }
43 42
44 class PrefRegistrySimple; 43 class PrefRegistrySimple;
45 class PrefService; 44 class PrefService;
46 45
47 namespace policy { 46 namespace policy {
48 47
49 struct DeviceLocalAccount; 48 struct DeviceLocalAccount;
49 class GetStatusState;
50 50
51 // Collects and summarizes the status of an enterprised-managed ChromeOS device. 51 // Collects and summarizes the status of an enterprised-managed ChromeOS device.
52 class DeviceStatusCollector { 52 class DeviceStatusCollector {
53 public: 53 public:
54 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
55 // way to mock geolocation exists.
56 typedef base::Callback<void(
57 const device::GeolocationProvider::LocationUpdateCallback& callback)>
58 LocationUpdateRequester;
59
60 using VolumeInfoFetcher = base::Callback< 54 using VolumeInfoFetcher = base::Callback<
61 std::vector<enterprise_management::VolumeInfo>( 55 std::vector<enterprise_management::VolumeInfo>(
62 const std::vector<std::string>& mount_points)>; 56 const std::vector<std::string>& mount_points)>;
63 57
64 // Reads the first CPU line from /proc/stat. Returns an empty string if 58 // Reads the first CPU line from /proc/stat. Returns an empty string if
65 // the cpu data could not be read. Broken out into a callback to enable 59 // the cpu data could not be read. Broken out into a callback to enable
66 // mocking for tests. 60 // mocking for tests.
67 // 61 //
68 // The format of this line from /proc/stat is: 62 // The format of this line from /proc/stat is:
69 // cpu user_time nice_time system_time idle_time 63 // cpu user_time nice_time system_time idle_time
70 using CPUStatisticsFetcher = base::Callback<std::string(void)>; 64 using CPUStatisticsFetcher = base::Callback<std::string(void)>;
71 65
72 // Reads CPU temperatures from /sys/class/hwmon/hwmon*/temp*_input and 66 // Reads CPU temperatures from /sys/class/hwmon/hwmon*/temp*_input and
73 // appropriate labels from /sys/class/hwmon/hwmon*/temp*_label. 67 // appropriate labels from /sys/class/hwmon/hwmon*/temp*_label.
74 using CPUTempFetcher = 68 using CPUTempFetcher =
75 base::Callback<std::vector<enterprise_management::CPUTempInfo>()>; 69 base::Callback<std::vector<enterprise_management::CPUTempInfo>()>;
76 70
77 // Called in the UI thread after the device status has been collected 71 // Called in the UI thread after the device and session status have been
78 // asynchronously in GetDeviceStatusAsync. 72 // collected asynchronously in GetDeviceAndSessionStatusAsync. Null pointers
79 using DeviceStatusCallback = base::Callback<void( 73 // indicate errors or that device or session status reporting is disabled.
80 std::unique_ptr<enterprise_management::DeviceStatusReportRequest>)>; 74 using StatusCallback = base::Callback<void(
81 75 std::unique_ptr<enterprise_management::DeviceStatusReportRequest>,
82 // Called in the UI thread after the device session status has been collected
83 // asynchronously in GetDeviceSessionStatusAsync.
84 using DeviceSessionStatusCallback = base::Callback<void(
85 std::unique_ptr<enterprise_management::SessionStatusReportRequest>)>; 76 std::unique_ptr<enterprise_management::SessionStatusReportRequest>)>;
86 77
87 // Constructor. Callers can inject their own VolumeInfoFetcher, 78 // Constructor. Callers can inject their own VolumeInfoFetcher,
88 // CPUStatisticsFetcher and CPUTempFetcher. These callbacks are executed on 79 // CPUStatisticsFetcher and CPUTempFetcher. These callbacks are executed on
89 // Blocking Pool. A null callback can be passed for either parameter, to use 80 // Blocking Pool. A null callback can be passed for either parameter, to use
90 // the default implementation. 81 // the default implementation.
91 DeviceStatusCollector( 82 DeviceStatusCollector(
92 PrefService* local_state, 83 PrefService* local_state,
93 chromeos::system::StatisticsProvider* provider, 84 chromeos::system::StatisticsProvider* provider,
94 const LocationUpdateRequester& location_update_requester,
95 const VolumeInfoFetcher& volume_info_fetcher, 85 const VolumeInfoFetcher& volume_info_fetcher,
96 const CPUStatisticsFetcher& cpu_statistics_fetcher, 86 const CPUStatisticsFetcher& cpu_statistics_fetcher,
97 const CPUTempFetcher& cpu_temp_fetcher); 87 const CPUTempFetcher& cpu_temp_fetcher);
98 virtual ~DeviceStatusCollector(); 88 virtual ~DeviceStatusCollector();
99 89
100 // Gathers device status information and calls the passed response callback. 90 // Gathers device and session status information and calls the passed response
101 // A null pointer passed into the response indicates an error or that 91 // callback. Null pointers passed into the response indicate errors or that
102 // device status reporting is disabled. 92 // device or session status reporting is disabled.
103 virtual void GetDeviceStatusAsync( 93 virtual void GetDeviceAndSessionStatusAsync(const StatusCallback& response);
104 const DeviceStatusCallback& response);
105
106 // Gathers device session status information and calls the passed response
107 // callback. A null pointer passed into the response indicates an error or
108 // that device session status reporting is disabled or that the active
109 // session is not a kiosk session.
110 virtual void GetDeviceSessionStatusAsync(
111 const DeviceSessionStatusCallback& response);
112 94
113 // Called after the status information has successfully been submitted to 95 // Called after the status information has successfully been submitted to
114 // the server. 96 // the server.
115 void OnSubmittedSuccessfully(); 97 void OnSubmittedSuccessfully();
116 98
117 static void RegisterPrefs(PrefRegistrySimple* registry); 99 static void RegisterPrefs(PrefRegistrySimple* registry);
118 100
119 // Returns the DeviceLocalAccount associated with the currently active 101 // Returns the DeviceLocalAccount associated with the currently active
120 // kiosk session, if the session was auto-launched with zero delay 102 // kiosk session, if the session was auto-launched with zero delay
121 // (this enables functionality such as network reporting). 103 // (this enables functionality such as network reporting).
(...skipping 12 matching lines...) Expand all
134 116
135 // Used instead of base::Time::Now(), to make testing possible. 117 // Used instead of base::Time::Now(), to make testing possible.
136 virtual base::Time GetCurrentTime(); 118 virtual base::Time GetCurrentTime();
137 119
138 // Callback which receives the results of the idle state check. 120 // Callback which receives the results of the idle state check.
139 void IdleStateCallback(ui::IdleState state); 121 void IdleStateCallback(ui::IdleState state);
140 122
141 // Gets the version of the passed app. Virtual to allow mocking. 123 // Gets the version of the passed app. Virtual to allow mocking.
142 virtual std::string GetAppVersion(const std::string& app_id); 124 virtual std::string GetAppVersion(const std::string& app_id);
143 125
144 // Samples the current hardware status to be sent up with the next device 126 // Samples the current hardware resource usage to be sent up with the
145 // status update. 127 // next device status update.
146 void SampleHardwareStatus(); 128 void SampleResourceUsage();
147 129
148 // The number of days in the past to store device activity. 130 // The number of days in the past to store device activity.
149 // This is kept in case device status uploads fail for a number of days. 131 // This is kept in case device status uploads fail for a number of days.
150 unsigned int max_stored_past_activity_days_; 132 unsigned int max_stored_past_activity_days_;
151 133
152 // The number of days in the future to store device activity. 134 // The number of days in the future to store device activity.
153 // When changing the system time and/or timezones, it's possible to record 135 // When changing the system time and/or timezones, it's possible to record
154 // activity time that is slightly in the future. 136 // activity time that is slightly in the future.
155 unsigned int max_stored_future_activity_days_; 137 unsigned int max_stored_future_activity_days_;
156 138
157 private: 139 private:
158 // Prevents the local store of activity periods from growing too large by 140 // Prevents the local store of activity periods from growing too large by
159 // removing entries that are outside the reporting window. 141 // removing entries that are outside the reporting window.
160 void PruneStoredActivityPeriods(base::Time base_time); 142 void PruneStoredActivityPeriods(base::Time base_time);
161 143
162 // Trims the store activity periods to only retain data within the 144 // Trims the store activity periods to only retain data within the
163 // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be 145 // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be
164 // adjusted by subtracting |min_day_trim_duration|. 146 // adjusted by subtracting |min_day_trim_duration|.
165 void TrimStoredActivityPeriods(int64_t min_day_key, 147 void TrimStoredActivityPeriods(int64_t min_day_key,
166 int min_day_trim_duration, 148 int min_day_trim_duration,
167 int64_t max_day_key); 149 int64_t max_day_key);
168 150
169 void AddActivePeriod(base::Time start, base::Time end); 151 void AddActivePeriod(base::Time start, base::Time end);
170 152
171 // Clears the cached hardware status. 153 // Clears the cached hardware resource usage.
172 void ClearCachedHardwareStatus(); 154 void ClearCachedResourceUsage();
173 155
174 // Callbacks from chromeos::VersionLoader. 156 // Callbacks from chromeos::VersionLoader.
175 void OnOSVersion(const std::string& version); 157 void OnOSVersion(const std::string& version);
176 void OnOSFirmware(const std::string& version); 158 void OnOSFirmware(const std::string& version);
177 159
178 // Helpers for the various portions of the status. Return true if they 160 void GetDeviceStatus(scoped_refptr<GetStatusState> state);
179 // actually report any status. 161 void GetSessionStatus(scoped_refptr<GetStatusState> state);
162
163 // Helpers for the various portions of DEVICE STATUS. Return true if they
164 // actually report any status. Functions that queue async queries take
165 // a |GetStatusState| instance.
180 bool GetActivityTimes( 166 bool GetActivityTimes(
181 enterprise_management::DeviceStatusReportRequest* request); 167 enterprise_management::DeviceStatusReportRequest* status);
182 bool GetVersionInfo( 168 bool GetVersionInfo(enterprise_management::DeviceStatusReportRequest* status);
183 enterprise_management::DeviceStatusReportRequest* request); 169 bool GetBootMode(enterprise_management::DeviceStatusReportRequest* status);
184 bool GetBootMode(
185 enterprise_management::DeviceStatusReportRequest* request);
186 bool GetLocation(
187 enterprise_management::DeviceStatusReportRequest* request);
188 bool GetNetworkInterfaces( 170 bool GetNetworkInterfaces(
189 enterprise_management::DeviceStatusReportRequest* request); 171 enterprise_management::DeviceStatusReportRequest* status);
190 bool GetUsers( 172 bool GetUsers(enterprise_management::DeviceStatusReportRequest* status);
191 enterprise_management::DeviceStatusReportRequest* request);
192 bool GetHardwareStatus( 173 bool GetHardwareStatus(
193 enterprise_management::DeviceStatusReportRequest* request); 174 enterprise_management::DeviceStatusReportRequest* status,
175 scoped_refptr<GetStatusState> state); // Queues async queries!
194 bool GetOsUpdateStatus( 176 bool GetOsUpdateStatus(
195 enterprise_management::DeviceStatusReportRequest* request); 177 enterprise_management::DeviceStatusReportRequest* status);
196 bool GetRunningKioskApp( 178 bool GetRunningKioskApp(
197 enterprise_management::DeviceStatusReportRequest* request); 179 enterprise_management::DeviceStatusReportRequest* status);
180
181 // Helpers for the various portions of SESSION STATUS. Return true if they
182 // actually report any status. Functions that queue async queries take
183 // a |GetStatusState| instance.
184 bool GetAccountStatus(
185 enterprise_management::SessionStatusReportRequest* status);
198 186
199 // Update the cached values of the reporting settings. 187 // Update the cached values of the reporting settings.
200 void UpdateReportingSettings(); 188 void UpdateReportingSettings();
201 189
202 void ScheduleGeolocationUpdateRequest();
203
204 // device::GeolocationUpdateCallback implementation.
205 void ReceiveGeolocationUpdate(const device::Geoposition&);
206
207 // Callback invoked to update our cached disk information.
208 void ReceiveVolumeInfo(
209 const std::vector<enterprise_management::VolumeInfo>& info);
210
211 // Callback invoked to update our cpu usage information. 190 // Callback invoked to update our cpu usage information.
212 void ReceiveCPUStatistics(const std::string& statistics); 191 void ReceiveCPUStatistics(const std::string& statistics);
213 192
214 // Callback invoked to update our CPU temp information.
215 void StoreCPUTempInfo(
216 const std::vector<enterprise_management::CPUTempInfo>& info);
217
218 PrefService* const local_state_; 193 PrefService* const local_state_;
219 194
220 // The last time an idle state check was performed. 195 // The last time an idle state check was performed.
221 base::Time last_idle_check_; 196 base::Time last_idle_check_;
222 197
223 // The maximum key that went into the last report generated by 198 // The maximum key that went into the last report generated by
224 // GetDeviceStatusAsync(), and the duration for it. This is used to trim 199 // GetDeviceStatusAsync(), and the duration for it. This is used to trim
225 // the stored data in OnSubmittedSuccessfully(). Trimming is delayed so 200 // the stored data in OnSubmittedSuccessfully(). Trimming is delayed so
226 // unsuccessful uploads don't result in dropped data. 201 // unsuccessful uploads don't result in dropped data.
227 int64_t last_reported_day_ = 0; 202 int64_t last_reported_day_ = 0;
228 int duration_for_last_reported_day_ = 0; 203 int duration_for_last_reported_day_ = 0;
229 204
230 // Whether a geolocation update is currently in progress.
231 bool geolocation_update_in_progress_ = false;
232
233 base::RepeatingTimer idle_poll_timer_; 205 base::RepeatingTimer idle_poll_timer_;
234 base::RepeatingTimer hardware_status_sampling_timer_; 206 base::RepeatingTimer resource_usage_sampling_timer_;
235 base::OneShotTimer geolocation_update_timer_;
236 207
237 std::string os_version_; 208 std::string os_version_;
238 std::string firmware_version_; 209 std::string firmware_version_;
239 210
240 device::Geoposition position_;
241
242 // Cached disk volume information.
243 std::vector<enterprise_management::VolumeInfo> volume_info_;
244
245 // Cached CPU temp information.
246 std::vector<enterprise_management::CPUTempInfo> cpu_temp_info_;
247
248 struct ResourceUsage { 211 struct ResourceUsage {
249 // Sample of percentage-of-CPU-used. 212 // Sample of percentage-of-CPU-used.
250 int cpu_usage_percent; 213 int cpu_usage_percent;
251 214
252 // Amount of free RAM (measures raw memory used by processes, not internal 215 // Amount of free RAM (measures raw memory used by processes, not internal
253 // memory waiting to be reclaimed by GC). 216 // memory waiting to be reclaimed by GC).
254 int64_t bytes_of_ram_free; 217 int64_t bytes_of_ram_free;
255 }; 218 };
256 219
257 // Samples of resource usage (contains multiple samples taken 220 // Samples of resource usage (contains multiple samples taken
(...skipping 10 matching lines...) Expand all
268 CPUTempFetcher cpu_temp_fetcher_; 231 CPUTempFetcher cpu_temp_fetcher_;
269 232
270 chromeos::system::StatisticsProvider* const statistics_provider_; 233 chromeos::system::StatisticsProvider* const statistics_provider_;
271 234
272 chromeos::CrosSettings* const cros_settings_; 235 chromeos::CrosSettings* const cros_settings_;
273 236
274 // The most recent CPU readings. 237 // The most recent CPU readings.
275 uint64_t last_cpu_active_ = 0; 238 uint64_t last_cpu_active_ = 0;
276 uint64_t last_cpu_idle_ = 0; 239 uint64_t last_cpu_idle_ = 0;
277 240
278 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
279 // way to mock geolocation exists.
280 LocationUpdateRequester location_update_requester_;
281
282 std::unique_ptr<device::GeolocationProvider::Subscription>
283 geolocation_subscription_;
284
285 // Cached values of the reporting settings from the device policy. 241 // Cached values of the reporting settings from the device policy.
286 bool report_version_info_ = false; 242 bool report_version_info_ = false;
287 bool report_activity_times_ = false; 243 bool report_activity_times_ = false;
288 bool report_boot_mode_ = false; 244 bool report_boot_mode_ = false;
289 bool report_location_ = false;
290 bool report_network_interfaces_ = false; 245 bool report_network_interfaces_ = false;
291 bool report_users_ = false; 246 bool report_users_ = false;
292 bool report_hardware_status_ = false; 247 bool report_hardware_status_ = false;
293 bool report_session_status_ = false; 248 bool report_session_status_ = false;
294 bool report_os_update_status_ = false; 249 bool report_os_update_status_ = false;
295 bool report_running_kiosk_app_ = false; 250 bool report_running_kiosk_app_ = false;
296 251
297 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 252 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
298 version_info_subscription_; 253 version_info_subscription_;
299 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 254 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
300 activity_times_subscription_; 255 activity_times_subscription_;
301 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 256 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
302 boot_mode_subscription_; 257 boot_mode_subscription_;
303 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 258 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
304 location_subscription_;
305 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
306 network_interfaces_subscription_; 259 network_interfaces_subscription_;
307 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 260 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
308 users_subscription_; 261 users_subscription_;
309 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 262 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
310 hardware_status_subscription_; 263 hardware_status_subscription_;
311 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 264 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
312 session_status_subscription_; 265 session_status_subscription_;
313 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 266 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
314 os_update_status_subscription_; 267 os_update_status_subscription_;
315 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 268 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
316 running_kiosk_app_subscription_; 269 running_kiosk_app_subscription_;
317 270
318 content::BrowserThread::ID creation_thread_; 271 // Task runner in the creation thread where responses are sent to.
272 scoped_refptr<base::SequencedTaskRunner> task_runner_;
273 base::ThreadChecker thread_checker_;
319 274
320 base::WeakPtrFactory<DeviceStatusCollector> weak_factory_; 275 base::WeakPtrFactory<DeviceStatusCollector> weak_factory_;
321 276
322 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector); 277 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector);
323 }; 278 };
324 279
325 } // namespace policy 280 } // namespace policy
326 281
327 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_ 282 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698