device_status_collector.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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. 4 5#ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_ 6#define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/compiler_specific.h" 12#include "base/memory/weak_ptr.h" 13#include "base/time.h" 14#include "base/timer.h" 15#include "chrome/browser/chromeos/version_loader.h" 16#include "chrome/browser/idle.h" 17#include "chrome/browser/policy/cloud/cloud_policy_client.h" 18#include "chrome/common/cancelable_task_tracker.h" 19#include "content/public/browser/geolocation.h" 20#include "content/public/browser/notification_observer.h" 21#include "content/public/common/geoposition.h" 22 23namespace chromeos { 24class CrosSettings; 25namespace system { 26class StatisticsProvider; 27} 28} 29 30namespace content { 31class NotificationDetails; 32class NotificationSource; 33} 34 35namespace enterprise_management { 36class DeviceStatusReportRequest; 37} 38 39class PrefService; 40class PrefRegistrySimple; 41 42namespace policy { 43 44// Collects and summarizes the status of an enterprised-managed ChromeOS device. 45class DeviceStatusCollector : public CloudPolicyClient::StatusProvider, 46 public content::NotificationObserver { 47 public: 48 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper 49 // way to mock geolocation exists. 50 typedef void(*LocationUpdateRequester)( 51 const content::GeolocationUpdateCallback& callback); 52 53 DeviceStatusCollector(PrefService* local_state, 54 chromeos::system::StatisticsProvider* provider, 55 LocationUpdateRequester location_update_requester); 56 virtual ~DeviceStatusCollector(); 57 58 void GetStatus(enterprise_management::DeviceStatusReportRequest* request); 59 60 // CloudPolicyClient::StatusProvider: 61 virtual bool GetDeviceStatus( 62 enterprise_management::DeviceStatusReportRequest* status) OVERRIDE; 63 virtual bool GetSessionStatus( 64 enterprise_management::SessionStatusReportRequest* status) OVERRIDE; 65 virtual void OnSubmittedSuccessfully() OVERRIDE; 66 67 static void RegisterPrefs(PrefRegistrySimple* registry); 68 69 // How often, in seconds, to poll to see if the user is idle. 70 static const unsigned int kIdlePollIntervalSeconds = 30; 71 72 protected: 73 // Check whether the user has been idle for a certain period of time. 74 virtual void CheckIdleState(); 75 76 // Used instead of base::Time::Now(), to make testing possible. 77 virtual base::Time GetCurrentTime(); 78 79 // Callback which receives the results of the idle state check. 80 void IdleStateCallback(IdleState state); 81 82 // The number of days in the past to store device activity. 83 // This is kept in case device status uploads fail for a number of days. 84 unsigned int max_stored_past_activity_days_; 85 86 // The number of days in the future to store device activity. 87 // When changing the system time and/or timezones, it's possible to record 88 // activity time that is slightly in the future. 89 unsigned int max_stored_future_activity_days_; 90 91 private: 92 // Prevents the local store of activity periods from growing too large by 93 // removing entries that are outside the reporting window. 94 void PruneStoredActivityPeriods(base::Time base_time); 95 96 // Trims the store activity periods to only retain data within the 97 // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be 98 // adjusted by subtracting |min_day_trim_duration|. 99 void TrimStoredActivityPeriods(int64 min_day_key, 100 int min_day_trim_duration, 101 int64 max_day_key); 102 103 void AddActivePeriod(base::Time start, base::Time end); 104 105 // Callbacks from chromeos::VersionLoader. 106 void OnOSVersion(const std::string& version); 107 void OnOSFirmware(const std::string& version); 108 109 // Helpers for the various portions of the status. 110 void GetActivityTimes( 111 enterprise_management::DeviceStatusReportRequest* request); 112 void GetVersionInfo( 113 enterprise_management::DeviceStatusReportRequest* request); 114 void GetBootMode( 115 enterprise_management::DeviceStatusReportRequest* request); 116 void GetLocation( 117 enterprise_management::DeviceStatusReportRequest* request); 118 119 // Update the cached values of the reporting settings. 120 void UpdateReportingSettings(); 121 122 // content::NotificationObserver interface. 123 virtual void Observe( 124 int type, 125 const content::NotificationSource& source, 126 const content::NotificationDetails& details) OVERRIDE; 127 128 void ScheduleGeolocationUpdateRequest(); 129 130 // content::GeolocationUpdateCallback implementation. 131 void ReceiveGeolocationUpdate(const content::Geoposition&); 132 133 // How often to poll to see if the user is idle. 134 int poll_interval_seconds_; 135 136 PrefService* local_state_; 137 138 // The last time an idle state check was performed. 139 base::Time last_idle_check_; 140 141 // The maximum key that went into the last report generated by 142 // GetDeviceStatus(), and the duration for it. This is used to trim the 143 // stored data in OnSubmittedSuccessfully(). Trimming is delayed so 144 // unsuccessful uploads don't result in dropped data. 145 int64 last_reported_day_; 146 int duration_for_last_reported_day_; 147 148 // Whether a geolocation update is currently in progress. 149 bool geolocation_update_in_progress_; 150 151 base::RepeatingTimer<DeviceStatusCollector> idle_poll_timer_; 152 base::OneShotTimer<DeviceStatusCollector> geolocation_update_timer_; 153 154 chromeos::VersionLoader version_loader_; 155 CancelableTaskTracker tracker_; 156 157 std::string os_version_; 158 std::string firmware_version_; 159 160 content::Geoposition position_; 161 162 chromeos::system::StatisticsProvider* statistics_provider_; 163 164 chromeos::CrosSettings* cros_settings_; 165 166 base::WeakPtrFactory<DeviceStatusCollector> weak_factory_; 167 168 // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper 169 // way to mock geolocation exists. 170 LocationUpdateRequester location_update_requester_; 171 172 // Cached values of the reporting settings from the device policy. 173 bool report_version_info_; 174 bool report_activity_times_; 175 bool report_boot_mode_; 176 bool report_location_; 177 178 DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector); 179}; 180 181} // namespace policy 182 183#endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_ 184