Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist_ downloader.h" | 5 #include "chrome/browser/android/file_downloader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 12 #include "net/http/http_status_code.h" | 13 #include "net/http/http_status_code.h" |
| 13 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_fetcher.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 using net::URLFetcher; | 18 using net::URLFetcher; |
| 18 | 19 |
| 19 const int kNumRetries = 1; | 20 const int kNumRetries = 1; |
| 20 | 21 |
| 21 SupervisedUserBlacklistDownloader::SupervisedUserBlacklistDownloader( | 22 FileDownloader::FileDownloader(const GURL& url, |
|
Bernhard Bauer
2015/08/03 17:04:50
Can we move this into platform independent code an
Marc Treib
2015/08/04 08:16:41
Dammit, I was sure I had written a comment saying
Bernhard Bauer
2015/08/04 08:30:02
SG.
Marc Treib
2015/08/04 08:58:42
Done.
| |
| 22 const GURL& url, | 23 const base::FilePath& path, |
| 23 const base::FilePath& path, | 24 net::URLRequestContextGetter* request_context, |
| 24 net::URLRequestContextGetter* request_context, | 25 const DownloadFinishedCallback& callback) |
| 25 const DownloadFinishedCallback& callback) | |
| 26 : callback_(callback), | 26 : callback_(callback), |
| 27 fetcher_(URLFetcher::Create(url, URLFetcher::GET, this)), | 27 fetcher_(URLFetcher::Create(url, URLFetcher::GET, this)), |
| 28 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
| 29 fetcher_->SetRequestContext(request_context); | 29 fetcher_->SetRequestContext(request_context); |
| 30 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 30 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 31 net::LOAD_DO_NOT_SAVE_COOKIES); | 31 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 32 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | 32 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
| 33 fetcher_->SaveResponseToFileAtPath( | 33 fetcher_->SaveResponseToFileAtPath( |
| 34 path, | 34 path, |
| 35 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 35 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 36 | 36 |
| 37 base::PostTaskAndReplyWithResult( | 37 base::PostTaskAndReplyWithResult( |
| 38 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 38 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 39 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | 39 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), |
| 40 FROM_HERE, | 40 FROM_HERE, |
| 41 base::Bind(&base::PathExists, path), | 41 base::Bind(&base::PathExists, path), |
| 42 base::Bind(&SupervisedUserBlacklistDownloader::OnFileExistsCheckDone, | 42 base::Bind(&FileDownloader::OnFileExistsCheckDone, |
| 43 weak_ptr_factory_.GetWeakPtr())); | 43 weak_ptr_factory_.GetWeakPtr())); |
| 44 } | 44 } |
| 45 | 45 |
| 46 SupervisedUserBlacklistDownloader::~SupervisedUserBlacklistDownloader() {} | 46 FileDownloader::~FileDownloader() {} |
| 47 | 47 |
| 48 void SupervisedUserBlacklistDownloader::OnURLFetchComplete( | 48 void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
| 49 const net::URLFetcher* source) { | |
| 50 DCHECK_EQ(fetcher_.get(), source); | 49 DCHECK_EQ(fetcher_.get(), source); |
| 50 // Delete |fetcher_| when we leave this method. This is necessary so the | |
| 51 // download file will be deleted if the download failed. | |
| 52 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass()); | |
| 51 | 53 |
| 52 const net::URLRequestStatus& status = source->GetStatus(); | 54 const net::URLRequestStatus& status = source->GetStatus(); |
| 53 if (!status.is_success()) { | 55 if (!status.is_success()) { |
| 54 DLOG(WARNING) << "URLRequestStatus error " << status.error(); | 56 DLOG(WARNING) << "URLRequestStatus error " << status.error(); |
| 55 callback_.Run(false); | 57 callback_.Run(false); |
| 56 return; | 58 return; |
| 57 } | 59 } |
| 58 | 60 |
| 59 int response_code = source->GetResponseCode(); | 61 int response_code = source->GetResponseCode(); |
| 60 if (response_code != net::HTTP_OK) { | 62 if (response_code != net::HTTP_OK) { |
| 61 DLOG(WARNING) << "HTTP error " << response_code; | 63 DLOG(WARNING) << "HTTP error " << response_code; |
| 62 callback_.Run(false); | 64 callback_.Run(false); |
| 63 return; | 65 return; |
| 64 } | 66 } |
| 65 | 67 |
| 66 // Take ownership of the new file. | 68 // Take ownership of the new file. |
| 67 base::FilePath response_path; | 69 base::FilePath response_path; |
| 68 bool success = source->GetResponseAsFilePath(true, &response_path); | 70 bool success = source->GetResponseAsFilePath(true, &response_path); |
| 69 callback_.Run(success); | 71 callback_.Run(success); |
| 70 } | 72 } |
| 71 | 73 |
| 72 void SupervisedUserBlacklistDownloader::OnFileExistsCheckDone(bool exists) { | 74 void FileDownloader::OnFileExistsCheckDone(bool exists) { |
| 73 if (exists) { | 75 if (exists) |
| 74 // TODO(treib): Figure out a strategy for updating the file. | |
| 75 callback_.Run(true); | 76 callback_.Run(true); |
| 76 } else { | 77 else |
| 77 fetcher_->Start(); | 78 fetcher_->Start(); |
| 78 } | |
| 79 } | 79 } |
| OLD | NEW |