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 |
(...skipping 26 matching lines...) Expand all Loading... | |
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 for (const auto& provider : providers_) |
86 provider->StartProvider(enable_high_accuracy_); | 88 provider->StartProvider(enable_high_accuracy_); |
Kevin M
2016/08/15 19:12:46
This is throwing away the return value of StartPro
CJ
2016/08/15 20:11:51
Done.
| |
89 return true; | |
87 } | 90 } |
88 | 91 |
89 void LocationArbitratorImpl::StopProviders() { | 92 void LocationArbitratorImpl::StopProvider() { |
90 // Reset the reference location state (provider+position) | 93 // Reset the reference location state (provider+position) |
91 // so that future starts use fresh locations from | 94 // so that future starts use fresh locations from |
92 // the newly constructed providers. | 95 // the newly constructed providers. |
93 position_provider_ = nullptr; | 96 position_provider_ = nullptr; |
94 position_ = Geoposition(); | 97 position_ = Geoposition(); |
95 | 98 |
96 providers_.clear(); | 99 providers_.clear(); |
97 is_running_ = false; | 100 is_running_ = false; |
98 } | 101 } |
99 | 102 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 DCHECK(new_position.Validate() || | 136 DCHECK(new_position.Validate() || |
134 new_position.error_code != Geoposition::ERROR_CODE_NONE); | 137 new_position.error_code != Geoposition::ERROR_CODE_NONE); |
135 if (!IsNewPositionBetter(position_, new_position, | 138 if (!IsNewPositionBetter(position_, new_position, |
136 provider == position_provider_)) | 139 provider == position_provider_)) |
137 return; | 140 return; |
138 position_provider_ = provider; | 141 position_provider_ = provider; |
139 position_ = new_position; | 142 position_ = new_position; |
140 arbitrator_update_callback_.Run(position_); | 143 arbitrator_update_callback_.Run(position_); |
141 } | 144 } |
142 | 145 |
146 void LocationArbitratorImpl::GetPosition(Geoposition* position) { | |
Kevin M
2016/08/15 19:12:46
Change this to return a const Geoposition& and jus
CJ
2016/08/15 20:11:51
Yeah I was wondering if I should refactor this. I
| |
147 *position = position_; | |
148 } | |
149 | |
150 void LocationArbitratorImpl::SetUpdateCallback( | |
151 const LocationProviderUpdateCallback& callback) { | |
Kevin M
2016/08/15 19:12:46
DCHECK(!callback.is_null())
CJ
2016/08/15 20:11:51
Done.
| |
152 provider_update_callback_ = callback; | |
153 } | |
154 | |
143 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { | 155 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { |
144 return delegate_->CreateAccessTokenStore(); | 156 return delegate_->CreateAccessTokenStore(); |
145 } | 157 } |
146 | 158 |
147 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { | 159 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { |
148 if (!access_token_store_) | 160 if (!access_token_store_) |
149 access_token_store_ = NewAccessTokenStore(); | 161 access_token_store_ = NewAccessTokenStore(); |
150 return access_token_store_; | 162 return access_token_store_; |
151 } | 163 } |
152 | 164 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 return true; | 210 return true; |
199 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > | 211 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > |
200 kFixStaleTimeoutMilliseconds) { | 212 kFixStaleTimeoutMilliseconds) { |
201 // Existing fix is stale. | 213 // Existing fix is stale. |
202 return true; | 214 return true; |
203 } | 215 } |
204 } | 216 } |
205 return false; | 217 return false; |
206 } | 218 } |
207 | 219 |
208 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { | 220 bool LocationArbitratorImpl::HasPermissionBeenGrantedForTest() const { |
209 return is_permission_granted_; | 221 return is_permission_granted_; |
210 } | 222 } |
211 | 223 |
212 } // namespace device | 224 } // namespace device |
OLD | NEW |