OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |
6 #define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ | 6 #define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "chrome/browser/printing/print_view_manager_base.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" | 9 #include "content/public/browser/web_contents_user_data.h" |
15 #include "printing/printed_pages_source.h" | |
16 | |
17 struct PrintHostMsg_DidPrintPage_Params; | |
18 | 10 |
19 namespace content { | 11 namespace content { |
20 class RenderProcessHost; | 12 class RenderProcessHost; |
21 class RenderViewHost; | |
22 } | 13 } |
23 | 14 |
24 namespace printing { | 15 namespace printing { |
25 | 16 |
26 class JobEventDetails; | |
27 class PrintJob; | |
28 class PrintJobWorkerOwner; | |
29 class PrintViewManagerObserver; | 17 class PrintViewManagerObserver; |
30 | 18 |
31 // Manages the print commands for a WebContents. | 19 // Manages the print commands for a WebContents. |
32 class PrintViewManager : public content::NotificationObserver, | 20 class PrintViewManager : public PrintViewManagerBase, |
33 public PrintedPagesSource, | |
34 public content::WebContentsObserver, | |
35 public content::WebContentsUserData<PrintViewManager> { | 21 public content::WebContentsUserData<PrintViewManager> { |
36 public: | 22 public: |
37 virtual ~PrintViewManager(); | 23 virtual ~PrintViewManager(); |
38 | 24 |
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 // Same as PrintNow(), but for the case where a user prints with the system | 25 // Same as PrintNow(), but for the case where a user prints with the system |
45 // dialog from print preview. | 26 // dialog from print preview. |
46 bool PrintForSystemDialogNow(); | 27 bool PrintForSystemDialogNow(); |
47 | 28 |
48 // Same as PrintNow(), but for the case where a user press "ctrl+shift+p" to | 29 // 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 and | 30 // show the native system dialog. This can happen from both initiator and |
50 // preview dialog. | 31 // preview dialog. |
51 bool AdvancedPrintNow(); | 32 bool AdvancedPrintNow(); |
52 | 33 |
53 // Same as PrintNow(), but for the case where we want to send the result to | 34 // Same as PrintNow(), but for the case where we want to send the result to |
54 // another destination. | 35 // another destination. |
55 // TODO(mad) Add an argument so we can pass the destination interface. | 36 // TODO(mad) Add an argument so we can pass the destination interface. |
56 bool PrintToDestination(); | 37 bool PrintToDestination(); |
57 | 38 |
58 // Initiate print preview of the current document by first notifying the | 39 // Initiate print preview of the current document by first notifying the |
59 // renderer. Since this happens asynchronous, the print preview dialog | 40 // renderer. Since this happens asynchronous, the print preview dialog |
60 // creation will not be completed on the return of this function. Returns | 41 // creation will not be completed on the return of this function. Returns |
61 // false if print preview is impossible at the moment. | 42 // false if print preview is impossible at the moment. |
62 bool PrintPreviewNow(bool selection_only); | 43 bool PrintPreviewNow(bool selection_only); |
63 | 44 |
64 // Notify PrintViewManager that print preview is starting in the renderer for | 45 // Notify PrintViewManager that print preview is starting in the renderer for |
65 // a particular WebNode. | 46 // a particular WebNode. |
66 void PrintPreviewForWebNode(); | 47 void PrintPreviewForWebNode(); |
67 | 48 |
68 // Notify PrintViewManager that print preview has finished. Unfreeze the | 49 // Notify PrintViewManager that print preview has finished. Unfreeze the |
69 // renderer in the case of scripted print preview. | 50 // renderer in the case of scripted print preview. |
70 void PrintPreviewDone(); | 51 void PrintPreviewDone(); |
71 | 52 |
72 // Whether to block scripted printing for our tab or not. | |
73 void UpdateScriptedPrintingBlocked(); | |
74 | |
75 // Sets |observer| as the current PrintViewManagerObserver. Pass in NULL to | 53 // Sets |observer| as the current PrintViewManagerObserver. Pass in NULL to |
76 // remove the current observer. |observer| may always be NULL, but |observer_| | 54 // remove the current observer. |observer| may always be NULL, but |observer_| |
77 // must be NULL if |observer| is non-NULL. | 55 // must be NULL if |observer| is non-NULL. |
78 void set_observer(PrintViewManagerObserver* observer); | 56 void set_observer(PrintViewManagerObserver* observer); |
79 | 57 |
80 // PrintedPagesSource implementation. | |
81 virtual string16 RenderSourceName() OVERRIDE; | |
82 | |
83 // content::NotificationObserver implementation. | |
84 virtual void Observe(int type, | |
85 const content::NotificationSource& source, | |
86 const content::NotificationDetails& details) OVERRIDE; | |
87 | |
88 // content::WebContentsObserver implementation. | |
89 virtual void DidStartLoading( | |
90 content::RenderViewHost* render_view_host) OVERRIDE; | |
91 | |
92 // content::WebContentsObserver implementation. | 58 // content::WebContentsObserver implementation. |
93 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 59 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
94 | 60 |
| 61 // content::WebContentsObserver implementation. |
95 // Terminates or cancels the print job if one was pending. | 62 // Terminates or cancels the print job if one was pending. |
96 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | 63 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; |
97 | 64 |
98 // Cancels the print job. | |
99 virtual void StopNavigation() OVERRIDE; | |
100 | |
101 private: | 65 private: |
102 explicit PrintViewManager(content::WebContents* web_contents); | 66 explicit PrintViewManager(content::WebContents* web_contents); |
103 friend class content::WebContentsUserData<PrintViewManager>; | 67 friend class content::WebContentsUserData<PrintViewManager>; |
104 | 68 |
105 enum PrintPreviewState { | 69 enum PrintPreviewState { |
106 NOT_PREVIEWING, | 70 NOT_PREVIEWING, |
107 USER_INITIATED_PREVIEW, | 71 USER_INITIATED_PREVIEW, |
108 SCRIPTED_PREVIEW, | 72 SCRIPTED_PREVIEW, |
109 }; | 73 }; |
110 | 74 |
111 // IPC Message handlers. | 75 // IPC Message handlers. |
112 void OnDidGetPrintedPagesCount(int cookie, int number_pages); | |
113 void OnDidGetDocumentCookie(int cookie); | |
114 void OnDidShowPrintDialog(); | 76 void OnDidShowPrintDialog(); |
115 void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params); | |
116 void OnPrintingFailed(int cookie); | |
117 | |
118 void OnScriptedPrintPreview(bool source_is_modifiable, | 77 void OnScriptedPrintPreview(bool source_is_modifiable, |
119 IPC::Message* reply_msg); | 78 IPC::Message* reply_msg); |
120 void OnScriptedPrintPreviewReply(IPC::Message* reply_msg); | 79 void OnScriptedPrintPreviewReply(IPC::Message* reply_msg); |
121 | 80 |
122 // Processes a NOTIFY_PRINT_JOB_EVENT notification. | |
123 void OnNotifyPrintJobEvent(const JobEventDetails& event_details); | |
124 | |
125 // Requests the RenderView to render all the missing pages for the print job. | |
126 // No-op if no print job is pending. Returns true if at least one page has | |
127 // been requested to the renderer. | |
128 bool RenderAllMissingPagesNow(); | |
129 | |
130 // Quits the current message loop if these conditions hold true: a document is | |
131 // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This | |
132 // function is called in DidPrintPage() or on ALL_PAGES_REQUESTED | |
133 // notification. The inner message loop is created was created by | |
134 // RenderAllMissingPagesNow(). | |
135 void ShouldQuitFromInnerMessageLoop(); | |
136 | |
137 // Creates a new empty print job. It has no settings loaded. If there is | |
138 // currently a print job, safely disconnect from it. Returns false if it is | |
139 // impossible to safely disconnect from the current print job or it is | |
140 // impossible to create a new print job. | |
141 bool CreateNewPrintJob(PrintJobWorkerOwner* job); | |
142 | |
143 // Makes sure the current print_job_ has all its data before continuing, and | |
144 // disconnect from it. | |
145 void DisconnectFromCurrentPrintJob(); | |
146 | |
147 // Notify that the printing is done. | |
148 void PrintingDone(bool success); | |
149 | |
150 // Terminates the print job. No-op if no print job has been created. If | |
151 // |cancel| is true, cancel it instead of waiting for the job to finish. Will | |
152 // call ReleasePrintJob(). | |
153 void TerminatePrintJob(bool cancel); | |
154 | |
155 // Releases print_job_. Correctly deregisters from notifications. No-op if | |
156 // no print job has been created. | |
157 void ReleasePrintJob(); | |
158 | |
159 // Runs an inner message loop. It will set inside_inner_message_loop_ to true | |
160 // while the blocking inner message loop is running. This is useful in cases | |
161 // where the RenderView is about to be destroyed while a printing job isn't | |
162 // finished. | |
163 bool RunInnerMessageLoop(); | |
164 | |
165 // In the case of Scripted Printing, where the renderer is controlling the | |
166 // control flow, print_job_ is initialized whenever possible. No-op is | |
167 // print_job_ is initialized. | |
168 bool OpportunisticallyCreatePrintJob(int cookie); | |
169 | |
170 // Helper method for Print*Now(). | |
171 bool PrintNowInternal(IPC::Message* message); | |
172 | |
173 // Release the PrinterQuery associated with our |cookie_|. | |
174 void ReleasePrinterQuery(); | |
175 | |
176 content::NotificationRegistrar registrar_; | |
177 | |
178 // Manages the low-level talk to the printer. | |
179 scoped_refptr<PrintJob> print_job_; | |
180 | |
181 // Number of pages to print in the print job. | |
182 int number_pages_; | |
183 | |
184 // Indication of success of the print job. | |
185 bool printing_succeeded_; | |
186 | |
187 // Running an inner message loop inside RenderAllMissingPagesNow(). This means | |
188 // we are _blocking_ until all the necessary pages have been rendered or the | |
189 // print settings are being loaded. | |
190 bool inside_inner_message_loop_; | |
191 | |
192 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
193 // Set to true when OnDidPrintPage() should be expecting the first page. | |
194 bool expecting_first_page_; | |
195 #endif | |
196 | |
197 // Weak pointer to an observer that is notified when the print dialog is | 81 // Weak pointer to an observer that is notified when the print dialog is |
198 // shown. | 82 // shown. |
199 PrintViewManagerObserver* observer_; | 83 PrintViewManagerObserver* observer_; |
200 | 84 |
201 // The document cookie of the current PrinterQuery. | |
202 int cookie_; | |
203 | |
204 // Current state of print preview for this view. | 85 // Current state of print preview for this view. |
205 PrintPreviewState print_preview_state_; | 86 PrintPreviewState print_preview_state_; |
206 | 87 |
207 // Keeps track of the pending callback during scripted print preview. | 88 // Keeps track of the pending callback during scripted print preview. |
208 content::RenderProcessHost* scripted_print_preview_rph_; | 89 content::RenderProcessHost* scripted_print_preview_rph_; |
209 | 90 |
210 // Whether printing is enabled. | |
211 BooleanPrefMember printing_enabled_; | |
212 | |
213 // Whether our content is in blocked state. | |
214 bool tab_content_blocked_; | |
215 | |
216 DISALLOW_COPY_AND_ASSIGN(PrintViewManager); | 91 DISALLOW_COPY_AND_ASSIGN(PrintViewManager); |
217 }; | 92 }; |
218 | 93 |
219 } // namespace printing | 94 } // namespace printing |
220 | 95 |
221 #endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ | 96 #endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |
OLD | NEW |