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 // Constructor. Callers can inject their own VolumeInfoFetcher and
70 // CPUStatisticsFetcher. A null callback can be passed for either parameter,
71 // to use the default implementation.
72 DeviceStatusCollector(
73 PrefService
* local_state
,
74 chromeos::system::StatisticsProvider
* provider
,
75 const LocationUpdateRequester
& location_update_requester
,
76 const VolumeInfoFetcher
& volume_info_fetcher
,
77 const CPUStatisticsFetcher
& cpu_statistics_fetcher
);
78 virtual ~DeviceStatusCollector();
80 // Fills in the passed proto with device status information. Will return
81 // false if no status information is filled in (because status reporting
83 virtual bool GetDeviceStatus(
84 enterprise_management::DeviceStatusReportRequest
* status
);
86 // Fills in the passed proto with session status information. Will return
87 // false if no status information is filled in (because status reporting
88 // is disabled, or because the active session is not a kiosk session).
89 virtual bool GetDeviceSessionStatus(
90 enterprise_management::SessionStatusReportRequest
* status
);
92 // Called after the status information has successfully been submitted to
94 void OnSubmittedSuccessfully();
96 static void RegisterPrefs(PrefRegistrySimple
* registry
);
98 // How often, in seconds, to poll to see if the user is idle.
99 static const unsigned int kIdlePollIntervalSeconds
= 30;
101 // The total number of hardware resource usage samples cached internally.
102 static const unsigned int kMaxResourceUsageSamples
= 10;
105 // Check whether the user has been idle for a certain period of time.
106 virtual void CheckIdleState();
108 // Used instead of base::Time::Now(), to make testing possible.
109 virtual base::Time
GetCurrentTime();
111 // Callback which receives the results of the idle state check.
112 void IdleStateCallback(ui::IdleState state
);
114 // Returns the DeviceLocalAccount associated with the currently active
115 // kiosk session, if the session was auto-launched with zero delay
116 // (this enables functionality such as network reporting).
117 // Virtual to allow mocking.
118 virtual scoped_ptr
<DeviceLocalAccount
> GetAutoLaunchedKioskSessionInfo();
120 // Gets the version of the passed app. Virtual to allow mocking.
121 virtual std::string
GetAppVersion(const std::string
& app_id
);
123 // Samples the current hardware status to be sent up with the next device
125 void SampleHardwareStatus();
127 // The number of days in the past to store device activity.
128 // This is kept in case device status uploads fail for a number of days.
129 unsigned int max_stored_past_activity_days_
;
131 // The number of days in the future to store device activity.
132 // When changing the system time and/or timezones, it's possible to record
133 // activity time that is slightly in the future.
134 unsigned int max_stored_future_activity_days_
;
137 // Prevents the local store of activity periods from growing too large by
138 // removing entries that are outside the reporting window.
139 void PruneStoredActivityPeriods(base::Time base_time
);
141 // Trims the store activity periods to only retain data within the
142 // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be
143 // adjusted by subtracting |min_day_trim_duration|.
144 void TrimStoredActivityPeriods(int64 min_day_key
,
145 int min_day_trim_duration
,
148 void AddActivePeriod(base::Time start
, base::Time end
);
150 // Clears the cached hardware status.
151 void ClearCachedHardwareStatus();
153 // Callbacks from chromeos::VersionLoader.
154 void OnOSVersion(const std::string
& version
);
155 void OnOSFirmware(const std::string
& version
);
157 // Helpers for the various portions of the status.
158 void GetActivityTimes(
159 enterprise_management::DeviceStatusReportRequest
* request
);
161 enterprise_management::DeviceStatusReportRequest
* request
);
163 enterprise_management::DeviceStatusReportRequest
* request
);
165 enterprise_management::DeviceStatusReportRequest
* request
);
166 void GetNetworkInterfaces(
167 enterprise_management::DeviceStatusReportRequest
* request
);
169 enterprise_management::DeviceStatusReportRequest
* request
);
170 void GetHardwareStatus(
171 enterprise_management::DeviceStatusReportRequest
* request
);
173 // Update the cached values of the reporting settings.
174 void UpdateReportingSettings();
176 void ScheduleGeolocationUpdateRequest();
178 // content::GeolocationUpdateCallback implementation.
179 void ReceiveGeolocationUpdate(const content::Geoposition
&);
181 // Callback invoked to update our cached disk information.
182 void ReceiveVolumeInfo(
183 const std::vector
<enterprise_management::VolumeInfo
>& info
);
185 // Callback invoked to update our cpu usage information.
186 void ReceiveCPUStatistics(const std::string
& statistics
);
188 PrefService
* local_state_
;
190 // The last time an idle state check was performed.
191 base::Time last_idle_check_
;
193 // The maximum key that went into the last report generated by
194 // GetDeviceStatus(), and the duration for it. This is used to trim the
195 // stored data in OnSubmittedSuccessfully(). Trimming is delayed so
196 // unsuccessful uploads don't result in dropped data.
197 int64 last_reported_day_
;
198 int duration_for_last_reported_day_
;
200 // Whether a geolocation update is currently in progress.
201 bool geolocation_update_in_progress_
;
203 base::RepeatingTimer
<DeviceStatusCollector
> idle_poll_timer_
;
204 base::RepeatingTimer
<DeviceStatusCollector
> hardware_status_sampling_timer_
;
205 base::OneShotTimer
<DeviceStatusCollector
> geolocation_update_timer_
;
207 std::string os_version_
;
208 std::string firmware_version_
;
210 content::Geoposition position_
;
212 // Cached disk volume information.
213 std::vector
<enterprise_management::VolumeInfo
> volume_info_
;
215 struct ResourceUsage
{
216 // Sample of percentage-of-CPU-used.
217 int cpu_usage_percent
;
219 // Amount of free RAM (measures raw memory used by processes, not internal
220 // memory waiting to be reclaimed by GC).
221 int64 bytes_of_ram_free
;
224 // Samples of resource usage (contains multiple samples taken
225 // periodically every kHardwareStatusSampleIntervalSeconds).
226 std::deque
<ResourceUsage
> resource_usage_
;
228 // Callback invoked to fetch information about the mounted disk volumes.
229 VolumeInfoFetcher volume_info_fetcher_
;
231 // Callback invoked to fetch information about cpu usage.
232 CPUStatisticsFetcher cpu_statistics_fetcher_
;
234 chromeos::system::StatisticsProvider
* statistics_provider_
;
236 chromeos::CrosSettings
* cros_settings_
;
238 // The most recent CPU readings.
239 uint64 last_cpu_active_
;
240 uint64 last_cpu_idle_
;
242 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
243 // way to mock geolocation exists.
244 LocationUpdateRequester location_update_requester_
;
246 scoped_ptr
<content::GeolocationProvider::Subscription
>
247 geolocation_subscription_
;
249 // Cached values of the reporting settings from the device policy.
250 bool report_version_info_
;
251 bool report_activity_times_
;
252 bool report_boot_mode_
;
253 bool report_location_
;
254 bool report_network_interfaces_
;
256 bool report_hardware_status_
;
257 bool report_session_status_
;
259 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
260 version_info_subscription_
;
261 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
262 activity_times_subscription_
;
263 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
264 boot_mode_subscription_
;
265 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
266 location_subscription_
;
267 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
268 network_interfaces_subscription_
;
269 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
271 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
272 hardware_status_subscription_
;
273 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
274 session_status_subscription_
;
276 base::WeakPtrFactory
<DeviceStatusCollector
> weak_factory_
;
278 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector
);
281 } // namespace policy
283 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_