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 // Returns the DeviceLocalAccount associated with the currently active
99 // kiosk session, if the session was auto-launched with zero delay
100 // (this enables functionality such as network reporting).
101 // Virtual to allow mocking.
102 virtual scoped_ptr
<DeviceLocalAccount
> GetAutoLaunchedKioskSessionInfo();
104 // How often, in seconds, to poll to see if the user is idle.
105 static const unsigned int kIdlePollIntervalSeconds
= 30;
107 // The total number of hardware resource usage samples cached internally.
108 static const unsigned int kMaxResourceUsageSamples
= 10;
111 // Check whether the user has been idle for a certain period of time.
112 virtual void CheckIdleState();
114 // Used instead of base::Time::Now(), to make testing possible.
115 virtual base::Time
GetCurrentTime();
117 // Callback which receives the results of the idle state check.
118 void IdleStateCallback(ui::IdleState state
);
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 // Helper routine to convert from Shill-provided signal strength (percent)
189 // to dBm units expected by server.
190 int ConvertWifiSignalStrength(int signal_strength
);
192 PrefService
* local_state_
;
194 // The last time an idle state check was performed.
195 base::Time last_idle_check_
;
197 // The maximum key that went into the last report generated by
198 // GetDeviceStatus(), and the duration for it. This is used to trim the
199 // stored data in OnSubmittedSuccessfully(). Trimming is delayed so
200 // unsuccessful uploads don't result in dropped data.
201 int64 last_reported_day_
;
202 int duration_for_last_reported_day_
;
204 // Whether a geolocation update is currently in progress.
205 bool geolocation_update_in_progress_
;
207 base::RepeatingTimer
<DeviceStatusCollector
> idle_poll_timer_
;
208 base::RepeatingTimer
<DeviceStatusCollector
> hardware_status_sampling_timer_
;
209 base::OneShotTimer
<DeviceStatusCollector
> geolocation_update_timer_
;
211 std::string os_version_
;
212 std::string firmware_version_
;
214 content::Geoposition position_
;
216 // Cached disk volume information.
217 std::vector
<enterprise_management::VolumeInfo
> volume_info_
;
219 struct ResourceUsage
{
220 // Sample of percentage-of-CPU-used.
221 int cpu_usage_percent
;
223 // Amount of free RAM (measures raw memory used by processes, not internal
224 // memory waiting to be reclaimed by GC).
225 int64 bytes_of_ram_free
;
228 // Samples of resource usage (contains multiple samples taken
229 // periodically every kHardwareStatusSampleIntervalSeconds).
230 std::deque
<ResourceUsage
> resource_usage_
;
232 // Callback invoked to fetch information about the mounted disk volumes.
233 VolumeInfoFetcher volume_info_fetcher_
;
235 // Callback invoked to fetch information about cpu usage.
236 CPUStatisticsFetcher cpu_statistics_fetcher_
;
238 chromeos::system::StatisticsProvider
* statistics_provider_
;
240 chromeos::CrosSettings
* cros_settings_
;
242 // The most recent CPU readings.
243 uint64 last_cpu_active_
;
244 uint64 last_cpu_idle_
;
246 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
247 // way to mock geolocation exists.
248 LocationUpdateRequester location_update_requester_
;
250 scoped_ptr
<content::GeolocationProvider::Subscription
>
251 geolocation_subscription_
;
253 // Cached values of the reporting settings from the device policy.
254 bool report_version_info_
;
255 bool report_activity_times_
;
256 bool report_boot_mode_
;
257 bool report_location_
;
258 bool report_network_interfaces_
;
260 bool report_hardware_status_
;
261 bool report_session_status_
;
263 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
264 version_info_subscription_
;
265 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
266 activity_times_subscription_
;
267 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
268 boot_mode_subscription_
;
269 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
270 location_subscription_
;
271 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
272 network_interfaces_subscription_
;
273 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
275 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
276 hardware_status_subscription_
;
277 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
278 session_status_subscription_
;
280 base::WeakPtrFactory
<DeviceStatusCollector
> weak_factory_
;
282 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector
);
285 } // namespace policy
287 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_