Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(591)

Side by Side Diff: content/browser/geolocation/location_arbitrator_impl.cc

Issue 2028823002: Refactor to make BlimpLocationProvider accessible to content layer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: In response to Wez's #86 comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/memory/ptr_util.h"
11 #include "build/build_config.h" 12 #include "build/build_config.h"
12 #include "content/browser/geolocation/network_location_provider.h" 13 #include "content/browser/geolocation/network_location_provider.h"
13 #include "content/public/browser/access_token_store.h" 14 #include "content/public/browser/access_token_store.h"
14 #include "content/public/browser/content_browser_client.h" 15 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/common/content_client.h" 16 #include "content/public/common/content_client.h"
16 #include "url/gurl.h" 17 #include "url/gurl.h"
17 18
18 namespace content { 19 namespace content {
19 namespace { 20 namespace {
20 21
(...skipping 24 matching lines...) Expand all
45 return GURL(kDefaultNetworkProviderUrl); 46 return GURL(kDefaultNetworkProviderUrl);
46 } 47 }
47 48
48 void LocationArbitratorImpl::OnPermissionGranted() { 49 void LocationArbitratorImpl::OnPermissionGranted() {
49 is_permission_granted_ = true; 50 is_permission_granted_ = true;
50 for (const auto& provider : providers_) 51 for (const auto& provider : providers_)
51 provider->OnPermissionGranted(); 52 provider->OnPermissionGranted();
52 } 53 }
53 54
54 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { 55 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) {
55 // GetAccessTokenStore() will return NULL for embedders not implementing 56 // Stash options as OnAccessTokenStoresLoaded has not yet been called.
56 // the AccessTokenStore class, so we report an error to avoid JavaScript 57 is_running_ = true;
57 // requests of location to wait eternally for a reply. 58 enable_high_accuracy_ = enable_high_accuracy;
58 AccessTokenStore* access_token_store = GetAccessTokenStore(); 59
59 if (!access_token_store) { 60 if (providers_.empty()) {
61 RegisterSystemProvider();
62
63 AccessTokenStore* access_token_store = GetAccessTokenStore();
64 if (access_token_store &&
65 GetContentClient()->browser()->UseNetworkLocationProviders()) {
66 DCHECK(DefaultNetworkProviderURL().is_valid());
67 token_store_callback_.Reset(
68 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded,
69 base::Unretained(this)));
70 access_token_store->LoadAccessTokens(token_store_callback_.callback());
71 return;
72 }
73 }
74 DoStartProviders();
75 }
76
77 void LocationArbitratorImpl::DoStartProviders() {
78 if (providers_.empty()) {
79 // If no providers are available, we report an error to avoid
Charlie Reis 2016/06/16 21:56:54 nit: Only one space after comma.
CJ 2016/06/21 22:16:07 Done.
80 // callers waiting indefinitely for a reply.
60 Geoposition position; 81 Geoposition position;
61 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; 82 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED;
62 arbitrator_update_callback_.Run(position); 83 arbitrator_update_callback_.Run(position);
63 return; 84 return;
64 } 85 }
65
66 // Stash options as OnAccessTokenStoresLoaded has not yet been called.
67 is_running_ = true;
68 enable_high_accuracy_ = enable_high_accuracy;
69 if (providers_.empty()) {
70 DCHECK(DefaultNetworkProviderURL().is_valid());
71 access_token_store->LoadAccessTokens(
72 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded,
73 base::Unretained(this)));
74 } else {
75 DoStartProviders();
76 }
77 }
78
79 void LocationArbitratorImpl::DoStartProviders() {
80 for (const auto& provider : providers_) 86 for (const auto& provider : providers_)
81 provider->StartProvider(enable_high_accuracy_); 87 provider->StartProvider(enable_high_accuracy_);
82 } 88 }
83 89
84 void LocationArbitratorImpl::StopProviders() { 90 void LocationArbitratorImpl::StopProviders() {
85 // Reset the reference location state (provider+position) 91 // Reset the reference location state (provider+position)
86 // so that future starts use fresh locations from 92 // so that future starts use fresh locations from
87 // the newly constructed providers. 93 // the newly constructed providers.
88 position_provider_ = NULL; 94 position_provider_ = NULL;
89 position_ = Geoposition(); 95 position_ = Geoposition();
90 96
91 providers_.clear(); 97 providers_.clear();
92 is_running_ = false; 98 is_running_ = false;
93 } 99 }
94 100
95 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( 101 void LocationArbitratorImpl::OnAccessTokenStoresLoaded(
96 AccessTokenStore::AccessTokenMap access_token_map, 102 AccessTokenStore::AccessTokenMap access_token_map,
97 net::URLRequestContextGetter* context_getter) { 103 net::URLRequestContextGetter* context_getter) {
98 if (!is_running_ || !providers_.empty()) {
99 // A second StartProviders() call may have arrived before the first
100 // completed.
101 return;
102 }
103 // If there are no access tokens, boot strap it with the default server URL. 104 // If there are no access tokens, boot strap it with the default server URL.
104 if (access_token_map.empty()) 105 if (access_token_map.empty())
105 access_token_map[DefaultNetworkProviderURL()]; 106 access_token_map[DefaultNetworkProviderURL()];
106 for (const auto& entry : access_token_map) { 107 for (const auto& entry : access_token_map) {
107 RegisterProvider(NewNetworkLocationProvider( 108 RegisterProvider(NewNetworkLocationProvider(
108 GetAccessTokenStore(), context_getter, entry.first, entry.second)); 109 GetAccessTokenStore(), context_getter, entry.first, entry.second));
109 } 110 }
110
111 LocationProvider* provider =
112 GetContentClient()->browser()->OverrideSystemLocationProvider();
113 if (!provider)
114 provider = NewSystemLocationProvider();
115 RegisterProvider(provider);
116 DoStartProviders(); 111 DoStartProviders();
117 } 112 }
118 113
119 void LocationArbitratorImpl::RegisterProvider( 114 void LocationArbitratorImpl::RegisterProvider(
120 LocationProvider* provider) { 115 std::unique_ptr<LocationProvider> provider) {
121 if (!provider) 116 if (!provider)
122 return; 117 return;
123 provider->SetUpdateCallback(provider_update_callback_); 118 provider->SetUpdateCallback(provider_update_callback_);
124 if (is_permission_granted_) 119 if (is_permission_granted_)
125 provider->OnPermissionGranted(); 120 provider->OnPermissionGranted();
126 providers_.push_back(provider); 121 providers_.push_back(std::move(provider));
122 }
123
124 void LocationArbitratorImpl::RegisterSystemProvider() {
125 std::unique_ptr<LocationProvider> provider = base::WrapUnique(
126 GetContentClient()->browser()->OverrideSystemLocationProvider());
127 if (!provider)
128 provider = NewSystemLocationProvider();
129 RegisterProvider(std::move(provider));
127 } 130 }
128 131
129 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider, 132 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider,
130 const Geoposition& new_position) { 133 const Geoposition& new_position) {
131 DCHECK(new_position.Validate() || 134 DCHECK(new_position.Validate() ||
132 new_position.error_code != Geoposition::ERROR_CODE_NONE); 135 new_position.error_code != Geoposition::ERROR_CODE_NONE);
133 if (!IsNewPositionBetter(position_, new_position, 136 if (!IsNewPositionBetter(position_, new_position,
134 provider == position_provider_)) 137 provider == position_provider_))
135 return; 138 return;
136 position_provider_ = provider; 139 position_provider_ = provider;
137 position_ = new_position; 140 position_ = new_position;
138 arbitrator_update_callback_.Run(position_); 141 arbitrator_update_callback_.Run(position_);
139 } 142 }
140 143
141 AccessTokenStore* LocationArbitratorImpl::NewAccessTokenStore() { 144 AccessTokenStore* LocationArbitratorImpl::NewAccessTokenStore() {
142 return GetContentClient()->browser()->CreateAccessTokenStore(); 145 return GetContentClient()->browser()->CreateAccessTokenStore();
143 } 146 }
144 147
145 AccessTokenStore* LocationArbitratorImpl::GetAccessTokenStore() { 148 AccessTokenStore* LocationArbitratorImpl::GetAccessTokenStore() {
146 if (!access_token_store_.get()) 149 if (!access_token_store_.get())
147 access_token_store_ = NewAccessTokenStore(); 150 access_token_store_ = NewAccessTokenStore();
148 return access_token_store_.get(); 151 return access_token_store_.get();
149 } 152 }
150 153
151 LocationProvider* LocationArbitratorImpl::NewNetworkLocationProvider( 154 std::unique_ptr<LocationProvider>
155 LocationArbitratorImpl::NewNetworkLocationProvider(
152 AccessTokenStore* access_token_store, 156 AccessTokenStore* access_token_store,
153 net::URLRequestContextGetter* context, 157 net::URLRequestContextGetter* context,
154 const GURL& url, 158 const GURL& url,
155 const base::string16& access_token) { 159 const base::string16& access_token) {
156 #if defined(OS_ANDROID) 160 #if defined(OS_ANDROID)
157 // Android uses its own SystemLocationProvider. 161 // Android uses its own SystemLocationProvider.
158 return NULL; 162 return NULL;
159 #else 163 #else
160 return new NetworkLocationProvider(access_token_store, context, url, 164 return base::WrapUnique(new NetworkLocationProvider(
161 access_token); 165 access_token_store, context, url, access_token));
162 #endif 166 #endif
163 } 167 }
164 168
165 LocationProvider* LocationArbitratorImpl::NewSystemLocationProvider() { 169 std::unique_ptr<LocationProvider>
170 LocationArbitratorImpl::NewSystemLocationProvider() {
166 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) 171 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
167 return NULL; 172 return NULL;
168 #else 173 #else
169 return content::NewSystemLocationProvider(); 174 return base::WrapUnique(content::NewSystemLocationProvider());
170 #endif 175 #endif
171 } 176 }
172 177
173 base::Time LocationArbitratorImpl::GetTimeNow() const { 178 base::Time LocationArbitratorImpl::GetTimeNow() const {
174 return base::Time::Now(); 179 return base::Time::Now();
175 } 180 }
176 181
177 bool LocationArbitratorImpl::IsNewPositionBetter( 182 bool LocationArbitratorImpl::IsNewPositionBetter(
178 const Geoposition& old_position, const Geoposition& new_position, 183 const Geoposition& old_position, const Geoposition& new_position,
179 bool from_same_provider) const { 184 bool from_same_provider) const {
(...skipping 18 matching lines...) Expand all
198 } 203 }
199 } 204 }
200 return false; 205 return false;
201 } 206 }
202 207
203 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { 208 bool LocationArbitratorImpl::HasPermissionBeenGranted() const {
204 return is_permission_granted_; 209 return is_permission_granted_;
205 } 210 }
206 211
207 } // namespace content 212 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698