Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/policy/device_status_collector.h" | 5 #include "chrome/browser/policy/device_status_collector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | |
|
pastarmovj
2012/01/27 15:39:03
What do you need the cros_library for?
Patrick Dubroy
2012/01/27 16:18:12
Done.
| |
| 11 #include "chrome/browser/chromeos/cros_settings.h" | |
| 12 #include "chrome/browser/chromeos/cros_settings_names.h" | |
| 10 #include "chrome/browser/chromeos/system/statistics_provider.h" | 13 #include "chrome/browser/chromeos/system/statistics_provider.h" |
| 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | |
| 14 #include "chrome/common/chrome_version_info.h" | 18 #include "chrome/common/chrome_version_info.h" |
| 19 #include "chrome/common/pref_names.h" | |
|
pastarmovj
2012/01/27 15:39:03
ditto.
Patrick Dubroy
2012/01/27 16:18:12
Done.
| |
| 15 | 20 |
| 16 using base::Time; | 21 using base::Time; |
| 17 using base::TimeDelta; | 22 using base::TimeDelta; |
| 18 using chromeos::VersionLoader; | 23 using chromeos::VersionLoader; |
| 19 | 24 |
| 20 namespace em = enterprise_management; | 25 namespace em = enterprise_management; |
| 21 | 26 |
| 22 namespace { | 27 namespace { |
| 23 // How many seconds of inactivity triggers the idle state. | 28 // How many seconds of inactivity triggers the idle state. |
| 24 const unsigned int kIdleStateThresholdSeconds = 300; | 29 const unsigned int kIdleStateThresholdSeconds = 300; |
| 25 | 30 |
| 26 // The maximum number of time periods stored in the local state. | 31 // The maximum number of time periods stored in the local state. |
| 27 const unsigned int kMaxStoredActivePeriods = 500; | 32 const unsigned int kMaxStoredActivePeriods = 500; |
| 28 | 33 |
| 29 // Stores the baseline timestamp, to which the active periods are relative. | |
| 30 const char* const kPrefBaselineTime = "device_status.baseline_timestamp"; | |
| 31 | |
| 32 // Stores a list of timestamps representing device active periods. | 34 // Stores a list of timestamps representing device active periods. |
| 33 const char* const kPrefDeviceActivePeriods = "device_status.active_periods"; | 35 const char* const kPrefDeviceActivePeriods = "device_status.active_periods"; |
| 34 | 36 |
| 35 bool GetTimestamp(const ListValue* list, int index, int64* out_value) { | 37 bool GetTimestamp(const ListValue* list, int index, int64* out_value) { |
| 36 std::string string_value; | 38 std::string string_value; |
| 37 if (list->GetString(index, &string_value)) | 39 if (list->GetString(index, &string_value)) |
| 38 return base::StringToInt64(string_value, out_value); | 40 return base::StringToInt64(string_value, out_value); |
| 39 return false; | 41 return false; |
| 40 } | 42 } |
| 41 | 43 |
| 42 } // namespace | 44 } // namespace |
| 43 | 45 |
| 44 namespace policy { | 46 namespace policy { |
| 45 | 47 |
| 46 DeviceStatusCollector::DeviceStatusCollector( | 48 DeviceStatusCollector::DeviceStatusCollector( |
| 47 PrefService* local_state, | 49 PrefService* local_state, |
| 48 chromeos::system::StatisticsProvider* provider) | 50 chromeos::system::StatisticsProvider* provider) |
| 49 : max_stored_active_periods_(kMaxStoredActivePeriods), | 51 : max_stored_active_periods_(kMaxStoredActivePeriods), |
| 50 local_state_(local_state), | 52 local_state_(local_state), |
| 51 last_idle_check_(Time()), | 53 last_idle_check_(Time()), |
| 52 last_idle_state_(IDLE_STATE_UNKNOWN), | 54 last_idle_state_(IDLE_STATE_UNKNOWN), |
| 53 statistics_provider_(provider) { | 55 statistics_provider_(provider), |
| 56 report_version_info_(false), | |
| 57 report_activity_times_(false), | |
| 58 report_boot_mode_(false) { | |
| 54 timer_.Start(FROM_HERE, | 59 timer_.Start(FROM_HERE, |
| 55 TimeDelta::FromSeconds( | 60 TimeDelta::FromSeconds( |
| 56 DeviceStatusCollector::kPollIntervalSeconds), | 61 DeviceStatusCollector::kPollIntervalSeconds), |
| 57 this, &DeviceStatusCollector::CheckIdleState); | 62 this, &DeviceStatusCollector::CheckIdleState); |
| 58 | 63 |
| 64 cros_settings_ = chromeos::CrosSettings::Get(); | |
| 65 | |
| 66 // Watch for changes to the individual policies that control what the status | |
| 67 // reports contain. | |
| 68 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); | |
| 69 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, | |
| 70 this); | |
| 71 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); | |
| 72 | |
| 73 // Fetch the current values of the policies. | |
| 74 UpdateReportingSettings(); | |
| 75 | |
| 59 // Get the the OS and firmware version info. | 76 // Get the the OS and firmware version info. |
| 60 version_loader_.GetVersion(&consumer_, | 77 version_loader_.GetVersion(&consumer_, |
| 61 base::Bind(&DeviceStatusCollector::OnOSVersion, | 78 base::Bind(&DeviceStatusCollector::OnOSVersion, |
| 62 base::Unretained(this)), | 79 base::Unretained(this)), |
| 63 VersionLoader::VERSION_FULL); | 80 VersionLoader::VERSION_FULL); |
| 64 version_loader_.GetFirmware(&consumer_, | 81 version_loader_.GetFirmware(&consumer_, |
| 65 base::Bind(&DeviceStatusCollector::OnOSFirmware, | 82 base::Bind(&DeviceStatusCollector::OnOSFirmware, |
| 66 base::Unretained(this))); | 83 base::Unretained(this))); |
| 67 } | 84 } |
| 68 | 85 |
| 69 DeviceStatusCollector::~DeviceStatusCollector() { | 86 DeviceStatusCollector::~DeviceStatusCollector() { |
| 87 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, | |
| 88 this); | |
| 89 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, | |
| 90 this); | |
| 91 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); | |
| 70 } | 92 } |
| 71 | 93 |
| 72 // static | 94 // static |
| 73 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { | 95 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { |
| 74 local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT()); | |
| 75 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); | 96 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); |
| 76 } | 97 } |
| 77 | 98 |
| 78 void DeviceStatusCollector::CheckIdleState() { | 99 void DeviceStatusCollector::CheckIdleState() { |
| 79 CalculateIdleState(kIdleStateThresholdSeconds, | 100 CalculateIdleState(kIdleStateThresholdSeconds, |
| 80 base::Bind(&DeviceStatusCollector::IdleStateCallback, | 101 base::Bind(&DeviceStatusCollector::IdleStateCallback, |
| 81 base::Unretained(this))); | 102 base::Unretained(this))); |
| 82 } | 103 } |
| 83 | 104 |
| 105 void DeviceStatusCollector::UpdateReportingSettings() { | |
| 106 // Attempt to fetch the current value of the reporting settings. | |
| 107 // If trusted values are not available, register this function to be called | |
| 108 // back when they are available. | |
| 109 bool is_trusted = cros_settings_->GetTrusted( | |
| 110 chromeos::kReportDeviceVersionInfo, | |
| 111 base::Bind(&DeviceStatusCollector::UpdateReportingSettings, | |
| 112 base::Unretained(this))); | |
| 113 if (is_trusted) { | |
| 114 cros_settings_->GetBoolean( | |
| 115 chromeos::kReportDeviceVersionInfo, &report_version_info_); | |
| 116 cros_settings_->GetBoolean( | |
| 117 chromeos::kReportDeviceActivityTimes, &report_activity_times_); | |
| 118 cros_settings_->GetBoolean( | |
| 119 chromeos::kReportDeviceBootMode, &report_boot_mode_); | |
| 120 } | |
| 121 } | |
| 122 | |
| 84 Time DeviceStatusCollector::GetCurrentTime() { | 123 Time DeviceStatusCollector::GetCurrentTime() { |
| 85 return Time::Now(); | 124 return Time::Now(); |
| 86 } | 125 } |
| 87 | 126 |
| 88 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { | 127 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { |
| 89 // Maintain the list of active periods in a local_state pref. | 128 // Maintain the list of active periods in a local_state pref. |
| 90 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); | 129 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); |
| 91 ListValue* active_periods = update.Get(); | 130 ListValue* active_periods = update.Get(); |
| 92 | 131 |
| 93 // Cap the number of active periods that we store. | 132 // Cap the number of active periods that we store. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 111 return; | 150 return; |
| 112 } | 151 } |
| 113 } | 152 } |
| 114 // Add a new period to the list. | 153 // Add a new period to the list. |
| 115 active_periods->Append( | 154 active_periods->Append( |
| 116 new StringValue(base::Int64ToString(start_timestamp))); | 155 new StringValue(base::Int64ToString(start_timestamp))); |
| 117 active_periods->Append(end_value); | 156 active_periods->Append(end_value); |
| 118 } | 157 } |
| 119 | 158 |
| 120 void DeviceStatusCollector::IdleStateCallback(IdleState state) { | 159 void DeviceStatusCollector::IdleStateCallback(IdleState state) { |
| 160 // Do nothing if device activity reporting is disabled. | |
| 161 if (!report_activity_times_) | |
| 162 return; | |
| 163 | |
| 121 Time now = GetCurrentTime(); | 164 Time now = GetCurrentTime(); |
| 122 | 165 |
| 123 if (state == IDLE_STATE_ACTIVE) { | 166 if (state == IDLE_STATE_ACTIVE) { |
| 124 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; | 167 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; |
| 125 | 168 |
| 126 // If it's been too long since the last report, assume that the system was | 169 // If it's been too long since the last report, assume that the system was |
| 127 // in standby, and only count a single interval of activity. | 170 // in standby, and only count a single interval of activity. |
| 128 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) | 171 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) |
| 129 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); | 172 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); |
| 130 else | 173 else |
| 131 AddActivePeriod(last_idle_check_, now); | 174 AddActivePeriod(last_idle_check_, now); |
| 132 } | 175 } |
| 133 last_idle_check_ = now; | 176 last_idle_check_ = now; |
| 134 last_idle_state_ = state; | 177 last_idle_state_ = state; |
| 135 } | 178 } |
| 136 | 179 |
| 137 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { | 180 void DeviceStatusCollector::GetActivityTimes( |
| 138 // Report device active periods. | 181 em::DeviceStatusReportRequest* request) { |
| 139 const ListValue* active_periods = | 182 const ListValue* active_periods = |
| 140 local_state_->GetList(kPrefDeviceActivePeriods); | 183 local_state_->GetList(kPrefDeviceActivePeriods); |
| 141 em::TimePeriod* time_period; | 184 em::TimePeriod* time_period; |
| 142 | 185 |
| 143 DCHECK(active_periods->GetSize() % 2 == 0); | 186 DCHECK(active_periods->GetSize() % 2 == 0); |
| 144 | 187 |
| 145 int period_count = active_periods->GetSize() / 2; | 188 int period_count = active_periods->GetSize() / 2; |
| 146 for (int i = 0; i < period_count; i++) { | 189 for (int i = 0; i < period_count; i++) { |
| 147 int64 start, end; | 190 int64 start, end; |
| 148 | 191 |
| 149 if (!GetTimestamp(active_periods, 2 * i, &start) || | 192 if (!GetTimestamp(active_periods, 2 * i, &start) || |
| 150 !GetTimestamp(active_periods, 2 * i + 1, &end) || | 193 !GetTimestamp(active_periods, 2 * i + 1, &end) || |
| 151 end < start) { | 194 end < start) { |
| 152 // Something is amiss -- bail out. | 195 // Something is amiss -- bail out. |
| 153 NOTREACHED(); | 196 NOTREACHED(); |
| 154 break; | 197 break; |
| 155 } | 198 } |
| 156 time_period = request->add_active_time(); | 199 time_period = request->add_active_time(); |
| 157 time_period->set_start_timestamp(start); | 200 time_period->set_start_timestamp(start); |
| 158 time_period->set_end_timestamp(end); | 201 time_period->set_end_timestamp(end); |
| 159 } | 202 } |
| 160 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); | 203 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); |
| 161 update.Get()->Clear(); | 204 update.Get()->Clear(); |
| 205 } | |
| 162 | 206 |
| 207 void DeviceStatusCollector::GetVersionInfo( | |
| 208 em::DeviceStatusReportRequest* request) { | |
| 163 chrome::VersionInfo version_info; | 209 chrome::VersionInfo version_info; |
| 164 request->set_browser_version(version_info.Version()); | 210 request->set_browser_version(version_info.Version()); |
| 165 request->set_os_version(os_version_); | 211 request->set_os_version(os_version_); |
| 166 request->set_firmware_version(firmware_version_); | 212 request->set_firmware_version(firmware_version_); |
| 213 } | |
| 167 | 214 |
| 168 // Report the state of the dev switch at boot. | 215 void DeviceStatusCollector::GetBootMode( |
| 216 em::DeviceStatusReportRequest* request) { | |
| 169 std::string dev_switch_mode; | 217 std::string dev_switch_mode; |
| 170 if (statistics_provider_->GetMachineStatistic( | 218 if (statistics_provider_->GetMachineStatistic( |
| 171 "devsw_boot", &dev_switch_mode)) { | 219 "devsw_boot", &dev_switch_mode)) { |
| 172 if (dev_switch_mode == "1") | 220 if (dev_switch_mode == "1") |
| 173 request->set_boot_mode("Dev"); | 221 request->set_boot_mode("Dev"); |
| 174 else if (dev_switch_mode == "0") | 222 else if (dev_switch_mode == "0") |
| 175 request->set_boot_mode("Verified"); | 223 request->set_boot_mode("Verified"); |
| 176 } | 224 } |
| 177 } | 225 } |
| 178 | 226 |
| 227 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { | |
| 228 if (report_activity_times_) | |
| 229 GetActivityTimes(request); | |
| 230 | |
| 231 if (report_version_info_) | |
| 232 GetVersionInfo(request); | |
| 233 | |
| 234 if (report_boot_mode_) | |
| 235 GetBootMode(request); | |
| 236 } | |
| 237 | |
| 179 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, | 238 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, |
| 180 std::string version) { | 239 std::string version) { |
| 181 os_version_ = version; | 240 os_version_ = version; |
| 182 } | 241 } |
| 183 | 242 |
| 184 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, | 243 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, |
| 185 std::string version) { | 244 std::string version) { |
| 186 firmware_version_ = version; | 245 firmware_version_ = version; |
| 187 } | 246 } |
| 188 | 247 |
| 248 void DeviceStatusCollector::Observe( | |
| 249 int type, | |
| 250 const content::NotificationSource& source, | |
| 251 const content::NotificationDetails& details) { | |
| 252 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED) { | |
|
pastarmovj
2012/01/27 15:39:03
Curlies not needed?
Patrick Dubroy
2012/01/27 16:18:12
Done.
| |
| 253 UpdateReportingSettings(); | |
| 254 } else { | |
| 255 NOTREACHED(); | |
| 256 } | |
| 257 } | |
| 258 | |
| 189 } // namespace policy | 259 } // namespace policy |
| OLD | NEW |