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/bind_helpers.h" |
| 9 #include "base/location.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" |
9 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/values.h" |
10 #include "chrome/browser/chromeos/cros_settings.h" | 14 #include "chrome/browser/chromeos/cros_settings.h" |
11 #include "chrome/browser/chromeos/cros_settings_names.h" | 15 #include "chrome/browser/chromeos/cros_settings_names.h" |
12 #include "chrome/browser/chromeos/system/statistics_provider.h" | 16 #include "chrome/browser/chromeos/system/statistics_provider.h" |
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 17 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
14 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
16 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_version_info.h" | 21 #include "chrome/common/chrome_version_info.h" |
18 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 23 #include "content/public/browser/notification_details.h" |
| 24 #include "content/public/browser/notification_source.h" |
19 | 25 |
20 using base::Time; | 26 using base::Time; |
21 using base::TimeDelta; | 27 using base::TimeDelta; |
22 using chromeos::VersionLoader; | 28 using chromeos::VersionLoader; |
23 | 29 |
24 namespace em = enterprise_management; | 30 namespace em = enterprise_management; |
25 | 31 |
26 namespace { | 32 namespace { |
27 // How many seconds of inactivity triggers the idle state. | 33 // How many seconds of inactivity triggers the idle state. |
28 const unsigned int kIdleStateThresholdSeconds = 300; | 34 const unsigned int kIdleStateThresholdSeconds = 300; |
29 | 35 |
30 // How many days in the past to store active periods for. | 36 // How many days in the past to store active periods for. |
31 const unsigned int kMaxStoredPastActivityDays = 30; | 37 const unsigned int kMaxStoredPastActivityDays = 30; |
32 | 38 |
33 // How many days in the future to store active periods for. | 39 // How many days in the future to store active periods for. |
34 const unsigned int kMaxStoredFutureActivityDays = 2; | 40 const unsigned int kMaxStoredFutureActivityDays = 2; |
35 | 41 |
| 42 // How often, in seconds, to update the device location. |
| 43 const unsigned int kGeolocationPollIntervalSeconds = 30 * 60; |
| 44 |
36 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; | 45 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; |
37 | 46 |
| 47 const char kLatitude[] = "latitude"; |
| 48 |
| 49 const char kLongitude[] = "longitude"; |
| 50 |
| 51 const char kAltitude[] = "altitude"; |
| 52 |
| 53 const char kAccuracy[] = "accuracy"; |
| 54 |
| 55 const char kAltitudeAccuracy[] = "altitude_accuracy"; |
| 56 |
| 57 const char kHeading[] = "heading"; |
| 58 |
| 59 const char kSpeed[] = "speed"; |
| 60 |
| 61 const char kTimestamp[] = "timestamp"; |
| 62 |
38 // Record device activity for the specified day into the given dictionary. | 63 // Record device activity for the specified day into the given dictionary. |
39 void AddDeviceActivity(DictionaryValue* activity_times, | 64 void AddDeviceActivity(base::DictionaryValue* activity_times, |
40 Time day_midnight, | 65 Time day_midnight, |
41 TimeDelta activity) { | 66 TimeDelta activity) { |
42 DCHECK(activity.InMilliseconds() < kMillisecondsPerDay); | 67 DCHECK(activity.InMilliseconds() < kMillisecondsPerDay); |
43 int64 day_start_timestamp = | 68 int64 day_start_timestamp = |
44 (day_midnight - Time::UnixEpoch()).InMilliseconds(); | 69 (day_midnight - Time::UnixEpoch()).InMilliseconds(); |
45 std::string day_key = base::Int64ToString(day_start_timestamp); | 70 std::string day_key = base::Int64ToString(day_start_timestamp); |
46 int previous_activity = 0; | 71 int previous_activity = 0; |
47 activity_times->GetInteger(day_key, &previous_activity); | 72 activity_times->GetInteger(day_key, &previous_activity); |
48 activity_times->SetInteger(day_key, | 73 activity_times->SetInteger(day_key, |
49 previous_activity + activity.InMilliseconds()); | 74 previous_activity + activity.InMilliseconds()); |
50 } | 75 } |
51 | 76 |
52 } // namespace | 77 } // namespace |
53 | 78 |
54 namespace policy { | 79 namespace policy { |
55 | 80 |
56 DeviceStatusCollector::DeviceStatusCollector( | 81 DeviceStatusCollector::DeviceStatusCollector( |
57 PrefService* local_state, | 82 PrefService* local_state, |
58 chromeos::system::StatisticsProvider* provider) | 83 chromeos::system::StatisticsProvider* provider, |
| 84 LocationUpdateRequester location_update_requester) |
59 : max_stored_past_activity_days_(kMaxStoredPastActivityDays), | 85 : max_stored_past_activity_days_(kMaxStoredPastActivityDays), |
60 max_stored_future_activity_days_(kMaxStoredFutureActivityDays), | 86 max_stored_future_activity_days_(kMaxStoredFutureActivityDays), |
61 local_state_(local_state), | 87 local_state_(local_state), |
62 last_idle_check_(Time()), | 88 last_idle_check_(Time()), |
| 89 geolocation_update_in_progress_(false), |
63 statistics_provider_(provider), | 90 statistics_provider_(provider), |
| 91 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 92 location_update_requester_(location_update_requester), |
64 report_version_info_(false), | 93 report_version_info_(false), |
65 report_activity_times_(false), | 94 report_activity_times_(false), |
66 report_boot_mode_(false) { | 95 report_boot_mode_(false), |
67 timer_.Start(FROM_HERE, | 96 report_location_(false) { |
68 TimeDelta::FromSeconds(kPollIntervalSeconds), | 97 if (!location_update_requester_) |
69 this, &DeviceStatusCollector::CheckIdleState); | 98 location_update_requester_ = &content::RequestLocationUpdate; |
| 99 idle_poll_timer_.Start(FROM_HERE, |
| 100 TimeDelta::FromSeconds(kIdlePollIntervalSeconds), |
| 101 this, &DeviceStatusCollector::CheckIdleState); |
70 | 102 |
71 cros_settings_ = chromeos::CrosSettings::Get(); | 103 cros_settings_ = chromeos::CrosSettings::Get(); |
72 | 104 |
73 // Watch for changes to the individual policies that control what the status | 105 // Watch for changes to the individual policies that control what the status |
74 // reports contain. | 106 // reports contain. |
75 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); | 107 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); |
76 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, | 108 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, |
77 this); | 109 this); |
78 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); | 110 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); |
| 111 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceLocation, this); |
| 112 |
| 113 // The last known location is persisted in local state. This makes location |
| 114 // information available immediately upon startup and avoids the need to |
| 115 // reacquire the location on every user session change or browser crash. |
| 116 content::Geoposition position; |
| 117 std::string timestamp_str; |
| 118 int64 timestamp; |
| 119 const base::DictionaryValue* location = |
| 120 local_state_->GetDictionary(prefs::kDeviceLocation); |
| 121 if (location->GetDouble(kLatitude, &position.latitude) && |
| 122 location->GetDouble(kLongitude, &position.longitude) && |
| 123 location->GetDouble(kAltitude, &position.altitude) && |
| 124 location->GetDouble(kAccuracy, &position.accuracy) && |
| 125 location->GetDouble(kAltitudeAccuracy, &position.altitude_accuracy) && |
| 126 location->GetDouble(kHeading, &position.heading) && |
| 127 location->GetDouble(kSpeed, &position.speed) && |
| 128 location->GetString(kTimestamp, ×tamp_str) && |
| 129 base::StringToInt64(timestamp_str, ×tamp)) { |
| 130 position.timestamp = Time::FromInternalValue(timestamp); |
| 131 position_ = position; |
| 132 } |
79 | 133 |
80 // Fetch the current values of the policies. | 134 // Fetch the current values of the policies. |
81 UpdateReportingSettings(); | 135 UpdateReportingSettings(); |
82 | 136 |
83 // Get the the OS and firmware version info. | 137 // Get the the OS and firmware version info. |
84 version_loader_.GetVersion(&consumer_, | 138 version_loader_.GetVersion(&consumer_, |
85 base::Bind(&DeviceStatusCollector::OnOSVersion, | 139 base::Bind(&DeviceStatusCollector::OnOSVersion, |
86 base::Unretained(this)), | 140 base::Unretained(this)), |
87 VersionLoader::VERSION_FULL); | 141 VersionLoader::VERSION_FULL); |
88 version_loader_.GetFirmware(&consumer_, | 142 version_loader_.GetFirmware(&consumer_, |
89 base::Bind(&DeviceStatusCollector::OnOSFirmware, | 143 base::Bind(&DeviceStatusCollector::OnOSFirmware, |
90 base::Unretained(this))); | 144 base::Unretained(this))); |
91 } | 145 } |
92 | 146 |
93 DeviceStatusCollector::~DeviceStatusCollector() { | 147 DeviceStatusCollector::~DeviceStatusCollector() { |
94 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, | 148 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, |
95 this); | 149 this); |
96 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, | 150 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, |
97 this); | 151 this); |
98 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); | 152 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); |
| 153 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceLocation, this); |
99 } | 154 } |
100 | 155 |
101 // static | 156 // static |
102 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { | 157 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { |
103 local_state->RegisterDictionaryPref(prefs::kDeviceActivityTimes, | 158 local_state->RegisterDictionaryPref(prefs::kDeviceActivityTimes, |
104 new DictionaryValue); | 159 new base::DictionaryValue); |
| 160 local_state->RegisterDictionaryPref(prefs::kDeviceLocation, |
| 161 new base::DictionaryValue); |
105 } | 162 } |
106 | 163 |
107 void DeviceStatusCollector::CheckIdleState() { | 164 void DeviceStatusCollector::CheckIdleState() { |
108 CalculateIdleState(kIdleStateThresholdSeconds, | 165 CalculateIdleState(kIdleStateThresholdSeconds, |
109 base::Bind(&DeviceStatusCollector::IdleStateCallback, | 166 base::Bind(&DeviceStatusCollector::IdleStateCallback, |
110 base::Unretained(this))); | 167 base::Unretained(this))); |
111 } | 168 } |
112 | 169 |
113 void DeviceStatusCollector::UpdateReportingSettings() { | 170 void DeviceStatusCollector::UpdateReportingSettings() { |
114 // Attempt to fetch the current value of the reporting settings. | 171 // Attempt to fetch the current value of the reporting settings. |
115 // If trusted values are not available, register this function to be called | 172 // If trusted values are not available, register this function to be called |
116 // back when they are available. | 173 // back when they are available. |
117 if (!cros_settings_->PrepareTrustedValues( | 174 if (!cros_settings_->PrepareTrustedValues( |
118 base::Bind(&DeviceStatusCollector::UpdateReportingSettings, | 175 base::Bind(&DeviceStatusCollector::UpdateReportingSettings, |
119 base::Unretained(this)))) { | 176 base::Unretained(this)))) { |
120 return; | 177 return; |
121 } | 178 } |
122 cros_settings_->GetBoolean( | 179 cros_settings_->GetBoolean( |
123 chromeos::kReportDeviceVersionInfo, &report_version_info_); | 180 chromeos::kReportDeviceVersionInfo, &report_version_info_); |
124 cros_settings_->GetBoolean( | 181 cros_settings_->GetBoolean( |
125 chromeos::kReportDeviceActivityTimes, &report_activity_times_); | 182 chromeos::kReportDeviceActivityTimes, &report_activity_times_); |
126 cros_settings_->GetBoolean( | 183 cros_settings_->GetBoolean( |
127 chromeos::kReportDeviceBootMode, &report_boot_mode_); | 184 chromeos::kReportDeviceBootMode, &report_boot_mode_); |
| 185 cros_settings_->GetBoolean( |
| 186 chromeos::kReportDeviceLocation, &report_location_); |
| 187 |
| 188 if (report_location_) { |
| 189 ScheduleGeolocationUpdateRequest(); |
| 190 } else { |
| 191 geolocation_update_timer_.Stop(); |
| 192 position_ = content::Geoposition(); |
| 193 local_state_->ClearPref(prefs::kDeviceLocation); |
| 194 } |
128 } | 195 } |
129 | 196 |
130 Time DeviceStatusCollector::GetCurrentTime() { | 197 Time DeviceStatusCollector::GetCurrentTime() { |
131 return Time::Now(); | 198 return Time::Now(); |
132 } | 199 } |
133 | 200 |
134 // Remove all out-of-range activity times from the local store. | 201 // Remove all out-of-range activity times from the local store. |
135 void DeviceStatusCollector::PruneStoredActivityPeriods(Time base_time) { | 202 void DeviceStatusCollector::PruneStoredActivityPeriods(Time base_time) { |
136 const DictionaryValue* activity_times = | 203 const base::DictionaryValue* activity_times = |
137 local_state_->GetDictionary(prefs::kDeviceActivityTimes); | 204 local_state_->GetDictionary(prefs::kDeviceActivityTimes); |
138 if (activity_times->size() <= | 205 if (activity_times->size() <= |
139 max_stored_past_activity_days_ + max_stored_future_activity_days_) | 206 max_stored_past_activity_days_ + max_stored_future_activity_days_) |
140 return; | 207 return; |
141 | 208 |
142 Time min_time = | 209 Time min_time = |
143 base_time - TimeDelta::FromDays(max_stored_past_activity_days_); | 210 base_time - TimeDelta::FromDays(max_stored_past_activity_days_); |
144 Time max_time = | 211 Time max_time = |
145 base_time + TimeDelta::FromDays(max_stored_future_activity_days_); | 212 base_time + TimeDelta::FromDays(max_stored_future_activity_days_); |
146 const Time epoch = Time::UnixEpoch(); | 213 const Time epoch = Time::UnixEpoch(); |
147 | 214 |
148 scoped_ptr<DictionaryValue> copy(activity_times->DeepCopy()); | 215 scoped_ptr<base::DictionaryValue> copy(activity_times->DeepCopy()); |
149 for (DictionaryValue::key_iterator it = activity_times->begin_keys(); | 216 for (base::DictionaryValue::key_iterator it = activity_times->begin_keys(); |
150 it != activity_times->end_keys(); ++it) { | 217 it != activity_times->end_keys(); ++it) { |
151 int64 timestamp; | 218 int64 timestamp; |
152 | 219 |
153 if (base::StringToInt64(*it, ×tamp)) { | 220 if (base::StringToInt64(*it, ×tamp)) { |
154 // Remove data that is too old, or too far in the future. | 221 // Remove data that is too old, or too far in the future. |
155 Time day_midnight = epoch + TimeDelta::FromMilliseconds(timestamp); | 222 Time day_midnight = epoch + TimeDelta::FromMilliseconds(timestamp); |
156 if (day_midnight > min_time && day_midnight < max_time) | 223 if (day_midnight > min_time && day_midnight < max_time) |
157 continue; | 224 continue; |
158 } | 225 } |
159 // The entry is out of range or couldn't be parsed. Remove it. | 226 // The entry is out of range or couldn't be parsed. Remove it. |
160 copy->Remove(*it, NULL); | 227 copy->Remove(*it, NULL); |
161 } | 228 } |
162 local_state_->Set(prefs::kDeviceActivityTimes, *copy); | 229 local_state_->Set(prefs::kDeviceActivityTimes, *copy); |
163 } | 230 } |
164 | 231 |
165 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { | 232 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { |
166 DCHECK(start < end); | 233 DCHECK(start < end); |
167 | 234 |
168 // Maintain the list of active periods in a local_state pref. | 235 // Maintain the list of active periods in a local_state pref. |
169 DictionaryPrefUpdate update(local_state_, prefs::kDeviceActivityTimes); | 236 DictionaryPrefUpdate update(local_state_, prefs::kDeviceActivityTimes); |
170 DictionaryValue* activity_times = update.Get(); | 237 base::DictionaryValue* activity_times = update.Get(); |
171 | 238 |
172 Time midnight = end.LocalMidnight(); | 239 Time midnight = end.LocalMidnight(); |
173 | 240 |
174 // Figure out UTC midnight on the same day as the local day. | 241 // Figure out UTC midnight on the same day as the local day. |
175 Time::Exploded exploded; | 242 Time::Exploded exploded; |
176 midnight.LocalExplode(&exploded); | 243 midnight.LocalExplode(&exploded); |
177 Time utc_midnight = Time::FromUTCExploded(exploded); | 244 Time utc_midnight = Time::FromUTCExploded(exploded); |
178 | 245 |
179 // Record the device activity for today. | 246 // Record the device activity for today. |
180 TimeDelta activity_today = end - MAX(midnight, start); | 247 TimeDelta activity_today = end - MAX(midnight, start); |
(...skipping 13 matching lines...) Expand all Loading... |
194 return; | 261 return; |
195 | 262 |
196 Time now = GetCurrentTime(); | 263 Time now = GetCurrentTime(); |
197 | 264 |
198 if (state == IDLE_STATE_ACTIVE) { | 265 if (state == IDLE_STATE_ACTIVE) { |
199 // If it's been too long since the last report, or if the activity is | 266 // If it's been too long since the last report, or if the activity is |
200 // negative (which can happen when the clock changes), assume a single | 267 // negative (which can happen when the clock changes), assume a single |
201 // interval of activity. | 268 // interval of activity. |
202 int active_seconds = (now - last_idle_check_).InSeconds(); | 269 int active_seconds = (now - last_idle_check_).InSeconds(); |
203 if (active_seconds < 0 || | 270 if (active_seconds < 0 || |
204 active_seconds >= static_cast<int>((2 * kPollIntervalSeconds))) | 271 active_seconds >= static_cast<int>((2 * kIdlePollIntervalSeconds))) |
205 AddActivePeriod(now - TimeDelta::FromSeconds(kPollIntervalSeconds), now); | 272 AddActivePeriod(now - TimeDelta::FromSeconds(kIdlePollIntervalSeconds), |
| 273 now); |
206 else | 274 else |
207 AddActivePeriod(last_idle_check_, now); | 275 AddActivePeriod(last_idle_check_, now); |
208 | 276 |
209 PruneStoredActivityPeriods(now); | 277 PruneStoredActivityPeriods(now); |
210 } | 278 } |
211 last_idle_check_ = now; | 279 last_idle_check_ = now; |
212 } | 280 } |
213 | 281 |
214 void DeviceStatusCollector::GetActivityTimes( | 282 void DeviceStatusCollector::GetActivityTimes( |
215 em::DeviceStatusReportRequest* request) { | 283 em::DeviceStatusReportRequest* request) { |
216 DictionaryPrefUpdate update(local_state_, prefs::kDeviceActivityTimes); | 284 DictionaryPrefUpdate update(local_state_, prefs::kDeviceActivityTimes); |
217 DictionaryValue* activity_times = update.Get(); | 285 base::DictionaryValue* activity_times = update.Get(); |
218 | 286 |
219 for (DictionaryValue::key_iterator it = activity_times->begin_keys(); | 287 for (base::DictionaryValue::key_iterator it = activity_times->begin_keys(); |
220 it != activity_times->end_keys(); ++it) { | 288 it != activity_times->end_keys(); ++it) { |
221 int64 start_timestamp; | 289 int64 start_timestamp; |
222 int activity_milliseconds; | 290 int activity_milliseconds; |
223 if (base::StringToInt64(*it, &start_timestamp) && | 291 if (base::StringToInt64(*it, &start_timestamp) && |
224 activity_times->GetInteger(*it, &activity_milliseconds)) { | 292 activity_times->GetInteger(*it, &activity_milliseconds)) { |
225 // This is correct even when there are leap seconds, because when a leap | 293 // This is correct even when there are leap seconds, because when a leap |
226 // second occurs, two consecutive seconds have the same timestamp. | 294 // second occurs, two consecutive seconds have the same timestamp. |
227 int64 end_timestamp = start_timestamp + kMillisecondsPerDay; | 295 int64 end_timestamp = start_timestamp + kMillisecondsPerDay; |
228 | 296 |
229 em::ActiveTimePeriod* active_period = request->add_active_period(); | 297 em::ActiveTimePeriod* active_period = request->add_active_period(); |
(...skipping 21 matching lines...) Expand all Loading... |
251 std::string dev_switch_mode; | 319 std::string dev_switch_mode; |
252 if (statistics_provider_->GetMachineStatistic( | 320 if (statistics_provider_->GetMachineStatistic( |
253 "devsw_boot", &dev_switch_mode)) { | 321 "devsw_boot", &dev_switch_mode)) { |
254 if (dev_switch_mode == "1") | 322 if (dev_switch_mode == "1") |
255 request->set_boot_mode("Dev"); | 323 request->set_boot_mode("Dev"); |
256 else if (dev_switch_mode == "0") | 324 else if (dev_switch_mode == "0") |
257 request->set_boot_mode("Verified"); | 325 request->set_boot_mode("Verified"); |
258 } | 326 } |
259 } | 327 } |
260 | 328 |
| 329 void DeviceStatusCollector::GetLocation( |
| 330 em::DeviceStatusReportRequest* request) { |
| 331 em::DeviceLocation* location = request->mutable_device_location(); |
| 332 if (!position_.Validate()) { |
| 333 location->set_error_code( |
| 334 em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE); |
| 335 location->set_error_message(position_.error_message); |
| 336 } else { |
| 337 location->set_latitude(position_.latitude); |
| 338 location->set_longitude(position_.longitude); |
| 339 location->set_accuracy(position_.accuracy); |
| 340 location->set_timestamp( |
| 341 (position_.timestamp - Time::UnixEpoch()).InMilliseconds()); |
| 342 // Lowest point on land is at approximately -400 meters. |
| 343 if (position_.altitude > -10000.) |
| 344 location->set_altitude(position_.altitude); |
| 345 if (position_.altitude_accuracy >= 0.) |
| 346 location->set_altitude_accuracy(position_.altitude_accuracy); |
| 347 if (position_.heading >= 0. && position_.heading <= 360) |
| 348 location->set_heading(position_.heading); |
| 349 if (position_.speed >= 0.) |
| 350 location->set_speed(position_.speed); |
| 351 location->set_error_code(em::DeviceLocation::ERROR_CODE_NONE); |
| 352 } |
| 353 } |
| 354 |
261 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { | 355 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { |
262 if (report_activity_times_) | 356 if (report_activity_times_) |
263 GetActivityTimes(request); | 357 GetActivityTimes(request); |
264 | 358 |
265 if (report_version_info_) | 359 if (report_version_info_) |
266 GetVersionInfo(request); | 360 GetVersionInfo(request); |
267 | 361 |
268 if (report_boot_mode_) | 362 if (report_boot_mode_) |
269 GetBootMode(request); | 363 GetBootMode(request); |
| 364 |
| 365 if (report_location_) |
| 366 GetLocation(request); |
270 } | 367 } |
271 | 368 |
272 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, | 369 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, |
273 std::string version) { | 370 std::string version) { |
274 os_version_ = version; | 371 os_version_ = version; |
275 } | 372 } |
276 | 373 |
277 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, | 374 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, |
278 std::string version) { | 375 std::string version) { |
279 firmware_version_ = version; | 376 firmware_version_ = version; |
280 } | 377 } |
281 | 378 |
282 void DeviceStatusCollector::Observe( | 379 void DeviceStatusCollector::Observe( |
283 int type, | 380 int type, |
284 const content::NotificationSource& source, | 381 const content::NotificationSource& source, |
285 const content::NotificationDetails& details) { | 382 const content::NotificationDetails& details) { |
286 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED) | 383 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED) |
287 UpdateReportingSettings(); | 384 UpdateReportingSettings(); |
288 else | 385 else |
289 NOTREACHED(); | 386 NOTREACHED(); |
290 } | 387 } |
291 | 388 |
| 389 void DeviceStatusCollector::ScheduleGeolocationUpdateRequest() { |
| 390 if (geolocation_update_timer_.IsRunning() || geolocation_update_in_progress_) |
| 391 return; |
| 392 |
| 393 if (position_.Validate()) { |
| 394 TimeDelta elapsed = Time::Now() - position_.timestamp; |
| 395 TimeDelta interval = |
| 396 TimeDelta::FromSeconds(kGeolocationPollIntervalSeconds); |
| 397 if (elapsed > interval) { |
| 398 geolocation_update_in_progress_ = true; |
| 399 location_update_requester_(base::Bind( |
| 400 &DeviceStatusCollector::ReceiveGeolocationUpdate, |
| 401 weak_factory_.GetWeakPtr())); |
| 402 } else { |
| 403 geolocation_update_timer_.Start( |
| 404 FROM_HERE, |
| 405 interval - elapsed, |
| 406 this, |
| 407 &DeviceStatusCollector::ScheduleGeolocationUpdateRequest); |
| 408 } |
| 409 } else { |
| 410 geolocation_update_in_progress_ = true; |
| 411 location_update_requester_(base::Bind( |
| 412 &DeviceStatusCollector::ReceiveGeolocationUpdate, |
| 413 weak_factory_.GetWeakPtr())); |
| 414 |
| 415 } |
| 416 } |
| 417 |
| 418 void DeviceStatusCollector::ReceiveGeolocationUpdate( |
| 419 const content::Geoposition& position) { |
| 420 geolocation_update_in_progress_ = false; |
| 421 |
| 422 // Ignore update if device location reporting has since been disabled. |
| 423 if (!report_location_) |
| 424 return; |
| 425 |
| 426 if (position.Validate()) { |
| 427 position_ = position; |
| 428 base::DictionaryValue location; |
| 429 location.SetDouble(kLatitude, position.latitude); |
| 430 location.SetDouble(kLongitude, position.longitude); |
| 431 location.SetDouble(kAltitude, position.altitude); |
| 432 location.SetDouble(kAccuracy, position.accuracy); |
| 433 location.SetDouble(kAltitudeAccuracy, position.altitude_accuracy); |
| 434 location.SetDouble(kHeading, position.heading); |
| 435 location.SetDouble(kSpeed, position.speed); |
| 436 location.SetString(kTimestamp, |
| 437 base::Int64ToString(position.timestamp.ToInternalValue())); |
| 438 local_state_->Set(prefs::kDeviceLocation, location); |
| 439 } |
| 440 |
| 441 ScheduleGeolocationUpdateRequest(); |
| 442 } |
| 443 |
292 } // namespace policy | 444 } // namespace policy |
OLD | NEW |