blob: 4db11387efa5e20f8cf7d18bdf8f99ba3c2dd2d8 [file] [log] [blame]
[email protected]9a26bb22012-01-24 23:50:251// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]bfd04a62009-02-01 18:16:565#ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_
6#define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:297
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/ref_counted.h"
[email protected]d9d42992010-09-13 19:39:199#include "base/string16.h"
[email protected]08ad9572012-07-26 21:03:3910#include "chrome/browser/prefs/pref_member.h"
[email protected]6c2381d2011-10-19 02:52:5311#include "content/public/browser/notification_observer.h"
12#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2513#include "content/public/browser/web_contents_observer.h"
[email protected]8ff1d422009-07-07 21:31:3914#include "printing/printed_pages_source.h"
initial.commit09911bf2008-07-26 23:55:2915
[email protected]c36a9e1d2012-06-05 14:31:0216class TabContents;
[email protected]1375e3ab2011-03-24 17:07:2217struct PrintHostMsg_DidPrintPage_Params;
initial.commit09911bf2008-07-26 23:55:2918
[email protected]a9c36832011-11-23 08:55:2419namespace content {
20class RenderProcessHost;
[email protected]08ad9572012-07-26 21:03:3921class RenderViewHost;
[email protected]a9c36832011-11-23 08:55:2422}
23
initial.commit09911bf2008-07-26 23:55:2924namespace printing {
25
26class JobEventDetails;
27class PrintJob;
28class PrintJobWorkerOwner;
[email protected]0b481bf2011-06-29 20:33:0029class PrintViewManagerObserver;
initial.commit09911bf2008-07-26 23:55:2930
[email protected]57dfa6202012-06-08 03:17:1831// Manages the print commands in relation to a TabContents.
32// TabContents delegates a few printing related commands to this
[email protected]e6ff1d52012-04-17 20:00:4033// instance.
[email protected]6c2381d2011-10-19 02:52:5334class PrintViewManager : public content::NotificationObserver,
[email protected]8d3347f2009-07-09 22:00:2135 public PrintedPagesSource,
[email protected]d8c660432011-12-22 20:51:2536 public content::WebContentsObserver {
initial.commit09911bf2008-07-26 23:55:2937 public:
[email protected]57dfa6202012-06-08 03:17:1838 explicit PrintViewManager(TabContents* tab);
[email protected]2d4537d52008-12-17 02:25:4439 virtual ~PrintViewManager();
initial.commit09911bf2008-07-26 23:55:2940
[email protected]49fe2262011-04-15 20:56:1541 // Prints the current document immediately. Since the rendering is
42 // asynchronous, the actual printing will not be completed on the return of
43 // this function. Returns false if printing is impossible at the moment.
44 bool PrintNow();
45
[email protected]3121c76b2011-07-22 02:34:3246 // Same as PrintNow(), but for the case where a user prints with the system
47 // dialog from print preview.
48 bool PrintForSystemDialogNow();
49
[email protected]0996e9b2011-08-26 17:59:0150 // Same as PrintNow(), but for the case where a user press "ctrl+shift+p" to
51 // show the native system dialog. This can happen from both initiator tab and
52 // preview tab.
53 bool AdvancedPrintNow();
54
[email protected]d53e4032012-06-29 18:58:3455 // Same as PrintNow(), but for the case where we want to send the result to
56 // another destination.
57 // TODO(mad) Add an argument so we can pass the destination interface.
58 bool PrintToDestination();
59
[email protected]8980e0d2011-05-27 19:16:2360 // Initiate print preview of the current document by first notifying the
61 // renderer. Since this happens asynchronous, the print preview tab creation
62 // will not be completed on the return of this function. Returns false if
63 // print preview is impossible at the moment.
64 bool PrintPreviewNow();
65
[email protected]9a26bb22012-01-24 23:50:2566 // Notify PrintViewManager that print preview is starting in the renderer for
67 // a particular WebNode.
68 void PrintPreviewForWebNode();
69
[email protected]a9c36832011-11-23 08:55:2470 // Notify PrintViewManager that print preview has finished. Unfreeze the
71 // renderer in the case of scripted print preview.
72 void PrintPreviewDone();
73
[email protected]08ad9572012-07-26 21:03:3974 // Whether to block scripted printing for our tab or not.
75 void UpdateScriptedPrintingBlocked();
[email protected]d53cbc02012-03-23 00:03:3176
[email protected]0b481bf2011-06-29 20:33:0077 // Sets |observer| as the current PrintViewManagerObserver. Pass in NULL to
78 // remove the current observer. |observer| may always be NULL, but |observer_|
79 // must be NULL if |observer| is non-NULL.
80 void set_observer(PrintViewManagerObserver* observer);
81
initial.commit09911bf2008-07-26 23:55:2982 // PrintedPagesSource implementation.
[email protected]184a6c8f2011-10-04 20:07:3083 virtual string16 RenderSourceName() OVERRIDE;
initial.commit09911bf2008-07-26 23:55:2984
[email protected]6c2381d2011-10-19 02:52:5385 // content::NotificationObserver implementation.
[email protected]432115822011-07-10 15:52:2786 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:5387 const content::NotificationSource& source,
88 const content::NotificationDetails& details) OVERRIDE;
initial.commit09911bf2008-07-26 23:55:2989
[email protected]d8c660432011-12-22 20:51:2590 // content::WebContentsObserver implementation.
[email protected]08ad9572012-07-26 21:03:3991 virtual void DidStartLoading(
92 content::RenderViewHost* render_view_host) OVERRIDE;
93
94 // content::WebContentsObserver implementation.
[email protected]184a6c8f2011-10-04 20:07:3095 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
[email protected]34f128d2011-01-25 19:07:4496
[email protected]2e3f4572011-03-25 19:24:4797 // Terminates or cancels the print job if one was pending.
[email protected]9cddb1a22011-11-15 15:04:2798 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE;
[email protected]2e3f4572011-03-25 19:24:4799
100 // Cancels the print job.
[email protected]9cddb1a22011-11-15 15:04:27101 virtual void StopNavigation() OVERRIDE;
[email protected]2e3f4572011-03-25 19:24:47102
initial.commit09911bf2008-07-26 23:55:29103 private:
[email protected]a9c36832011-11-23 08:55:24104 enum PrintPreviewState {
105 NOT_PREVIEWING,
106 USER_INITIATED_PREVIEW,
107 SCRIPTED_PREVIEW,
108 };
109
[email protected]b3ee0b522011-05-06 04:06:40110 // IPC Message handlers.
[email protected]34f128d2011-01-25 19:07:44111 void OnDidGetPrintedPagesCount(int cookie, int number_pages);
[email protected]da089562011-07-01 01:54:46112 void OnDidGetDocumentCookie(int cookie);
[email protected]0b481bf2011-06-29 20:33:00113 void OnDidShowPrintDialog();
[email protected]1375e3ab2011-03-24 17:07:22114 void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params);
[email protected]b3ee0b522011-05-06 04:06:40115 void OnPrintingFailed(int cookie);
[email protected]34f128d2011-01-25 19:07:44116
[email protected]a8528382011-12-01 04:29:47117 void OnScriptedPrintPreview(bool source_is_modifiable,
118 IPC::Message* reply_msg);
[email protected]a9c36832011-11-23 08:55:24119 void OnScriptedPrintPreviewReply(IPC::Message* reply_msg);
120
initial.commit09911bf2008-07-26 23:55:29121 // Processes a NOTIFY_PRINT_JOB_EVENT notification.
122 void OnNotifyPrintJobEvent(const JobEventDetails& event_details);
123
initial.commit09911bf2008-07-26 23:55:29124 // Requests the RenderView to render all the missing pages for the print job.
[email protected]184a6c8f2011-10-04 20:07:30125 // No-op if no print job is pending. Returns true if at least one page has
126 // been requested to the renderer.
initial.commit09911bf2008-07-26 23:55:29127 bool RenderAllMissingPagesNow();
128
129 // Quits the current message loop if these conditions hold true: a document is
130 // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This
131 // function is called in DidPrintPage() or on ALL_PAGES_REQUESTED
132 // notification. The inner message loop is created was created by
133 // RenderAllMissingPagesNow().
134 void ShouldQuitFromInnerMessageLoop();
135
136 // Creates a new empty print job. It has no settings loaded. If there is
137 // currently a print job, safely disconnect from it. Returns false if it is
138 // impossible to safely disconnect from the current print job or it is
139 // impossible to create a new print job.
140 bool CreateNewPrintJob(PrintJobWorkerOwner* job);
141
142 // Makes sure the current print_job_ has all its data before continuing, and
143 // disconnect from it.
144 void DisconnectFromCurrentPrintJob();
145
[email protected]82270452009-06-19 15:58:01146 // Notify that the printing is done.
147 void PrintingDone(bool success);
148
[email protected]da089562011-07-01 01:54:46149 // Terminates the print job. No-op if no print job has been created. If
initial.commit09911bf2008-07-26 23:55:29150 // |cancel| is true, cancel it instead of waiting for the job to finish. Will
151 // call ReleasePrintJob().
152 void TerminatePrintJob(bool cancel);
153
[email protected]da089562011-07-01 01:54:46154 // Releases print_job_. Correctly deregisters from notifications. No-op if
initial.commit09911bf2008-07-26 23:55:29155 // no print job has been created.
156 void ReleasePrintJob();
157
initial.commit09911bf2008-07-26 23:55:29158 // Runs an inner message loop. It will set inside_inner_message_loop_ to true
159 // while the blocking inner message loop is running. This is useful in cases
160 // where the RenderView is about to be destroyed while a printing job isn't
161 // finished.
162 bool RunInnerMessageLoop();
163
164 // In the case of Scripted Printing, where the renderer is controlling the
165 // control flow, print_job_ is initialized whenever possible. No-op is
166 // print_job_ is initialized.
167 bool OpportunisticallyCreatePrintJob(int cookie);
168
[email protected]3121c76b2011-07-22 02:34:32169 // Helper method for Print*Now().
170 bool PrintNowInternal(IPC::Message* message);
171
[email protected]7d6171c2012-02-25 00:08:43172 // Release the PrinterQuery associated with our |cookie_|.
173 void ReleasePrinterQuery();
174
[email protected]57dfa6202012-06-08 03:17:18175 // TabContents we're associated with.
176 TabContents* tab_;
[email protected]34d38be2011-06-09 20:44:58177
[email protected]6c2381d2011-10-19 02:52:53178 content::NotificationRegistrar registrar_;
[email protected]0f08bf32009-05-21 20:33:17179
initial.commit09911bf2008-07-26 23:55:29180 // Manages the low-level talk to the printer.
181 scoped_refptr<PrintJob> print_job_;
182
[email protected]f0815752011-02-01 22:23:44183 // Number of pages to print in the print job.
184 int number_pages_;
185
[email protected]82270452009-06-19 15:58:01186 // Indication of success of the print job.
187 bool printing_succeeded_;
188
initial.commit09911bf2008-07-26 23:55:29189 // Running an inner message loop inside RenderAllMissingPagesNow(). This means
190 // we are _blocking_ until all the necessary pages have been rendered or the
191 // print settings are being loaded.
192 bool inside_inner_message_loop_;
193
[email protected]da4eefd2011-03-03 23:40:27194#if defined(OS_POSIX) && !defined(OS_MACOSX)
195 // Set to true when OnDidPrintPage() should be expecting the first page.
196 bool expecting_first_page_;
197#endif
198
[email protected]0b481bf2011-06-29 20:33:00199 // Weak pointer to an observer that is notified when the print dialog is
200 // shown.
201 PrintViewManagerObserver* observer_;
202
[email protected]da089562011-07-01 01:54:46203 // The document cookie of the current PrinterQuery.
204 int cookie_;
205
[email protected]a9c36832011-11-23 08:55:24206 // Current state of print preview for this view.
207 PrintPreviewState print_preview_state_;
208
209 // Keeps track of the pending callback during scripted print preview.
210 content::RenderProcessHost* scripted_print_preview_rph_;
211
[email protected]08ad9572012-07-26 21:03:39212 // Whether printing is enabled.
213 BooleanPrefMember printing_enabled_;
214
215 // Whether our tab content is in blocked state.
216 bool tab_content_blocked_;
217
[email protected]bfd04a62009-02-01 18:16:56218 DISALLOW_COPY_AND_ASSIGN(PrintViewManager);
initial.commit09911bf2008-07-26 23:55:29219};
220
221} // namespace printing
222
[email protected]bfd04a62009-02-01 18:16:56223#endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_