diff --git a/atom/browser/atom_access_token_store.cc b/atom/browser/atom_access_token_store.cc index 5a4482b00b41..7c04113ff502 100644 --- a/atom/browser/atom_access_token_store.cc +++ b/atom/browser/atom_access_token_store.cc @@ -11,6 +11,8 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/geolocation_provider.h" +using content::BrowserThread; + namespace atom { namespace { @@ -22,6 +24,49 @@ const char* kGeolocationProviderURL = "https://www.googleapis.com/geolocation/v1/geolocate?key=" GOOGLEAPIS_API_KEY; +// Loads access tokens and other necessary data on the UI thread, and +// calls back to the originator on the originating thread. +class TokenLoadingJob : public base::RefCountedThreadSafe { + public: + explicit TokenLoadingJob( + 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)); + } + + private: + friend class base::RefCountedThreadSafe; + + ~TokenLoadingJob() {} + + void PerformWorkOnUIThread() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + auto browser_context = AtomBrowserContext::From("", false); + request_context_getter_ = browser_context->GetRequestContext(); + } + + void RespondOnOriginatingThread() { + // Equivelent 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. + content::AccessTokenStore::AccessTokenMap access_token_map; + std::pair token_pair; + token_pair.first = GURL(kGeolocationProviderURL); + access_token_map.insert(token_pair); + + callback_.Run(access_token_map, request_context_getter_); + } + + content::AccessTokenStore::LoadAccessTokensCallback callback_; + net::URLRequestContextGetter* request_context_getter_; +}; + } // namespace AtomAccessTokenStore::AtomAccessTokenStore() { @@ -33,35 +78,12 @@ AtomAccessTokenStore::~AtomAccessTokenStore() { void AtomAccessTokenStore::LoadAccessTokens( const LoadAccessTokensCallback& callback) { - content::BrowserThread::PostTaskAndReply( - content::BrowserThread::UI, - FROM_HERE, - base::Bind(&AtomAccessTokenStore::GetRequestContextOnUIThread, this), - base::Bind(&AtomAccessTokenStore::RespondOnOriginatingThread, - this, callback)); + scoped_refptr job(new TokenLoadingJob(callback)); + job->Run(); } void AtomAccessTokenStore::SaveAccessToken(const GURL& server_url, const base::string16& access_token) { } -void AtomAccessTokenStore::GetRequestContextOnUIThread() { - auto browser_context = AtomBrowserContext::From("", false); - request_context_getter_ = browser_context->GetRequestContext(); -} - -void AtomAccessTokenStore::RespondOnOriginatingThread( - const LoadAccessTokensCallback& callback) { - // Equivelent 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. - AccessTokenMap access_token_map; - std::pair token_pair; - token_pair.first = GURL(kGeolocationProviderURL); - access_token_map.insert(token_pair); - - callback.Run(access_token_map, request_context_getter_.get()); - request_context_getter_ = nullptr; -} - } // namespace atom diff --git a/atom/browser/atom_access_token_store.h b/atom/browser/atom_access_token_store.h index 27c1911a65fa..d70d44a0cd3e 100644 --- a/atom/browser/atom_access_token_store.h +++ b/atom/browser/atom_access_token_store.h @@ -21,11 +21,6 @@ class AtomAccessTokenStore : public content::AccessTokenStore { const base::string16& access_token) override; private: - void GetRequestContextOnUIThread(); - void RespondOnOriginatingThread(const LoadAccessTokensCallback& callback); - - scoped_refptr request_context_getter_; - DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore); };