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/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(const base::FilePath& /* path to the MHTML file */,
     29       int64 /* size of the file */)> GenerateMHTMLCallback;
     30 
     31   // Instructs the render view to generate a MHTML representation of the current
     32   // page for |web_contents|.
     33   void GenerateMHTML(WebContents* web_contents,
     34                      const base::FilePath& file,
     35                      const GenerateMHTMLCallback& callback);
     36 
     37   // Notification from the renderer that the MHTML generation finished.
     38   // |mhtml_data_size| contains the size in bytes of the generated MHTML data,
     39   // or -1 in case of failure.
     40   void MHTMLGenerated(int job_id, int64 mhtml_data_size);
     41 
     42  private:
     43   friend struct DefaultSingletonTraits<MHTMLGenerationManager>;
     44 
     45   struct Job{
     46     Job();
     47     ~Job();
     48 
     49     base::FilePath file_path;
     50 
     51     // The handles to file the MHTML is saved to, for the browser and renderer
     52     // processes.
     53     base::PlatformFile browser_file;
     54     IPC::PlatformFileForTransit renderer_file;
     55 
     56     // The IDs mapping to a specific contents.
     57     int process_id;
     58     int routing_id;
     59 
     60     // The callback to call once generation is complete.
     61     GenerateMHTMLCallback callback;
     62   };
     63 
     64   MHTMLGenerationManager();
     65   virtual ~MHTMLGenerationManager();
     66 
     67   // Called on the file thread to create |file|.
     68   void CreateFile(int job_id,
     69                   const base::FilePath& file,
     70                   base::ProcessHandle renderer_process);
     71 
     72   // 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
     74   // and one for the renderer process. These handles are
     75   // kInvalidPlatformFileValue if the file could not be opened.
     76   void FileCreated(int job_id,
     77                    base::PlatformFile browser_file,
     78                    IPC::PlatformFileForTransit renderer_file);
     79 
     80   // Called on the file thread to close the file the MHTML was saved to.
     81   void CloseFile(base::PlatformFile file);
     82 
     83   // 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.
     85   // |mhtml_data_size| is -1 if the MHTML generation failed.
     86   void JobFinished(int job_id, int64 mhtml_data_size);
     87 
     88   // Implementation of NotificationObserver.
     89   virtual void Observe(int type,
     90                        const NotificationSource& source,
     91                        const NotificationDetails& details) OVERRIDE;
     92 
     93   typedef std::map<int, Job> IDToJobMap;
     94   IDToJobMap id_to_job_;
     95   NotificationRegistrar registrar_;
     96 
     97   DISALLOW_COPY_AND_ASSIGN(MHTMLGenerationManager);
     98 };
     99 
    100 }  // namespace content
    101 
    102 #endif  // CONTENT_BROWSER_DOWNLOAD_MHTML_GENERATION_MANAGER_H_
    103