• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 CEF_LIBCEF_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_
6 #define CEF_LIBCEF_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_
7 
8 #include "include/internal/cef_types_wrappers.h"
9 
10 #include "base/callback_forward.h"
11 #include "chrome/browser/printing/print_view_manager.h"
12 #include "components/printing/common/print.mojom-forward.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/browser/web_contents_user_data.h"
15 #include "printing/mojom/print.mojom.h"
16 
17 namespace content {
18 class RenderFrameHost;
19 class RenderProcessHost;
20 class WebContentsObserver;
21 }  // namespace content
22 
23 class CefBrowserInfo;
24 
25 namespace printing {
26 
27 // CEF handler for print commands.
28 class CefPrintViewManager : public PrintViewManager,
29                             public mojom::PrintPreviewUI {
30  public:
31   CefPrintViewManager(const CefPrintViewManager&) = delete;
32   CefPrintViewManager& operator=(const CefPrintViewManager&) = delete;
33 
34   ~CefPrintViewManager() override;
35 
36   static void BindPrintManagerHost(
37       mojo::PendingAssociatedReceiver<mojom::PrintManagerHost> receiver,
38       content::RenderFrameHost* rfh);
39 
40   // Callback executed on PDF printing completion.
41   using PdfPrintCallback = base::OnceCallback<void(bool /*ok*/)>;
42 
43   // Print the current document to a PDF file. Execute |callback| on completion.
44   bool PrintToPDF(content::RenderFrameHost* rfh,
45                   const base::FilePath& path,
46                   const CefPdfPrintSettings& settings,
47                   PdfPrintCallback callback);
48 
49   // mojom::PrintManagerHost methods:
50   void GetDefaultPrintSettings(
51       GetDefaultPrintSettingsCallback callback) override;
52   void DidShowPrintDialog() override;
53   void RequestPrintPreview(mojom::RequestPrintPreviewParamsPtr params) override;
54   void CheckForCancel(int32_t preview_ui_id,
55                       int32_t request_id,
56                       CheckForCancelCallback callback) override;
57 
58   // printing::mojo::PrintPreviewUI methods:
SetOptionsFromDocument(const mojom::OptionsFromDocumentParamsPtr params,int32_t request_id)59   void SetOptionsFromDocument(const mojom::OptionsFromDocumentParamsPtr params,
60                               int32_t request_id) override {}
DidPrepareDocumentForPreview(int32_t document_cookie,int32_t request_id)61   void DidPrepareDocumentForPreview(int32_t document_cookie,
62                                     int32_t request_id) override {}
DidPreviewPage(mojom::DidPreviewPageParamsPtr params,int32_t request_id)63   void DidPreviewPage(mojom::DidPreviewPageParamsPtr params,
64                       int32_t request_id) override {}
65   void MetafileReadyForPrinting(mojom::DidPreviewDocumentParamsPtr params,
66                                 int32_t request_id) override;
67   void PrintPreviewFailed(int32_t document_cookie, int32_t request_id) override;
68   void PrintPreviewCancelled(int32_t document_cookie,
69                              int32_t request_id) override;
PrinterSettingsInvalid(int32_t document_cookie,int32_t request_id)70   void PrinterSettingsInvalid(int32_t document_cookie,
71                               int32_t request_id) override {}
DidGetDefaultPageLayout(mojom::PageSizeMarginsPtr page_layout_in_points,const gfx::Rect & printable_area_in_points,bool has_custom_page_size_style,int32_t request_id)72   void DidGetDefaultPageLayout(mojom::PageSizeMarginsPtr page_layout_in_points,
73                                const gfx::Rect& printable_area_in_points,
74                                bool has_custom_page_size_style,
75                                int32_t request_id) override {}
DidStartPreview(mojom::DidStartPreviewParamsPtr params,int32_t request_id)76   void DidStartPreview(mojom::DidStartPreviewParamsPtr params,
77                        int32_t request_id) override {}
78 
79   // content::WebContentsObserver methods:
80   void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
81   void NavigationStopped() override;
82   void PrimaryMainFrameRenderProcessGone(
83       base::TerminationStatus status) override;
84 
85   // Inline versions of the content::WebContentsUserData methods to avoid
86   // ambiguous warnings due to the PrintViewManager base class also extending
87   // WebContentsUserData.
88   static void CreateForWebContents(content::WebContents* contents);
89   static CefPrintViewManager* FromWebContents(content::WebContents* contents);
90   static const CefPrintViewManager* FromWebContents(
91       const content::WebContents* contents);
92 
93  private:
94   explicit CefPrintViewManager(content::WebContents* web_contents);
95 
96   void TerminatePdfPrintJob();
97 
98   // Used for printing to PDF. Only accessed on the browser process UI thread.
99   int next_pdf_request_id_ = content::RenderFrameHost::kNoFrameTreeNodeId;
100   struct PdfPrintState;
101   std::unique_ptr<PdfPrintState> pdf_print_state_;
102   mojo::AssociatedReceiver<mojom::PrintPreviewUI> pdf_print_receiver_{this};
103 };
104 
105 }  // namespace printing
106 
107 #endif  // CEF_LIBCEF_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_