| OLD | NEW |
| 1 // Copyright 2015 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/net/file_downloader.h" | 5 #include "chrome/browser/net/file_downloader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/task_scheduler/post_task.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 14 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
| 15 #include "net/url_request/url_fetcher.h" | 15 #include "net/url_request/url_fetcher.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 using content::BrowserThread; | 18 using content::BrowserThread; |
| 19 using net::URLFetcher; | 19 using net::URLFetcher; |
| 20 | 20 |
| 21 const int kNumRetries = 1; | 21 const int kNumRetries = 1; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 36 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 36 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 37 net::LOAD_DO_NOT_SAVE_COOKIES); | 37 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 38 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | 38 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
| 39 fetcher_->SaveResponseToTemporaryFile( | 39 fetcher_->SaveResponseToTemporaryFile( |
| 40 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)); | 40 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)); |
| 41 | 41 |
| 42 if (overwrite) { | 42 if (overwrite) { |
| 43 fetcher_->Start(); | 43 fetcher_->Start(); |
| 44 } else { | 44 } else { |
| 45 base::PostTaskAndReplyWithResult( | 45 base::PostTaskAndReplyWithResult( |
| 46 BrowserThread::GetBlockingPool() | 46 base::CreateTaskRunnerWithTraits( |
| 47 ->GetTaskRunnerWithShutdownBehavior( | 47 base::TaskTraits() |
| 48 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | 48 .MayBlock() |
| 49 .WithPriority(base::TaskPriority::BACKGROUND) |
| 50 .WithShutdownBehavior( |
| 51 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)) |
| 49 .get(), | 52 .get(), |
| 50 FROM_HERE, base::Bind(&base::PathExists, local_path_), | 53 FROM_HERE, base::Bind(&base::PathExists, local_path_), |
| 51 base::Bind(&FileDownloader::OnFileExistsCheckDone, | 54 base::Bind(&FileDownloader::OnFileExistsCheckDone, |
| 52 weak_ptr_factory_.GetWeakPtr())); | 55 weak_ptr_factory_.GetWeakPtr())); |
| 53 } | 56 } |
| 54 } | 57 } |
| 55 | 58 |
| 56 FileDownloader::~FileDownloader() {} | 59 FileDownloader::~FileDownloader() {} |
| 57 | 60 |
| 58 void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { | 61 void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 75 } | 78 } |
| 76 | 79 |
| 77 base::FilePath response_path; | 80 base::FilePath response_path; |
| 78 bool success = source->GetResponseAsFilePath(false, &response_path); | 81 bool success = source->GetResponseAsFilePath(false, &response_path); |
| 79 if (!success) { | 82 if (!success) { |
| 80 callback_.Run(FAILED); | 83 callback_.Run(FAILED); |
| 81 return; | 84 return; |
| 82 } | 85 } |
| 83 | 86 |
| 84 base::PostTaskAndReplyWithResult( | 87 base::PostTaskAndReplyWithResult( |
| 85 BrowserThread::GetBlockingPool() | 88 base::CreateTaskRunnerWithTraits( |
| 86 ->GetTaskRunnerWithShutdownBehavior( | 89 base::TaskTraits() |
| 87 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | 90 .MayBlock() |
| 91 .WithPriority(base::TaskPriority::BACKGROUND) |
| 92 .WithShutdownBehavior( |
| 93 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)) |
| 88 .get(), | 94 .get(), |
| 89 FROM_HERE, base::Bind(&base::Move, response_path, local_path_), | 95 FROM_HERE, base::Bind(&base::Move, response_path, local_path_), |
| 90 base::Bind(&FileDownloader::OnFileMoveDone, | 96 base::Bind(&FileDownloader::OnFileMoveDone, |
| 91 weak_ptr_factory_.GetWeakPtr())); | 97 weak_ptr_factory_.GetWeakPtr())); |
| 92 } | 98 } |
| 93 | 99 |
| 94 void FileDownloader::OnFileExistsCheckDone(bool exists) { | 100 void FileDownloader::OnFileExistsCheckDone(bool exists) { |
| 95 if (exists) | 101 if (exists) |
| 96 callback_.Run(EXISTS); | 102 callback_.Run(EXISTS); |
| 97 else | 103 else |
| 98 fetcher_->Start(); | 104 fetcher_->Start(); |
| 99 } | 105 } |
| 100 | 106 |
| 101 void FileDownloader::OnFileMoveDone(bool success) { | 107 void FileDownloader::OnFileMoveDone(bool success) { |
| 102 if (!success) { | 108 if (!success) { |
| 103 DLOG(WARNING) << "Could not move file to " | 109 DLOG(WARNING) << "Could not move file to " |
| 104 << local_path_.LossyDisplayName(); | 110 << local_path_.LossyDisplayName(); |
| 105 } | 111 } |
| 106 | 112 |
| 107 callback_.Run(success ? DOWNLOADED : FAILED); | 113 callback_.Run(success ? DOWNLOADED : FAILED); |
| 108 } | 114 } |
| OLD | NEW |