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.
5 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/browser/web_contents.h"
14 #include "net/base/filename_util.h"
16 namespace offline_pages
{
18 const base::FilePath::CharType kMHTMLExtension
[] = FILE_PATH_LITERAL("mhtml");
19 const base::FilePath::CharType kDefaultFileName
[] =
20 FILE_PATH_LITERAL("offline_page");
21 const char kMHTMLFileNameExtension
[] = ".mhtml";
23 base::FilePath
GenerateFileName(GURL url
, base::string16 title
) {
24 return net::GenerateFileName(url
,
25 std::string(), // content disposition
26 std::string(), // charset
27 base::UTF16ToUTF8(title
), // suggested name
28 std::string(), // mime-type
30 .AddExtension(kMHTMLExtension
);
36 std::string
OfflinePageMHTMLArchiver::GetFileNameExtension() {
37 return kMHTMLFileNameExtension
;
40 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver(
41 content::WebContents
* web_contents
,
42 const base::FilePath
& archive_dir
)
43 : archive_dir_(archive_dir
),
44 web_contents_(web_contents
),
45 weak_ptr_factory_(this) {
46 DCHECK(web_contents_
);
49 OfflinePageMHTMLArchiver::OfflinePageMHTMLArchiver(
50 const base::FilePath
& archive_dir
)
51 : archive_dir_(archive_dir
),
52 web_contents_(nullptr),
53 weak_ptr_factory_(this) {
56 OfflinePageMHTMLArchiver::~OfflinePageMHTMLArchiver() {
59 void OfflinePageMHTMLArchiver::CreateArchive(
60 const CreateArchiveCallback
& callback
) {
61 DCHECK(callback_
.is_null());
62 DCHECK(!callback
.is_null());
68 void OfflinePageMHTMLArchiver::GenerateMHTML() {
69 if (archive_dir_
.empty()) {
70 DVLOG(1) << "Archive path was empty. Can't create archive.";
71 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED
);
76 DVLOG(1) << "WebContents is missing. Can't create archive.";
77 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE
);
81 if (!web_contents_
->GetRenderViewHost()) {
82 DVLOG(1) << "RenderViewHost is not created yet. Can't create archive.";
83 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE
);
90 void OfflinePageMHTMLArchiver::DoGenerateMHTML() {
91 // TODO(fgorski): Figure out if the actual URL can be different at
92 // the end of MHTML generation. Perhaps we should pull it out after the MHTML
94 GURL
url(web_contents_
->GetLastCommittedURL());
95 base::string16
title(web_contents_
->GetTitle());
96 base::FilePath
file_path(archive_dir_
.Append(GenerateFileName(url
, title
)));
98 web_contents_
->GenerateMHTML(
99 file_path
, base::Bind(&OfflinePageMHTMLArchiver::OnGenerateMHTMLDone
,
100 weak_ptr_factory_
.GetWeakPtr(), url
, file_path
));
103 void OfflinePageMHTMLArchiver::OnGenerateMHTMLDone(
105 const base::FilePath
& file_path
,
108 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED
);
110 callback_
.Run(this, ArchiverResult::SUCCESSFULLY_CREATED
, url
, file_path
,
115 void OfflinePageMHTMLArchiver::ReportFailure(ArchiverResult result
) {
116 DCHECK(result
!= ArchiverResult::SUCCESSFULLY_CREATED
);
117 callback_
.Run(this, result
, GURL(), base::FilePath(), 0);
120 } // namespace offline_pages