| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/offline_pages/offline_page_mhtml_archiver.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 | |
| 15 namespace offline_pages { | |
| 16 | |
| 17 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver( | |
| 18 content::WebContents* web_contents, | |
| 19 const base::FilePath& file_path, | |
| 20 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | |
| 21 : file_path_(file_path), | |
| 22 web_contents_(web_contents), | |
| 23 task_runner_(task_runner), | |
| 24 weak_ptr_factory_(this) { | |
| 25 DCHECK(web_contents_); | |
| 26 } | |
| 27 | |
| 28 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver( | |
| 29 const base::FilePath& file_path, | |
| 30 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | |
| 31 : file_path_(file_path), | |
| 32 web_contents_(nullptr), | |
| 33 task_runner_(task_runner), | |
| 34 weak_ptr_factory_(this) { | |
| 35 } | |
| 36 | |
| 37 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() { | |
| 38 } | |
| 39 | |
| 40 void OfflinePageMHTMLArchiver::CreateArchive( | |
| 41 const CreateArchiveCallback& callback) { | |
| 42 DCHECK(callback_.is_null()); | |
| 43 DCHECK(!callback.is_null()); | |
| 44 callback_ = callback; | |
| 45 | |
| 46 if (!IsWebContentsValid()) { | |
| 47 DVLOG(1) << "WebContents is invalid. Can't create archive."; | |
| 48 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 GenerateMHTML(); | |
| 53 } | |
| 54 | |
| 55 bool OfflinePageMHTMLArchiver::IsWebContentsValid() const { | |
| 56 // TODO(fgorski): Make sure that web_contents is valid (use WCObserver). | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 void OfflinePageMHTMLArchiver::GenerateMHTML() { | |
| 61 if (!web_contents_) { | |
| 62 DVLOG(1) << "WebContents is missing. Can't create archive."; | |
| 63 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | |
| 64 return; | |
| 65 } | |
| 66 // TODO(fgorski): Figure out if the actual URL or title can be different at | |
| 67 // the end of MHTML generation. Perhaps we should pull it out after the MHTML | |
| 68 // is generated. | |
| 69 web_contents_->GenerateMHTML( | |
| 70 file_path_, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone, | |
| 71 weak_ptr_factory_.GetWeakPtr(), | |
| 72 web_contents_->GetLastCommittedURL(), | |
| 73 web_contents_->GetTitle())); | |
| 74 } | |
| 75 | |
| 76 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone(const GURL& url, | |
| 77 const base::string16& title, | |
| 78 int64 file_size) { | |
| 79 ArchiverResult result = | |
| 80 file_size < 0 ? ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED : | |
| 81 ArchiverResult::SUCCESSFULLY_CREATED; | |
| 82 ReportResult(result, url, title, file_size); | |
| 83 } | |
| 84 | |
| 85 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result) { | |
| 86 DCHECK(result != ArchiverResult::SUCCESSFULLY_CREATED); | |
| 87 ReportResult(result, GURL(), base::string16(), 0); | |
| 88 } | |
| 89 | |
| 90 void OfflinePageMHTMLArchiver::ReportResult(ArchiverResult result, | |
| 91 const GURL& url, | |
| 92 const base::string16& title, | |
| 93 int64 file_size) { | |
| 94 base::FilePath file_path; | |
| 95 if (result == ArchiverResult::SUCCESSFULLY_CREATED) { | |
| 96 // Pass an actual file path and report file size only upon success. | |
| 97 file_path = file_path_; | |
| 98 } else { | |
| 99 // Make sure both file path and file size are empty on failure. | |
| 100 file_size = 0; | |
| 101 } | |
| 102 task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 103 callback_, this, result, url, title, file_path, file_size)); | |
| 104 } | |
| 105 | |
| 106 } // namespace offline_pages | |
| OLD | NEW |