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