| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ios/chrome/browser/reading_list/url_downloader.h" | 5 #include "ios/chrome/browser/reading_list/url_downloader.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 task_tracker_.PostTaskAndReplyWithResult( | 43 task_tracker_.PostTaskAndReplyWithResult( |
| 44 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), | 44 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), |
| 45 FROM_HERE, | 45 FROM_HERE, |
| 46 base::Bind(&base::PathExists, | 46 base::Bind(&base::PathExists, |
| 47 reading_list::OfflinePageAbsolutePath(base_directory_, url)), | 47 reading_list::OfflinePageAbsolutePath(base_directory_, url)), |
| 48 callback); | 48 callback); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void URLDownloader::RemoveOfflineURL(const GURL& url) { | 51 void URLDownloader::RemoveOfflineURL(const GURL& url) { |
| 52 // Remove all download tasks for this url as it would be pointless work. | 52 // Remove all download tasks for this url as it would be pointless work. |
| 53 tasks_.erase( | 53 CancelDownloadOfflineURL(url); |
| 54 std::remove(tasks_.begin(), tasks_.end(), std::make_pair(DOWNLOAD, url)), | |
| 55 tasks_.end()); | |
| 56 tasks_.push_back(std::make_pair(DELETE, url)); | 54 tasks_.push_back(std::make_pair(DELETE, url)); |
| 57 HandleNextTask(); | 55 HandleNextTask(); |
| 58 } | 56 } |
| 59 | 57 |
| 60 void URLDownloader::DownloadOfflineURL(const GURL& url) { | 58 void URLDownloader::DownloadOfflineURL(const GURL& url) { |
| 61 if (std::find(tasks_.begin(), tasks_.end(), std::make_pair(DOWNLOAD, url)) == | 59 if (std::find(tasks_.begin(), tasks_.end(), std::make_pair(DOWNLOAD, url)) == |
| 62 tasks_.end()) { | 60 tasks_.end()) { |
| 63 tasks_.push_back(std::make_pair(DOWNLOAD, url)); | 61 tasks_.push_back(std::make_pair(DOWNLOAD, url)); |
| 64 HandleNextTask(); | 62 HandleNextTask(); |
| 65 } | 63 } |
| 66 } | 64 } |
| 67 | 65 |
| 66 void URLDownloader::CancelDownloadOfflineURL(const GURL& url) { |
| 67 tasks_.erase( |
| 68 std::remove(tasks_.begin(), tasks_.end(), std::make_pair(DOWNLOAD, url)), |
| 69 tasks_.end()); |
| 70 } |
| 71 |
| 68 void URLDownloader::DownloadCompletionHandler(const GURL& url, | 72 void URLDownloader::DownloadCompletionHandler(const GURL& url, |
| 69 const std::string& title, | 73 const std::string& title, |
| 70 SuccessState success) { | 74 SuccessState success) { |
| 71 DCHECK(working_); | 75 DCHECK(working_); |
| 72 | 76 |
| 73 auto post_delete = base::Bind( | 77 auto post_delete = base::Bind( |
| 74 [](URLDownloader* _this, const GURL& url, const std::string& title, | 78 [](URLDownloader* _this, const GURL& url, const std::string& title, |
| 75 SuccessState success) { | 79 SuccessState success) { |
| 76 _this->download_completion_.Run( | 80 _this->download_completion_.Run( |
| 77 url, success, reading_list::OfflinePagePath(url), title); | 81 url, success, reading_list::OfflinePagePath(url), title); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 232 } |
| 229 | 233 |
| 230 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { | 234 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { |
| 231 if (html.empty()) { | 235 if (html.empty()) { |
| 232 return false; | 236 return false; |
| 233 } | 237 } |
| 234 base::FilePath path = | 238 base::FilePath path = |
| 235 reading_list::OfflinePageAbsolutePath(base_directory_, url); | 239 reading_list::OfflinePageAbsolutePath(base_directory_, url); |
| 236 return base::WriteFile(path, html.c_str(), html.length()) > 0; | 240 return base::WriteFile(path, html.c_str(), html.length()) > 0; |
| 237 } | 241 } |
| OLD | NEW |