| 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/location_arbitrator_impl.h" | 5 #include "content/browser/geolocation/location_arbitrator_impl.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { | 44 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { |
| 45 return GURL(kDefaultNetworkProviderUrl); | 45 return GURL(kDefaultNetworkProviderUrl); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void LocationArbitratorImpl::OnPermissionGranted() { | 48 void LocationArbitratorImpl::OnPermissionGranted() { |
| 49 is_permission_granted_ = true; | 49 is_permission_granted_ = true; |
| 50 for (const auto& provider : providers_) | 50 for (const auto& provider : providers_) |
| 51 provider->OnPermissionGranted(); | 51 provider->OnPermissionGranted(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { | 54 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy, |
| 55 // GetAccessTokenStore() will return NULL for embedders not implementing | 55 bool use_network) { |
| 56 // the AccessTokenStore class, so we report an error to avoid JavaScript | |
| 57 // requests of location to wait eternally for a reply. | |
| 58 AccessTokenStore* access_token_store = GetAccessTokenStore(); | |
| 59 if (!access_token_store) { | |
| 60 Geoposition position; | |
| 61 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; | |
| 62 arbitrator_update_callback_.Run(position); | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 // Stash options as OnAccessTokenStoresLoaded has not yet been called. | 56 // Stash options as OnAccessTokenStoresLoaded has not yet been called. |
| 67 is_running_ = true; | 57 is_running_ = true; |
| 68 enable_high_accuracy_ = enable_high_accuracy; | 58 enable_high_accuracy_ = enable_high_accuracy; |
| 59 |
| 69 if (providers_.empty()) { | 60 if (providers_.empty()) { |
| 70 DCHECK(DefaultNetworkProviderURL().is_valid()); | 61 if (use_network) { |
| 71 access_token_store->LoadAccessTokens( | 62 // GetAccessTokenStore() will return NULL for embedders not implementing |
| 72 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, | 63 // the AccessTokenStore class, so we report an error to avoid JavaScript |
| 73 base::Unretained(this))); | 64 // requests of location to wait eternally for a reply. |
| 65 AccessTokenStore* access_token_store = GetAccessTokenStore(); |
| 66 if (!access_token_store) { |
| 67 Geoposition position; |
| 68 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; |
| 69 arbitrator_update_callback_.Run(position); |
| 70 return; |
| 71 } |
| 72 DCHECK(DefaultNetworkProviderURL().is_valid()); |
| 73 access_token_store->LoadAccessTokens( |
| 74 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, |
| 75 base::Unretained(this))); |
| 76 } else { |
| 77 RegisterSystemProvider(); |
| 78 } |
| 74 } else { | 79 } else { |
| 75 DoStartProviders(); | 80 DoStartProviders(); |
| 76 } | 81 } |
| 77 } | 82 } |
| 78 | 83 |
| 79 void LocationArbitratorImpl::DoStartProviders() { | 84 void LocationArbitratorImpl::DoStartProviders() { |
| 80 for (const auto& provider : providers_) | 85 for (const auto& provider : providers_) |
| 81 provider->StartProvider(enable_high_accuracy_); | 86 provider->StartProvider(enable_high_accuracy_); |
| 82 } | 87 } |
| 83 | 88 |
| 84 void LocationArbitratorImpl::StopProviders() { | 89 void LocationArbitratorImpl::StopProviders() { |
| 85 // Reset the reference location state (provider+position) | 90 // Reset the reference location state (provider+position) |
| 86 // so that future starts use fresh locations from | 91 // so that future starts use fresh locations from |
| 87 // the newly constructed providers. | 92 // the newly constructed providers. |
| 88 position_provider_ = NULL; | 93 position_provider_ = NULL; |
| 89 position_ = Geoposition(); | 94 position_ = Geoposition(); |
| 90 | 95 |
| 91 providers_.clear(); | 96 providers_.clear(); |
| 92 is_running_ = false; | 97 is_running_ = false; |
| 93 } | 98 } |
| 94 | 99 |
| 100 void LocationArbitratorImpl::RegisterSystemProvider() { |
| 101 LocationProvider* provider = GetOverrideSystemLocationProvider(); |
| 102 if (!provider) |
| 103 provider = NewSystemLocationProvider(); |
| 104 RegisterProvider(provider); |
| 105 DoStartProviders(); |
| 106 } |
| 107 |
| 95 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( | 108 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( |
| 96 AccessTokenStore::AccessTokenMap access_token_map, | 109 AccessTokenStore::AccessTokenMap access_token_map, |
| 97 net::URLRequestContextGetter* context_getter) { | 110 net::URLRequestContextGetter* context_getter) { |
| 98 if (!is_running_ || !providers_.empty()) { | 111 if (!is_running_ || !providers_.empty()) { |
| 99 // A second StartProviders() call may have arrived before the first | 112 // A second StartProviders() call may have arrived before the first |
| 100 // completed. | 113 // completed. |
| 101 return; | 114 return; |
| 102 } | 115 } |
| 103 LocationProvider* sole_provider = | 116 if (GetContentClient()->browser()->UseNetworkLocationProviders()) { |
| 104 GetContentClient()->browser()->SoleLocationProvider(); | |
| 105 if (!sole_provider) { | |
| 106 // If there are no access tokens, boot strap it with the default server URL. | 117 // If there are no access tokens, boot strap it with the default server URL. |
| 107 if (access_token_map.empty()) | 118 if (access_token_map.empty()) |
| 108 access_token_map[DefaultNetworkProviderURL()]; | 119 access_token_map[DefaultNetworkProviderURL()]; |
| 109 for (const auto& entry : access_token_map) { | 120 for (const auto& entry : access_token_map) { |
| 110 RegisterProvider(NewNetworkLocationProvider( | 121 RegisterProvider(NewNetworkLocationProvider( |
| 111 GetAccessTokenStore(), context_getter, entry.first, entry.second)); | 122 GetAccessTokenStore(), context_getter, entry.first, entry.second)); |
| 112 } | 123 } |
| 113 | |
| 114 LocationProvider* provider = | |
| 115 GetContentClient()->browser()->OverrideSystemLocationProvider(); | |
| 116 if (!provider) | |
| 117 provider = NewSystemLocationProvider(); | |
| 118 RegisterProvider(provider); | |
| 119 } else { | |
| 120 RegisterProvider(sole_provider); | |
| 121 } | 124 } |
| 122 DoStartProviders(); | 125 RegisterSystemProvider(); |
| 123 } | 126 } |
| 124 | 127 |
| 125 void LocationArbitratorImpl::RegisterProvider( | 128 void LocationArbitratorImpl::RegisterProvider( |
| 126 LocationProvider* provider) { | 129 LocationProvider* provider) { |
| 127 if (!provider) | 130 if (!provider) |
| 128 return; | 131 return; |
| 129 provider->SetUpdateCallback(provider_update_callback_); | 132 provider->SetUpdateCallback(provider_update_callback_); |
| 130 if (is_permission_granted_) | 133 if (is_permission_granted_) |
| 131 provider->OnPermissionGranted(); | 134 provider->OnPermissionGranted(); |
| 132 providers_.push_back(provider); | 135 providers_.push_back(provider); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 } | 172 } |
| 170 | 173 |
| 171 LocationProvider* LocationArbitratorImpl::NewSystemLocationProvider() { | 174 LocationProvider* LocationArbitratorImpl::NewSystemLocationProvider() { |
| 172 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | 175 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
| 173 return NULL; | 176 return NULL; |
| 174 #else | 177 #else |
| 175 return content::NewSystemLocationProvider(); | 178 return content::NewSystemLocationProvider(); |
| 176 #endif | 179 #endif |
| 177 } | 180 } |
| 178 | 181 |
| 182 LocationProvider* LocationArbitratorImpl::GetOverrideSystemLocationProvider() { |
| 183 return GetContentClient()->browser()->OverrideSystemLocationProvider(); |
| 184 } |
| 185 |
| 179 base::Time LocationArbitratorImpl::GetTimeNow() const { | 186 base::Time LocationArbitratorImpl::GetTimeNow() const { |
| 180 return base::Time::Now(); | 187 return base::Time::Now(); |
| 181 } | 188 } |
| 182 | 189 |
| 183 bool LocationArbitratorImpl::IsNewPositionBetter( | 190 bool LocationArbitratorImpl::IsNewPositionBetter( |
| 184 const Geoposition& old_position, const Geoposition& new_position, | 191 const Geoposition& old_position, const Geoposition& new_position, |
| 185 bool from_same_provider) const { | 192 bool from_same_provider) const { |
| 186 // Updates location_info if it's better than what we currently have, | 193 // Updates location_info if it's better than what we currently have, |
| 187 // or if it's a newer update from the same provider. | 194 // or if it's a newer update from the same provider. |
| 188 if (!old_position.Validate()) { | 195 if (!old_position.Validate()) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 204 } | 211 } |
| 205 } | 212 } |
| 206 return false; | 213 return false; |
| 207 } | 214 } |
| 208 | 215 |
| 209 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { | 216 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { |
| 210 return is_permission_granted_; | 217 return is_permission_granted_; |
| 211 } | 218 } |
| 212 | 219 |
| 213 } // namespace content | 220 } // namespace content |
| OLD | NEW |