blob: 529692c27f466105465c923e32189f118691f6a4 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
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
Keishi Hattori0e45c022021-11-27 09:25:528#include "base/memory/raw_ptr.h"
Yuta Hijikatafd3ad1d2025-03-07 02:01:249#include "build/build_config.h"
[email protected]1e54c1c2013-08-12 17:16:0510#include "chrome/browser/printing/print_view_manager_base.h"
Gyuyoung Kimf9d551d2020-02-20 00:50:2111#include "components/printing/common/print.mojom-forward.h"
[email protected]46b3c982012-10-09 18:38:3012#include "content/public/browser/web_contents_user_data.h"
Julie Jeongeun Kim52b2da72019-11-20 00:02:3513#include "mojo/public/cpp/bindings/pending_associated_remote.h"
Scott Violet318a55f2018-03-30 19:08:1914#include "printing/buildflags/buildflags.h"
initial.commit09911bf2008-07-26 23:55:2915
[email protected]a9c36832011-11-23 08:55:2416namespace content {
thestigcb959ce2016-11-17 05:56:3217class RenderFrameHost;
[email protected]a9c36832011-11-23 08:55:2418class RenderProcessHost;
Lei Zhang60a3b182022-02-25 22:29:0319struct GlobalRenderFrameHostId;
[email protected]a9c36832011-11-23 08:55:2420}
21
initial.commit09911bf2008-07-26 23:55:2922namespace printing {
23
[email protected]e3a7e6f2012-09-18 22:44:3124// Manages the print commands for a WebContents.
[email protected]1e54c1c2013-08-12 17:16:0525class PrintViewManager : public PrintViewManagerBase,
[email protected]46b3c982012-10-09 18:38:3026 public content::WebContentsUserData<PrintViewManager> {
initial.commit09911bf2008-07-26 23:55:2927 public:
Peter Boström53c6c5952021-09-17 09:41:2628 PrintViewManager(const PrintViewManager&) = delete;
29 PrintViewManager& operator=(const PrintViewManager&) = delete;
30
dchengdf7e44a72014-10-21 23:50:1931 ~PrintViewManager() override;
initial.commit09911bf2008-07-26 23:55:2932
Robert Sesek926bda62021-08-10 20:59:4633 static void BindPrintManagerHost(
34 mojo::PendingAssociatedReceiver<mojom::PrintManagerHost> receiver,
35 content::RenderFrameHost* rfh);
36
[email protected]3121c76b2011-07-22 02:34:3237 // Same as PrintNow(), but for the case where a user prints with the system
38 // dialog from print preview.
Cuc Doan8c419232022-01-22 01:32:5139 // `dialog_shown_callback` is called when the print dialog is shown.
Lei Zhang3398fa52018-10-31 18:27:1140 bool PrintForSystemDialogNow(base::OnceClosure dialog_shown_callback);
[email protected]3121c76b2011-07-22 02:34:3241
[email protected]0996e9b2011-08-26 17:59:0142 // Same as PrintNow(), but for the case where a user press "ctrl+shift+p" to
[email protected]e6c97f6d2013-08-02 00:47:0543 // show the native system dialog. This can happen from both initiator and
[email protected]c248f5552013-01-26 01:58:4944 // preview dialog.
thestigcb959ce2016-11-17 05:56:3245 bool BasicPrint(content::RenderFrameHost* rfh);
[email protected]0996e9b2011-08-26 17:59:0146
Jesse Schettlerfd3df7e82019-09-16 23:23:1847 // Initiate print preview of the current document and specify whether a
48 // selection or the entire frame is being printed.
thestigcb959ce2016-11-17 05:56:3249 bool PrintPreviewNow(content::RenderFrameHost* rfh, bool has_selection);
[email protected]8980e0d2011-05-27 19:16:2350
Yuta Hijikatafd3ad1d2025-03-07 02:01:2451#if BUILDFLAG(IS_CHROMEOS)
Jesse Schettlerfd3df7e82019-09-16 23:23:1852 // Initiate print preview of the current document and provide the renderer
53 // a printing::mojom::PrintRenderer to perform the actual rendering of
54 // the print document.
55 bool PrintPreviewWithPrintRenderer(
56 content::RenderFrameHost* rfh,
Julie Jeongeun Kim52b2da72019-11-20 00:02:3557 mojo::PendingAssociatedRemote<mojom::PrintRenderer> print_renderer);
Lei Zhangbe4236bc2023-04-27 18:54:0858#endif
Jesse Schettlerfd3df7e82019-09-16 23:23:1859
Lei Zhang862c6f57a2023-06-16 21:44:2860 // Initiate print preview for the node under the context menu.
61 void PrintPreviewForNodeUnderContextMenu(content::RenderFrameHost* rfh);
[email protected]9a26bb22012-01-24 23:50:2562
Lei Zhang218f742d02019-03-05 05:18:4063 // Notify PrintViewManager that print preview is about to finish. Unblock the
64 // renderer in the case of scripted print preview if needed.
65 void PrintPreviewAlmostDone();
66
67 // Notify PrintViewManager that print preview has finished. Unblock the
68 // renderer in the case of scripted print preview if needed.
[email protected]a9c36832011-11-23 08:55:2469 void PrintPreviewDone();
70
Julie Jeongeun Kima75e43f2020-08-25 07:21:3271 // mojom::PrintManagerHost:
72 void DidShowPrintDialog() override;
Julie Jeongeun Kimac7fed62021-01-22 08:01:2173 void SetupScriptedPrintPreview(
74 SetupScriptedPrintPreviewCallback callback) override;
Julie Jeongeun Kimec9f86b2020-12-08 03:32:3575 void ShowScriptedPrintPreview(bool source_is_modifiable) override;
Julie Jeongeun Kim514a77b2021-01-07 06:05:0376 void RequestPrintPreview(mojom::RequestPrintPreviewParamsPtr params) override;
Julie Jeongeun Kimbe7f41242021-01-21 14:48:3677 void CheckForCancel(int32_t preview_ui_id,
78 int32_t request_id,
79 CheckForCancelCallback callback) override;
Julie Jeongeun Kima75e43f2020-08-25 07:21:3280
[email protected]08ad9572012-07-26 21:03:3981 // content::WebContentsObserver implementation.
thestigcb959ce2016-11-17 05:56:3282 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
sgurun356feed52016-11-16 16:13:0683
thestigcb959ce2016-11-17 05:56:3284 content::RenderFrameHost* print_preview_rfh() { return print_preview_rfh_; }
[email protected]2e3f4572011-03-25 19:24:4785
Robert Sesek926bda62021-08-10 20:59:4686 // Sets the target object for BindPrintManagerHost() for tests.
87 static void SetReceiverImplForTesting(PrintManager* impl);
88
rbpotterfdff0672018-04-12 20:46:5889 protected:
[email protected]e3a7e6f2012-09-18 22:44:3190 explicit PrintViewManager(content::WebContents* web_contents);
rbpotterfdff0672018-04-12 20:46:5891
92 private:
[email protected]46b3c982012-10-09 18:38:3093 friend class content::WebContentsUserData<PrintViewManager>;
[email protected]e3a7e6f2012-09-18 22:44:3194
[email protected]a9c36832011-11-23 08:55:2495 enum PrintPreviewState {
96 NOT_PREVIEWING,
97 USER_INITIATED_PREVIEW,
98 SCRIPTED_PREVIEW,
99 };
100
Jesse Schettlerfd3df7e82019-09-16 23:23:18101 // Helper method for PrintPreviewNow() and PrintPreviewWithRenderer().
102 // Initiate print preview of the current document by first notifying the
103 // renderer. Since this happens asynchronously, the print preview dialog
104 // creation will not be completed on the return of this function. Returns
105 // false if print preview is impossible at the moment.
Julie Jeongeun Kim52b2da72019-11-20 00:02:35106 bool PrintPreview(
107 content::RenderFrameHost* rfh,
Yuta Hijikatafd3ad1d2025-03-07 02:01:24108#if BUILDFLAG(IS_CHROMEOS)
Julie Jeongeun Kim52b2da72019-11-20 00:02:35109 mojo::PendingAssociatedRemote<mojom::PrintRenderer> print_renderer,
Lei Zhangbe4236bc2023-04-27 18:54:08110#endif
Julie Jeongeun Kim52b2da72019-11-20 00:02:35111 bool has_selection);
Jesse Schettlerfd3df7e82019-09-16 23:23:18112
Lei Zhang862c6f57a2023-06-16 21:44:28113 // Notify PrintViewManager that print preview is starting in the renderer for
114 // a particular WebNode.
115 void PrintPreviewForWebNode(content::RenderFrameHost* rfh);
116
Aida Zolic164bbe12021-10-13 08:03:09117 // Helper method for ShowScriptedPrintPreview(), called from
118 // RejectPrintPreviewRequestIfRestricted(). Based on value of
Cuc Doan8c419232022-01-22 01:32:51119 // `should_proceed`, continues to show the print preview or cancels it.
Aida Zolic164bbe12021-10-13 08:03:09120 void OnScriptedPrintPreviewCallback(bool source_is_modifiable,
Lei Zhang60a3b182022-02-25 22:29:03121 content::GlobalRenderFrameHostId rfh_id,
Aida Zolic164bbe12021-10-13 08:03:09122 bool should_proceed);
Aida Zolic56c20212021-08-03 10:44:35123
Aida Zolic164bbe12021-10-13 08:03:09124 // Helper method for RequestPrintPreview(), called from
125 // RejectPrintPreviewRequestIfRestricted(). Based on value of
Cuc Doan8c419232022-01-22 01:32:51126 // `should_proceed`, continues to show the print preview or cancels it.
Aida Zolic164bbe12021-10-13 08:03:09127 void OnRequestPrintPreviewCallback(mojom::RequestPrintPreviewParamsPtr params,
Lei Zhang60a3b182022-02-25 22:29:03128 content::GlobalRenderFrameHostId rfh_id,
Aida Zolic164bbe12021-10-13 08:03:09129 bool should_proceed);
Aida Zolic56c20212021-08-03 10:44:35130
Lei Zhang218f742d02019-03-05 05:18:40131 void MaybeUnblockScriptedPreviewRPH();
132
Lei Zhang86bf1042023-01-18 21:48:43133 // Helper used to set `print_preview_rfh_` to `rfh`. Performs sanity checks
134 // before doing so.
135 void SetPrintPreviewRenderFrameHost(content::RenderFrameHost* rfh);
136
Julie Jeongeun Kim514a77b2021-01-07 06:05:03137 // Checks whether printing is currently restricted and aborts print preview if
Cuc Doan8c419232022-01-22 01:32:51138 // needed. Since this check is performed asynchronously, invokes `callback`
Aida Zolic164bbe12021-10-13 08:03:09139 // with an indicator whether to proceed or not.
Aida Zolic37afb402021-11-12 11:11:39140 // Virtual to allow tests to override.
141 virtual void RejectPrintPreviewRequestIfRestricted(
Dominique Fauteux-Chapleau81980fc2022-04-14 20:10:44142 content::GlobalRenderFrameHostId rfh_id,
Aida Zolic164bbe12021-10-13 08:03:09143 base::OnceCallback<void(bool should_proceed)> callback);
Aida Zolic56c20212021-08-03 10:44:35144
145 // Helper method for RejectPrintPreviewRequestIfRestricted(). Handles any
146 // tasks that need to be done when the request is rejected due to
147 // restrictions.
Lei Zhang60a3b182022-02-25 22:29:03148 void OnPrintPreviewRequestRejected(content::GlobalRenderFrameHostId rfh_id);
Julie Jeongeun Kim514a77b2021-01-07 06:05:03149
Lei Zhangc326b52f2022-06-29 08:47:42150 // Virtual method that tests can override, in order to avoid actually
151 // displaying a system print dialog.
152 virtual void PrintForSystemDialogImpl();
153
Aida Zolic70465be2021-09-27 17:50:34154 // Virtual method to be overridden in tests, in order to be notified whether
155 // the print preview is shown or not due to policies or user actions.
156 virtual void PrintPreviewRejectedForTesting();
157 // Virtual method to be overridden in tests, in order to be notified when the
158 // print preview is not prevented by policies or user actions.
159 virtual void PrintPreviewAllowedForTesting();
160
Lei Zhang3398fa52018-10-31 18:27:11161 base::OnceClosure on_print_dialog_shown_callback_;
[email protected]0b481bf2011-06-29 20:33:00162
[email protected]a9c36832011-11-23 08:55:24163 // Current state of print preview for this view.
Lei Zhang3398fa52018-10-31 18:27:11164 PrintPreviewState print_preview_state_ = NOT_PREVIEWING;
[email protected]a9c36832011-11-23 08:55:24165
thestigcb959ce2016-11-17 05:56:32166 // The current RFH that is print previewing. It should be a nullptr when
Cuc Doan8c419232022-01-22 01:32:51167 // `print_preview_state_` is NOT_PREVIEWING.
Keishi Hattori0e45c022021-11-27 09:25:52168 raw_ptr<content::RenderFrameHost> print_preview_rfh_ = nullptr;
thestigcb959ce2016-11-17 05:56:32169
[email protected]a9c36832011-11-23 08:55:24170 // Keeps track of the pending callback during scripted print preview.
Keishi Hattori0e45c022021-11-27 09:25:52171 raw_ptr<content::RenderProcessHost> scripted_print_preview_rph_ = nullptr;
[email protected]a9c36832011-11-23 08:55:24172
Cuc Doan8c419232022-01-22 01:32:51173 // True if `scripted_print_preview_rph_` needs to be unblocked.
Lei Zhang218f742d02019-03-05 05:18:40174 bool scripted_print_preview_rph_set_blocked_ = false;
175
Kent Tamura18c99f9a2017-09-25 05:39:32176 // Indicates whether we're switching from print preview to system dialog. This
177 // flag is true between PrintForSystemDialogNow() and PrintPreviewDone().
Lei Zhang3398fa52018-10-31 18:27:11178 bool is_switching_to_system_dialog_ = false;
Kent Tamura18c99f9a2017-09-25 05:39:32179
François Doray4f51d5d2018-12-03 22:26:24180 WEB_CONTENTS_USER_DATA_KEY_DECL();
181
Lei Zhanga2cb1fb2021-07-28 18:05:14182 // Keep this last so that all weak pointers will be invalidated at the
183 // beginning of destruction. Note that PrintViewManagerBase has its own
184 // base::WeakPtrFactory as well, but PrintViewManager should use this one.
185 base::WeakPtrFactory<PrintViewManager> weak_factory_{this};
initial.commit09911bf2008-07-26 23:55:29186};
187
188} // namespace printing
189
[email protected]bfd04a62009-02-01 18:16:56190#endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_