browser: fix race in creation of default browser context by AtomAccessTokenStore

This commit is contained in:
deepak1556 2016-10-15 01:00:54 +05:30
parent 988e2334f5
commit bd34db256b
2 changed files with 32 additions and 20 deletions

View file

@ -17,7 +17,7 @@ using content::BrowserThread;
namespace atom { namespace atom {
namespace { namespace internal {
// Loads access tokens and other necessary data on the UI thread, and // Loads access tokens and other necessary data on the UI thread, and
// calls back to the originator on the originating thread. // calls back to the originator on the originating thread.
@ -27,12 +27,15 @@ class TokenLoadingJob : public base::RefCountedThreadSafe<TokenLoadingJob> {
const content::AccessTokenStore::LoadAccessTokensCallback& callback) const content::AccessTokenStore::LoadAccessTokensCallback& callback)
: callback_(callback), request_context_getter_(nullptr) {} : callback_(callback), request_context_getter_(nullptr) {}
void Run() { void Run(AtomBrowserContext* browser_context) {
BrowserThread::PostTaskAndReply( DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::UI, request_context_getter_ = browser_context->GetRequestContext();
FROM_HERE, std::unique_ptr<base::Environment> env(base::Environment::Create());
base::Bind(&TokenLoadingJob::PerformWorkOnUIThread, this), if (!env->GetVar("GOOGLE_API_KEY", &api_key_))
base::Bind(&TokenLoadingJob::RespondOnOriginatingThread, this)); api_key_ = GOOGLEAPIS_API_KEY;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&TokenLoadingJob::RespondOnIOThread, this));
} }
private: private:
@ -40,16 +43,7 @@ class TokenLoadingJob : public base::RefCountedThreadSafe<TokenLoadingJob> {
~TokenLoadingJob() {} ~TokenLoadingJob() {}
void PerformWorkOnUIThread() { void RespondOnIOThread() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto browser_context = AtomBrowserContext::From("", false);
request_context_getter_ = browser_context->GetRequestContext();
std::unique_ptr<base::Environment> env(base::Environment::Create());
if (!env->GetVar("GOOGLE_API_KEY", &api_key_))
api_key_ = GOOGLEAPIS_API_KEY;
}
void RespondOnOriginatingThread() {
// Equivalent to access_token_map[kGeolocationProviderURL]. // Equivalent to access_token_map[kGeolocationProviderURL].
// Somehow base::string16 is causing compilation errors when used in a pair // Somehow base::string16 is causing compilation errors when used in a pair
// of std::map on Linux, this can work around it. // of std::map on Linux, this can work around it.
@ -66,9 +60,10 @@ class TokenLoadingJob : public base::RefCountedThreadSafe<TokenLoadingJob> {
std::string api_key_; std::string api_key_;
}; };
} // namespace } // namespace internal
AtomAccessTokenStore::AtomAccessTokenStore() { AtomAccessTokenStore::AtomAccessTokenStore() {
browser_context_ = AtomBrowserContext::From("", false);
content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices(); content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
} }
@ -77,8 +72,16 @@ AtomAccessTokenStore::~AtomAccessTokenStore() {
void AtomAccessTokenStore::LoadAccessTokens( void AtomAccessTokenStore::LoadAccessTokens(
const LoadAccessTokensCallback& callback) { const LoadAccessTokensCallback& callback) {
scoped_refptr<TokenLoadingJob> job(new TokenLoadingJob(callback)); scoped_refptr<internal::TokenLoadingJob> job(
job->Run(); new internal::TokenLoadingJob(callback));
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&AtomAccessTokenStore::RunTokenLoadingJob,
this, base::RetainedRef(job)));
}
void AtomAccessTokenStore::RunTokenLoadingJob(
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,6 +9,12 @@
namespace atom { namespace atom {
class AtomBrowserContext;
namespace internal {
class TokenLoadingJob;
}
class AtomAccessTokenStore : public content::AccessTokenStore { class AtomAccessTokenStore : public content::AccessTokenStore {
public: public:
AtomAccessTokenStore(); AtomAccessTokenStore();
@ -21,6 +27,9 @@ class AtomAccessTokenStore : public content::AccessTokenStore {
const base::string16& access_token) override; const base::string16& access_token) override;
private: private:
void RunTokenLoadingJob(scoped_refptr<internal::TokenLoadingJob> job);
scoped_refptr<AtomBrowserContext> browser_context_;
DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore); DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore);
}; };