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 "device/geolocation/location_arbitrator_impl.h" | 5 #include "device/geolocation/location_arbitrator_impl.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | |
9 #include <utility> | |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
11 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
12 #include "build/build_config.h" | 14 #include "build/build_config.h" |
13 #include "device/geolocation/access_token_store.h" | 15 #include "device/geolocation/access_token_store.h" |
14 #include "device/geolocation/geolocation_delegate.h" | 16 #include "device/geolocation/geolocation_delegate.h" |
15 #include "device/geolocation/network_location_provider.h" | 17 #include "device/geolocation/network_location_provider.h" |
16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
17 | 19 |
18 namespace device { | 20 namespace device { |
19 namespace { | 21 namespace { |
20 | 22 |
21 const char* kDefaultNetworkProviderUrl = | 23 const char* kDefaultNetworkProviderUrl = |
22 "https://www.googleapis.com/geolocation/v1/geolocate"; | 24 "https://www.googleapis.com/geolocation/v1/geolocate"; |
23 } // namespace | 25 } // namespace |
24 | 26 |
25 // To avoid oscillations, set this to twice the expected update interval of a | 27 // To avoid oscillations, set this to twice the expected update interval of a |
26 // a GPS-type location provider (in case it misses a beat) plus a little. | 28 // a GPS-type location provider (in case it misses a beat) plus a little. |
27 const int64_t LocationArbitratorImpl::kFixStaleTimeoutMilliseconds = | 29 const int64_t LocationArbitratorImpl::kFixStaleTimeoutMilliseconds = |
28 11 * base::Time::kMillisecondsPerSecond; | 30 11 * base::Time::kMillisecondsPerSecond; |
29 | 31 |
30 LocationArbitratorImpl::LocationArbitratorImpl( | 32 LocationArbitratorImpl::LocationArbitratorImpl( |
31 const LocationUpdateCallback& callback, | 33 const LocationUpdateCallback& callback, |
Wez
2016/08/19 01:56:30
Looks like we should get rid of this parameter, si
CJ
2016/08/22 17:41:31
Done.
| |
32 GeolocationDelegate* delegate) | 34 GeolocationDelegate* delegate) |
33 : delegate_(delegate), | 35 : delegate_(delegate), |
34 arbitrator_update_callback_(callback), | 36 arbitrator_update_callback_(callback), |
35 provider_update_callback_( | 37 provider_update_callback_( |
36 base::Bind(&LocationArbitratorImpl::OnLocationUpdate, | 38 base::Bind(&LocationArbitratorImpl::OnLocationUpdate, |
37 base::Unretained(this))), | 39 base::Unretained(this))), |
38 position_provider_(nullptr), | 40 position_provider_(nullptr), |
39 is_permission_granted_(false), | 41 is_permission_granted_(false), |
40 is_running_(false) {} | 42 is_running_(false) {} |
41 | 43 |
42 LocationArbitratorImpl::~LocationArbitratorImpl() {} | 44 LocationArbitratorImpl::~LocationArbitratorImpl() {} |
43 | 45 |
44 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { | 46 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { |
45 return GURL(kDefaultNetworkProviderUrl); | 47 return GURL(kDefaultNetworkProviderUrl); |
46 } | 48 } |
47 | 49 |
48 void LocationArbitratorImpl::OnPermissionGranted() { | 50 void LocationArbitratorImpl::OnPermissionGranted() { |
49 is_permission_granted_ = true; | 51 is_permission_granted_ = true; |
50 for (const auto& provider : providers_) | 52 for (const auto& provider : providers_) |
51 provider->OnPermissionGranted(); | 53 provider->OnPermissionGranted(); |
52 } | 54 } |
53 | 55 |
54 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { | 56 bool LocationArbitratorImpl::StartProvider(bool enable_high_accuracy) { |
55 // Stash options as OnAccessTokenStoresLoaded has not yet been called. | 57 // Stash options as OnAccessTokenStoresLoaded has not yet been called. |
56 is_running_ = true; | 58 is_running_ = true; |
57 enable_high_accuracy_ = enable_high_accuracy; | 59 enable_high_accuracy_ = enable_high_accuracy; |
58 | 60 |
59 if (providers_.empty()) { | 61 if (providers_.empty()) { |
60 RegisterSystemProvider(); | 62 RegisterSystemProvider(); |
61 | 63 |
62 const scoped_refptr<AccessTokenStore> access_token_store = | 64 const scoped_refptr<AccessTokenStore> access_token_store = |
63 GetAccessTokenStore(); | 65 GetAccessTokenStore(); |
64 if (access_token_store && delegate_->UseNetworkLocationProviders()) { | 66 if (access_token_store && delegate_->UseNetworkLocationProviders()) { |
65 DCHECK(DefaultNetworkProviderURL().is_valid()); | 67 DCHECK(DefaultNetworkProviderURL().is_valid()); |
66 token_store_callback_.Reset( | 68 token_store_callback_.Reset( |
67 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, | 69 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, |
68 base::Unretained(this))); | 70 base::Unretained(this))); |
69 access_token_store->LoadAccessTokens(token_store_callback_.callback()); | 71 access_token_store->LoadAccessTokens(token_store_callback_.callback()); |
70 return; | 72 return true; |
71 } | 73 } |
72 } | 74 } |
73 DoStartProviders(); | 75 return DoStartProviders(); |
74 } | 76 } |
75 | 77 |
76 void LocationArbitratorImpl::DoStartProviders() { | 78 bool LocationArbitratorImpl::DoStartProviders() { |
77 if (providers_.empty()) { | 79 if (providers_.empty()) { |
78 // If no providers are available, we report an error to avoid | 80 // If no providers are available, we report an error to avoid |
79 // callers waiting indefinitely for a reply. | 81 // callers waiting indefinitely for a reply. |
80 Geoposition position; | 82 Geoposition position; |
81 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; | 83 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; |
82 arbitrator_update_callback_.Run(position); | 84 arbitrator_update_callback_.Run(position); |
83 return; | 85 return false; |
84 } | 86 } |
85 for (const auto& provider : providers_) | 87 bool started = false; |
86 provider->StartProvider(enable_high_accuracy_); | 88 for (const auto& provider : providers_) { |
89 started = provider->StartProvider(enable_high_accuracy_) || started; | |
Kevin M
2016/08/17 17:44:37
super nit: put "started || before provider->StartP
CJ
2016/08/17 23:07:11
Because of short-circuiting, once the first StartP
Wez
2016/08/19 01:33:11
Acknowledged.
| |
90 } | |
91 return started; | |
87 } | 92 } |
88 | 93 |
89 void LocationArbitratorImpl::StopProviders() { | 94 void LocationArbitratorImpl::StopProvider() { |
90 // Reset the reference location state (provider+position) | 95 // Reset the reference location state (provider+position) |
91 // so that future starts use fresh locations from | 96 // so that future starts use fresh locations from |
92 // the newly constructed providers. | 97 // the newly constructed providers. |
93 position_provider_ = nullptr; | 98 position_provider_ = nullptr; |
94 position_ = Geoposition(); | 99 position_ = Geoposition(); |
95 | 100 |
96 providers_.clear(); | 101 providers_.clear(); |
97 is_running_ = false; | 102 is_running_ = false; |
98 } | 103 } |
99 | 104 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 DCHECK(new_position.Validate() || | 138 DCHECK(new_position.Validate() || |
134 new_position.error_code != Geoposition::ERROR_CODE_NONE); | 139 new_position.error_code != Geoposition::ERROR_CODE_NONE); |
135 if (!IsNewPositionBetter(position_, new_position, | 140 if (!IsNewPositionBetter(position_, new_position, |
136 provider == position_provider_)) | 141 provider == position_provider_)) |
137 return; | 142 return; |
138 position_provider_ = provider; | 143 position_provider_ = provider; |
139 position_ = new_position; | 144 position_ = new_position; |
140 arbitrator_update_callback_.Run(position_); | 145 arbitrator_update_callback_.Run(position_); |
141 } | 146 } |
142 | 147 |
148 const Geoposition& LocationArbitratorImpl::GetPosition() { | |
149 return position_; | |
150 } | |
151 | |
152 void LocationArbitratorImpl::SetUpdateCallback( | |
153 const LocationProviderUpdateCallback& callback) { | |
154 DCHECK(!callback.is_null()); | |
155 provider_update_callback_ = callback; | |
Wez
2016/08/19 01:56:30
This doesn't look right - you should be (re)assign
CJ
2016/08/22 17:41:31
Done.
| |
156 } | |
157 | |
143 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { | 158 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { |
144 return delegate_->CreateAccessTokenStore(); | 159 return delegate_->CreateAccessTokenStore(); |
145 } | 160 } |
146 | 161 |
147 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { | 162 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { |
148 if (!access_token_store_) | 163 if (!access_token_store_) |
149 access_token_store_ = NewAccessTokenStore(); | 164 access_token_store_ = NewAccessTokenStore(); |
150 return access_token_store_; | 165 return access_token_store_; |
151 } | 166 } |
152 | 167 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 return true; | 213 return true; |
199 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > | 214 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > |
200 kFixStaleTimeoutMilliseconds) { | 215 kFixStaleTimeoutMilliseconds) { |
201 // Existing fix is stale. | 216 // Existing fix is stale. |
202 return true; | 217 return true; |
203 } | 218 } |
204 } | 219 } |
205 return false; | 220 return false; |
206 } | 221 } |
207 | 222 |
208 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { | 223 bool LocationArbitratorImpl::HasPermissionBeenGrantedForTest() const { |
Wez
2016/08/19 01:33:11
nit: Move this impl to the equivalent position in
CJ
2016/08/22 17:41:31
Done.
| |
209 return is_permission_granted_; | 224 return is_permission_granted_; |
210 } | 225 } |
211 | 226 |
212 } // namespace device | 227 } // namespace device |
OLD | NEW |