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