diff --git a/atom/browser/atom_access_token_store.cc b/atom/browser/atom_access_token_store.cc index b4e19086d4..a92112db82 100644 --- a/atom/browser/atom_access_token_store.cc +++ b/atom/browser/atom_access_token_store.cc @@ -17,7 +17,7 @@ using content::BrowserThread; namespace atom { -namespace { +namespace internal { // Loads access tokens and other necessary data on the UI thread, and // calls back to the originator on the originating thread. @@ -27,12 +27,15 @@ class TokenLoadingJob : public base::RefCountedThreadSafe { const content::AccessTokenStore::LoadAccessTokensCallback& callback) : callback_(callback), request_context_getter_(nullptr) {} - void Run() { - BrowserThread::PostTaskAndReply( - BrowserThread::UI, - FROM_HERE, - base::Bind(&TokenLoadingJob::PerformWorkOnUIThread, this), - base::Bind(&TokenLoadingJob::RespondOnOriginatingThread, this)); + void Run(AtomBrowserContext* browser_context) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + request_context_getter_ = browser_context->GetRequestContext(); + std::unique_ptr 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: @@ -40,16 +43,7 @@ class TokenLoadingJob : public base::RefCountedThreadSafe { ~TokenLoadingJob() {} - void PerformWorkOnUIThread() { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - auto browser_context = AtomBrowserContext::From("", false); - request_context_getter_ = browser_context->GetRequestContext(); - std::unique_ptr env(base::Environment::Create()); - if (!env->GetVar("GOOGLE_API_KEY", &api_key_)) - api_key_ = GOOGLEAPIS_API_KEY; - } - - void RespondOnOriginatingThread() { + void RespondOnIOThread() { // 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. @@ -66,9 +60,10 @@ class TokenLoadingJob : public base::RefCountedThreadSafe { std::string api_key_; }; -} // namespace +} // namespace internal AtomAccessTokenStore::AtomAccessTokenStore() { + browser_context_ = AtomBrowserContext::From("", false); content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices(); } @@ -77,8 +72,16 @@ AtomAccessTokenStore::~AtomAccessTokenStore() { void AtomAccessTokenStore::LoadAccessTokens( const LoadAccessTokensCallback& callback) { - scoped_refptr job(new TokenLoadingJob(callback)); - job->Run(); + scoped_refptr job( + new internal::TokenLoadingJob(callback)); + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, + base::Bind(&AtomAccessTokenStore::RunTokenLoadingJob, + this, base::RetainedRef(job))); +} + +void AtomAccessTokenStore::RunTokenLoadingJob( + scoped_refptr job) { + job->Run(browser_context_.get()); } void AtomAccessTokenStore::SaveAccessToken(const GURL& server_url, diff --git a/atom/browser/atom_access_token_store.h b/atom/browser/atom_access_token_store.h index d70d44a0cd..4ebe1a0bc3 100644 --- a/atom/browser/atom_access_token_store.h +++ b/atom/browser/atom_access_token_store.h @@ -9,6 +9,12 @@ namespace atom { +class AtomBrowserContext; + +namespace internal { +class TokenLoadingJob; +} + class AtomAccessTokenStore : public content::AccessTokenStore { public: AtomAccessTokenStore(); @@ -21,6 +27,9 @@ class AtomAccessTokenStore : public content::AccessTokenStore { const base::string16& access_token) override; private: + void RunTokenLoadingJob(scoped_refptr job); + + scoped_refptr browser_context_; DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore); };