Home | History | Annotate | Download | only in download
      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/files/file.h"
     11 #include "base/memory/singleton.h"
     12 #include "base/process/process.h"
     13 #include "ipc/ipc_platform_file.h"
     14 
     15 namespace base {
     16 class FilePath;
     17 }
     18 
     19 namespace content {
     20 
     21 class WebContents;
     22 
     23 class MHTMLGenerationManager {
     24  public:
     25   static MHTMLGenerationManager* GetInstance();
     26 
     27   typedef base::Callback<void(int64 /* size of the file */)>
     28       GenerateMHTMLCallback;
     29 
     30   // Instructs the render view to generate a MHTML representation of the current
     31   // page for |web_contents|.
     32   void SaveMHTML(WebContents* web_contents,
     33                  const base::FilePath& file,
     34                  const GenerateMHTMLCallback& callback);
     35 
     36   // Instructs the render view to generate a MHTML representation of the current
     37   // page for |web_contents|.
     38   void StreamMHTML(WebContents* web_contents,
     39                    base::File file,
     40                    const GenerateMHTMLCallback& callback);
     41 
     42   // Notification from the renderer that the MHTML generation finished.
     43   // |mhtml_data_size| contains the size in bytes of the generated MHTML data,
     44   // or -1 in case of failure.
     45   void MHTMLGenerated(int job_id, int64 mhtml_data_size);
     46 
     47  private:
     48   friend struct DefaultSingletonTraits<MHTMLGenerationManager>;
     49   class Job;
     50 
     51   MHTMLGenerationManager();
     52   virtual ~MHTMLGenerationManager();
     53 
     54   // Called on the file thread to create |file|.
     55   void CreateFile(int job_id,
     56                   const base::FilePath& file,
     57                   base::ProcessHandle renderer_process);
     58 
     59   // Called on the UI thread when the file that should hold the MHTML data has
     60   // been created.  This receives a handle to that file for the browser process
     61   // and one for the renderer process.
     62   void FileAvailable(int job_id,
     63                      base::File browser_file,
     64                      IPC::PlatformFileForTransit renderer_file);
     65 
     66   // Called on the file thread to close the file the MHTML was saved to.
     67   void CloseFile(base::File file);
     68 
     69   // Called on the UI thread when a job has been processed (successfully or
     70   // not).  Closes the file and removes the job from the job map.
     71   // |mhtml_data_size| is -1 if the MHTML generation failed.
     72   void JobFinished(int job_id, int64 mhtml_data_size);
     73 
     74   // Creates an register a new job.
     75   int NewJob(WebContents* web_contents, const GenerateMHTMLCallback& callback);
     76 
     77   // Called when the render process connected to a job exits.
     78   void RenderProcessExited(Job* job);
     79 
     80   typedef std::map<int, Job*> IDToJobMap;
     81   IDToJobMap id_to_job_;
     82 
     83   DISALLOW_COPY_AND_ASSIGN(MHTMLGenerationManager);
     84 };
     85 
     86 }  // namespace content
     87 
     88 #endif  // CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_
     89