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 // Constructor. Callers can inject their own VolumeInfoFetcher, or if a null
62 // callback is passed, the default implementation will be used.
63 DeviceStatusCollector(
64 PrefService
* local_state
,
65 chromeos::system::StatisticsProvider
* provider
,
66 const LocationUpdateRequester
& location_update_requester
,
67 const VolumeInfoFetcher
& volume_info_fetcher
);
68 virtual ~DeviceStatusCollector();
70 // Fills in the passed proto with device status information. Will return
71 // false if no status information is filled in (because status reporting
73 virtual bool GetDeviceStatus(
74 enterprise_management::DeviceStatusReportRequest
* status
);
76 // Fills in the passed proto with session status information. Will return
77 // false if no status information is filled in (because status reporting
78 // is disabled, or because the active session is not a kiosk session).
79 virtual bool GetDeviceSessionStatus(
80 enterprise_management::SessionStatusReportRequest
* status
);
82 // Called after the status information has successfully been submitted to
84 void OnSubmittedSuccessfully();
86 static void RegisterPrefs(PrefRegistrySimple
* registry
);
88 // How often, in seconds, to poll to see if the user is idle.
89 static const unsigned int kIdlePollIntervalSeconds
= 30;
91 // The total number of hardware resource usage samples cached internally.
92 static const unsigned int kMaxResourceUsageSamples
= 10;
95 // Check whether the user has been idle for a certain period of time.
96 virtual void CheckIdleState();
98 // Used instead of base::Time::Now(), to make testing possible.
99 virtual base::Time
GetCurrentTime();
101 // Callback which receives the results of the idle state check.
102 void IdleStateCallback(ui::IdleState state
);
104 // Returns the DeviceLocalAccount associated with the currently active
105 // kiosk session, if the session was auto-launched with zero delay
106 // (this enables functionality such as network reporting).
107 // Virtual to allow mocking.
108 virtual scoped_ptr
<DeviceLocalAccount
> GetAutoLaunchedKioskSessionInfo();
110 // Samples the current CPU and RAM usage and updates our cache of samples.
111 void SampleResourceUsage();
113 // Returns the percentage of total CPU that each process uses. Virtual so it
115 virtual std::vector
<double> GetPerProcessCPUUsage();
117 // Gets the version of the passed app. Virtual to allow mocking.
118 virtual std::string
GetAppVersion(const std::string
& app_id
);
120 // The number of days in the past to store device activity.
121 // This is kept in case device status uploads fail for a number of days.
122 unsigned int max_stored_past_activity_days_
;
124 // The number of days in the future to store device activity.
125 // When changing the system time and/or timezones, it's possible to record
126 // activity time that is slightly in the future.
127 unsigned int max_stored_future_activity_days_
;
130 // Prevents the local store of activity periods from growing too large by
131 // removing entries that are outside the reporting window.
132 void PruneStoredActivityPeriods(base::Time base_time
);
134 // Trims the store activity periods to only retain data within the
135 // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be
136 // adjusted by subtracting |min_day_trim_duration|.
137 void TrimStoredActivityPeriods(int64 min_day_key
,
138 int min_day_trim_duration
,
141 void AddActivePeriod(base::Time start
, base::Time end
);
143 // Samples the current hardware status to be sent up with the next device
145 void SampleHardwareStatus();
147 // Clears the cached hardware status.
148 void ClearCachedHardwareStatus();
150 // Callbacks from chromeos::VersionLoader.
151 void OnOSVersion(const std::string
& version
);
152 void OnOSFirmware(const std::string
& version
);
154 // Helpers for the various portions of the status.
155 void GetActivityTimes(
156 enterprise_management::DeviceStatusReportRequest
* request
);
158 enterprise_management::DeviceStatusReportRequest
* request
);
160 enterprise_management::DeviceStatusReportRequest
* request
);
162 enterprise_management::DeviceStatusReportRequest
* request
);
163 void GetNetworkInterfaces(
164 enterprise_management::DeviceStatusReportRequest
* request
);
166 enterprise_management::DeviceStatusReportRequest
* request
);
167 void GetHardwareStatus(
168 enterprise_management::DeviceStatusReportRequest
* request
);
170 // Update the cached values of the reporting settings.
171 void UpdateReportingSettings();
173 void ScheduleGeolocationUpdateRequest();
175 // content::GeolocationUpdateCallback implementation.
176 void ReceiveGeolocationUpdate(const content::Geoposition
&);
178 // Callback invoked to update our cached disk information.
179 void ReceiveVolumeInfo(
180 const std::vector
<enterprise_management::VolumeInfo
>& info
);
182 PrefService
* local_state_
;
184 // The last time an idle state check was performed.
185 base::Time last_idle_check_
;
187 // The maximum key that went into the last report generated by
188 // GetDeviceStatus(), and the duration for it. This is used to trim the
189 // stored data in OnSubmittedSuccessfully(). Trimming is delayed so
190 // unsuccessful uploads don't result in dropped data.
191 int64 last_reported_day_
;
192 int duration_for_last_reported_day_
;
194 // Whether a geolocation update is currently in progress.
195 bool geolocation_update_in_progress_
;
197 base::RepeatingTimer
<DeviceStatusCollector
> idle_poll_timer_
;
198 base::RepeatingTimer
<DeviceStatusCollector
> hardware_status_sampling_timer_
;
199 base::OneShotTimer
<DeviceStatusCollector
> geolocation_update_timer_
;
201 std::string os_version_
;
202 std::string firmware_version_
;
204 content::Geoposition position_
;
206 // Cached disk volume information.
207 std::vector
<enterprise_management::VolumeInfo
> volume_info_
;
209 struct ResourceUsage
{
210 // Sample of percentage-of-CPU-used across all processes (0-100)
211 int cpu_usage_percent
;
213 // Amount of free RAM (measures raw memory used by processes, not internal
214 // memory waiting to be reclaimed by GC).
215 int64 bytes_of_ram_free
;
218 // Samples of resource usage (contains multiple samples taken
219 // periodically every kHardwareStatusSampleIntervalSeconds).
220 std::deque
<ResourceUsage
> resource_usage_
;
222 // Callback invoked to fetch information about the mounted disk volumes.
223 VolumeInfoFetcher volume_info_fetcher_
;
225 chromeos::system::StatisticsProvider
* statistics_provider_
;
227 chromeos::CrosSettings
* cros_settings_
;
229 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
230 // way to mock geolocation exists.
231 LocationUpdateRequester location_update_requester_
;
233 scoped_ptr
<content::GeolocationProvider::Subscription
>
234 geolocation_subscription_
;
236 // Cached values of the reporting settings from the device policy.
237 bool report_version_info_
;
238 bool report_activity_times_
;
239 bool report_boot_mode_
;
240 bool report_location_
;
241 bool report_network_interfaces_
;
243 bool report_hardware_status_
;
244 bool report_session_status_
;
246 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
247 version_info_subscription_
;
248 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
249 activity_times_subscription_
;
250 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
251 boot_mode_subscription_
;
252 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
253 location_subscription_
;
254 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
255 network_interfaces_subscription_
;
256 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
258 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
259 hardware_status_subscription_
;
260 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
261 session_status_subscription_
;
263 base::WeakPtrFactory
<DeviceStatusCollector
> weak_factory_
;
265 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector
);
268 } // namespace policy
270 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_