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