| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "base/process/process.h" | 12 #include "base/process/process.h" |
| 13 #include "content/public/browser/notification_observer.h" | 13 #include "content/public/browser/notification_observer.h" |
| 14 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
| 15 #include "ipc/ipc_platform_file.h" | 15 #include "ipc/ipc_platform_file.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class FilePath; | 18 class FilePath; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 class WebContents; | 22 class WebContents; |
| 23 | 23 |
| 24 class MHTMLGenerationManager : public NotificationObserver { | 24 class MHTMLGenerationManager : public NotificationObserver { |
| 25 public: | 25 public: |
| 26 static MHTMLGenerationManager* GetInstance(); | 26 static MHTMLGenerationManager* GetInstance(); |
| 27 | 27 |
| 28 typedef base::Callback<void(const base::FilePath& /* path to the MHTML file */
, | 28 typedef base::Callback<void(int64 /* size of the file */)> |
| 29 int64 /* size of the file */)> GenerateMHTMLCallback; | 29 GenerateMHTMLCallback; |
| 30 | 30 |
| 31 // Instructs the render view to generate a MHTML representation of the current | 31 // Instructs the render view to generate a MHTML representation of the current |
| 32 // page for |web_contents|. | 32 // page for |web_contents|. |
| 33 void GenerateMHTML(WebContents* web_contents, | 33 void SaveMHTML(WebContents* web_contents, |
| 34 const base::FilePath& file, | 34 const base::FilePath& file, |
| 35 const GenerateMHTMLCallback& callback); | 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); |
| 36 | 42 |
| 37 // Notification from the renderer that the MHTML generation finished. | 43 // Notification from the renderer that the MHTML generation finished. |
| 38 // |mhtml_data_size| contains the size in bytes of the generated MHTML data, | 44 // |mhtml_data_size| contains the size in bytes of the generated MHTML data, |
| 39 // or -1 in case of failure. | 45 // or -1 in case of failure. |
| 40 void MHTMLGenerated(int job_id, int64 mhtml_data_size); | 46 void MHTMLGenerated(int job_id, int64 mhtml_data_size); |
| 41 | 47 |
| 42 private: | 48 private: |
| 43 friend struct DefaultSingletonTraits<MHTMLGenerationManager>; | 49 friend struct DefaultSingletonTraits<MHTMLGenerationManager>; |
| 44 | 50 |
| 45 struct Job{ | 51 struct Job{ |
| 46 Job(); | 52 Job(); |
| 47 ~Job(); | 53 ~Job(); |
| 48 | 54 |
| 49 base::FilePath file_path; | |
| 50 | |
| 51 // The handles to file the MHTML is saved to, for the browser and renderer | 55 // The handles to file the MHTML is saved to, for the browser and renderer |
| 52 // processes. | 56 // processes. |
| 53 base::PlatformFile browser_file; | 57 base::PlatformFile browser_file; |
| 54 IPC::PlatformFileForTransit renderer_file; | 58 IPC::PlatformFileForTransit renderer_file; |
| 55 | 59 |
| 56 // The IDs mapping to a specific contents. | 60 // The IDs mapping to a specific contents. |
| 57 int process_id; | 61 int process_id; |
| 58 int routing_id; | 62 int routing_id; |
| 59 | 63 |
| 60 // The callback to call once generation is complete. | 64 // The callback to call once generation is complete. |
| 61 GenerateMHTMLCallback callback; | 65 GenerateMHTMLCallback callback; |
| 62 }; | 66 }; |
| 63 | 67 |
| 64 MHTMLGenerationManager(); | 68 MHTMLGenerationManager(); |
| 65 virtual ~MHTMLGenerationManager(); | 69 virtual ~MHTMLGenerationManager(); |
| 66 | 70 |
| 67 // Called on the file thread to create |file|. | 71 // Called on the file thread to create |file|. |
| 68 void CreateFile(int job_id, | 72 void CreateFile(int job_id, |
| 69 const base::FilePath& file, | 73 const base::FilePath& file, |
| 70 base::ProcessHandle renderer_process); | 74 base::ProcessHandle renderer_process); |
| 71 | 75 |
| 72 // Called on the UI thread when the file that should hold the MHTML data has | 76 // Called on the UI thread when the file that should hold the MHTML data has |
| 73 // been created. This returns a handle to that file for the browser process | 77 // been created. This returns a handle to that file for the browser process |
| 74 // and one for the renderer process. These handles are | 78 // and one for the renderer process. These handles are |
| 75 // kInvalidPlatformFileValue if the file could not be opened. | 79 // kInvalidPlatformFileValue if the file could not be opened. |
| 76 void FileCreated(int job_id, | 80 void FileHandleAvailable(int job_id, |
| 77 base::PlatformFile browser_file, | 81 base::PlatformFile browser_file, |
| 78 IPC::PlatformFileForTransit renderer_file); | 82 IPC::PlatformFileForTransit renderer_file); |
| 79 | 83 |
| 80 // Called on the file thread to close the file the MHTML was saved to. | 84 // Called on the file thread to close the file the MHTML was saved to. |
| 81 void CloseFile(base::PlatformFile file); | 85 void CloseFile(base::PlatformFile file); |
| 82 | 86 |
| 83 // Called on the UI thread when a job has been processed (successfully or | 87 // Called on the UI thread when a job has been processed (successfully or |
| 84 // not). Closes the file and removes the job from the job map. | 88 // not). Closes the file and removes the job from the job map. |
| 85 // |mhtml_data_size| is -1 if the MHTML generation failed. | 89 // |mhtml_data_size| is -1 if the MHTML generation failed. |
| 86 void JobFinished(int job_id, int64 mhtml_data_size); | 90 void JobFinished(int job_id, int64 mhtml_data_size); |
| 87 | 91 |
| 92 // Creates an register a new job. |
| 93 int NewJob(WebContents* web_contents, const GenerateMHTMLCallback& callback); |
| 94 |
| 88 // Implementation of NotificationObserver. | 95 // Implementation of NotificationObserver. |
| 89 virtual void Observe(int type, | 96 virtual void Observe(int type, |
| 90 const NotificationSource& source, | 97 const NotificationSource& source, |
| 91 const NotificationDetails& details) OVERRIDE; | 98 const NotificationDetails& details) OVERRIDE; |
| 92 | 99 |
| 93 typedef std::map<int, Job> IDToJobMap; | 100 typedef std::map<int, Job> IDToJobMap; |
| 94 IDToJobMap id_to_job_; | 101 IDToJobMap id_to_job_; |
| 95 NotificationRegistrar registrar_; | 102 NotificationRegistrar registrar_; |
| 96 | 103 |
| 97 DISALLOW_COPY_AND_ASSIGN(MHTMLGenerationManager); | 104 DISALLOW_COPY_AND_ASSIGN(MHTMLGenerationManager); |
| 98 }; | 105 }; |
| 99 | 106 |
| 100 } // namespace content | 107 } // namespace content |
| 101 | 108 |
| 102 #endif // CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_ | 109 #endif // CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_ |
| OLD | NEW |