| 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 18 matching lines...) Expand all  Loading... | 
|   29     11 * base::Time::kMillisecondsPerSecond; |   29     11 * base::Time::kMillisecondsPerSecond; | 
|   30  |   30  | 
|   31 LocationArbitratorImpl::LocationArbitratorImpl( |   31 LocationArbitratorImpl::LocationArbitratorImpl( | 
|   32     const LocationUpdateCallback& callback, |   32     const LocationUpdateCallback& callback, | 
|   33     GeolocationDelegate* delegate) |   33     GeolocationDelegate* delegate) | 
|   34     : delegate_(delegate), |   34     : delegate_(delegate), | 
|   35       arbitrator_update_callback_(callback), |   35       arbitrator_update_callback_(callback), | 
|   36       provider_update_callback_( |   36       provider_update_callback_( | 
|   37           base::Bind(&LocationArbitratorImpl::OnLocationUpdate, |   37           base::Bind(&LocationArbitratorImpl::OnLocationUpdate, | 
|   38                      base::Unretained(this))), |   38                      base::Unretained(this))), | 
|   39       position_provider_(NULL), |   39       position_provider_(nullptr), | 
|   40       is_permission_granted_(false), |   40       is_permission_granted_(false), | 
|   41       is_running_(false) {} |   41       is_running_(false) {} | 
|   42  |   42  | 
|   43 LocationArbitratorImpl::~LocationArbitratorImpl() { |   43 LocationArbitratorImpl::~LocationArbitratorImpl() { | 
|   44 } |   44 } | 
|   45  |   45  | 
|   46 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { |   46 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { | 
|   47   return GURL(kDefaultNetworkProviderUrl); |   47   return GURL(kDefaultNetworkProviderUrl); | 
|   48 } |   48 } | 
|   49  |   49  | 
|   50 void LocationArbitratorImpl::OnPermissionGranted() { |   50 void LocationArbitratorImpl::OnPermissionGranted() { | 
|   51   is_permission_granted_ = true; |   51   is_permission_granted_ = true; | 
|   52   for (const auto& provider : providers_) |   52   for (const auto& provider : providers_) | 
|   53     provider->OnPermissionGranted(); |   53     provider->OnPermissionGranted(); | 
|   54 } |   54 } | 
|   55  |   55  | 
|   56 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { |   56 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { | 
|   57   // Stash options as OnAccessTokenStoresLoaded has not yet been called. |   57   // Stash options as OnAccessTokenStoresLoaded has not yet been called. | 
|   58   is_running_ = true; |   58   is_running_ = true; | 
|   59   enable_high_accuracy_ = enable_high_accuracy; |   59   enable_high_accuracy_ = enable_high_accuracy; | 
|   60  |   60  | 
|   61   if (providers_.empty()) { |   61   if (providers_.empty()) { | 
|   62     RegisterSystemProvider(); |   62     RegisterSystemProvider(); | 
|   63  |   63  | 
|   64     AccessTokenStore* access_token_store = GetAccessTokenStore(); |   64     const scoped_refptr<AccessTokenStore> access_token_store = | 
 |   65         GetAccessTokenStore(); | 
|   65     if (access_token_store && delegate_->UseNetworkLocationProviders()) { |   66     if (access_token_store && delegate_->UseNetworkLocationProviders()) { | 
|   66       DCHECK(DefaultNetworkProviderURL().is_valid()); |   67       DCHECK(DefaultNetworkProviderURL().is_valid()); | 
|   67       token_store_callback_.Reset( |   68       token_store_callback_.Reset( | 
|   68           base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, |   69           base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, | 
|   69                      base::Unretained(this))); |   70                      base::Unretained(this))); | 
|   70       access_token_store->LoadAccessTokens(token_store_callback_.callback()); |   71       access_token_store->LoadAccessTokens(token_store_callback_.callback()); | 
|   71       return; |   72       return; | 
|   72     } |   73     } | 
|   73   } |   74   } | 
|   74   DoStartProviders(); |   75   DoStartProviders(); | 
|   75 } |   76 } | 
|   76  |   77  | 
|   77 void LocationArbitratorImpl::DoStartProviders() { |   78 void LocationArbitratorImpl::DoStartProviders() { | 
|   78   if (providers_.empty()) { |   79   if (providers_.empty()) { | 
|   79     // If no providers are available, we report an error to avoid |   80     // If no providers are available, we report an error to avoid | 
|   80     // callers waiting indefinitely for a reply. |   81     // callers waiting indefinitely for a reply. | 
|   81     Geoposition position; |   82     Geoposition position; | 
|   82     position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; |   83     position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; | 
|   83     arbitrator_update_callback_.Run(position); |   84     arbitrator_update_callback_.Run(position); | 
|   84     return; |   85     return; | 
|   85   } |   86   } | 
|   86   for (const auto& provider : providers_) |   87   for (const auto& provider : providers_) | 
|   87     provider->StartProvider(enable_high_accuracy_); |   88     provider->StartProvider(enable_high_accuracy_); | 
|   88 } |   89 } | 
|   89  |   90  | 
|   90 void LocationArbitratorImpl::StopProviders() { |   91 void LocationArbitratorImpl::StopProviders() { | 
|   91   // Reset the reference location state (provider+position) |   92   // Reset the reference location state (provider+position) | 
|   92   // so that future starts use fresh locations from |   93   // so that future starts use fresh locations from | 
|   93   // the newly constructed providers. |   94   // the newly constructed providers. | 
|   94   position_provider_ = NULL; |   95   position_provider_ = nullptr; | 
|   95   position_ = Geoposition(); |   96   position_ = Geoposition(); | 
|   96  |   97  | 
|   97   providers_.clear(); |   98   providers_.clear(); | 
|   98   is_running_ = false; |   99   is_running_ = false; | 
|   99 } |  100 } | 
|  100  |  101  | 
|  101 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( |  102 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( | 
|  102     AccessTokenStore::AccessTokenMap access_token_map, |  103     AccessTokenStore::AccessTokenMap access_token_map, | 
|  103     net::URLRequestContextGetter* context_getter) { |  104     const scoped_refptr<net::URLRequestContextGetter>& context_getter) { | 
|  104   // If there are no access tokens, boot strap it with the default server URL. |  105   // If there are no access tokens, boot strap it with the default server URL. | 
|  105   if (access_token_map.empty()) |  106   if (access_token_map.empty()) | 
|  106     access_token_map[DefaultNetworkProviderURL()]; |  107     access_token_map[DefaultNetworkProviderURL()]; | 
|  107   for (const auto& entry : access_token_map) { |  108   for (const auto& entry : access_token_map) { | 
|  108     RegisterProvider(NewNetworkLocationProvider( |  109     RegisterProvider(NewNetworkLocationProvider( | 
|  109         GetAccessTokenStore(), context_getter, entry.first, entry.second)); |  110         GetAccessTokenStore(), context_getter, entry.first, entry.second)); | 
|  110   } |  111   } | 
|  111   DoStartProviders(); |  112   DoStartProviders(); | 
|  112 } |  113 } | 
|  113  |  114  | 
 |  115 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { | 
 |  116   if (!access_token_store_) | 
 |  117     access_token_store_ = NewAccessTokenStore(); | 
 |  118   return access_token_store_; | 
 |  119 } | 
 |  120  | 
