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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
12 #include "base/basictypes.h"
13 #include "base/callback_forward.h"
14 #include "base/callback_list.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/task/cancelable_task_tracker.h"
20 #include "base/time/time.h"
21 #include "base/timer/timer.h"
22 #include "chrome/browser/chromeos/settings/cros_settings.h"
23 #include "chromeos/system/version_loader.h"
24 #include "content/public/browser/geolocation_provider.h"
25 #include "content/public/common/geoposition.h"
26 #include "policy/proto/device_management_backend.pb.h"
27 #include "ui/base/idle/idle.h"
32 class StatisticsProvider
;
37 class NotificationDetails
;
38 class NotificationSource
;
41 class PrefRegistrySimple
;
46 struct DeviceLocalAccount
;
48 // Collects and summarizes the status of an enterprised-managed ChromeOS device.
49 class DeviceStatusCollector
{
51 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
52 // way to mock geolocation exists.
53 typedef base::Callback
<void(
54 const content::GeolocationProvider::LocationUpdateCallback
& callback
)>
55 LocationUpdateRequester
;
57 using VolumeInfoFetcher
= base::Callback
<
58 std::vector
<enterprise_management::VolumeInfo
>(
59 const std::vector
<std::string
>& mount_points
)>;
61 // Reads the first CPU line from /proc/stat. Returns an empty string if
62 // the cpu data could not be read. Broken out into a callback to enable
65 // The format of this line from /proc/stat is:
66 // cpu user_time nice_time system_time idle_time
67 using CPUStatisticsFetcher
= base::Callback
<std::string(void)>;
69 // Reads CPU temperatures from /sys/class/hwmon/hwmon*/temp*_input and
70 // appropriate labels from /sys/class/hwmon/hwmon*/temp*_label.
71 using CPUTempFetcher
=
72 base::Callback
<std::vector
<enterprise_management::CPUTempInfo
>()>;
74 // Constructor. Callers can inject their own VolumeInfoFetcher,
75 // CPUStatisticsFetcher and CPUTempFetcher. These callbacks are executed on
76 // Blocking Pool. A null callback can be passed for either parameter, to use
77 // the default implementation.
78 DeviceStatusCollector(
79 PrefService
* local_state
,
80 chromeos::system::StatisticsProvider
* provider
,
81 const LocationUpdateRequester
& location_update_requester
,
82 const VolumeInfoFetcher
& volume_info_fetcher
,
83 const CPUStatisticsFetcher
& cpu_statistics_fetcher
,
84 const CPUTempFetcher
& cpu_temp_fetcher
);
85 virtual ~DeviceStatusCollector();
87 // Fills in the passed proto with device status information. Will return
88 // false if no status information is filled in (because status reporting
90 virtual bool GetDeviceStatus(
91 enterprise_management::DeviceStatusReportRequest
* status
);
93 // Fills in the passed proto with session status information. Will return
94 // false if no status information is filled in (because status reporting
95 // is disabled, or because the active session is not a kiosk session).
96 virtual bool GetDeviceSessionStatus(
97 enterprise_management::SessionStatusReportRequest
* status
);
99 // Called after the status information has successfully been submitted to
101 void OnSubmittedSuccessfully();
103 static void RegisterPrefs(PrefRegistrySimple
* registry
);
105 // Returns the DeviceLocalAccount associated with the currently active
106 // kiosk session, if the session was auto-launched with zero delay
107 // (this enables functionality such as network reporting).
108 // Virtual to allow mocking.
109 virtual scoped_ptr
<DeviceLocalAccount
> GetAutoLaunchedKioskSessionInfo();
111 // How often, in seconds, to poll to see if the user is idle.
112 static const unsigned int kIdlePollIntervalSeconds
= 30;
114 // The total number of hardware resource usage samples cached internally.
115 static const unsigned int kMaxResourceUsageSamples
= 10;
118 // Check whether the user has been idle for a certain period of time.
119 virtual void CheckIdleState();
121 // Used instead of base::Time::Now(), to make testing possible.
122 virtual base::Time
GetCurrentTime();
124 // Callback which receives the results of the idle state check.
125 void IdleStateCallback(ui::IdleState state
);
127 // Gets the version of the passed app. Virtual to allow mocking.
128 virtual std::string
GetAppVersion(const std::string
& app_id
);
130 // Samples the current hardware status to be sent up with the next device
132 void SampleHardwareStatus();
134 // The number of days in the past to store device activity.
135 // This is kept in case device status uploads fail for a number of days.
136 unsigned int max_stored_past_activity_days_
;
138 // The number of days in the future to store device activity.
139 // When changing the system time and/or timezones, it's possible to record
140 // activity time that is slightly in the future.
141 unsigned int max_stored_future_activity_days_
;
144 // Prevents the local store of activity periods from growing too large by
145 // removing entries that are outside the reporting window.
146 void PruneStoredActivityPeriods(base::Time base_time
);
148 // Trims the store activity periods to only retain data within the
149 // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be
150 // adjusted by subtracting |min_day_trim_duration|.
151 void TrimStoredActivityPeriods(int64 min_day_key
,
152 int min_day_trim_duration
,
155 void AddActivePeriod(base::Time start
, base::Time end
);
157 // Clears the cached hardware status.
158 void ClearCachedHardwareStatus();
160 // Callbacks from chromeos::VersionLoader.
161 void OnOSVersion(const std::string
& version
);
162 void OnOSFirmware(const std::string
& version
);
164 // Helpers for the various portions of the status.
165 void GetActivityTimes(
166 enterprise_management::DeviceStatusReportRequest
* request
);
168 enterprise_management::DeviceStatusReportRequest
* request
);
170 enterprise_management::DeviceStatusReportRequest
* request
);
172 enterprise_management::DeviceStatusReportRequest
* request
);
173 void GetNetworkInterfaces(
174 enterprise_management::DeviceStatusReportRequest
* request
);
176 enterprise_management::DeviceStatusReportRequest
* request
);
177 void GetHardwareStatus(
178 enterprise_management::DeviceStatusReportRequest
* request
);
180 // Update the cached values of the reporting settings.
181 void UpdateReportingSettings();
183 void ScheduleGeolocationUpdateRequest();
185 // content::GeolocationUpdateCallback implementation.
186 void ReceiveGeolocationUpdate(const content::Geoposition
&);
188 // Callback invoked to update our cached disk information.
189 void ReceiveVolumeInfo(
190 const std::vector
<enterprise_management::VolumeInfo
>& info
);
192 // Callback invoked to update our cpu usage information.
193 void ReceiveCPUStatistics(const std::string
& statistics
);
195 // Callback invoked to update our CPU temp information.
196 void StoreCPUTempInfo(
197 const std::vector
<enterprise_management::CPUTempInfo
>& info
);
199 // Helper routine to convert from Shill-provided signal strength (percent)
200 // to dBm units expected by server.
201 int ConvertWifiSignalStrength(int signal_strength
);
203 PrefService
* local_state_
;
205 // The last time an idle state check was performed.
206 base::Time last_idle_check_
;
208 // The maximum key that went into the last report generated by
209 // GetDeviceStatus(), and the duration for it. This is used to trim the
210 // stored data in OnSubmittedSuccessfully(). Trimming is delayed so
211 // unsuccessful uploads don't result in dropped data.
212 int64 last_reported_day_
;
213 int duration_for_last_reported_day_
;
215 // Whether a geolocation update is currently in progress.
216 bool geolocation_update_in_progress_
;
218 base::RepeatingTimer
<DeviceStatusCollector
> idle_poll_timer_
;
219 base::RepeatingTimer
<DeviceStatusCollector
> hardware_status_sampling_timer_
;
220 base::OneShotTimer
<DeviceStatusCollector
> geolocation_update_timer_
;
222 std::string os_version_
;
223 std::string firmware_version_
;
225 content::Geoposition position_
;
227 // Cached disk volume information.
228 std::vector
<enterprise_management::VolumeInfo
> volume_info_
;
230 // Cached CPU temp information.
231 std::vector
<enterprise_management::CPUTempInfo
> cpu_temp_info_
;
233 struct ResourceUsage
{
234 // Sample of percentage-of-CPU-used.
235 int cpu_usage_percent
;
237 // Amount of free RAM (measures raw memory used by processes, not internal
238 // memory waiting to be reclaimed by GC).
239 int64 bytes_of_ram_free
;
242 // Samples of resource usage (contains multiple samples taken
243 // periodically every kHardwareStatusSampleIntervalSeconds).
244 std::deque
<ResourceUsage
> resource_usage_
;
246 // Callback invoked to fetch information about the mounted disk volumes.
247 VolumeInfoFetcher volume_info_fetcher_
;
249 // Callback invoked to fetch information about cpu usage.
250 CPUStatisticsFetcher cpu_statistics_fetcher_
;
252 // Callback invoked to fetch information about cpu temperature.
253 CPUTempFetcher cpu_temp_fetcher_
;
255 chromeos::system::StatisticsProvider
* statistics_provider_
;
257 chromeos::CrosSettings
* cros_settings_
;
259 // The most recent CPU readings.
260 uint64 last_cpu_active_
;
261 uint64 last_cpu_idle_
;
263 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
264 // way to mock geolocation exists.
265 LocationUpdateRequester location_update_requester_
;
267 scoped_ptr
<content::GeolocationProvider::Subscription
>
268 geolocation_subscription_
;
270 // Cached values of the reporting settings from the device policy.
271 bool report_version_info_
;
272 bool report_activity_times_
;
273 bool report_boot_mode_
;
274 bool report_location_
;
275 bool report_network_interfaces_
;
277 bool report_hardware_status_
;
278 bool report_session_status_
;
280 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
281 version_info_subscription_
;
282 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
283 activity_times_subscription_
;
284 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
285 boot_mode_subscription_
;
286 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
287 location_subscription_
;
288 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
289 network_interfaces_subscription_
;
290 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
292 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
293 hardware_status_subscription_
;
294 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
295 session_status_subscription_
;
297 base::WeakPtrFactory
<DeviceStatusCollector
> weak_factory_
;
299 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector
);
302 } // namespace policy
304 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_