Home | History | Annotate | Download | only in printing
      1 // Copyright (c) 2011 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_H_
      6 #define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_
      7 #pragma once
      8 
      9 #include "base/memory/ref_counted.h"
     10 #include "base/string16.h"
     11 #include "content/browser/tab_contents/tab_contents_observer.h"
     12 #include "content/common/notification_observer.h"
     13 #include "content/common/notification_registrar.h"
     14 #include "printing/printed_pages_source.h"
     15 
     16 class RenderViewHost;
     17 class TabContents;
     18 struct PrintHostMsg_DidPrintPage_Params;
     19 
     20 namespace printing {
     21 
     22 class JobEventDetails;
     23 class PrintJob;
     24 class PrintJobWorkerOwner;
     25 
     26 // Manages the print commands in relation to a TabContents. TabContents
     27 // delegates a few printing related commands to this instance.
     28 class PrintViewManager : public NotificationObserver,
     29                          public PrintedPagesSource,
     30                          public TabContentsObserver {
     31  public:
     32   explicit PrintViewManager(TabContents* tab_contents);
     33   virtual ~PrintViewManager();
     34 
     35   // Override the title for this PrintViewManager's PrintJobs using the title
     36   // in |tab_contents|.
     37   void OverrideTitle(TabContents* tab_contents);
     38 
     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   bool PrintNow();
     43 
     44   // PrintedPagesSource implementation.
     45   virtual string16 RenderSourceName();
     46   virtual GURL RenderSourceUrl();
     47 
     48   // NotificationObserver implementation.
     49   virtual void Observe(NotificationType type,
     50                        const NotificationSource& source,
     51                        const NotificationDetails& details);
     52 
     53   // TabContentsObserver implementation.
     54   virtual bool OnMessageReceived(const IPC::Message& message);
     55 
     56   // Terminates or cancels the print job if one was pending.
     57   virtual void RenderViewGone();
     58 
     59   // Cancels the print job.
     60   virtual void StopNavigation();
     61 
     62  private:
     63   void OnDidGetPrintedPagesCount(int cookie, int number_pages);
     64   void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params);
     65 
     66   // Processes a NOTIFY_PRINT_JOB_EVENT notification.
     67   void OnNotifyPrintJobEvent(const JobEventDetails& event_details);
     68 
     69   // Requests the RenderView to render all the missing pages for the print job.
     70   // Noop if no print job is pending. Returns true if at least one page has been
     71   // requested to the renderer.
     72   bool RenderAllMissingPagesNow();
     73 
     74   // Quits the current message loop if these conditions hold true: a document is
     75   // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This
     76   // function is called in DidPrintPage() or on ALL_PAGES_REQUESTED
     77   // notification. The inner message loop is created was created by
     78   // RenderAllMissingPagesNow().
     79   void ShouldQuitFromInnerMessageLoop();
     80 
     81   // Creates a new empty print job. It has no settings loaded. If there is
     82   // currently a print job, safely disconnect from it. Returns false if it is
     83   // impossible to safely disconnect from the current print job or it is
     84   // impossible to create a new print job.
     85   bool CreateNewPrintJob(PrintJobWorkerOwner* job);
     86 
     87   // Makes sure the current print_job_ has all its data before continuing, and
     88   // disconnect from it.
     89   void DisconnectFromCurrentPrintJob();
     90 
     91   // Notify that the printing is done.
     92   void PrintingDone(bool success);
     93 
     94   // Terminates the print job. Noop if no print job has been created. If
     95   // |cancel| is true, cancel it instead of waiting for the job to finish. Will
     96   // call ReleasePrintJob().
     97   void TerminatePrintJob(bool cancel);
     98 
     99   // Releases print_job_. Correctly deregisters from notifications. Noop if
    100   // no print job has been created.
    101   void ReleasePrintJob();
    102 
    103   // Runs an inner message loop. It will set inside_inner_message_loop_ to true
    104   // while the blocking inner message loop is running. This is useful in cases
    105   // where the RenderView is about to be destroyed while a printing job isn't
    106   // finished.
    107   bool RunInnerMessageLoop();
    108 
    109   // In the case of Scripted Printing, where the renderer is controlling the
    110   // control flow, print_job_ is initialized whenever possible. No-op is
    111   // print_job_ is initialized.
    112   bool OpportunisticallyCreatePrintJob(int cookie);
    113 
    114   NotificationRegistrar registrar_;
    115 
    116   // Manages the low-level talk to the printer.
    117   scoped_refptr<PrintJob> print_job_;
    118 
    119   // Number of pages to print in the print job.
    120   int number_pages_;
    121 
    122   // Waiting for print_job_ initialization to be completed to start printing.
    123   // Specifically the DEFAULT_INIT_DONE notification. Set when PrintNow() is
    124   // called.
    125   bool waiting_to_print_;
    126 
    127   // Indication of success of the print job.
    128   bool printing_succeeded_;
    129 
    130   // Running an inner message loop inside RenderAllMissingPagesNow(). This means
    131   // we are _blocking_ until all the necessary pages have been rendered or the
    132   // print settings are being loaded.
    133   bool inside_inner_message_loop_;
    134 
    135 #if defined(OS_POSIX) && !defined(OS_MACOSX)
    136   // Set to true when OnDidPrintPage() should be expecting the first page.
    137   bool expecting_first_page_;
    138 #endif
    139 
    140   // Title override.
    141   bool is_title_overridden_;
    142   string16 overridden_title_;
    143 
    144   DISALLOW_COPY_AND_ASSIGN(PrintViewManager);
    145 };
    146 
    147 }  // namespace printing
    148 
    149 #endif  // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_
    150