- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / download / mhtml_generation_manager.h
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.
4
5 #ifndef CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_
6 #define CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_
7
8 #include <map>
9
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"
16
17 namespace base {
18 class FilePath;
19 }
20
21 namespace content {
22 class WebContents;
23
24 class MHTMLGenerationManager : public NotificationObserver {
25  public:
26   static MHTMLGenerationManager* GetInstance();
27
28   typedef base::Callback<void(int64 /* size of the file */)>
29       GenerateMHTMLCallback;
30
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);
36
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);
42
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);
47
48  private:
49   friend struct DefaultSingletonTraits<MHTMLGenerationManager>;
50
51   struct Job{
52     Job();
53     ~Job();
54
55     // The handles to file the MHTML is saved to, for the browser and renderer
56     // processes.
57     base::PlatformFile browser_file;
58     IPC::PlatformFileForTransit renderer_file;
59
60     // The IDs mapping to a specific contents.
61     int process_id;
62     int routing_id;
63
64     // The callback to call once generation is complete.
65     GenerateMHTMLCallback callback;
66   };
67
68   MHTMLGenerationManager();
69   virtual ~MHTMLGenerationManager();
70
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);
75
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);
83
84   // Called on the file thread to close the file the MHTML was saved to.
85   void CloseFile(base::PlatformFile file);
86
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);
91
92   // Creates an register a new job.
93   int NewJob(WebContents* web_contents, const GenerateMHTMLCallback& callback);
94
95   // Implementation of NotificationObserver.
96   virtual void Observe(int type,
97                        const NotificationSource& source,
98                        const NotificationDetails& details) OVERRIDE;
99
100   typedef std::map<int, Job> IDToJobMap;
101   IDToJobMap id_to_job_;
102   NotificationRegistrar registrar_;
103
104   DISALLOW_COPY_AND_ASSIGN(MHTMLGenerationManager);
105 };
106
107 }  // namespace content
108
109 #endif  // CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_