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