Delay DownloadManager creation by adding a callback queue to DownloadService.

ExtensionDownloadsEventRouter's ctor pushes a Callback to this queue instead of calling GetDownloadManager(), which would create the manager. EDER doesn't really need the manager in its ctor, it just needs the manager before the manager does something interesting.

BUG=106502

Review URL: http://codereview.chromium.org/8804018

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113892 0039d316-1c4b-4281-b951-d872f2087c98
parent bb8fdacc
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <string> #include <string>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/logging.h" #include "base/logging.h"
...@@ -474,16 +475,24 @@ base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { ...@@ -474,16 +475,24 @@ base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) {
} }
} // anonymous namespace } // anonymous namespace
ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(Profile* profile)
Profile* profile)
: profile_(profile), : profile_(profile),
manager_( manager_(NULL) {
profile ?
DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() :
NULL) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile_); DCHECK(profile_);
DCHECK(manager_); // Register a callback with the DownloadService for this profile to be called
// when it creates the DownloadManager, or now if the manager already exists.
DownloadServiceFactory::GetForProfile(profile)->OnManagerCreated(base::Bind(
&ExtensionDownloadsEventRouter::Init, base::Unretained(this)));
}
// The only public methods on this class are ModelChanged() and
// ManagerGoingDown(), and they are only called by DownloadManager, so
// there's no way for any methods on this class to be called before
// DownloadService calls Init() via the OnManagerCreated Callback above.
void ExtensionDownloadsEventRouter::Init(DownloadManager* manager) {
DCHECK(manager_ == NULL);
manager_ = manager;
manager_->AddObserver(this); manager_->AddObserver(this);
} }
......
...@@ -262,6 +262,7 @@ class ExtensionDownloadsEventRouter : public DownloadManager::Observer { ...@@ -262,6 +262,7 @@ class ExtensionDownloadsEventRouter : public DownloadManager::Observer {
virtual void ManagerGoingDown() OVERRIDE; virtual void ManagerGoingDown() OVERRIDE;
private: private:
void Init(DownloadManager* manager);
void DispatchEvent(const char* event_name, base::Value* json_arg); void DispatchEvent(const char* event_name, base::Value* json_arg);
typedef base::hash_map<int, DownloadItem*> ItemMap; typedef base::hash_map<int, DownloadItem*> ItemMap;
typedef std::set<int> DownloadIdSet; typedef std::set<int> DownloadIdSet;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/download/download_service.h" #include "chrome/browser/download/download_service.h"
#include "base/callback.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h" #include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_service_factory.h" #include "chrome/browser/download/download_service_factory.h"
...@@ -29,6 +30,15 @@ DownloadIdFactory* DownloadService::GetDownloadIdFactory() const { ...@@ -29,6 +30,15 @@ DownloadIdFactory* DownloadService::GetDownloadIdFactory() const {
return id_factory_.get(); return id_factory_.get();
} }
void DownloadService::OnManagerCreated(
const DownloadService::OnManagerCreatedCallback& cb) {
if (download_manager_created_) {
cb.Run(manager_.get());
} else {
on_manager_created_callbacks_.push_back(cb);
}
}
DownloadManager* DownloadService::GetDownloadManager() { DownloadManager* DownloadService::GetDownloadManager() {
if (!download_manager_created_) { if (!download_manager_created_) {
// In case the delegate has already been set by // In case the delegate has already been set by
...@@ -42,6 +52,12 @@ DownloadManager* DownloadService::GetDownloadManager() { ...@@ -42,6 +52,12 @@ DownloadManager* DownloadService::GetDownloadManager() {
manager_->Init(profile_); manager_->Init(profile_);
manager_delegate_->SetDownloadManager(manager_); manager_delegate_->SetDownloadManager(manager_);
download_manager_created_ = true; download_manager_created_ = true;
for (std::vector<OnManagerCreatedCallback>::iterator cb
= on_manager_created_callbacks_.begin();
cb != on_manager_created_callbacks_.end(); ++cb) {
cb->Run(manager_.get());
}
on_manager_created_callbacks_.clear();
} }
return manager_.get(); return manager_.get();
} }
......
...@@ -6,7 +6,10 @@ ...@@ -6,7 +6,10 @@
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SERVICE_H_ #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SERVICE_H_
#pragma once #pragma once
#include <vector>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "chrome/browser/profiles/profile_keyed_service.h" #include "chrome/browser/profiles/profile_keyed_service.h"
...@@ -25,6 +28,10 @@ class DownloadService : public ProfileKeyedService { ...@@ -25,6 +28,10 @@ class DownloadService : public ProfileKeyedService {
DownloadIdFactory* GetDownloadIdFactory() const; DownloadIdFactory* GetDownloadIdFactory() const;
// Register a callback to be called whenever the DownloadManager is created.
typedef base::Callback<void(DownloadManager*)> OnManagerCreatedCallback;
void OnManagerCreated(const OnManagerCreatedCallback& cb);
// Get the download manager. Creates the download manager if // Get the download manager. Creates the download manager if
// it does not already exist. // it does not already exist.
DownloadManager* GetDownloadManager(); DownloadManager* GetDownloadManager();
...@@ -64,6 +71,8 @@ class DownloadService : public ProfileKeyedService { ...@@ -64,6 +71,8 @@ class DownloadService : public ProfileKeyedService {
scoped_refptr<DownloadManager> manager_; scoped_refptr<DownloadManager> manager_;
scoped_refptr<ChromeDownloadManagerDelegate> manager_delegate_; scoped_refptr<ChromeDownloadManagerDelegate> manager_delegate_;
std::vector<OnManagerCreatedCallback> on_manager_created_callbacks_;
DISALLOW_COPY_AND_ASSIGN(DownloadService); DISALLOW_COPY_AND_ASSIGN(DownloadService);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment