1 // Copyright (c) 2012 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 CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_
6 #define CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_
10 #include "base/memory/singleton.h"
11 #include "base/platform_file.h"
12 #include "base/process/process.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "ipc/ipc_platform_file.h"
24 class MHTMLGenerationManager : public NotificationObserver {
26 static MHTMLGenerationManager* GetInstance();
28 typedef base::Callback<void(int64 /* size of the file */)>
29 GenerateMHTMLCallback;
31 // Instructs the render view to generate a MHTML representation of the current
32 // page for |web_contents|.
33 void SaveMHTML(WebContents* web_contents,
34 const base::FilePath& file,
35 const GenerateMHTMLCallback& callback);
37 // Instructs the render view to generate a MHTML representation of the current
38 // page for |web_contents|.
39 void StreamMHTML(WebContents* web_contents,
40 const base::PlatformFile file,
41 const GenerateMHTMLCallback& callback);
43 // Notification from the renderer that the MHTML generation finished.
44 // |mhtml_data_size| contains the size in bytes of the generated MHTML data,
45 // or -1 in case of failure.
46 void MHTMLGenerated(int job_id, int64 mhtml_data_size);
49 friend struct DefaultSingletonTraits<MHTMLGenerationManager>;
55 // The handles to file the MHTML is saved to, for the browser and renderer
57 base::PlatformFile browser_file;
58 IPC::PlatformFileForTransit renderer_file;
60 // The IDs mapping to a specific contents.
64 // The callback to call once generation is complete.
65 GenerateMHTMLCallback callback;
68 MHTMLGenerationManager();
69 virtual ~MHTMLGenerationManager();
71 // Called on the file thread to create |file|.
72 void CreateFile(int job_id,
73 const base::FilePath& file,
74 base::ProcessHandle renderer_process);
76 // Called on the UI thread when the file that should hold the MHTML data has
77 // been created. This returns a handle to that file for the browser process
78 // and one for the renderer process. These handles are
79 // kInvalidPlatformFileValue if the file could not be opened.
80 void FileHandleAvailable(int job_id,
81 base::PlatformFile browser_file,
82 IPC::PlatformFileForTransit renderer_file);
84 // Called on the file thread to close the file the MHTML was saved to.
85 void CloseFile(base::PlatformFile file);
87 // Called on the UI thread when a job has been processed (successfully or
88 // not). Closes the file and removes the job from the job map.
89 // |mhtml_data_size| is -1 if the MHTML generation failed.
90 void JobFinished(int job_id, int64 mhtml_data_size);
92 // Creates an register a new job.
93 int NewJob(WebContents* web_contents, const GenerateMHTMLCallback& callback);
95 // Implementation of NotificationObserver.
96 virtual void Observe(int type,
97 const NotificationSource& source,
98 const NotificationDetails& details) OVERRIDE;
100 typedef std::map<int, Job> IDToJobMap;
101 IDToJobMap id_to_job_;
102 NotificationRegistrar registrar_;
104 DISALLOW_COPY_AND_ASSIGN(MHTMLGenerationManager);
107 } // namespace content
109 #endif // CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_