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

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: Rebase Created 4 years, 3 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 GetStatusAsync.
Andrew T Wilson (Slow) 2016/09/26 16:33:42 Is GetStatusAsync() even a function any more?
ljusten (tachyonic) 2016/09/27 16:53:55 Thanks, should be GetDeviceAndSessionStatusAsync.
79 using DeviceStatusCallback = base::Callback<void( 73 using StatusCallback = base::Callback<void(
80 std::unique_ptr<enterprise_management::DeviceStatusReportRequest>)>; 74 std::unique_ptr<enterprise_management::DeviceStatusReportRequest>,
81
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>)>; 75 std::unique_ptr<enterprise_management::SessionStatusReportRequest>)>;
86 76
87 // Constructor. Callers can inject their own VolumeInfoFetcher, 77 // Constructor. Callers can inject their own VolumeInfoFetcher,
88 // CPUStatisticsFetcher and CPUTempFetcher. These callbacks are executed on 78 // CPUStatisticsFetcher and CPUTempFetcher. These callbacks are executed on
89 // Blocking Pool. A null callback can be passed for either parameter, to use 79 // Blocking Pool. A null callback can be passed for either parameter, to use
90 // the default implementation. 80 // the default implementation.
91 DeviceStatusCollector( 81 DeviceStatusCollector(
92 PrefService* local_state, 82 PrefService* local_state,
93 chromeos::system::StatisticsProvider* provider, 83 chromeos::system::StatisticsProvider* provider,
94 const LocationUpdateRequester& location_update_requester,
95 const VolumeInfoFetcher& volume_info_fetcher, 84 const VolumeInfoFetcher& volume_info_fetcher,
96 const CPUStatisticsFetcher& cpu_statistics_fetcher, 85 const CPUStatisticsFetcher& cpu_statistics_fetcher,
97 const CPUTempFetcher& cpu_temp_fetcher); 86 const CPUTempFetcher& cpu_temp_fetcher);
98 virtual ~DeviceStatusCollector(); 87 virtual ~DeviceStatusCollector();
99 88
100 // Gathers device status information and calls the passed response callback. 89 // Gathers device and session status information and calls the passed response
101 // A null pointer passed into the response indicates an error or that 90 // callback. Null pointers passed into the response indicate errors or that
Andrew T Wilson (Slow) 2016/09/26 16:33:42 I think you should document the meaning of null po
ljusten (tachyonic) 2016/09/27 16:53:55 Done.
102 // device status reporting is disabled. 91 // device or session status reporting is disabled.
103 virtual void GetDeviceStatusAsync( 92 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 93
113 // Called after the status information has successfully been submitted to 94 // Called after the status information has successfully been submitted to
114 // the server. 95 // the server.
115 void OnSubmittedSuccessfully(); 96 void OnSubmittedSuccessfully();
116 97
117 static void RegisterPrefs(PrefRegistrySimple* registry); 98 static void RegisterPrefs(PrefRegistrySimple* registry);
118 99
119 // Returns the DeviceLocalAccount associated with the currently active 100 // Returns the DeviceLocalAccount associated with the currently active
120 // kiosk session, if the session was auto-launched with zero delay 101 // kiosk session, if the session was auto-launched with zero delay
121 // (this enables functionality such as network reporting). 102 // (this enables functionality such as network reporting).
(...skipping 12 matching lines...) Expand all
134 115
135 // Used instead of base::Time::Now(), to make testing possible. 116 // Used instead of base::Time::Now(), to make testing possible.
136 virtual base::Time GetCurrentTime(); 117 virtual base::Time GetCurrentTime();
137 118
138 // Callback which receives the results of the idle state check. 119 // Callback which receives the results of the idle state check.
139 void IdleStateCallback(ui::IdleState state); 120 void IdleStateCallback(ui::IdleState state);
140 121
141 // Gets the version of the passed app. Virtual to allow mocking. 122 // Gets the version of the passed app. Virtual to allow mocking.
142 virtual std::string GetAppVersion(const std::string& app_id); 123 virtual std::string GetAppVersion(const std::string& app_id);
143 124
144 // Samples the current hardware status to be sent up with the next device 125 // Samples the current hardware resource usage to be sent up with the
145 // status update. 126 // next device status update.
146 void SampleHardwareStatus(); 127 void SampleResourceUsage();
147 128
148 // The number of days in the past to store device activity. 129 // 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. 130 // This is kept in case device status uploads fail for a number of days.
150 unsigned int max_stored_past_activity_days_; 131 unsigned int max_stored_past_activity_days_;
151 132
152 // The number of days in the future to store device activity. 133 // 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 134 // When changing the system time and/or timezones, it's possible to record
154 // activity time that is slightly in the future. 135 // activity time that is slightly in the future.
155 unsigned int max_stored_future_activity_days_; 136 unsigned int max_stored_future_activity_days_;
156 137
157 private: 138 private:
158 // Prevents the local store of activity periods from growing too large by 139 // Prevents the local store of activity periods from growing too large by
159 // removing entries that are outside the reporting window. 140 // removing entries that are outside the reporting window.
160 void PruneStoredActivityPeriods(base::Time base_time); 141 void PruneStoredActivityPeriods(base::Time base_time);
161 142
162 // Trims the store activity periods to only retain data within the 143 // 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 144 // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be
164 // adjusted by subtracting |min_day_trim_duration|. 145 // adjusted by subtracting |min_day_trim_duration|.
165 void TrimStoredActivityPeriods(int64_t min_day_key, 146 void TrimStoredActivityPeriods(int64_t min_day_key,
166 int min_day_trim_duration, 147 int min_day_trim_duration,
167 int64_t max_day_key); 148 int64_t max_day_key);
168 149
169 void AddActivePeriod(base::Time start, base::Time end); 150 void AddActivePeriod(base::Time start, base::Time end);
170 151
171 // Clears the cached hardware status. 152 // Clears the cached hardware resource usage.
172 void ClearCachedHardwareStatus(); 153 void ClearCachedResourceUsage();
173 154
174 // Callbacks from chromeos::VersionLoader. 155 // Callbacks from chromeos::VersionLoader.
175 void OnOSVersion(const std::string& version); 156 void OnOSVersion(const std::string& version);
176 void OnOSFirmware(const std::string& version); 157 void OnOSFirmware(const std::string& version);
177 158
178 // Helpers for the various portions of the status. Return true if they 159 void GetDeviceStatus(scoped_refptr<GetStatusState> state);
179 // actually report any status. 160 void GetSessionStatus(scoped_refptr<GetStatusState> state);
161
162 // Helpers for the various portions of DEVICE STATUS. Return true if they
163 // actually report any status. Functions that queue async queries take
164 // a |GetStatusState| instance.
180 bool GetActivityTimes( 165 bool GetActivityTimes(
181 enterprise_management::DeviceStatusReportRequest* request); 166 enterprise_management::DeviceStatusReportRequest* status);
182 bool GetVersionInfo( 167 bool GetVersionInfo(enterprise_management::DeviceStatusReportRequest* status);
183 enterprise_management::DeviceStatusReportRequest* request); 168 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( 169 bool GetNetworkInterfaces(
189 enterprise_management::DeviceStatusReportRequest* request); 170 enterprise_management::DeviceStatusReportRequest* status);
190 bool GetUsers( 171 bool GetUsers(enterprise_management::DeviceStatusReportRequest* status);
191 enterprise_management::DeviceStatusReportRequest* request);
192 bool GetHardwareStatus( 172 bool GetHardwareStatus(
193 enterprise_management::DeviceStatusReportRequest* request); 173 enterprise_management::DeviceStatusReportRequest* status,
174 scoped_refptr<GetStatusState> state); // Queues async queries!
194 bool GetOsUpdateStatus( 175 bool GetOsUpdateStatus(
195 enterprise_management::DeviceStatusReportRequest* request); 176 enterprise_management::DeviceStatusReportRequest* status);
196 bool GetRunningKioskApp( 177 bool GetRunningKioskApp(
197 enterprise_management::DeviceStatusReportRequest* request); 178 enterprise_management::DeviceStatusReportRequest* status);
179
180 // Helpers for the various portions of SESSION STATUS. Return true if they
181 // actually report any status. Functions that queue async queries take
182 // a |GetStatusState| instance.
183 bool GetAccountStatus(
184 enterprise_management::SessionStatusReportRequest* status);
198 185
199 // Update the cached values of the reporting settings. 186 // Update the cached values of the reporting settings.
200 void UpdateReportingSettings(); 187 void UpdateReportingSettings();
201 188
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. 189 // Callback invoked to update our cpu usage information.
212 void ReceiveCPUStatistics(const std::string& statistics); 190 void ReceiveCPUStatistics(const std::string& statistics);
213 191
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_; 192 PrefService* const local_state_;
219 193
220 // The last time an idle state check was performed. 194 // The last time an idle state check was performed.
221 base::Time last_idle_check_; 195 base::Time last_idle_check_;
222 196
223 // The maximum key that went into the last report generated by 197 // The maximum key that went into the last report generated by
224 // GetDeviceStatusAsync(), and the duration for it. This is used to trim 198 // GetDeviceStatusAsync(), and the duration for it. This is used to trim
225 // the stored data in OnSubmittedSuccessfully(). Trimming is delayed so 199 // the stored data in OnSubmittedSuccessfully(). Trimming is delayed so
226 // unsuccessful uploads don't result in dropped data. 200 // unsuccessful uploads don't result in dropped data.
227 int64_t last_reported_day_ = 0; 201 int64_t last_reported_day_ = 0;
228 int duration_for_last_reported_day_ = 0; 202 int duration_for_last_reported_day_ = 0;
229 203
230 // Whether a geolocation update is currently in progress.
231 bool geolocation_update_in_progress_ = false;
232
233 base::RepeatingTimer idle_poll_timer_; 204 base::RepeatingTimer idle_poll_timer_;
234 base::RepeatingTimer hardware_status_sampling_timer_; 205 base::RepeatingTimer resource_usage_sampling_timer_;
235 base::OneShotTimer geolocation_update_timer_;
236 206
237 std::string os_version_; 207 std::string os_version_;
238 std::string firmware_version_; 208 std::string firmware_version_;
239 209
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 { 210 struct ResourceUsage {
249 // Sample of percentage-of-CPU-used. 211 // Sample of percentage-of-CPU-used.
250 int cpu_usage_percent; 212 int cpu_usage_percent;
251 213
252 // Amount of free RAM (measures raw memory used by processes, not internal 214 // Amount of free RAM (measures raw memory used by processes, not internal
253 // memory waiting to be reclaimed by GC). 215 // memory waiting to be reclaimed by GC).
254 int64_t bytes_of_ram_free; 216 int64_t bytes_of_ram_free;
255 }; 217 };
256 218
257 // Samples of resource usage (contains multiple samples taken 219 // Samples of resource usage (contains multiple samples taken
(...skipping 10 matching lines...) Expand all
268 CPUTempFetcher cpu_temp_fetcher_; 230 CPUTempFetcher cpu_temp_fetcher_;
269 231
270 chromeos::system::StatisticsProvider* const statistics_provider_; 232 chromeos::system::StatisticsProvider* const statistics_provider_;
271 233
272 chromeos::CrosSettings* const cros_settings_; 234 chromeos::CrosSettings* const cros_settings_;
273 235
274 // The most recent CPU readings. 236 // The most recent CPU readings.
275 uint64_t last_cpu_active_ = 0; 237 uint64_t last_cpu_active_ = 0;
276 uint64_t last_cpu_idle_ = 0; 238 uint64_t last_cpu_idle_ = 0;
277 239
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. 240 // Cached values of the reporting settings from the device policy.
286 bool report_version_info_ = false; 241 bool report_version_info_ = false;
287 bool report_activity_times_ = false; 242 bool report_activity_times_ = false;
288 bool report_boot_mode_ = false; 243 bool report_boot_mode_ = false;
289 bool report_location_ = false;
290 bool report_network_interfaces_ = false; 244 bool report_network_interfaces_ = false;
291 bool report_users_ = false; 245 bool report_users_ = false;
292 bool report_hardware_status_ = false; 246 bool report_hardware_status_ = false;
293 bool report_session_status_ = false; 247 bool report_session_status_ = false;
294 bool report_os_update_status_ = false; 248 bool report_os_update_status_ = false;
295 bool report_running_kiosk_app_ = false; 249 bool report_running_kiosk_app_ = false;
296 250
297 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 251 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
298 version_info_subscription_; 252 version_info_subscription_;
299 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 253 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
300 activity_times_subscription_; 254 activity_times_subscription_;
301 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 255 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
302 boot_mode_subscription_; 256 boot_mode_subscription_;
303 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 257 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
304 location_subscription_;
305 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
306 network_interfaces_subscription_; 258 network_interfaces_subscription_;
307 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 259 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
308 users_subscription_; 260 users_subscription_;
309 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 261 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
310 hardware_status_subscription_; 262 hardware_status_subscription_;
311 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 263 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
312 session_status_subscription_; 264 session_status_subscription_;
313 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 265 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
314 os_update_status_subscription_; 266 os_update_status_subscription_;
315 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription> 267 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
316 running_kiosk_app_subscription_; 268 running_kiosk_app_subscription_;
317 269
318 content::BrowserThread::ID creation_thread_; 270 // Task runner in the creation thread where responses are sent to.
271 scoped_refptr<base::SequencedTaskRunner> task_runner_;
272 base::ThreadChecker thread_checker_;
319 273
320 base::WeakPtrFactory<DeviceStatusCollector> weak_factory_; 274 base::WeakPtrFactory<DeviceStatusCollector> weak_factory_;
321 275
322 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector); 276 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector);
323 }; 277 };
324 278
325 } // namespace policy 279 } // namespace policy
326 280
327 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_ 281 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698