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 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MHTML_ARCHIVER_H_
6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MHTML_ARCHIVER_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/offline_pages/offline_page_archiver.h"
17 class SingleThreadTaskRunner
;
22 } // namespace content
24 namespace offline_pages
{
26 // Class implementing an offline page archiver using MHTML as an archive format.
28 // To generate an MHTML archiver for a given URL, a WebContents instance should
29 // have that URL loaded.
32 // void SavePageOffline(content::WebContents* web_contents) {
33 // const GURL& url = web_contents->GetLastCommittedURL();
34 // scoped_ptr<OfflinePageMHTMLArchiver> archiver(
35 // new OfflinePageMHTMLArchiver(
36 // web_contents, archive_dir));
37 // // Callback is of type OfflinePageModel::SavePageCallback.
38 // model->SavePage(url, archiver.Pass(), callback);
40 class OfflinePageMHTMLArchiver
: public OfflinePageArchiver
{
42 // Returns the extension name of the offline page file.
43 static std::string
GetFileNameExtension();
45 OfflinePageMHTMLArchiver(content::WebContents
* web_contents
,
46 const base::FilePath
& archive_dir
);
47 ~OfflinePageMHTMLArchiver() override
;
49 // OfflinePageArchiver implementation:
50 void CreateArchive(const CreateArchiveCallback
& callback
) override
;
53 // Allows to overload the archiver for testing.
54 explicit OfflinePageMHTMLArchiver(const base::FilePath
& archive_dir
);
56 // Try to generate MHTML.
57 // Might be overridden for testing purpose.
58 virtual void GenerateMHTML();
60 // Actual call to generate MHTML.
61 // Might be overridden for testing purpose.
62 virtual void DoGenerateMHTML();
64 // Callback for Generating MHTML.
65 void OnGenerateMHTMLDone(const GURL
& url
,
66 const base::FilePath
& file_path
,
69 // Sends the result of archiving a page to the client that requested archive
71 void ReportResult(ArchiverResult result
,
73 const base::FilePath
& file_path
,
75 void ReportFailure(ArchiverResult result
);
78 // Path to the archive directory. It the path is empty, creation of the
80 const base::FilePath archive_dir_
;
81 // Contents of the web page to be serialized. Not owned.
82 // TODO(fgorski): Add WebContentsObserver to know when the page navigates away
84 content::WebContents
* web_contents_
;
86 CreateArchiveCallback callback_
;
88 base::WeakPtrFactory
<OfflinePageMHTMLArchiver
> weak_ptr_factory_
;
90 DISALLOW_COPY_AND_ASSIGN(OfflinePageMHTMLArchiver
);
93 } // namespace offline_pages
95 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MHTML_ARCHIVER_H_