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