[email protected] | f081575 | 2011-02-01 22:23:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 10 | #include "base/string16.h" |
[email protected] | 299425b | 2011-03-02 07:45:20 | [diff] [blame] | 11 | #include "content/browser/tab_contents/tab_contents_observer.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 12 | #include "content/public/browser/notification_observer.h" |
| 13 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 14 | #include "printing/printed_pages_source.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
[email protected] | 34d38be | 2011-06-09 20:44:58 | [diff] [blame] | 16 | class TabContentsWrapper; |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 17 | struct PrintHostMsg_DidPrintPage_Params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame^] | 19 | namespace content { |
| 20 | class RenderProcessHost; |
| 21 | } |
| 22 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | namespace printing { |
| 24 | |
| 25 | class JobEventDetails; |
| 26 | class PrintJob; |
| 27 | class PrintJobWorkerOwner; |
[email protected] | 0b481bf | 2011-06-29 20:33:00 | [diff] [blame] | 28 | class PrintViewManagerObserver; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 30 | // Manages the print commands in relation to a TabContents. TabContents |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | // delegates a few printing related commands to this instance. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 32 | class PrintViewManager : public content::NotificationObserver, |
[email protected] | 8d3347f | 2009-07-09 22:00:21 | [diff] [blame] | 33 | public PrintedPagesSource, |
[email protected] | 585b3036 | 2011-01-28 02:30:17 | [diff] [blame] | 34 | public TabContentsObserver { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | public: |
[email protected] | 34d38be | 2011-06-09 20:44:58 | [diff] [blame] | 36 | explicit PrintViewManager(TabContentsWrapper* tab); |
[email protected] | 2d4537d5 | 2008-12-17 02:25:44 | [diff] [blame] | 37 | virtual ~PrintViewManager(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | |
[email protected] | 49fe226 | 2011-04-15 20:56:15 | [diff] [blame] | 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 | |
[email protected] | 3121c76b | 2011-07-22 02:34:32 | [diff] [blame] | 44 | // Same as PrintNow(), but for the case where a user prints with the system |
| 45 | // dialog from print preview. |
| 46 | bool PrintForSystemDialogNow(); |
| 47 | |
[email protected] | 0996e9b | 2011-08-26 17:59:01 | [diff] [blame] | 48 | // Same as PrintNow(), but for the case where a user press "ctrl+shift+p" to |
| 49 | // show the native system dialog. This can happen from both initiator tab and |
| 50 | // preview tab. |
| 51 | bool AdvancedPrintNow(); |
| 52 | |
[email protected] | 8980e0d | 2011-05-27 19:16:23 | [diff] [blame] | 53 | // Initiate print preview of the current document by first notifying the |
| 54 | // renderer. Since this happens asynchronous, the print preview tab creation |
| 55 | // will not be completed on the return of this function. Returns false if |
| 56 | // print preview is impossible at the moment. |
| 57 | bool PrintPreviewNow(); |
| 58 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame^] | 59 | // Notify PrintViewManager that print preview has finished. Unfreeze the |
| 60 | // renderer in the case of scripted print preview. |
| 61 | void PrintPreviewDone(); |
| 62 | |
[email protected] | 428f0239 | 2011-06-11 01:30:55 | [diff] [blame] | 63 | // Handles cancelled preview printing request. |
| 64 | void PreviewPrintingRequestCancelled(); |
| 65 | |
[email protected] | 0b481bf | 2011-06-29 20:33:00 | [diff] [blame] | 66 | // Sets |observer| as the current PrintViewManagerObserver. Pass in NULL to |
| 67 | // remove the current observer. |observer| may always be NULL, but |observer_| |
| 68 | // must be NULL if |observer| is non-NULL. |
| 69 | void set_observer(PrintViewManagerObserver* observer); |
| 70 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | // PrintedPagesSource implementation. |
[email protected] | 184a6c8f | 2011-10-04 20:07:30 | [diff] [blame] | 72 | virtual string16 RenderSourceName() OVERRIDE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 74 | // content::NotificationObserver implementation. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 75 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 76 | const content::NotificationSource& source, |
| 77 | const content::NotificationDetails& details) OVERRIDE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | |
[email protected] | 585b3036 | 2011-01-28 02:30:17 | [diff] [blame] | 79 | // TabContentsObserver implementation. |
[email protected] | 184a6c8f | 2011-10-04 20:07:30 | [diff] [blame] | 80 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
[email protected] | 34f128d | 2011-01-25 19:07:44 | [diff] [blame] | 81 | |
[email protected] | 2e3f457 | 2011-03-25 19:24:47 | [diff] [blame] | 82 | // Terminates or cancels the print job if one was pending. |
[email protected] | 9cddb1a2 | 2011-11-15 15:04:27 | [diff] [blame] | 83 | virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE; |
[email protected] | 2e3f457 | 2011-03-25 19:24:47 | [diff] [blame] | 84 | |
| 85 | // Cancels the print job. |
[email protected] | 9cddb1a2 | 2011-11-15 15:04:27 | [diff] [blame] | 86 | virtual void StopNavigation() OVERRIDE; |
[email protected] | 2e3f457 | 2011-03-25 19:24:47 | [diff] [blame] | 87 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | private: |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame^] | 89 | enum PrintPreviewState { |
| 90 | NOT_PREVIEWING, |
| 91 | USER_INITIATED_PREVIEW, |
| 92 | SCRIPTED_PREVIEW, |
| 93 | }; |
| 94 | |
[email protected] | b3ee0b52 | 2011-05-06 04:06:40 | [diff] [blame] | 95 | // IPC Message handlers. |
[email protected] | 34f128d | 2011-01-25 19:07:44 | [diff] [blame] | 96 | void OnDidGetPrintedPagesCount(int cookie, int number_pages); |
[email protected] | da08956 | 2011-07-01 01:54:46 | [diff] [blame] | 97 | void OnDidGetDocumentCookie(int cookie); |
[email protected] | 0b481bf | 2011-06-29 20:33:00 | [diff] [blame] | 98 | void OnDidShowPrintDialog(); |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 99 | void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params); |
[email protected] | b3ee0b52 | 2011-05-06 04:06:40 | [diff] [blame] | 100 | void OnPrintingFailed(int cookie); |
[email protected] | 34f128d | 2011-01-25 19:07:44 | [diff] [blame] | 101 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame^] | 102 | void OnScriptedPrintPreview(IPC::Message* reply_msg); |
| 103 | void OnScriptedPrintPreviewReply(IPC::Message* reply_msg); |
| 104 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | // Processes a NOTIFY_PRINT_JOB_EVENT notification. |
| 106 | void OnNotifyPrintJobEvent(const JobEventDetails& event_details); |
| 107 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | // Requests the RenderView to render all the missing pages for the print job. |
[email protected] | 184a6c8f | 2011-10-04 20:07:30 | [diff] [blame] | 109 | // No-op if no print job is pending. Returns true if at least one page has |
| 110 | // been requested to the renderer. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | bool RenderAllMissingPagesNow(); |
| 112 | |
| 113 | // Quits the current message loop if these conditions hold true: a document is |
| 114 | // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This |
| 115 | // function is called in DidPrintPage() or on ALL_PAGES_REQUESTED |
| 116 | // notification. The inner message loop is created was created by |
| 117 | // RenderAllMissingPagesNow(). |
| 118 | void ShouldQuitFromInnerMessageLoop(); |
| 119 | |
| 120 | // Creates a new empty print job. It has no settings loaded. If there is |
| 121 | // currently a print job, safely disconnect from it. Returns false if it is |
| 122 | // impossible to safely disconnect from the current print job or it is |
| 123 | // impossible to create a new print job. |
| 124 | bool CreateNewPrintJob(PrintJobWorkerOwner* job); |
| 125 | |
| 126 | // Makes sure the current print_job_ has all its data before continuing, and |
| 127 | // disconnect from it. |
| 128 | void DisconnectFromCurrentPrintJob(); |
| 129 | |
[email protected] | 8227045 | 2009-06-19 15:58:01 | [diff] [blame] | 130 | // Notify that the printing is done. |
| 131 | void PrintingDone(bool success); |
| 132 | |
[email protected] | da08956 | 2011-07-01 01:54:46 | [diff] [blame] | 133 | // Terminates the print job. No-op if no print job has been created. If |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | // |cancel| is true, cancel it instead of waiting for the job to finish. Will |
| 135 | // call ReleasePrintJob(). |
| 136 | void TerminatePrintJob(bool cancel); |
| 137 | |
[email protected] | da08956 | 2011-07-01 01:54:46 | [diff] [blame] | 138 | // Releases print_job_. Correctly deregisters from notifications. No-op if |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | // no print job has been created. |
| 140 | void ReleasePrintJob(); |
| 141 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | // Runs an inner message loop. It will set inside_inner_message_loop_ to true |
| 143 | // while the blocking inner message loop is running. This is useful in cases |
| 144 | // where the RenderView is about to be destroyed while a printing job isn't |
| 145 | // finished. |
| 146 | bool RunInnerMessageLoop(); |
| 147 | |
| 148 | // In the case of Scripted Printing, where the renderer is controlling the |
| 149 | // control flow, print_job_ is initialized whenever possible. No-op is |
| 150 | // print_job_ is initialized. |
| 151 | bool OpportunisticallyCreatePrintJob(int cookie); |
| 152 | |
[email protected] | 3121c76b | 2011-07-22 02:34:32 | [diff] [blame] | 153 | // Helper method for Print*Now(). |
| 154 | bool PrintNowInternal(IPC::Message* message); |
| 155 | |
[email protected] | 34d38be | 2011-06-09 20:44:58 | [diff] [blame] | 156 | // TabContentsWrapper we're associated with. |
| 157 | TabContentsWrapper* tab_; |
| 158 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 159 | content::NotificationRegistrar registrar_; |
[email protected] | 0f08bf3 | 2009-05-21 20:33:17 | [diff] [blame] | 160 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | // Manages the low-level talk to the printer. |
| 162 | scoped_refptr<PrintJob> print_job_; |
| 163 | |
[email protected] | f081575 | 2011-02-01 22:23:44 | [diff] [blame] | 164 | // Number of pages to print in the print job. |
| 165 | int number_pages_; |
| 166 | |
[email protected] | 8227045 | 2009-06-19 15:58:01 | [diff] [blame] | 167 | // Indication of success of the print job. |
| 168 | bool printing_succeeded_; |
| 169 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | // Running an inner message loop inside RenderAllMissingPagesNow(). This means |
| 171 | // we are _blocking_ until all the necessary pages have been rendered or the |
| 172 | // print settings are being loaded. |
| 173 | bool inside_inner_message_loop_; |
| 174 | |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 175 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 176 | // Set to true when OnDidPrintPage() should be expecting the first page. |
| 177 | bool expecting_first_page_; |
| 178 | #endif |
| 179 | |
[email protected] | 0b481bf | 2011-06-29 20:33:00 | [diff] [blame] | 180 | // Weak pointer to an observer that is notified when the print dialog is |
| 181 | // shown. |
| 182 | PrintViewManagerObserver* observer_; |
| 183 | |
[email protected] | da08956 | 2011-07-01 01:54:46 | [diff] [blame] | 184 | // The document cookie of the current PrinterQuery. |
| 185 | int cookie_; |
| 186 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame^] | 187 | // Current state of print preview for this view. |
| 188 | PrintPreviewState print_preview_state_; |
| 189 | |
| 190 | // Keeps track of the pending callback during scripted print preview. |
| 191 | content::RenderProcessHost* scripted_print_preview_rph_; |
| 192 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 193 | DISALLOW_COPY_AND_ASSIGN(PrintViewManager); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | } // namespace printing |
| 197 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 198 | #endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |