Create separate request context for geolocation service.

* Geolocation service cannot hold reference to browser context,
    since it is destroyed at the end of everything and this will
    confuse the shutdown path of browser context.
  * Geolocation service run on its own thread.
This commit is contained in:
deepak1556 2017-03-14 21:08:10 +05:30
parent a215e8fb82
commit 16f9754445
2 changed files with 40 additions and 50 deletions

View file

@ -7,11 +7,13 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "atom/browser/atom_browser_context.h"
#include "atom/common/google_api_key.h" #include "atom/common/google_api_key.h"
#include "base/environment.h" #include "base/environment.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "device/geolocation/geolocation_provider.h" #include "device/geolocation/geolocation_provider.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context_getter.h"
using content::BrowserThread; using content::BrowserThread;
@ -19,51 +21,40 @@ namespace atom {
namespace internal { namespace internal {
// Loads access tokens and other necessary data on the UI thread, and class GeoURLRequestContextGetter : public net::URLRequestContextGetter {
// calls back to the originator on the originating thread.
class TokenLoadingJob : public base::RefCountedThreadSafe<TokenLoadingJob> {
public: public:
explicit TokenLoadingJob( net::URLRequestContext* GetURLRequestContext() override {
const device::AccessTokenStore::LoadAccessTokensCallback& callback) DCHECK_CURRENTLY_ON(BrowserThread::IO);
: callback_(callback), request_context_getter_(nullptr) {} if (!url_request_context_.get()) {
net::URLRequestContextBuilder builder;
builder.set_proxy_config_service(
net::ProxyService::CreateSystemProxyConfigService(
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)));
url_request_context_ = builder.Build();
}
return url_request_context_.get();
}
void Run(AtomBrowserContext* browser_context) { scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
DCHECK_CURRENTLY_ON(BrowserThread::UI); const override {
request_context_getter_ = browser_context->GetRequestContext(); return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
std::unique_ptr<base::Environment> env(base::Environment::Create());
if (!env->GetVar("GOOGLE_API_KEY", &api_key_))
api_key_ = GOOGLEAPIS_API_KEY;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&TokenLoadingJob::RespondOnIOThread, this));
} }
private: private:
friend class base::RefCountedThreadSafe<TokenLoadingJob>; friend class atom::AtomAccessTokenStore;
~TokenLoadingJob() {} GeoURLRequestContextGetter() {}
~GeoURLRequestContextGetter() override {}
void RespondOnIOThread() { std::unique_ptr<net::URLRequestContext> url_request_context_;
// Equivalent to access_token_map[kGeolocationProviderURL]. DISALLOW_COPY_AND_ASSIGN(GeoURLRequestContextGetter);
// Somehow base::string16 is causing compilation errors when used in a pair
// of std::map on Linux, this can work around it.
device::AccessTokenStore::AccessTokenMap access_token_map;
std::pair<GURL, base::string16> token_pair;
token_pair.first = GURL(GOOGLEAPIS_ENDPOINT + api_key_);
access_token_map.insert(token_pair);
callback_.Run(access_token_map, request_context_getter_);
}
device::AccessTokenStore::LoadAccessTokensCallback callback_;
net::URLRequestContextGetter* request_context_getter_;
std::string api_key_;
}; };
} // namespace internal } // namespace internal
AtomAccessTokenStore::AtomAccessTokenStore() { AtomAccessTokenStore::AtomAccessTokenStore()
browser_context_ = AtomBrowserContext::From("", false); : request_context_getter_(new internal::GeoURLRequestContextGetter) {
device::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices(); device::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
} }
@ -72,16 +63,19 @@ AtomAccessTokenStore::~AtomAccessTokenStore() {
void AtomAccessTokenStore::LoadAccessTokens( void AtomAccessTokenStore::LoadAccessTokens(
const LoadAccessTokensCallback& callback) { const LoadAccessTokensCallback& callback) {
scoped_refptr<internal::TokenLoadingJob> job( std::unique_ptr<base::Environment> env(base::Environment::Create());
new internal::TokenLoadingJob(callback)); std::string api_key;
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, if (!env->GetVar("GOOGLE_API_KEY", &api_key))
base::Bind(&AtomAccessTokenStore::RunTokenLoadingJob, api_key = GOOGLEAPIS_API_KEY;
this, base::RetainedRef(job))); // Equivalent to access_token_map[kGeolocationProviderURL].
} // Somehow base::string16 is causing compilation errors when used in a pair
// of std::map on Linux, this can work around it.
device::AccessTokenStore::AccessTokenMap access_token_map;
std::pair<GURL, base::string16> token_pair;
token_pair.first = GURL(GOOGLEAPIS_ENDPOINT + api_key);
access_token_map.insert(token_pair);
void AtomAccessTokenStore::RunTokenLoadingJob( callback.Run(access_token_map, request_context_getter_.get());
scoped_refptr<internal::TokenLoadingJob> job) {
job->Run(browser_context_.get());
} }
void AtomAccessTokenStore::SaveAccessToken(const GURL& server_url, void AtomAccessTokenStore::SaveAccessToken(const GURL& server_url,

View file

@ -9,10 +9,8 @@
namespace atom { namespace atom {
class AtomBrowserContext;
namespace internal { namespace internal {
class TokenLoadingJob; class GeoURLRequestContextGetter;
} }
class AtomAccessTokenStore : public device::AccessTokenStore { class AtomAccessTokenStore : public device::AccessTokenStore {
@ -27,9 +25,7 @@ class AtomAccessTokenStore : public device::AccessTokenStore {
const base::string16& access_token) override; const base::string16& access_token) override;
private: private:
void RunTokenLoadingJob(scoped_refptr<internal::TokenLoadingJob> job); scoped_refptr<internal::GeoURLRequestContextGetter> request_context_getter_;
scoped_refptr<AtomBrowserContext> browser_context_;
DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore); DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore);
}; };