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" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/chromeos/cros_settings.h" | 8 #include "chrome/browser/chromeos/cros_settings.h" |
| 11 #include "chrome/browser/chromeos/cros_settings_names.h" | |
| 12 #include "chrome/browser/chromeos/system/statistics_provider.h" | 9 #include "chrome/browser/chromeos/system/statistics_provider.h" |
| 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 11 #include "chrome/browser/policy/device_status_location_helper.h" | |
|
Joao da Silva
2012/04/20 14:21:09
Nit: order
bartfab (slow)
2012/04/23 09:48:54
Done.
| |
| 14 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
| 18 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 19 | 17 |
| 20 using base::Time; | 18 using base::Time; |
| 21 using base::TimeDelta; | 19 using base::TimeDelta; |
| 22 using chromeos::VersionLoader; | 20 using chromeos::VersionLoader; |
| 23 | 21 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 54 namespace policy { | 52 namespace policy { |
| 55 | 53 |
| 56 DeviceStatusCollector::DeviceStatusCollector( | 54 DeviceStatusCollector::DeviceStatusCollector( |
| 57 PrefService* local_state, | 55 PrefService* local_state, |
| 58 chromeos::system::StatisticsProvider* provider) | 56 chromeos::system::StatisticsProvider* provider) |
| 59 : max_stored_past_activity_days_(kMaxStoredPastActivityDays), | 57 : max_stored_past_activity_days_(kMaxStoredPastActivityDays), |
| 60 max_stored_future_activity_days_(kMaxStoredFutureActivityDays), | 58 max_stored_future_activity_days_(kMaxStoredFutureActivityDays), |
| 61 local_state_(local_state), | 59 local_state_(local_state), |
| 62 last_idle_check_(Time()), | 60 last_idle_check_(Time()), |
| 63 statistics_provider_(provider), | 61 statistics_provider_(provider), |
| 62 geolocation_provider_for_test_(NULL), | |
| 63 location_helper_(NULL), | |
| 64 report_version_info_(false), | 64 report_version_info_(false), |
| 65 report_activity_times_(false), | 65 report_activity_times_(false), |
| 66 report_boot_mode_(false) { | 66 report_boot_mode_(false), |
| 67 report_location_(false) { | |
| 67 timer_.Start(FROM_HERE, | 68 timer_.Start(FROM_HERE, |
| 68 TimeDelta::FromSeconds(kPollIntervalSeconds), | 69 TimeDelta::FromSeconds(kPollIntervalSeconds), |
| 69 this, &DeviceStatusCollector::CheckIdleState); | 70 this, &DeviceStatusCollector::CheckIdleState); |
| 70 | 71 |
| 71 cros_settings_ = chromeos::CrosSettings::Get(); | 72 cros_settings_ = chromeos::CrosSettings::Get(); |
| 72 | 73 |
| 73 // Watch for changes to the individual policies that control what the status | 74 // Watch for changes to the individual policies that control what the status |
| 74 // reports contain. | 75 // reports contain. |
| 75 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); | 76 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); |
| 76 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, | 77 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, |
| 77 this); | 78 this); |
| 78 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); | 79 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); |
| 80 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceLocation, this); | |
| 79 | 81 |
| 80 // Fetch the current values of the policies. | 82 // Fetch the current values of the policies. |
| 81 UpdateReportingSettings(); | 83 UpdateReportingSettings(); |
| 82 | 84 |
| 83 // Get the the OS and firmware version info. | 85 // Get the the OS and firmware version info. |
| 84 version_loader_.GetVersion(&consumer_, | 86 version_loader_.GetVersion(&consumer_, |
| 85 base::Bind(&DeviceStatusCollector::OnOSVersion, | 87 base::Bind(&DeviceStatusCollector::OnOSVersion, |
| 86 base::Unretained(this)), | 88 base::Unretained(this)), |
| 87 VersionLoader::VERSION_FULL); | 89 VersionLoader::VERSION_FULL); |
| 88 version_loader_.GetFirmware(&consumer_, | 90 version_loader_.GetFirmware(&consumer_, |
| 89 base::Bind(&DeviceStatusCollector::OnOSFirmware, | 91 base::Bind(&DeviceStatusCollector::OnOSFirmware, |
| 90 base::Unretained(this))); | 92 base::Unretained(this))); |
| 91 } | 93 } |
| 92 | 94 |
| 93 DeviceStatusCollector::~DeviceStatusCollector() { | 95 DeviceStatusCollector::~DeviceStatusCollector() { |
| 94 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, | 96 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, |
| 95 this); | 97 this); |
| 96 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, | 98 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, |
| 97 this); | 99 this); |
| 98 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); | 100 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); |
| 101 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceLocation, this); | |
| 102 | |
| 103 if (location_helper_) | |
| 104 location_helper_->Destruct(); | |
| 99 } | 105 } |
| 100 | 106 |
| 101 // static | 107 // static |
| 102 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { | 108 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { |
| 103 local_state->RegisterDictionaryPref(prefs::kDeviceActivityTimes, | 109 local_state->RegisterDictionaryPref(prefs::kDeviceActivityTimes, |
| 104 new DictionaryValue); | 110 new DictionaryValue); |
| 111 DeviceStatusLocationHelper::RegisterPrefs(local_state); | |
| 105 } | 112 } |
| 106 | 113 |
| 107 void DeviceStatusCollector::CheckIdleState() { | 114 void DeviceStatusCollector::CheckIdleState() { |
| 108 CalculateIdleState(kIdleStateThresholdSeconds, | 115 CalculateIdleState(kIdleStateThresholdSeconds, |
| 109 base::Bind(&DeviceStatusCollector::IdleStateCallback, | 116 base::Bind(&DeviceStatusCollector::IdleStateCallback, |
| 110 base::Unretained(this))); | 117 base::Unretained(this))); |
| 111 } | 118 } |
| 112 | 119 |
| 120 void DeviceStatusCollector::SetGeolocationProviderForTest( | |
| 121 GeolocationProvider* provider) { | |
| 122 geolocation_provider_for_test_ = provider; | |
| 123 if (location_helper_) | |
| 124 location_helper_->SetGeolocationProviderForTest(provider); | |
| 125 } | |
| 126 | |
| 113 void DeviceStatusCollector::UpdateReportingSettings() { | 127 void DeviceStatusCollector::UpdateReportingSettings() { |
| 114 // Attempt to fetch the current value of the reporting settings. | 128 // Attempt to fetch the current value of the reporting settings. |
| 115 // If trusted values are not available, register this function to be called | 129 // If trusted values are not available, register this function to be called |
| 116 // back when they are available. | 130 // back when they are available. |
| 117 if (!cros_settings_->PrepareTrustedValues( | 131 if (!cros_settings_->PrepareTrustedValues( |
| 118 base::Bind(&DeviceStatusCollector::UpdateReportingSettings, | 132 base::Bind(&DeviceStatusCollector::UpdateReportingSettings, |
| 119 base::Unretained(this)))) { | 133 base::Unretained(this)))) { |
| 120 return; | 134 return; |
| 121 } | 135 } |
| 122 cros_settings_->GetBoolean( | 136 cros_settings_->GetBoolean( |
| 123 chromeos::kReportDeviceVersionInfo, &report_version_info_); | 137 chromeos::kReportDeviceVersionInfo, &report_version_info_); |
| 124 cros_settings_->GetBoolean( | 138 cros_settings_->GetBoolean( |
| 125 chromeos::kReportDeviceActivityTimes, &report_activity_times_); | 139 chromeos::kReportDeviceActivityTimes, &report_activity_times_); |
| 126 cros_settings_->GetBoolean( | 140 cros_settings_->GetBoolean( |
| 127 chromeos::kReportDeviceBootMode, &report_boot_mode_); | 141 chromeos::kReportDeviceBootMode, &report_boot_mode_); |
| 142 cros_settings_->GetBoolean( | |
| 143 chromeos::kReportDeviceLocation, &report_location_); | |
| 144 | |
| 145 if (report_location_) { | |
| 146 if (!location_helper_) { | |
| 147 location_helper_ = new DeviceStatusLocationHelper(local_state_); | |
| 148 if (geolocation_provider_for_test_) { | |
| 149 location_helper_->SetGeolocationProviderForTest( | |
| 150 geolocation_provider_for_test_); | |
| 151 } | |
| 152 } | |
| 153 } else { | |
| 154 if (location_helper_) { | |
| 155 location_helper_->Destruct(); | |
| 156 location_helper_ = NULL; | |
| 157 } | |
| 158 local_state_->ClearPref(prefs::kDeviceLocation); | |
| 159 } | |
| 128 } | 160 } |
| 129 | 161 |
| 130 Time DeviceStatusCollector::GetCurrentTime() { | 162 Time DeviceStatusCollector::GetCurrentTime() { |
| 131 return Time::Now(); | 163 return Time::Now(); |
| 132 } | 164 } |
| 133 | 165 |
| 134 // Remove all out-of-range activity times from the local store. | 166 // Remove all out-of-range activity times from the local store. |
| 135 void DeviceStatusCollector::PruneStoredActivityPeriods(Time base_time) { | 167 void DeviceStatusCollector::PruneStoredActivityPeriods(Time base_time) { |
| 136 const DictionaryValue* activity_times = | 168 const DictionaryValue* activity_times = |
| 137 local_state_->GetDictionary(prefs::kDeviceActivityTimes); | 169 local_state_->GetDictionary(prefs::kDeviceActivityTimes); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 std::string dev_switch_mode; | 283 std::string dev_switch_mode; |
| 252 if (statistics_provider_->GetMachineStatistic( | 284 if (statistics_provider_->GetMachineStatistic( |
| 253 "devsw_boot", &dev_switch_mode)) { | 285 "devsw_boot", &dev_switch_mode)) { |
| 254 if (dev_switch_mode == "1") | 286 if (dev_switch_mode == "1") |
| 255 request->set_boot_mode("Dev"); | 287 request->set_boot_mode("Dev"); |
| 256 else if (dev_switch_mode == "0") | 288 else if (dev_switch_mode == "0") |
| 257 request->set_boot_mode("Verified"); | 289 request->set_boot_mode("Verified"); |
| 258 } | 290 } |
| 259 } | 291 } |
| 260 | 292 |
| 293 void DeviceStatusCollector::GetLocation( | |
| 294 em::DeviceStatusReportRequest* request) { | |
| 295 em::DeviceLocation* location = request->mutable_device_location(); | |
| 296 if (!location_helper_) { | |
| 297 location->set_error_code( | |
| 298 em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE); | |
| 299 } else { | |
| 300 const Geoposition& position = location_helper_->GetPosition(); | |
| 301 if (!position.IsValidFix()) { | |
| 302 location->set_error_code( | |
| 303 em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE); | |
| 304 location->set_error_message(position.error_message); | |
| 305 } else { | |
| 306 location->set_latitude(position.latitude); | |
| 307 location->set_longitude(position.longitude); | |
| 308 location->set_accuracy(position.accuracy); | |
| 309 location->set_timestamp( | |
| 310 (position.timestamp - Time::UnixEpoch()).InMilliseconds()); | |
| 311 if (position.is_valid_altitude()) | |
| 312 location->set_altitude(position.altitude); | |
| 313 if (position.is_valid_altitude_accuracy()) | |
| 314 location->set_altitude_accuracy(position.altitude_accuracy); | |
| 315 if (position.is_valid_heading()) | |
| 316 location->set_heading(position.heading); | |
| 317 if (position.is_valid_speed()) | |
| 318 location->set_speed(position.speed); | |
| 319 location->set_error_code(em::DeviceLocation::ERROR_CODE_NONE); | |
| 320 } | |
| 321 } | |
| 322 } | |
| 323 | |
| 261 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { | 324 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { |
| 262 if (report_activity_times_) | 325 if (report_activity_times_) |
| 263 GetActivityTimes(request); | 326 GetActivityTimes(request); |
| 264 | 327 |
| 265 if (report_version_info_) | 328 if (report_version_info_) |
| 266 GetVersionInfo(request); | 329 GetVersionInfo(request); |
| 267 | 330 |
| 268 if (report_boot_mode_) | 331 if (report_boot_mode_) |
| 269 GetBootMode(request); | 332 GetBootMode(request); |
| 333 | |
| 334 if (report_location_) | |
| 335 GetLocation(request); | |
| 270 } | 336 } |
| 271 | 337 |
| 272 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, | 338 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, |
| 273 std::string version) { | 339 std::string version) { |
| 274 os_version_ = version; | 340 os_version_ = version; |
| 275 } | 341 } |
| 276 | 342 |
| 277 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, | 343 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, |
| 278 std::string version) { | 344 std::string version) { |
| 279 firmware_version_ = version; | 345 firmware_version_ = version; |
| 280 } | 346 } |
| 281 | 347 |
| 282 void DeviceStatusCollector::Observe( | 348 void DeviceStatusCollector::Observe( |
| 283 int type, | 349 int type, |
| 284 const content::NotificationSource& source, | 350 const content::NotificationSource& source, |
| 285 const content::NotificationDetails& details) { | 351 const content::NotificationDetails& details) { |
| 286 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED) | 352 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED) |
| 287 UpdateReportingSettings(); | 353 UpdateReportingSettings(); |
| 288 else | 354 else |
| 289 NOTREACHED(); | 355 NOTREACHED(); |
| 290 } | 356 } |
| 291 | 357 |
| 292 } // namespace policy | 358 } // namespace policy |
| OLD | NEW |