OLD | NEW |
(Empty) | |
| 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 #include "chrome/browser/policy/device_status_location_helper.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/location.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/string_number_conversions.h" |
| 14 #include "base/time.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/common/pref_names.h" |
| 18 #include "content/browser/geolocation/geolocation_observer.h" |
| 19 #include "content/browser/geolocation/geolocation_provider.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 |
| 22 using content::BrowserThread; |
| 23 |
| 24 namespace policy { |
| 25 |
| 26 DeviceStatusLocationHelper::DeviceStatusLocationHelper( |
| 27 PrefService* local_state) |
| 28 : local_state_(local_state), |
| 29 timer_(NULL), |
| 30 provider_(NULL), |
| 31 observing_(false) { |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 33 Geoposition position; |
| 34 std::string timestamp_str; |
| 35 int64 timestamp; |
| 36 // The last known location is persisted in local state. This makes location |
| 37 // information available immediately upon startup and avoids the need to |
| 38 // reacquire the location on every user session change or browser crash. |
| 39 const base::DictionaryValue* location = |
| 40 local_state_->GetDictionary(prefs::kDeviceLocation); |
| 41 if (location->GetDouble("latitude", &position.latitude) && |
| 42 location->GetDouble("longitude", &position.longitude) && |
| 43 location->GetDouble("altitude", &position.altitude) && |
| 44 location->GetDouble("accuracy", &position.accuracy) && |
| 45 location->GetDouble("altitude_accuracy", |
| 46 &position.altitude_accuracy) && |
| 47 location->GetDouble("heading", &position.heading) && |
| 48 location->GetDouble("speed", &position.speed) && |
| 49 location->GetString("timestamp", ×tamp_str) && |
| 50 base::StringToInt64(timestamp_str, ×tamp)) { |
| 51 position.timestamp = base::Time::FromInternalValue(timestamp); |
| 52 position_ = position; |
| 53 } |
| 54 BrowserThread::PostTask(BrowserThread::IO, |
| 55 FROM_HERE, |
| 56 base::Bind(&DeviceStatusLocationHelper::Init, |
| 57 base::Unretained(this), |
| 58 position_)); |
| 59 } |
| 60 |
| 61 void DeviceStatusLocationHelper::Destruct() { |
| 62 // This class has a specific shutdown sequence that ensures all classes it |
| 63 // interfaces with are notified on the appropriate threads and any tasks |
| 64 // posted to it are processed before destruction. |
| 65 // |
| 66 // The shutdown sequence is initiated by calling Destruct() on the UI thread: |
| 67 // |
| 68 // 1. Destruct() makes the UI thread ignore any further updates it receives, |
| 69 // then posts DestructInternal() to the IO thread. |
| 70 // 2. DestructInternal() reaches the IO thread after any updates queued up for |
| 71 // it have been processed. This function destroys the poll timer and |
| 72 // detaches from the GeolocationProvider so that no further updates can be |
| 73 // received by the IO thread. Then, it posts DeleteSoon() back to the UI |
| 74 // thread. |
| 75 // 3. DeleteSoon() reaches the UI thread after any updates queued for it have |
| 76 // been processed. At this point, there are no more tasks queued up for |
| 77 // either of the threads and it is safe for the object to be deleted. |
| 78 // 4. ~DeviceStatusLocationHelper() is run by DeleteSoon() and the object RIP. |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 80 local_state_ = NULL; |
| 81 BrowserThread::PostTask( |
| 82 BrowserThread::IO, |
| 83 FROM_HERE, |
| 84 base::Bind(&DeviceStatusLocationHelper::DestructInternal, |
| 85 base::Unretained(this))); |
| 86 } |
| 87 |
| 88 const Geoposition& DeviceStatusLocationHelper::GetPosition() const { |
| 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 90 return position_; |
| 91 } |
| 92 |
| 93 // static |
| 94 void DeviceStatusLocationHelper::RegisterPrefs(PrefService* local_state) { |
| 95 local_state->RegisterDictionaryPref(prefs::kDeviceLocation, |
| 96 new DictionaryValue); |
| 97 } |
| 98 |
| 99 void DeviceStatusLocationHelper::OnLocationUpdate(const Geoposition& position) { |
| 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 101 if (!position.IsValidFix()) |
| 102 return; |
| 103 BrowserThread::PostTask(BrowserThread::UI, |
| 104 FROM_HERE, |
| 105 base::Bind(&DeviceStatusLocationHelper::ReceiveUpdate, |
| 106 base::Unretained(this), position)); |
| 107 StopObserving(); |
| 108 ScheduleUpdate(position); |
| 109 } |
| 110 |
| 111 void DeviceStatusLocationHelper::SetGeolocationProviderForTest( |
| 112 GeolocationProvider* provider) { |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 114 DCHECK(!provider_); |
| 115 provider_ = provider; |
| 116 } |
| 117 |
| 118 DeviceStatusLocationHelper::~DeviceStatusLocationHelper() { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 120 DCHECK(local_state_ == NULL); |
| 121 DCHECK(timer_ == NULL); |
| 122 } |
| 123 |
| 124 void DeviceStatusLocationHelper::Init(const Geoposition& position) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 126 if (!provider_) |
| 127 provider_ = GeolocationProvider::GetInstance(); |
| 128 timer_ = new base::OneShotTimer<DeviceStatusLocationHelper>(); |
| 129 provider_->OnPermissionGranted(); |
| 130 ScheduleUpdate(position); |
| 131 } |
| 132 |
| 133 void DeviceStatusLocationHelper::DestructInternal() { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 135 delete timer_; |
| 136 timer_ = 0; |
| 137 StopObserving(); |
| 138 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); |
| 139 } |
| 140 |
| 141 void DeviceStatusLocationHelper::ScheduleUpdate(const Geoposition& position) { |
| 142 // A timer is used to schedule location updates every kPollIntervalSeconds. |
| 143 // This serves two purposes, depending on circumstances: |
| 144 // 1. This class is the only client of the geolocation stack: In this case, |
| 145 // battery is saved as the geolocation stack needs to become active only |
| 146 // once every kPollIntervalSeconds (which is expected to be orders of |
| 147 // magnitude larger than its internal refresh period). |
| 148 // 2. The geolocation stack is active and continuously updating due to other |
| 149 // clients: In this case, user privacy is protected by ignoring the more |
| 150 // frequent updates. |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 152 if (position.IsValidFix()) { |
| 153 base::TimeDelta elapsed = base::Time::Now() - position.timestamp; |
| 154 base::TimeDelta interval = |
| 155 base::TimeDelta::FromSeconds(kPollIntervalSeconds); |
| 156 if (elapsed > interval) { |
| 157 StartObserving(); |
| 158 } else { |
| 159 timer_->Start(FROM_HERE, |
| 160 interval - elapsed, |
| 161 this, |
| 162 &DeviceStatusLocationHelper::StartObserving); |
| 163 } |
| 164 } else { |
| 165 StartObserving(); |
| 166 } |
| 167 } |
| 168 |
| 169 void DeviceStatusLocationHelper::StartObserving() { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 171 timer_->Stop(); |
| 172 provider_->AddObserver(this, GeolocationObserverOptions(true)); |
| 173 observing_ = true; |
| 174 } |
| 175 |
| 176 void DeviceStatusLocationHelper::StopObserving() { |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 178 if (!observing_) |
| 179 return; |
| 180 provider_->RemoveObserver(this); |
| 181 observing_ = false; |
| 182 } |
| 183 |
| 184 void DeviceStatusLocationHelper::ReceiveUpdate(const Geoposition& position) { |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 186 if (!local_state_) |
| 187 return; |
| 188 position_ = position; |
| 189 DictionaryValue location; |
| 190 location.SetDouble("latitude", position.latitude); |
| 191 location.SetDouble("longitude", position.longitude); |
| 192 location.SetDouble("altitude", position.altitude); |
| 193 location.SetDouble("accuracy", position.accuracy); |
| 194 location.SetDouble("altitude_accuracy", position.altitude_accuracy); |
| 195 location.SetDouble("heading", position.heading); |
| 196 location.SetDouble("speed", position.speed); |
| 197 location.SetString("timestamp", |
| 198 base::Int64ToString(position.timestamp.ToInternalValue())); |
| 199 local_state_->Set(prefs::kDeviceLocation, location); |
| 200 } |
| 201 |
| 202 } // namespace policy |
OLD | NEW |