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 for (CallbackList::iterator it = callbacks_.begin(); | |
109 it != callbacks_.end(); | |
110 ++it) | |
111 it->Run(position); | |
John Knottenbelt
2012/05/04 10:16:58
It seems possible, perhaps likely, that a callback
bartfab (slow)
2012/05/04 10:35:58
Nice catch. Done.
| |
112 callbacks_.clear(); | |
113 OnClientsChanged(); | |
114 } | |
84 } | 115 } |
85 | 116 |
86 void GeolocationProvider::StartProviders( | 117 void GeolocationProvider::StartProviders( |
87 const GeolocationObserverOptions& options) { | 118 const GeolocationObserverOptions& options) { |
88 DCHECK(OnGeolocationThread()); | 119 DCHECK(OnGeolocationThread()); |
89 DCHECK(arbitrator_); | 120 DCHECK(arbitrator_); |
90 arbitrator_->StartProviders(options); | 121 arbitrator_->StartProviders(options); |
91 } | 122 } |
92 | 123 |
93 void GeolocationProvider::StopProviders() { | 124 void GeolocationProvider::StopProviders() { |
94 DCHECK(OnGeolocationThread()); | 125 DCHECK(OnGeolocationThread()); |
95 DCHECK(arbitrator_); | 126 DCHECK(arbitrator_); |
96 arbitrator_->StopProviders(); | 127 arbitrator_->StopProviders(); |
97 } | 128 } |
98 | 129 |
99 void GeolocationProvider::OnPermissionGranted() { | 130 void GeolocationProvider::OnPermissionGranted() { |
100 DCHECK(OnClientThread()); | 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
132 bool was_permission_granted = is_permission_granted_; | |
101 is_permission_granted_ = true; | 133 is_permission_granted_ = true; |
102 if (IsRunning()) | 134 if (IsRunning() && !was_permission_granted) |
103 InformProvidersPermissionGranted(); | 135 InformProvidersPermissionGranted(); |
104 } | 136 } |
105 | 137 |
106 void GeolocationProvider::InformProvidersPermissionGranted() { | 138 void GeolocationProvider::InformProvidersPermissionGranted() { |
107 DCHECK(IsRunning()); | 139 DCHECK(IsRunning()); |
108 if (!OnGeolocationThread()) { | 140 if (!OnGeolocationThread()) { |
109 message_loop()->PostTask( | 141 message_loop()->PostTask( |
110 FROM_HERE, | 142 FROM_HERE, |
111 base::Bind(&GeolocationProvider::InformProvidersPermissionGranted, | 143 base::Bind(&GeolocationProvider::InformProvidersPermissionGranted, |
112 base::Unretained(this))); | 144 base::Unretained(this))); |
(...skipping 15 matching lines...) Expand all Loading... | |
128 delete arbitrator_; | 160 delete arbitrator_; |
129 arbitrator_ = NULL; | 161 arbitrator_ = NULL; |
130 } | 162 } |
131 | 163 |
132 void GeolocationProvider::OnLocationUpdate( | 164 void GeolocationProvider::OnLocationUpdate( |
133 const content::Geoposition& position) { | 165 const content::Geoposition& position) { |
134 DCHECK(OnGeolocationThread()); | 166 DCHECK(OnGeolocationThread()); |
135 // Will be true only in testing. | 167 // Will be true only in testing. |
136 if (ignore_location_updates_) | 168 if (ignore_location_updates_) |
137 return; | 169 return; |
138 client_loop_->PostTask( | 170 BrowserThread::PostTask(BrowserThread::IO, |
139 FROM_HERE, | 171 FROM_HERE, |
140 base::Bind(&GeolocationProvider::NotifyObservers, | 172 base::Bind(&GeolocationProvider::NotifyClients, |
141 base::Unretained(this), position)); | 173 base::Unretained(this), position)); |
142 } | 174 } |
143 | 175 |
144 void GeolocationProvider::OverrideLocationForTesting( | 176 void GeolocationProvider::OverrideLocationForTesting( |
145 const content::Geoposition& position) { | 177 const content::Geoposition& position) { |
146 DCHECK(OnClientThread()); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
147 position_ = position; | 179 position_ = position; |
148 ignore_location_updates_ = true; | 180 ignore_location_updates_ = true; |
149 NotifyObservers(position); | 181 NotifyClients(position); |
150 } | 182 } |
151 | 183 |
152 bool GeolocationProvider::HasPermissionBeenGranted() const { | 184 bool GeolocationProvider::HasPermissionBeenGranted() const { |
153 DCHECK(OnClientThread()); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
154 return is_permission_granted_; | 186 return is_permission_granted_; |
155 } | 187 } |
156 | 188 |
157 bool GeolocationProvider::OnClientThread() const { | |
158 return client_loop_->BelongsToCurrentThread(); | |
159 } | |
160 | |
161 bool GeolocationProvider::OnGeolocationThread() const { | 189 bool GeolocationProvider::OnGeolocationThread() const { |
162 return MessageLoop::current() == message_loop(); | 190 return MessageLoop::current() == message_loop(); |
163 } | 191 } |
OLD | NEW |