Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / android / offline_pages / offline_page_mhtml_archiver.h
blobb00d391da07347f83fc138cdceb1ce0835547474
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 {
25 class TestMHTMLArchiver;
27 // Class implementing an offline page archiver using MHTML as an archive format.
29 // To generate an MHTML archiver for a given URL, a WebContents instance should
30 // have that URL loaded.
32 // Example:
33 // void SavePageOffline(content::WebContents* web_contents) {
34 // const GURL& url = web_contents->GetLastCommittedURL();
35 // scoped_ptr<OfflinePageMHTMLArchiver> archiver(
36 // new OfflinePageMHTMLArchiver(
37 // web_contents, archiver_dir, task_runner));
38 // // Callback is of type OfflinePageModel::SavePageCallback.
39 // model->SavePage(url, archiver.Pass(), callback);
40 // }
41 class OfflinePageMHTMLArchiver : public OfflinePageArchiver {
42 public:
43 OfflinePageMHTMLArchiver(
44 content::WebContents* web_contents,
45 const base::FilePath& file_path,
46 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
47 ~OfflinePageMHTMLArchiver() override;
49 // OfflinePageArchiver implementation:
50 void CreateArchive(const CreateArchiveCallback& callback) override;
52 private:
53 friend class offline_pages::TestMHTMLArchiver;
55 // Allows to overload the archiver for testing.
56 OfflinePageMHTMLArchiver(
57 const base::FilePath& file_path,
58 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
60 // Sends the result of archiving a page to the client that requested archive
61 // creation.
62 void ReportResult(ArchiverResult result,
63 const GURL& url,
64 const base::string16& title,
65 int64 file_size);
66 void ReportFailure(ArchiverResult result);
68 // Checks that |web_contents_| is still valid.
69 virtual bool IsWebContentsValid() const;
70 // Actual call to generate MHTML.
71 virtual void GenerateMHTML();
72 // Callback for Generating MHTML.
73 void OnGenerateMHTMLDone(const GURL& url,
74 const base::string16& title,
75 int64 file_size);
77 // Path to the archive file.
78 const base::FilePath file_path_;
79 // Contents of the web page to be serialized. Not owned.
80 // TODO(fgorski): Add WebContentsObserver to know when the page navigates away
81 // or shuts down.
82 content::WebContents* web_contents_;
84 CreateArchiveCallback callback_;
86 // Task runner, which will be used to post the callback.
87 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
89 base::WeakPtrFactory<OfflinePageMHTMLArchiver> weak_ptr_factory_;
91 DISALLOW_COPY_AND_ASSIGN(OfflinePageMHTMLArchiver);
94 } // namespace offline_pages
96 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MHTML_ARCHIVER_H_