| 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" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "components/reading_list/ios/offline_url_utils.h" | 14 #include "components/reading_list/ios/offline_url_utils.h" |
| 15 #include "ios/chrome/browser/chrome_paths.h" | 15 #include "ios/chrome/browser/chrome_paths.h" |
| 16 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h" | 16 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h" |
| 17 #include "ios/chrome/browser/reading_list/reading_list_distiller_page.h" |
| 17 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h" | 18 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h" |
| 18 #include "ios/web/public/web_thread.h" | 19 #include "ios/web/public/web_thread.h" |
| 19 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 // This script disables context menu on img elements. | 24 // This script disables context menu on img elements. |
| 24 // The pages are stored locally and long pressing on them will trigger a context | 25 // The pages are stored locally and long pressing on them will trigger a context |
| 25 // menu on the file:// URL which cannot be opened. Disable the context menu. | 26 // menu on the file:// URL which cannot be opened. Disable the context menu. |
| 26 const char kDisableImageContextMenuScript[] = | 27 const char kDisableImageContextMenuScript[] = |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 88 } |
| 88 | 89 |
| 89 void URLDownloader::DownloadCompletionHandler(const GURL& url, | 90 void URLDownloader::DownloadCompletionHandler(const GURL& url, |
| 90 const std::string& title, | 91 const std::string& title, |
| 91 SuccessState success) { | 92 SuccessState success) { |
| 92 DCHECK(working_); | 93 DCHECK(working_); |
| 93 | 94 |
| 94 auto post_delete = base::Bind( | 95 auto post_delete = base::Bind( |
| 95 [](URLDownloader* _this, const GURL& url, const std::string& title, | 96 [](URLDownloader* _this, const GURL& url, const std::string& title, |
| 96 SuccessState success) { | 97 SuccessState success) { |
| 97 _this->download_completion_.Run( | 98 _this->download_completion_.Run(url, _this->distilled_url_, success, |
| 98 url, success, reading_list::OfflinePagePath(url), title); | 99 reading_list::OfflinePagePath(url), |
| 100 title); |
| 99 _this->distiller_.reset(); | 101 _this->distiller_.reset(); |
| 100 _this->working_ = false; | 102 _this->working_ = false; |
| 101 _this->HandleNextTask(); | 103 _this->HandleNextTask(); |
| 102 }, | 104 }, |
| 103 base::Unretained(this), url, title, success); | 105 base::Unretained(this), url, title, success); |
| 104 | 106 |
| 105 // If downloading failed, clean up any partial download. | 107 // If downloading failed, clean up any partial download. |
| 106 if (success == ERROR_RETRY || success == ERROR_PERMANENT) { | 108 if (success == ERROR_RETRY || success == ERROR_PERMANENT) { |
| 107 task_tracker_.PostTaskAndReply( | 109 task_tracker_.PostTaskAndReply( |
| 108 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), | 110 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 base::Unretained(this), url)); | 152 base::Unretained(this), url)); |
| 151 } | 153 } |
| 152 } | 154 } |
| 153 | 155 |
| 154 void URLDownloader::DownloadURL(GURL url, bool offline_url_exists) { | 156 void URLDownloader::DownloadURL(GURL url, bool offline_url_exists) { |
| 155 if (offline_url_exists) { | 157 if (offline_url_exists) { |
| 156 DownloadCompletionHandler(url, std::string(), DOWNLOAD_EXISTS); | 158 DownloadCompletionHandler(url, std::string(), DOWNLOAD_EXISTS); |
| 157 return; | 159 return; |
| 158 } | 160 } |
| 159 | 161 |
| 162 original_url_ = url; |
| 163 distilled_url_ = url; |
| 164 std::unique_ptr<reading_list::ReadingListDistillerPage> |
| 165 reading_list_distiller_page = |
| 166 distiller_page_factory_->CreateReadingListDistillerPage(); |
| 167 reading_list_distiller_page->SetRedirectionCallback( |
| 168 base::Bind(&URLDownloader::RedirectionCallback, base::Unretained(this))); |
| 169 |
| 160 distiller_.reset(new dom_distiller::DistillerViewer( | 170 distiller_.reset(new dom_distiller::DistillerViewer( |
| 161 distiller_service_, pref_service_, url, | 171 distiller_service_, pref_service_, url, |
| 162 base::Bind(&URLDownloader::DistillerCallback, base::Unretained(this)), | 172 base::Bind(&URLDownloader::DistillerCallback, base::Unretained(this)), |
| 163 distiller_page_factory_)); | 173 std::move(reading_list_distiller_page))); |
| 174 } |
| 175 |
| 176 void URLDownloader::RedirectionCallback(const GURL& page_url, |
| 177 const GURL& redirected_url) { |
| 178 DCHECK(original_url_ == page_url); |
| 179 distilled_url_ = redirected_url; |
| 164 } | 180 } |
| 165 | 181 |
| 166 void URLDownloader::DistillerCallback( | 182 void URLDownloader::DistillerCallback( |
| 167 const GURL& page_url, | 183 const GURL& page_url, |
| 168 const std::string& html, | 184 const std::string& html, |
| 169 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& | 185 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& |
| 170 images, | 186 images, |
| 171 const std::string& title) { | 187 const std::string& title) { |
| 172 if (html.empty()) { | 188 if (html.empty()) { |
| 173 DownloadCompletionHandler(page_url, std::string(), ERROR_RETRY); | 189 DownloadCompletionHandler(page_url, std::string(), ERROR_RETRY); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } | 272 } |
| 257 | 273 |
| 258 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { | 274 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { |
| 259 if (html.empty()) { | 275 if (html.empty()) { |
| 260 return false; | 276 return false; |
| 261 } | 277 } |
| 262 base::FilePath path = | 278 base::FilePath path = |
| 263 reading_list::OfflinePageAbsolutePath(base_directory_, url); | 279 reading_list::OfflinePageAbsolutePath(base_directory_, url); |
| 264 return base::WriteFile(path, html.c_str(), html.length()) > 0; | 280 return base::WriteFile(path, html.c_str(), html.length()) > 0; |
| 265 } | 281 } |
| OLD | NEW |