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