Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / offline_pages / offline_page_mhtml_archiver.h
blob245622ba3d6db1da11067b6ddec573b62a14a16b
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_
8 #include <map>
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"
15 namespace base {
16 class FilePath;
17 class SingleThreadTaskRunner;
18 } // namespace base
20 namespace content {
21 class WebContents;
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.
31 // Example:
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);
39 // }
40 class OfflinePageMHTMLArchiver : public OfflinePageArchiver {
41 public:
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;
52 protected:
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,
67 int64 file_size);
69 // Sends the result of archiving a page to the client that requested archive
70 // creation.
71 void ReportResult(ArchiverResult result,
72 const GURL& url,
73 const base::FilePath& file_path,
74 int64 file_size);
75 void ReportFailure(ArchiverResult result);
77 private:
78 // Path to the archive directory. It the path is empty, creation of the
79 // archive will fail.
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
83 // or shuts down.
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_