Commit 1b6587dc authored by sorin@chromium.org's avatar sorin@chromium.org

Refactor the interface for the CrxDownloader to take a callback.

To be consistent with the rest of the artifacts in the component updater,
the non-blocking calls for the CrxDownloader take a callback to be called
when the non-blocking call completes.

This change allows adding further callbacks besides the completion
callback, such as a callback to be called when download progress is made.

This change is a pure refactoring and it should be a NOP in terms of
existing functionality.

BUG=366388

Review URL: https://codereview.chromium.org/250383008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266304 0039d316-1c4b-4281-b951-d872f2087c98
parent b878bc10
...@@ -386,9 +386,8 @@ HRESULT CleanupStaleJobs( ...@@ -386,9 +386,8 @@ HRESULT CleanupStaleJobs(
BackgroundDownloader::BackgroundDownloader( BackgroundDownloader::BackgroundDownloader(
scoped_ptr<CrxDownloader> successor, scoped_ptr<CrxDownloader> successor,
net::URLRequestContextGetter* context_getter, net::URLRequestContextGetter* context_getter,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner)
const DownloadCallback& download_callback) : CrxDownloader(successor.Pass()),
: CrxDownloader(successor.Pass(), download_callback),
context_getter_(context_getter), context_getter_(context_getter),
task_runner_(task_runner), task_runner_(task_runner),
is_completed_(false) { is_completed_(false) {
......
...@@ -28,8 +28,7 @@ class BackgroundDownloader : public CrxDownloader { ...@@ -28,8 +28,7 @@ class BackgroundDownloader : public CrxDownloader {
friend class CrxDownloader; friend class CrxDownloader;
BackgroundDownloader(scoped_ptr<CrxDownloader> successor, BackgroundDownloader(scoped_ptr<CrxDownloader> successor,
net::URLRequestContextGetter* context_getter, net::URLRequestContextGetter* context_getter,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner);
const DownloadCallback& download_callback);
virtual ~BackgroundDownloader(); virtual ~BackgroundDownloader();
private: private:
......
...@@ -680,14 +680,13 @@ void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) { ...@@ -680,14 +680,13 @@ void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
!workitem->on_demand && allow_background_download && !workitem->on_demand && allow_background_download &&
config_->UseBackgroundDownloader(); config_->UseBackgroundDownloader();
crx_downloader_.reset(CrxDownloader::Create( crx_downloader_.reset(CrxDownloader::Create(is_background_download,
is_background_download, config_->RequestContext(),
config_->RequestContext(), blocking_task_runner_));
blocking_task_runner_, crx_downloader_->StartDownload(*urls,
base::Bind(&CrxUpdateService::DownloadComplete, base::Bind(&CrxUpdateService::DownloadComplete,
base::Unretained(this), base::Unretained(this),
base::Passed(&crx_context)))); base::Passed(&crx_context)));
crx_downloader_->StartDownload(*urls);
} }
void CrxUpdateService::UpdateCheckComplete( void CrxUpdateService::UpdateCheckComplete(
......
...@@ -28,31 +28,25 @@ CrxDownloader::DownloadMetrics::DownloadMetrics() ...@@ -28,31 +28,25 @@ CrxDownloader::DownloadMetrics::DownloadMetrics()
CrxDownloader* CrxDownloader::Create( CrxDownloader* CrxDownloader::Create(
bool is_background_download, bool is_background_download,
net::URLRequestContextGetter* context_getter, net::URLRequestContextGetter* context_getter,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner) {
const DownloadCallback& download_callback) {
scoped_ptr<CrxDownloader> url_fetcher_downloader( scoped_ptr<CrxDownloader> url_fetcher_downloader(
new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(), new UrlFetcherDownloader(scoped_ptr<CrxDownloader>().Pass(),
context_getter, context_getter,
task_runner, task_runner));
download_callback));
#if defined (OS_WIN) #if defined (OS_WIN)
if (is_background_download) { if (is_background_download) {
return new BackgroundDownloader(url_fetcher_downloader.Pass(), return new BackgroundDownloader(url_fetcher_downloader.Pass(),
context_getter, context_getter,
task_runner, task_runner);
download_callback);
} }
#endif #endif
return url_fetcher_downloader.release(); return url_fetcher_downloader.release();
} }
CrxDownloader::CrxDownloader( CrxDownloader::CrxDownloader(scoped_ptr<CrxDownloader> successor)
scoped_ptr<CrxDownloader> successor, : successor_(successor.Pass()) {
const DownloadCallback& download_callback) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
: successor_(successor.Pass()),
download_callback_(download_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
} }
CrxDownloader::~CrxDownloader() { CrxDownloader::~CrxDownloader() {
...@@ -74,20 +68,23 @@ CrxDownloader::download_metrics() const { ...@@ -74,20 +68,23 @@ CrxDownloader::download_metrics() const {
return retval; return retval;
} }
void CrxDownloader::StartDownloadFromUrl(const GURL& url) { void CrxDownloader::StartDownloadFromUrl(
const GURL& url,
const DownloadCallback& download_callback) {
std::vector<GURL> urls; std::vector<GURL> urls;
urls.push_back(url); urls.push_back(url);
StartDownload(urls); StartDownload(urls, download_callback);
} }
void CrxDownloader::StartDownload(const std::vector<GURL>& urls) { void CrxDownloader::StartDownload(const std::vector<GURL>& urls,
const DownloadCallback& download_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (urls.empty()) { if (urls.empty()) {
// Make a result and complete the download with a generic error for now. // Make a result and complete the download with a generic error for now.
Result result; Result result;
result.error = -1; result.error = -1;
download_callback_.Run(result); download_callback.Run(result);
return; return;
} }
...@@ -97,6 +94,7 @@ void CrxDownloader::StartDownload(const std::vector<GURL>& urls) { ...@@ -97,6 +94,7 @@ void CrxDownloader::StartDownload(const std::vector<GURL>& urls) {
// reset at this point, and the iterator will be valid in all conditions. // reset at this point, and the iterator will be valid in all conditions.
urls_ = urls; urls_ = urls;
current_url_ = urls_.begin(); current_url_ = urls_.begin();
download_callback_ = download_callback;
DoStartDownload(*current_url_); DoStartDownload(*current_url_);
} }
...@@ -133,7 +131,7 @@ void CrxDownloader::OnDownloadComplete( ...@@ -133,7 +131,7 @@ void CrxDownloader::OnDownloadComplete(
// of urls. Otherwise, the request ends here since the current downloader // of urls. Otherwise, the request ends here since the current downloader
// has tried all urls and it can't fall back on any other downloader. // has tried all urls and it can't fall back on any other downloader.
if (successor_ && !urls_.empty()) { if (successor_ && !urls_.empty()) {
successor_->StartDownload(urls_); successor_->StartDownload(urls_, download_callback_);
return; return;
} }
} }
......
...@@ -78,22 +78,22 @@ class CrxDownloader { ...@@ -78,22 +78,22 @@ class CrxDownloader {
static CrxDownloader* Create( static CrxDownloader* Create(
bool is_background_download, bool is_background_download,
net::URLRequestContextGetter* context_getter, net::URLRequestContextGetter* context_getter,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner);
const DownloadCallback& download_callback);
virtual ~CrxDownloader(); virtual ~CrxDownloader();
// Starts the download. One instance of the class handles one download only. // Starts the download. One instance of the class handles one download only.
// One instance of CrxDownloader can only be started once, otherwise the // One instance of CrxDownloader can only be started once, otherwise the
// behavior is undefined. The callback gets invoked if the download can't // behavior is undefined. The callback gets invoked if the download can't
// be started. // be started.
void StartDownloadFromUrl(const GURL& url); void StartDownloadFromUrl(const GURL& url,
void StartDownload(const std::vector<GURL>& urls); const DownloadCallback& download_callback);
void StartDownload(const std::vector<GURL>& urls,
const DownloadCallback& download_callback);
const std::vector<DownloadMetrics> download_metrics() const; const std::vector<DownloadMetrics> download_metrics() const;
protected: protected:
CrxDownloader(scoped_ptr<CrxDownloader> successor, explicit CrxDownloader(scoped_ptr<CrxDownloader> successor);
const DownloadCallback& download_callback);
// Handles the fallback in the case of multiple urls and routing of the // Handles the fallback in the case of multiple urls and routing of the
// download to the following successor in the chain. Derived classes must call // download to the following successor in the chain. Derived classes must call
......
...@@ -54,6 +54,8 @@ class CrxDownloaderTest : public testing::Test { ...@@ -54,6 +54,8 @@ class CrxDownloaderTest : public testing::Test {
protected: protected:
scoped_ptr<CrxDownloader> crx_downloader_; scoped_ptr<CrxDownloader> crx_downloader_;
CrxDownloader::DownloadCallback callback_;
int crx_context_; int crx_context_;
int error_; int error_;
base::FilePath response_; base::FilePath response_;
...@@ -73,7 +75,10 @@ class CrxDownloaderTest : public testing::Test { ...@@ -73,7 +75,10 @@ class CrxDownloaderTest : public testing::Test {
const int CrxDownloaderTest::kExpectedContext; const int CrxDownloaderTest::kExpectedContext;
CrxDownloaderTest::CrxDownloaderTest() CrxDownloaderTest::CrxDownloaderTest()
: crx_context_(0), : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete,
base::Unretained(this),
kExpectedContext)),
crx_context_(0),
error_(0), error_(0),
num_calls_(0), num_calls_(0),
blocking_task_runner_(BrowserThread::GetBlockingPool()-> blocking_task_runner_(BrowserThread::GetBlockingPool()->
...@@ -94,10 +99,7 @@ void CrxDownloaderTest::SetUp() { ...@@ -94,10 +99,7 @@ void CrxDownloaderTest::SetUp() {
crx_downloader_.reset(CrxDownloader::Create( crx_downloader_.reset(CrxDownloader::Create(
false, // Do not use the background downloader in these tests. false, // Do not use the background downloader in these tests.
context_.get(), context_.get(),
blocking_task_runner_, blocking_task_runner_));
base::Bind(&CrxDownloaderTest::DownloadComplete,
base::Unretained(this),
kExpectedContext)));
} }
void CrxDownloaderTest::TearDown() { void CrxDownloaderTest::TearDown() {
...@@ -137,7 +139,7 @@ void CrxDownloaderTest::RunThreadsUntilIdle() { ...@@ -137,7 +139,7 @@ void CrxDownloaderTest::RunThreadsUntilIdle() {
// Tests that starting a download without a url results in an error. // Tests that starting a download without a url results in an error.
TEST_F(CrxDownloaderTest, NoUrl) { TEST_F(CrxDownloaderTest, NoUrl) {
std::vector<GURL> urls; std::vector<GURL> urls;
crx_downloader_->StartDownload(urls); crx_downloader_->StartDownload(urls, callback_);
RunThreadsUntilIdle(); RunThreadsUntilIdle();
EXPECT_EQ(-1, error_); EXPECT_EQ(-1, error_);
...@@ -156,7 +158,7 @@ TEST_F(CrxDownloaderTest, OneUrl) { ...@@ -156,7 +158,7 @@ TEST_F(CrxDownloaderTest, OneUrl) {
expected_crx_url, expected_crx_url,
test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
crx_downloader_->StartDownloadFromUrl(expected_crx_url); crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_);
RunThreads(); RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount()); EXPECT_EQ(1, interceptor.GetHitCount());
...@@ -187,7 +189,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) { ...@@ -187,7 +189,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) {
urls.push_back(expected_crx_url); urls.push_back(expected_crx_url);
urls.push_back(expected_crx_url); urls.push_back(expected_crx_url);
crx_downloader_->StartDownload(urls); crx_downloader_->StartDownload(urls, callback_);
RunThreads(); RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount()); EXPECT_EQ(1, interceptor.GetHitCount());
...@@ -209,7 +211,8 @@ TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { ...@@ -209,7 +211,8 @@ TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) {
crx_downloader_->StartDownloadFromUrl( crx_downloader_->StartDownloadFromUrl(
GURL("http://no.such.host" GURL("http://no.such.host"
"/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"),
callback_);
RunThreads(); RunThreads();
EXPECT_EQ(0, interceptor.GetHitCount()); EXPECT_EQ(0, interceptor.GetHitCount());
...@@ -229,7 +232,9 @@ TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { ...@@ -229,7 +232,9 @@ TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) {
expected_crx_url, expected_crx_url,
test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file")); crx_downloader_->StartDownloadFromUrl(
GURL("http://localhost/no/such/file"),
callback_);
RunThreads(); RunThreads();
EXPECT_EQ(0, interceptor.GetHitCount()); EXPECT_EQ(0, interceptor.GetHitCount());
...@@ -259,7 +264,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { ...@@ -259,7 +264,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) {
urls.push_back(GURL("http://localhost/no/such/file")); urls.push_back(GURL("http://localhost/no/such/file"));
urls.push_back(expected_crx_url); urls.push_back(expected_crx_url);
crx_downloader_->StartDownload(urls); crx_downloader_->StartDownload(urls, callback_);
RunThreads(); RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount()); EXPECT_EQ(1, interceptor.GetHitCount());
...@@ -284,7 +289,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { ...@@ -284,7 +289,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
urls.push_back(expected_crx_url); urls.push_back(expected_crx_url);
urls.push_back(GURL("http://localhost/no/such/file")); urls.push_back(GURL("http://localhost/no/such/file"));
crx_downloader_->StartDownload(urls); crx_downloader_->StartDownload(urls, callback_);
RunThreads(); RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount()); EXPECT_EQ(1, interceptor.GetHitCount());
...@@ -309,7 +314,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { ...@@ -309,7 +314,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
urls.push_back(GURL("http://no.such.host/" urls.push_back(GURL("http://no.such.host/"
"/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"));
crx_downloader_->StartDownload(urls); crx_downloader_->StartDownload(urls, callback_);
RunThreads(); RunThreads();
EXPECT_EQ(0, interceptor.GetHitCount()); EXPECT_EQ(0, interceptor.GetHitCount());
...@@ -320,4 +325,3 @@ TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { ...@@ -320,4 +325,3 @@ TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
} }
} // namespace component_updater } // namespace component_updater
...@@ -17,9 +17,8 @@ namespace component_updater { ...@@ -17,9 +17,8 @@ namespace component_updater {
UrlFetcherDownloader::UrlFetcherDownloader( UrlFetcherDownloader::UrlFetcherDownloader(
scoped_ptr<CrxDownloader> successor, scoped_ptr<CrxDownloader> successor,
net::URLRequestContextGetter* context_getter, net::URLRequestContextGetter* context_getter,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner)
const DownloadCallback& download_callback) : CrxDownloader(successor.Pass()),
: CrxDownloader(successor.Pass(), download_callback),
context_getter_(context_getter), context_getter_(context_getter),
task_runner_(task_runner), task_runner_(task_runner),
downloaded_bytes_(-1), downloaded_bytes_(-1),
......
...@@ -26,8 +26,7 @@ class UrlFetcherDownloader : public CrxDownloader, ...@@ -26,8 +26,7 @@ class UrlFetcherDownloader : public CrxDownloader,
friend class CrxDownloader; friend class CrxDownloader;
UrlFetcherDownloader(scoped_ptr<CrxDownloader> successor, UrlFetcherDownloader(scoped_ptr<CrxDownloader> successor,
net::URLRequestContextGetter* context_getter, net::URLRequestContextGetter* context_getter,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner);
const DownloadCallback& download_callback);
virtual ~UrlFetcherDownloader(); virtual ~UrlFetcherDownloader();
private: private:
......
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