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