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 OfflinePageMHTMLArchiver(content::WebContents
* web_contents
,
43 const base::FilePath
& archive_dir
);
44 ~OfflinePageMHTMLArchiver() override
;
46 // OfflinePageArchiver implementation:
47 void CreateArchive(const CreateArchiveCallback
& callback
) override
;
50 // Allows to overload the archiver for testing.
51 explicit OfflinePageMHTMLArchiver(const base::FilePath
& archive_dir
);
53 // Try to generate MHTML.
54 // Might be overridden for testing purpose.
55 virtual void GenerateMHTML();
57 // Actual call to generate MHTML.
58 // Might be overridden for testing purpose.
59 virtual void DoGenerateMHTML();
61 // Callback for Generating MHTML.
62 void OnGenerateMHTMLDone(const GURL
& url
,
63 const base::FilePath
& file_path
,
66 // Sends the result of archiving a page to the client that requested archive
68 void ReportResult(ArchiverResult result
,
70 const base::FilePath
& file_path
,
72 void ReportFailure(ArchiverResult result
);
75 // Path to the archive directory. It the path is empty, creation of the
77 const base::FilePath archive_dir_
;
78 // Contents of the web page to be serialized. Not owned.
79 // TODO(fgorski): Add WebContentsObserver to know when the page navigates away
81 content::WebContents
* web_contents_
;
83 CreateArchiveCallback callback_
;
85 base::WeakPtrFactory
<OfflinePageMHTMLArchiver
> weak_ptr_factory_
;
87 DISALLOW_COPY_AND_ASSIGN(OfflinePageMHTMLArchiver
);
90 } // namespace offline_pages
92 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MHTML_ARCHIVER_H_