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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 const GURL& url, | 97 const GURL& url, |
98 const std::string& title, | 98 const std::string& title, |
99 const base::FilePath& offline_path, | 99 const base::FilePath& offline_path, |
100 SuccessState success) { | 100 SuccessState success) { |
101 DCHECK(working_); | 101 DCHECK(working_); |
102 | 102 |
103 auto post_delete = base::Bind( | 103 auto post_delete = base::Bind( |
104 [](URLDownloader* _this, const GURL& url, const std::string& title, | 104 [](URLDownloader* _this, const GURL& url, const std::string& title, |
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, _this->saved_size_, |
| 108 title); |
108 _this->distiller_.reset(); | 109 _this->distiller_.reset(); |
109 _this->working_ = false; | 110 _this->working_ = false; |
110 _this->HandleNextTask(); | 111 _this->HandleNextTask(); |
111 }, | 112 }, |
112 base::Unretained(this), url, title, offline_path, success); | 113 base::Unretained(this), url, title, offline_path, success); |
113 | 114 |
114 // If downloading failed, clean up any partial download. | 115 // If downloading failed, clean up any partial download. |
115 if (success == ERROR) { | 116 if (success == ERROR) { |
116 base::FilePath directory_path = | 117 base::FilePath directory_path = |
117 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); | 118 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 163 |
163 void URLDownloader::DownloadURL(const GURL& url, bool offline_url_exists) { | 164 void URLDownloader::DownloadURL(const GURL& url, bool offline_url_exists) { |
164 if (offline_url_exists) { | 165 if (offline_url_exists) { |
165 DownloadCompletionHandler(url, std::string(), base::FilePath(), | 166 DownloadCompletionHandler(url, std::string(), base::FilePath(), |
166 DOWNLOAD_EXISTS); | 167 DOWNLOAD_EXISTS); |
167 return; | 168 return; |
168 } | 169 } |
169 | 170 |
170 original_url_ = url; | 171 original_url_ = url; |
171 distilled_url_ = url; | 172 distilled_url_ = url; |
| 173 saved_size_ = 0; |
172 std::unique_ptr<reading_list::ReadingListDistillerPage> | 174 std::unique_ptr<reading_list::ReadingListDistillerPage> |
173 reading_list_distiller_page = | 175 reading_list_distiller_page = |
174 distiller_page_factory_->CreateReadingListDistillerPage(this); | 176 distiller_page_factory_->CreateReadingListDistillerPage(this); |
175 | 177 |
176 distiller_.reset(new dom_distiller::DistillerViewer( | 178 distiller_.reset(new dom_distiller::DistillerViewer( |
177 distiller_factory_, std::move(reading_list_distiller_page), pref_service_, | 179 distiller_factory_, std::move(reading_list_distiller_page), pref_service_, |
178 url, | 180 url, |
179 base::Bind(&URLDownloader::DistillerCallback, base::Unretained(this)))); | 181 base::Bind(&URLDownloader::DistillerCallback, base::Unretained(this)))); |
180 } | 182 } |
181 | 183 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 URLDownloader::SuccessState URLDownloader::SavePDFFile( | 233 URLDownloader::SuccessState URLDownloader::SavePDFFile( |
232 const base::FilePath& temporary_path) { | 234 const base::FilePath& temporary_path) { |
233 if (CreateOfflineURLDirectory(original_url_)) { | 235 if (CreateOfflineURLDirectory(original_url_)) { |
234 base::FilePath path = reading_list::OfflinePagePath( | 236 base::FilePath path = reading_list::OfflinePagePath( |
235 original_url_, reading_list::OFFLINE_TYPE_PDF); | 237 original_url_, reading_list::OFFLINE_TYPE_PDF); |
236 base::FilePath absolute_path = | 238 base::FilePath absolute_path = |
237 reading_list::OfflineURLAbsolutePathFromRelativePath(base_directory_, | 239 reading_list::OfflineURLAbsolutePathFromRelativePath(base_directory_, |
238 path); | 240 path); |
239 | 241 |
240 if (base::Move(temporary_path, absolute_path)) { | 242 if (base::Move(temporary_path, absolute_path)) { |
| 243 int64_t pdf_file_size; |
| 244 base::GetFileSize(absolute_path, &pdf_file_size); |
| 245 saved_size_ += pdf_file_size; |
241 return DOWNLOAD_SUCCESS; | 246 return DOWNLOAD_SUCCESS; |
242 } else { | 247 } else { |
243 return ERROR; | 248 return ERROR; |
244 } | 249 } |
245 } | 250 } |
246 | 251 |
247 return ERROR; | 252 return ERROR; |
248 } | 253 } |
249 | 254 |
250 void URLDownloader::DistillerCallback( | 255 void URLDownloader::DistillerCallback( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 bool URLDownloader::SaveImage(const GURL& url, | 309 bool URLDownloader::SaveImage(const GURL& url, |
305 const GURL& image_url, | 310 const GURL& image_url, |
306 const std::string& data, | 311 const std::string& data, |
307 std::string* image_name) { | 312 std::string* image_name) { |
308 std::string image_hash = base::MD5String(image_url.spec()); | 313 std::string image_hash = base::MD5String(image_url.spec()); |
309 *image_name = image_hash; | 314 *image_name = image_hash; |
310 base::FilePath directory_path = | 315 base::FilePath directory_path = |
311 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); | 316 reading_list::OfflineURLDirectoryAbsolutePath(base_directory_, url); |
312 base::FilePath path = directory_path.Append(image_hash); | 317 base::FilePath path = directory_path.Append(image_hash); |
313 if (!base::PathExists(path)) { | 318 if (!base::PathExists(path)) { |
314 return base::WriteFile(path, data.c_str(), data.length()) > 0; | 319 int written = base::WriteFile(path, data.c_str(), data.length()); |
| 320 if (written <= 0) { |
| 321 return false; |
| 322 } |
| 323 saved_size_ += written; |
| 324 return true; |
315 } | 325 } |
316 return true; | 326 return true; |
317 } | 327 } |
318 | 328 |
319 std::string URLDownloader::SaveAndReplaceImagesInHTML( | 329 std::string URLDownloader::SaveAndReplaceImagesInHTML( |
320 const GURL& url, | 330 const GURL& url, |
321 const std::string& html, | 331 const std::string& html, |
322 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& | 332 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& |
323 images) { | 333 images) { |
324 std::string mutable_html = html; | 334 std::string mutable_html = html; |
(...skipping 30 matching lines...) Expand all Loading... |
355 return mutable_html; | 365 return mutable_html; |
356 } | 366 } |
357 | 367 |
358 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { | 368 bool URLDownloader::SaveHTMLForURL(std::string html, const GURL& url) { |
359 if (html.empty()) { | 369 if (html.empty()) { |
360 return false; | 370 return false; |
361 } | 371 } |
362 base::FilePath path = reading_list::OfflineURLAbsolutePathFromRelativePath( | 372 base::FilePath path = reading_list::OfflineURLAbsolutePathFromRelativePath( |
363 base_directory_, | 373 base_directory_, |
364 reading_list::OfflinePagePath(url, reading_list::OFFLINE_TYPE_HTML)); | 374 reading_list::OfflinePagePath(url, reading_list::OFFLINE_TYPE_HTML)); |
365 return base::WriteFile(path, html.c_str(), html.length()) > 0; | 375 int written = base::WriteFile(path, html.c_str(), html.length()); |
| 376 if (written <= 0) { |
| 377 return false; |
| 378 } |
| 379 saved_size_ += written; |
| 380 return true; |
366 } | 381 } |
OLD | NEW |