1 // Copyright 2015 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 COMPONENTS_PRINTING_BROWSER_PRINT_MANAGER_H_
6 #define COMPONENTS_PRINTING_BROWSER_PRINT_MANAGER_H_
7 
8 #include <map>
9 #include <memory>
10 
11 #include "base/macros.h"
12 #include "build/build_config.h"
13 #include "components/printing/common/print.mojom.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "mojo/public/cpp/bindings/associated_remote.h"
16 
17 #if defined(OS_ANDROID)
18 #include "base/callback.h"
19 #endif
20 
21 namespace IPC {
22 class Message;
23 }
24 
25 struct PrintHostMsg_DidPrintDocument_Params;
26 struct PrintHostMsg_ScriptedPrint_Params;
27 
28 namespace printing {
29 
30 class PrintManager : public content::WebContentsObserver {
31  public:
32   ~PrintManager() override;
33 
34 #if defined(OS_ANDROID)
35   // TODO(timvolodine): consider introducing PrintManagerAndroid (crbug/500960)
36   using PdfWritingDoneCallback =
37       base::RepeatingCallback<void(int /* page count */)>;
38 
39   virtual void PdfWritingDone(int page_count) = 0;
40 #endif
41 
42  protected:
43   explicit PrintManager(content::WebContents* contents);
44 
45   // Helper method to fetch the PrintRenderFrame associated remote interface
46   // pointer.
47   const mojo::AssociatedRemote<printing::mojom::PrintRenderFrame>&
48   GetPrintRenderFrame(content::RenderFrameHost* rfh);
49 
50   // Terminates or cancels the print job if one was pending.
51   void PrintingRenderFrameDeleted();
52 
53   // content::WebContentsObserver
54   bool OnMessageReceived(const IPC::Message& message,
55                          content::RenderFrameHost* render_frame_host) override;
56   void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
57 
58   // IPC handling support
59   struct FrameDispatchHelper;
60 
61   // IPC message PrintHostMsg_DidPrintDocument can require handling in other
62   // processes beyond the rendering process running OnMessageReceived(),
63   // requiring that the renderer needs to wait.
64   class DelayedFrameDispatchHelper : public content::WebContentsObserver {
65    public:
66     DelayedFrameDispatchHelper(content::WebContents* contents,
67                                content::RenderFrameHost* render_frame_host,
68                                IPC::Message* reply_msg);
69     DelayedFrameDispatchHelper(const DelayedFrameDispatchHelper&) = delete;
70     ~DelayedFrameDispatchHelper() override;
71     DelayedFrameDispatchHelper& operator=(const DelayedFrameDispatchHelper&) =
72         delete;
73 
74     // content::WebContentsObserver
75     void RenderFrameDeleted(
76         content::RenderFrameHost* render_frame_host) override;
77 
78     // SendCompleted() can be called at most once, since it provides the success
79     // reply for a message. A failure reply for the message is automatically
80     // sent if this is never called.
81     void SendCompleted();
82 
83    private:
84     content::RenderFrameHost* const render_frame_host_;
85     IPC::Message* reply_msg_;
86   };
87 
88   // IPC handlers
89   virtual void OnDidGetPrintedPagesCount(int cookie, int number_pages);
90   virtual void OnDidPrintDocument(
91       content::RenderFrameHost* render_frame_host,
92       const PrintHostMsg_DidPrintDocument_Params& params,
93       std::unique_ptr<DelayedFrameDispatchHelper> helper) = 0;
94   virtual void OnGetDefaultPrintSettings(
95       content::RenderFrameHost* render_frame_host,
96       IPC::Message* reply_msg) = 0;
97   virtual void OnPrintingFailed(int cookie);
98   virtual void OnScriptedPrint(content::RenderFrameHost* render_frame_host,
99                                const PrintHostMsg_ScriptedPrint_Params& params,
100                                IPC::Message* reply_msg) = 0;
101 
102   int number_pages_ = 0;  // Number of pages to print in the print job.
103   int cookie_ = 0;        // The current document cookie.
104 
105 #if defined(OS_ANDROID)
106   // Callback to execute when done writing pdf.
107   PdfWritingDoneCallback pdf_writing_done_callback_;
108 #endif
109 
110  private:
111   void OnDidGetDocumentCookie(int cookie);
112 
113   // Stores a PrintRenderFrame associated remote with the RenderFrameHost used
114   // to bind it. The PrintRenderFrame is used to transmit mojo interface method
115   // calls to the associated receiver.
116   std::map<content::RenderFrameHost*,
117            mojo::AssociatedRemote<printing::mojom::PrintRenderFrame>>
118       print_render_frames_;
119 
120   DISALLOW_COPY_AND_ASSIGN(PrintManager);
121 };
122 
123 }  // namespace printing
124 
125 #endif  // COMPONENTS_PRINTING_BROWSER_PRINT_MANAGER_H_
126