|  114 void LocationArbitratorImpl::RegisterProvider( |  121 void LocationArbitratorImpl::RegisterProvider( | 
|  115     std::unique_ptr<LocationProvider> provider) { |  122     std::unique_ptr<LocationProvider> provider) { | 
|  116   if (!provider) |  123   if (!provider) | 
|  117     return; |  124     return; | 
|  118   provider->SetUpdateCallback(provider_update_callback_); |  125   provider->SetUpdateCallback(provider_update_callback_); | 
|  119   if (is_permission_granted_) |  126   if (is_permission_granted_) | 
|  120     provider->OnPermissionGranted(); |  127     provider->OnPermissionGranted(); | 
|  121   providers_.push_back(std::move(provider)); |  128   providers_.push_back(std::move(provider)); | 
|  122 } |  129 } | 
|  123  |  130  | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|  134   DCHECK(new_position.Validate() || |  141   DCHECK(new_position.Validate() || | 
|  135          new_position.error_code != Geoposition::ERROR_CODE_NONE); |  142          new_position.error_code != Geoposition::ERROR_CODE_NONE); | 
|  136   if (!IsNewPositionBetter(position_, new_position, |  143   if (!IsNewPositionBetter(position_, new_position, | 
|  137                            provider == position_provider_)) |  144                            provider == position_provider_)) | 
|  138     return; |  145     return; | 
|  139   position_provider_ = provider; |  146   position_provider_ = provider; | 
|  140   position_ = new_position; |  147   position_ = new_position; | 
|  141   arbitrator_update_callback_.Run(position_); |  148   arbitrator_update_callback_.Run(position_); | 
|  142 } |  149 } | 
|  143  |  150  | 
|  144 AccessTokenStore* LocationArbitratorImpl::NewAccessTokenStore() { |  151 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { | 
|  145   return delegate_->CreateAccessTokenStore(); |  152   return delegate_->CreateAccessTokenStore(); | 
|  146 } |  153 } | 
|  147  |  154  | 
|  148 AccessTokenStore* LocationArbitratorImpl::GetAccessTokenStore() { |  | 
|  149   if (!access_token_store_.get()) |  | 
|  150     access_token_store_ = NewAccessTokenStore(); |  | 
|  151   return access_token_store_.get(); |  | 
|  152 } |  | 
|  153  |  | 
|  154 std::unique_ptr<LocationProvider> |  155 std::unique_ptr<LocationProvider> | 
|  155 LocationArbitratorImpl::NewNetworkLocationProvider( |  156 LocationArbitratorImpl::NewNetworkLocationProvider( | 
|  156     AccessTokenStore* access_token_store, |  157     const scoped_refptr<AccessTokenStore>& access_token_store, | 
|  157     net::URLRequestContextGetter* context, |  158     const scoped_refptr<net::URLRequestContextGetter>& context, | 
|  158     const GURL& url, |  159     const GURL& url, | 
|  159     const base::string16& access_token) { |  160     const base::string16& access_token) { | 
|  160 #if defined(OS_ANDROID) |  161 #if defined(OS_ANDROID) | 
|  161   // Android uses its own SystemLocationProvider. |  162   // Android uses its own SystemLocationProvider. | 
|  162   return NULL; |  163   return nullptr; | 
|  163 #else |  164 #else | 
|  164   return base::WrapUnique(new NetworkLocationProvider( |  165   return base::WrapUnique(new NetworkLocationProvider( | 
|  165       access_token_store, context, url, access_token)); |  166       access_token_store, context, url, access_token)); | 
|  166 #endif |  167 #endif | 
|  167 } |  168 } | 
|  168  |  169  | 
|  169 std::unique_ptr<LocationProvider> |  170 std::unique_ptr<LocationProvider> | 
|  170 LocationArbitratorImpl::NewSystemLocationProvider() { |  171 LocationArbitratorImpl::NewSystemLocationProvider() { | 
|  171 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |  172 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | 
|  172   return NULL; |  173   return nullptr; | 
|  173 #else |  174 #else | 
|  174   return base::WrapUnique(content::NewSystemLocationProvider()); |  175   return content::NewSystemLocationProvider(); | 
|  175 #endif |  176 #endif | 
|  176 } |  177 } | 
|  177  |  178  | 
|  178 base::Time LocationArbitratorImpl::GetTimeNow() const { |  179 base::Time LocationArbitratorImpl::GetTimeNow() const { | 
|  179   return base::Time::Now(); |  180   return base::Time::Now(); | 
|  180 } |  181 } | 
|  181  |  182  | 
|  182 bool LocationArbitratorImpl::IsNewPositionBetter( |  183 bool LocationArbitratorImpl::IsNewPositionBetter( | 
|  183     const Geoposition& old_position, const Geoposition& new_position, |  184     const Geoposition& old_position, const Geoposition& new_position, | 
|  184     bool from_same_provider) const { |  185     bool from_same_provider) const { | 
| (...skipping 18 matching lines...) Expand all  Loading... | 
|  203     } |  204     } | 
|  204   } |  205   } | 
|  205   return false; |  206   return false; | 
|  206 } |  207 } | 
|  207  |  208  | 
|  208 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { |  209 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { | 
|  209   return is_permission_granted_; |  210   return is_permission_granted_; | 
|  210 } |  211 } | 
|  211  |  212  | 
|  212 }  // namespace content |  213 }  // namespace content | 
| OLD | NEW |