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 "content/browser/geolocation/geolocation_provider.h" | 5 #include "content/browser/geolocation/geolocation_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/location.h" |
| 11 #include "base/logging.h" |
9 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
10 #include "base/threading/thread_restrictions.h" | 13 #include "base/message_loop.h" |
11 #include "content/browser/geolocation/location_arbitrator.h" | 14 #include "content/browser/geolocation/location_arbitrator.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 |
| 17 using content::BrowserThread; |
12 | 18 |
13 GeolocationProvider* GeolocationProvider::GetInstance() { | 19 GeolocationProvider* GeolocationProvider::GetInstance() { |
| 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
14 return Singleton<GeolocationProvider>::get(); | 21 return Singleton<GeolocationProvider>::get(); |
15 } | 22 } |
16 | 23 |
17 GeolocationProvider::GeolocationProvider() | 24 GeolocationProvider::GeolocationProvider() |
18 : base::Thread("Geolocation"), | 25 : base::Thread("Geolocation"), |
19 client_loop_(base::MessageLoopProxy::current()), | |
20 is_permission_granted_(false), | 26 is_permission_granted_(false), |
21 ignore_location_updates_(false), | 27 ignore_location_updates_(false), |
22 arbitrator_(NULL) { | 28 arbitrator_(NULL) { |
| 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
23 } | 30 } |
24 | 31 |
25 GeolocationProvider::~GeolocationProvider() { | 32 GeolocationProvider::~GeolocationProvider() { |
26 DCHECK(observers_.empty()); // observers must unregister. | 33 // All observers should have unregistered before this singleton is destructed. |
| 34 DCHECK(observers_.empty()); |
27 Stop(); | 35 Stop(); |
28 DCHECK(!arbitrator_); | 36 DCHECK(!arbitrator_); |
29 } | 37 } |
30 | 38 |
31 void GeolocationProvider::AddObserver(GeolocationObserver* observer, | 39 void GeolocationProvider::AddObserver(GeolocationObserver* observer, |
32 const GeolocationObserverOptions& update_options) { | 40 const GeolocationObserverOptions& update_options) { |
33 DCHECK(OnClientThread()); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
34 observers_[observer] = update_options; | 42 observers_[observer] = update_options; |
35 OnObserversChanged(); | 43 OnClientsChanged(); |
36 if (position_.Validate() || | 44 if (position_.Validate() || |
37 position_.error_code != content::Geoposition::ERROR_CODE_NONE) | 45 position_.error_code != content::Geoposition::ERROR_CODE_NONE) |
38 observer->OnLocationUpdate(position_); | 46 observer->OnLocationUpdate(position_); |
39 } | 47 } |
40 | 48 |
41 bool GeolocationProvider::RemoveObserver(GeolocationObserver* observer) { | 49 bool GeolocationProvider::RemoveObserver(GeolocationObserver* observer) { |
42 DCHECK(OnClientThread()); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
43 size_t remove = observers_.erase(observer); | 51 size_t removed = observers_.erase(observer); |
44 OnObserversChanged(); | 52 if (removed) |
45 return remove > 0; | 53 OnClientsChanged(); |
| 54 return removed > 0; |
46 } | 55 } |
47 | 56 |
48 void GeolocationProvider::OnObserversChanged() { | 57 void GeolocationProvider::RequestCallback( |
49 DCHECK(OnClientThread()); | 58 const content::GeolocationUpdateCallback& callback) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 60 callbacks_.push_back(callback); |
| 61 OnClientsChanged(); |
| 62 OnPermissionGranted(); |
| 63 } |
| 64 |
| 65 void GeolocationProvider::OnClientsChanged() { |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
50 base::Closure task; | 67 base::Closure task; |
51 if (observers_.empty()) { | 68 if (observers_.empty() && callbacks_.empty()) { |
52 DCHECK(IsRunning()); | 69 DCHECK(IsRunning()); |
53 task = base::Bind(&GeolocationProvider::StopProviders, | 70 task = base::Bind(&GeolocationProvider::StopProviders, |
54 base::Unretained(this)); | 71 base::Unretained(this)); |
55 } else { | 72 } else { |
56 if (!IsRunning()) { | 73 if (!IsRunning()) { |
57 Start(); | 74 Start(); |
58 if (HasPermissionBeenGranted()) | 75 if (HasPermissionBeenGranted()) |
59 InformProvidersPermissionGranted(); | 76 InformProvidersPermissionGranted(); |
60 } | 77 } |
| 78 // Determine a set of options that satisfies all clients. |
| 79 GeolocationObserverOptions options = |
| 80 GeolocationObserverOptions::Collapse(observers_); |
| 81 // For callbacks, high accuracy position information is always requested. |
| 82 if (!callbacks_.empty()) |
| 83 options.Collapse(GeolocationObserverOptions(true)); |
61 | 84 |
62 // The high accuracy requirement may have changed. | 85 // Send the current options to the providers as they may have changed. |
63 task = base::Bind(&GeolocationProvider::StartProviders, | 86 task = base::Bind(&GeolocationProvider::StartProviders, |
64 base::Unretained(this), | 87 base::Unretained(this), |
65 GeolocationObserverOptions::Collapse(observers_)); | 88 options); |
66 } | 89 } |
67 | 90 |
68 message_loop()->PostTask(FROM_HERE, task); | 91 message_loop()->PostTask(FROM_HERE, task); |
69 } | 92 } |
70 | 93 |
71 void GeolocationProvider::NotifyObservers( | 94 void GeolocationProvider::NotifyClients(const content::Geoposition& position) { |
72 const content::Geoposition& position) { | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
73 DCHECK(OnClientThread()); | |
74 DCHECK(position.Validate() || | 96 DCHECK(position.Validate() || |
75 position.error_code != content::Geoposition::ERROR_CODE_NONE); | 97 position.error_code != content::Geoposition::ERROR_CODE_NONE); |
76 position_ = position; | 98 position_ = position; |
77 ObserverMap::const_iterator it = observers_.begin(); | 99 ObserverMap::const_iterator it = observers_.begin(); |
78 while (it != observers_.end()) { | 100 while (it != observers_.end()) { |
79 // Advance iterator before callback to guard against synchronous unregister. | 101 // Advance iterator before calling the observer to guard against synchronous |
| 102 // unregister. |
80 GeolocationObserver* observer = it->first; | 103 GeolocationObserver* observer = it->first; |
81 ++it; | 104 ++it; |
82 observer->OnLocationUpdate(position_); | 105 observer->OnLocationUpdate(position_); |
83 } | 106 } |
| 107 if (!callbacks_.empty()) { |
| 108 // Copy the callback list to guard against synchronous callback requests |
| 109 // reallocating the vector and invalidating the iterator. |
| 110 CallbackList callbacks = callbacks_; |
| 111 callbacks_.clear(); |
| 112 for (CallbackList::iterator it = callbacks.begin(); |
| 113 it != callbacks.end(); |
| 114 ++it) |
| 115 it->Run(position); |
| 116 OnClientsChanged(); |
| 117 } |
84 } | 118 } |
85 | 119 |
86 void GeolocationProvider::StartProviders( | 120 void GeolocationProvider::StartProviders( |
87 const GeolocationObserverOptions& options) { | 121 const GeolocationObserverOptions& options) { |
88 DCHECK(OnGeolocationThread()); | 122 DCHECK(OnGeolocationThread()); |
89 DCHECK(arbitrator_); | 123 DCHECK(arbitrator_); |
90 arbitrator_->StartProviders(options); | 124 arbitrator_->StartProviders(options); |
91 } | 125 } |
92 | 126 |
93 void GeolocationProvider::StopProviders() { | 127 void GeolocationProvider::StopProviders() { |
94 DCHECK(OnGeolocationThread()); | 128 DCHECK(OnGeolocationThread()); |
95 DCHECK(arbitrator_); | 129 DCHECK(arbitrator_); |
96 arbitrator_->StopProviders(); | 130 arbitrator_->StopProviders(); |
97 } | 131 } |
98 | 132 |
99 void GeolocationProvider::OnPermissionGranted() { | 133 void GeolocationProvider::OnPermissionGranted() { |
100 DCHECK(OnClientThread()); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 135 bool was_permission_granted = is_permission_granted_; |
101 is_permission_granted_ = true; | 136 is_permission_granted_ = true; |
102 if (IsRunning()) | 137 if (IsRunning() && !was_permission_granted) |
103 InformProvidersPermissionGranted(); | 138 InformProvidersPermissionGranted(); |
104 } | 139 } |
105 | 140 |
106 void GeolocationProvider::InformProvidersPermissionGranted() { | 141 void GeolocationProvider::InformProvidersPermissionGranted() { |
107 DCHECK(IsRunning()); | 142 DCHECK(IsRunning()); |
108 if (!OnGeolocationThread()) { | 143 if (!OnGeolocationThread()) { |
109 message_loop()->PostTask( | 144 message_loop()->PostTask( |
110 FROM_HERE, | 145 FROM_HERE, |
111 base::Bind(&GeolocationProvider::InformProvidersPermissionGranted, | 146 base::Bind(&GeolocationProvider::InformProvidersPermissionGranted, |
112 base::Unretained(this))); | 147 base::Unretained(this))); |
(...skipping 15 matching lines...) Expand all Loading... |
128 delete arbitrator_; | 163 delete arbitrator_; |
129 arbitrator_ = NULL; | 164 arbitrator_ = NULL; |
130 } | 165 } |
131 | 166 |
132 void GeolocationProvider::OnLocationUpdate( | 167 void GeolocationProvider::OnLocationUpdate( |
133 const content::Geoposition& position) { | 168 const content::Geoposition& position) { |
134 DCHECK(OnGeolocationThread()); | 169 DCHECK(OnGeolocationThread()); |
135 // Will be true only in testing. | 170 // Will be true only in testing. |
136 if (ignore_location_updates_) | 171 if (ignore_location_updates_) |
137 return; | 172 return; |
138 client_loop_->PostTask( | 173 BrowserThread::PostTask(BrowserThread::IO, |
139 FROM_HERE, | 174 FROM_HERE, |
140 base::Bind(&GeolocationProvider::NotifyObservers, | 175 base::Bind(&GeolocationProvider::NotifyClients, |
141 base::Unretained(this), position)); | 176 base::Unretained(this), position)); |
142 } | 177 } |
143 | 178 |
144 void GeolocationProvider::OverrideLocationForTesting( | 179 void GeolocationProvider::OverrideLocationForTesting( |
145 const content::Geoposition& position) { | 180 const content::Geoposition& position) { |
146 DCHECK(OnClientThread()); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
147 position_ = position; | 182 position_ = position; |
148 ignore_location_updates_ = true; | 183 ignore_location_updates_ = true; |
149 NotifyObservers(position); | 184 NotifyClients(position); |
150 } | 185 } |
151 | 186 |
152 bool GeolocationProvider::HasPermissionBeenGranted() const { | 187 bool GeolocationProvider::HasPermissionBeenGranted() const { |
153 DCHECK(OnClientThread()); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
154 return is_permission_granted_; | 189 return is_permission_granted_; |
155 } | 190 } |
156 | 191 |
157 bool GeolocationProvider::OnClientThread() const { | |
158 return client_loop_->BelongsToCurrentThread(); | |
159 } | |
160 | |
161 bool GeolocationProvider::OnGeolocationThread() const { | 192 bool GeolocationProvider::OnGeolocationThread() const { |
162 return MessageLoop::current() == message_loop(); | 193 return MessageLoop::current() == message_loop(); |
163 } | 194 } |
OLD | NEW |