Home | History | Annotate | Download | only in printing
      1 // Copyright 2013 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 CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_
      6 #define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/prefs/pref_member.h"
     10 #include "base/strings/string16.h"
     11 #include "content/public/browser/notification_observer.h"
     12 #include "content/public/browser/notification_registrar.h"
     13 #include "content/public/browser/web_contents_observer.h"
     14 #include "content/public/browser/web_contents_user_data.h"
     15 #include "printing/printed_pages_source.h"
     16 
     17 struct PrintHostMsg_DidPrintPage_Params;
     18 
     19 namespace content {
     20 class RenderViewHost;
     21 }
     22 
     23 namespace printing {
     24 
     25 class JobEventDetails;
     26 class MetafilePlayer;
     27 class PrintJob;
     28 class PrintJobWorkerOwner;
     29 class PrintQueriesQueue;
     30 
     31 // Base class for managing the print commands for a WebContents.
     32 class PrintViewManagerBase : public content::NotificationObserver,
     33                              public PrintedPagesSource,
     34                              public content::WebContentsObserver {
     35  public:
     36   virtual ~PrintViewManagerBase();
     37 
     38 #if !defined(DISABLE_BASIC_PRINTING)
     39   // Prints the current document immediately. Since the rendering is
     40   // asynchronous, the actual printing will not be completed on the return of
     41   // this function. Returns false if printing is impossible at the moment.
     42   virtual bool PrintNow();
     43 #endif  // !DISABLE_BASIC_PRINTING
     44 
     45   // Whether to block scripted printing for our tab or not.
     46   void UpdateScriptedPrintingBlocked();
     47 
     48   // PrintedPagesSource implementation.
     49   virtual base::string16 RenderSourceName() OVERRIDE;
     50 
     51  protected:
     52   explicit PrintViewManagerBase(content::WebContents* web_contents);
     53 
     54   // Helper method for Print*Now().
     55   bool PrintNowInternal(IPC::Message* message);
     56 
     57   // Terminates or cancels the print job if one was pending.
     58   virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
     59 
     60   // content::WebContentsObserver implementation.
     61   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     62 
     63   // IPC Message handlers.
     64   virtual void OnPrintingFailed(int cookie);
     65 
     66  private:
     67   // content::NotificationObserver implementation.
     68   virtual void Observe(int type,
     69                        const content::NotificationSource& source,
     70                        const content::NotificationDetails& details) OVERRIDE;
     71 
     72   // content::WebContentsObserver implementation.
     73   virtual void DidStartLoading(
     74       content::RenderViewHost* render_view_host) OVERRIDE;
     75 
     76   // Cancels the print job.
     77   virtual void NavigationStopped() OVERRIDE;
     78 
     79   // IPC Message handlers.
     80   void OnDidGetPrintedPagesCount(int cookie, int number_pages);
     81   void OnDidGetDocumentCookie(int cookie);
     82   void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params);
     83   void OnShowInvalidPrinterSettingsError();
     84 
     85   // Processes a NOTIFY_PRINT_JOB_EVENT notification.
     86   void OnNotifyPrintJobEvent(const JobEventDetails& event_details);
     87 
     88   // Requests the RenderView to render all the missing pages for the print job.
     89   // No-op if no print job is pending. Returns true if at least one page has
     90   // been requested to the renderer.
     91   bool RenderAllMissingPagesNow();
     92 
     93   // Quits the current message loop if these conditions hold true: a document is
     94   // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This
     95   // function is called in DidPrintPage() or on ALL_PAGES_REQUESTED
     96   // notification. The inner message loop is created was created by
     97   // RenderAllMissingPagesNow().
     98   void ShouldQuitFromInnerMessageLoop();
     99 
    100   // Creates a new empty print job. It has no settings loaded. If there is
    101   // currently a print job, safely disconnect from it. Returns false if it is
    102   // impossible to safely disconnect from the current print job or it is
    103   // impossible to create a new print job.
    104   bool CreateNewPrintJob(PrintJobWorkerOwner* job);
    105 
    106   // Makes sure the current print_job_ has all its data before continuing, and
    107   // disconnect from it.
    108   void DisconnectFromCurrentPrintJob();
    109 
    110   // Notify that the printing is done.
    111   void PrintingDone(bool success);
    112 
    113   // Terminates the print job. No-op if no print job has been created. If
    114   // |cancel| is true, cancel it instead of waiting for the job to finish. Will
    115   // call ReleasePrintJob().
    116   void TerminatePrintJob(bool cancel);
    117 
    118   // Releases print_job_. Correctly deregisters from notifications. No-op if
    119   // no print job has been created.
    120   void ReleasePrintJob();
    121 
    122   // Runs an inner message loop. It will set inside_inner_message_loop_ to true
    123   // while the blocking inner message loop is running. This is useful in cases
    124   // where the RenderView is about to be destroyed while a printing job isn't
    125   // finished.
    126   bool RunInnerMessageLoop();
    127 
    128   // In the case of Scripted Printing, where the renderer is controlling the
    129   // control flow, print_job_ is initialized whenever possible. No-op is
    130   // print_job_ is initialized.
    131   bool OpportunisticallyCreatePrintJob(int cookie);
    132 
    133   // Release the PrinterQuery associated with our |cookie_|.
    134   void ReleasePrinterQuery();
    135 
    136   content::NotificationRegistrar registrar_;
    137 
    138   // Manages the low-level talk to the printer.
    139   scoped_refptr<PrintJob> print_job_;
    140 
    141   // Number of pages to print in the print job.
    142   int number_pages_;
    143 
    144   // Indication of success of the print job.
    145   bool printing_succeeded_;
    146 
    147   // Running an inner message loop inside RenderAllMissingPagesNow(). This means
    148   // we are _blocking_ until all the necessary pages have been rendered or the
    149   // print settings are being loaded.
    150   bool inside_inner_message_loop_;
    151 
    152 #if !defined(OS_MACOSX)
    153   // Set to true when OnDidPrintPage() should be expecting the first page.
    154   bool expecting_first_page_;
    155 #endif  // OS_MACOSX
    156 
    157   // The document cookie of the current PrinterQuery.
    158   int cookie_;
    159 
    160   // Whether printing is enabled.
    161   BooleanPrefMember printing_enabled_;
    162 
    163   scoped_refptr<printing::PrintQueriesQueue> queue_;
    164 
    165   DISALLOW_COPY_AND_ASSIGN(PrintViewManagerBase);
    166 };
    167 
    168 }  // namespace printing
    169 
    170 #endif  // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_
    171