| 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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 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 base::CreateTaskRunnerWithTraits( | 46 base::CreateTaskRunnerWithTraits( |
| 47 base::TaskTraits() | 47 {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 48 .MayBlock() | 48 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}) |
| 49 .WithPriority(base::TaskPriority::BACKGROUND) | |
| 50 .WithShutdownBehavior( | |
| 51 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)) | |
| 52 .get(), | 49 .get(), |
| 53 FROM_HERE, base::Bind(&base::PathExists, local_path_), | 50 FROM_HERE, base::Bind(&base::PathExists, local_path_), |
| 54 base::Bind(&FileDownloader::OnFileExistsCheckDone, | 51 base::Bind(&FileDownloader::OnFileExistsCheckDone, |
| 55 weak_ptr_factory_.GetWeakPtr())); | 52 weak_ptr_factory_.GetWeakPtr())); |
| 56 } | 53 } |
| 57 } | 54 } |
| 58 | 55 |
| 59 FileDownloader::~FileDownloader() {} | 56 FileDownloader::~FileDownloader() {} |
| 60 | 57 |
| 61 void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { | 58 void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 79 | 76 |
| 80 base::FilePath response_path; | 77 base::FilePath response_path; |
| 81 bool success = source->GetResponseAsFilePath(false, &response_path); | 78 bool success = source->GetResponseAsFilePath(false, &response_path); |
| 82 if (!success) { | 79 if (!success) { |
| 83 callback_.Run(FAILED); | 80 callback_.Run(FAILED); |
| 84 return; | 81 return; |
| 85 } | 82 } |
| 86 | 83 |
| 87 base::PostTaskAndReplyWithResult( | 84 base::PostTaskAndReplyWithResult( |
| 88 base::CreateTaskRunnerWithTraits( | 85 base::CreateTaskRunnerWithTraits( |
| 89 base::TaskTraits() | 86 {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 90 .MayBlock() | 87 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}) |
| 91 .WithPriority(base::TaskPriority::BACKGROUND) | |
| 92 .WithShutdownBehavior( | |
| 93 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)) | |
| 94 .get(), | 88 .get(), |
| 95 FROM_HERE, base::Bind(&base::Move, response_path, local_path_), | 89 FROM_HERE, base::Bind(&base::Move, response_path, local_path_), |
| 96 base::Bind(&FileDownloader::OnFileMoveDone, | 90 base::Bind(&FileDownloader::OnFileMoveDone, |
| 97 weak_ptr_factory_.GetWeakPtr())); | 91 weak_ptr_factory_.GetWeakPtr())); |
| 98 } | 92 } |
| 99 | 93 |
| 100 void FileDownloader::OnFileExistsCheckDone(bool exists) { | 94 void FileDownloader::OnFileExistsCheckDone(bool exists) { |
| 101 if (exists) | 95 if (exists) |
| 102 callback_.Run(EXISTS); | 96 callback_.Run(EXISTS); |
| 103 else | 97 else |
| 104 fetcher_->Start(); | 98 fetcher_->Start(); |
| 105 } | 99 } |
| 106 | 100 |
| 107 void FileDownloader::OnFileMoveDone(bool success) { | 101 void FileDownloader::OnFileMoveDone(bool success) { |
| 108 if (!success) { | 102 if (!success) { |
| 109 DLOG(WARNING) << "Could not move file to " | 103 DLOG(WARNING) << "Could not move file to " |
| 110 << local_path_.LossyDisplayName(); | 104 << local_path_.LossyDisplayName(); |
| 111 } | 105 } |
| 112 | 106 |
| 113 callback_.Run(success ? DOWNLOADED : FAILED); | 107 callback_.Run(success ? DOWNLOADED : FAILED); |
| 114 } | 108 } |
| OLD | NEW |