| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 const base::FilePath& offline_path, SuccessState success) { | 105 const base::FilePath& offline_path, SuccessState success) { |
| 106 _this->download_completion_.Run(url, _this->distilled_url_, success, | 106 _this->download_completion_.Run(url, _this->distilled_url_, success, |
| 107 offline_path, title); | 107 offline_path, title); |
| 108 _this->distiller_.reset(); | 108 _this->distiller_.reset(); |
| 109 _this->working_ = false; | 109 _this->working_ = false; |
| 110 _this->HandleNextTask(); | 110 _this->HandleNextTask(); |
| 111 }, | 111 }, |
| 112 base::Unretained(this), url, title, offline_path, success); | 112 base::Unretained(this), url, title, offline_path, success); |
| 113 | 113 |
| 114 // If downloading failed, clean up any partial download. | 114 // If downloading failed, clean up any partial download. |
| 115 if (success == ERROR_RETRY || success == ERROR_PERMANENT) { | 115 if (success == ERROR) { |
| 116 base::FilePath directory_path = | 116 base::FilePath directory_path = |
| 117 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); | 117 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); |
| 118 task_tracker_.PostTaskAndReply( | 118 task_tracker_.PostTaskAndReply( |
| 119 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), | 119 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), |
| 120 FROM_HERE, base::Bind( | 120 FROM_HERE, base::Bind( |
| 121 [](const base::FilePath& offline_directory_path) { | 121 [](const base::FilePath& offline_directory_path) { |
| 122 base::DeleteFile(offline_directory_path, true); | 122 base::DeleteFile(offline_directory_path, true); |
| 123 }, | 123 }, |
| 124 directory_path), | 124 directory_path), |
| 125 post_delete); | 125 post_delete); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 DCHECK(source == fetcher_.get()); | 195 DCHECK(source == fetcher_.get()); |
| 196 // At the moment, only pdf files are downloaded using URLFetcher. | 196 // At the moment, only pdf files are downloaded using URLFetcher. |
| 197 DCHECK(mime_type_ == "application/pdf"); | 197 DCHECK(mime_type_ == "application/pdf"); |
| 198 base::FilePath path = reading_list::OfflinePagePath( | 198 base::FilePath path = reading_list::OfflinePagePath( |
| 199 original_url_, reading_list::OFFLINE_TYPE_PDF); | 199 original_url_, reading_list::OFFLINE_TYPE_PDF); |
| 200 std::string mime_type; | 200 std::string mime_type; |
| 201 if (fetcher_->GetResponseHeaders()) { | 201 if (fetcher_->GetResponseHeaders()) { |
| 202 fetcher_->GetResponseHeaders()->GetMimeType(&mime_type); | 202 fetcher_->GetResponseHeaders()->GetMimeType(&mime_type); |
| 203 } | 203 } |
| 204 if (!fetcher_->GetStatus().is_success() || mime_type != mime_type_) { | 204 if (!fetcher_->GetStatus().is_success() || mime_type != mime_type_) { |
| 205 return DownloadCompletionHandler(original_url_, "", path, ERROR_RETRY); | 205 return DownloadCompletionHandler(original_url_, "", path, ERROR); |
| 206 } | 206 } |
| 207 base::FilePath temporary_path; | 207 base::FilePath temporary_path; |
| 208 // Do not take ownership of the file until the file is moved. This ensures | 208 // Do not take ownership of the file until the file is moved. This ensures |
| 209 // that the file is cleaned if there a problem before file is moved. | 209 // that the file is cleaned if there a problem before file is moved. |
| 210 fetcher_->GetResponseAsFilePath(false, &temporary_path); | 210 fetcher_->GetResponseAsFilePath(false, &temporary_path); |
| 211 | 211 |
| 212 task_tracker_.PostTaskAndReplyWithResult( | 212 task_tracker_.PostTaskAndReplyWithResult( |
| 213 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), | 213 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), |
| 214 FROM_HERE, base::Bind(&URLDownloader::SavePDFFile, base::Unretained(this), | 214 FROM_HERE, base::Bind(&URLDownloader::SavePDFFile, base::Unretained(this), |
| 215 temporary_path), | 215 temporary_path), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 233 if (CreateOfflineURLDirectory(original_url_)) { | 233 if (CreateOfflineURLDirectory(original_url_)) { |
| 234 base::FilePath path = reading_list::OfflinePagePath( | 234 base::FilePath path = reading_list::OfflinePagePath( |
| 235 original_url_, reading_list::OFFLINE_TYPE_PDF); | 235 original_url_, reading_list::OFFLINE_TYPE_PDF); |
| 236 base::FilePath absolute_path = | 236 base::FilePath absolute_path = |
| 237 reading_list::OfflineURLAbsolutePathFromRelativePath(base_directory_, | 237 reading_list::OfflineURLAbsolutePathFromRelativePath(base_directory_, |
| 238 path); | 238 path); |
| 239 | 239 |
| 240 if (base::Move(temporary_path, absolute_path)) { | 240 if (base::Move(temporary_path, absolute_path)) { |
| 241 return DOWNLOAD_SUCCESS; | 241 return DOWNLOAD_SUCCESS; |
| 242 } else { | 242 } else { |
| 243 return ERROR_PERMANENT; | 243 return ERROR; |
| 244 } | 244 } |
| 245 } | 245 } |
| 246 | 246 |
| 247 return ERROR_PERMANENT; | 247 return ERROR; |
| 248 } | 248 } |
| 249 | 249 |
| 250 void URLDownloader::DistillerCallback( | 250 void URLDownloader::DistillerCallback( |
| 251 const GURL& page_url, | 251 const GURL& page_url, |
| 252 const std::string& html, | 252 const std::string& html, |
| 253 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& | 253 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& |
| 254 images, | 254 images, |
| 255 const std::string& title) { | 255 const std::string& title) { |
| 256 if (html.empty()) { | 256 if (html.empty()) { |
| 257 // The page may not be HTML. Check the mime-type to see if another handler | 257 // The page may not be HTML. Check the mime-type to see if another handler |
| 258 // can save offline content. | 258 // can save offline content. |
| 259 if (mime_type_ == "application/pdf") { | 259 if (mime_type_ == "application/pdf") { |
| 260 // PDF handler just downloads the PDF file. | 260 // PDF handler just downloads the PDF file. |
| 261 FetchPDFFile(); | 261 FetchPDFFile(); |
| 262 return; | 262 return; |
| 263 } | 263 } |
| 264 // This content cannot be processed, return an error value to the client. | 264 // This content cannot be processed, return an error value to the client. |
| 265 DownloadCompletionHandler(page_url, std::string(), base::FilePath(), | 265 DownloadCompletionHandler(page_url, std::string(), base::FilePath(), ERROR); |
| 266 ERROR_RETRY); | |
| 267 return; | 266 return; |
| 268 } | 267 } |
| 269 | 268 |
| 270 std::vector<dom_distiller::DistillerViewer::ImageInfo> images_block = images; | 269 std::vector<dom_distiller::DistillerViewer::ImageInfo> images_block = images; |
| 271 std::string block_html = html; | 270 std::string block_html = html; |
| 272 task_tracker_.PostTaskAndReplyWithResult( | 271 task_tracker_.PostTaskAndReplyWithResult( |
| 273 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), | 272 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE).get(), |
| 274 FROM_HERE, | 273 FROM_HERE, |
| 275 base::Bind(&URLDownloader::SaveDistilledHTML, base::Unretained(this), | 274 base::Bind(&URLDownloader::SaveDistilledHTML, base::Unretained(this), |
| 276 page_url, images_block, block_html), | 275 page_url, images_block, block_html), |
| 277 base::Bind(&URLDownloader::DownloadCompletionHandler, | 276 base::Bind(&URLDownloader::DownloadCompletionHandler, |
| 278 base::Unretained(this), page_url, title, | 277 base::Unretained(this), page_url, title, |
| 279 reading_list::OfflinePagePath( | 278 reading_list::OfflinePagePath( |
| 280 page_url, reading_list::OFFLINE_TYPE_HTML))); | 279 page_url, reading_list::OFFLINE_TYPE_HTML))); |
| 281 } | 280 } |
| 282 | 281 |
| 283 URLDownloader::SuccessState URLDownloader::SaveDistilledHTML( | 282 URLDownloader::SuccessState URLDownloader::SaveDistilledHTML( |
| 284 const GURL& url, | 283 const GURL& url, |
| 285 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& | 284 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& |
| 286 images, | 285 images, |
| 287 const std::string& html) { | 286 const std::string& html) { |
| 288 if (CreateOfflineURLDirectory(url)) { | 287 if (CreateOfflineURLDirectory(url)) { |
| 289 return SaveHTMLForURL(SaveAndReplaceImagesInHTML(url, html, images), url) | 288 return SaveHTMLForURL(SaveAndReplaceImagesInHTML(url, html, images), url) |
| 290 ? DOWNLOAD_SUCCESS | 289 ? DOWNLOAD_SUCCESS |
| 291 : ERROR_PERMANENT; | 290 : ERROR; |
| 292 } | 291 } |
| 293 return ERROR_PERMANENT; | 292 return ERROR; |
| 294 } | 293 } |
| 295 | 294 |
| 296 bool URLDownloader::CreateOfflineURLDirectory(const GURL& url) { | 295 bool URLDownloader::CreateOfflineURLDirectory(const GURL& url) { |
| 297 base::FilePath directory_path = | 296 base::FilePath directory_path = |
| 298 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); | 297 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); |
| 299 if (!DirectoryExists(directory_path)) { | 298 if (!DirectoryExists(directory_path)) { |
| 300 return CreateDirectoryAndGetError(directory_path, nil); | 299 return CreateDirectoryAndGetError(directory_path, nil); |
| 301 } | 300 } |
| 302 return true; | 301 return true; |
| 303 } | 302 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 | 357 |
| 359 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { | 358 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { |
| 360 if (html.empty()) { | 359 if (html.empty()) { |
| 361 return false; | 360 return false; |
| 362 } | 361 } |
| 363 base::FilePath path = reading_list::OfflineURLAbsolutePathFromRelativePath( | 362 base::FilePath path = reading_list::OfflineURLAbsolutePathFromRelativePath( |
| 364 base_directory_, | 363 base_directory_, |
| 365 reading_list::OfflinePagePath(url, reading_list::OFFLINE_TYPE_HTML)); | 364 reading_list::OfflinePagePath(url, reading_list::OFFLINE_TYPE_HTML)); |
| 366 return base::WriteFile(path, html.c_str(), html.length()) > 0; | 365 return base::WriteFile(path, html.c_str(), html.length()) > 0; |
| 367 } | 366 } |
| OLD | NEW |