Browse Source

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

deepak1556 8 years ago
parent
commit
bd34db256b
2 changed files with 32 additions and 20 deletions
  1. 23 20
      atom/browser/atom_access_token_store.cc
  2. 9 0
      atom/browser/atom_access_token_store.h

+ 23 - 20
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,29 +27,23 @@ class TokenLoadingJob : public base::RefCountedThreadSafe<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>;
-
-  ~TokenLoadingJob() {}
-
-  void PerformWorkOnUIThread() {
+  void Run(AtomBrowserContext* browser_context) {
     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;
+    BrowserThread::PostTask(
+        BrowserThread::IO, FROM_HERE,
+        base::Bind(&TokenLoadingJob::RespondOnIOThread, this));
   }
 
-  void RespondOnOriginatingThread() {
+ private:
+  friend class base::RefCountedThreadSafe<TokenLoadingJob>;
+
+  ~TokenLoadingJob() {}
+
+  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<TokenLoadingJob> {
   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<TokenLoadingJob> job(new TokenLoadingJob(callback));
-  job->Run();
+  scoped_refptr<internal::TokenLoadingJob> job(
+      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,

+ 9 - 0
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<internal::TokenLoadingJob> job);
+
+  scoped_refptr<AtomBrowserContext> browser_context_;
   DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore);
 };