Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
Yuta Hijikata | fd3ad1d | 2025-03-07 02:01:24 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | 1e54c1c | 2013-08-12 17:16:05 | [diff] [blame] | 10 | #include "chrome/browser/printing/print_view_manager_base.h" |
Gyuyoung Kim | f9d551d | 2020-02-20 00:50:21 | [diff] [blame] | 11 | #include "components/printing/common/print.mojom-forward.h" |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 12 | #include "content/public/browser/web_contents_user_data.h" |
Julie Jeongeun Kim | 52b2da7 | 2019-11-20 00:02:35 | [diff] [blame] | 13 | #include "mojo/public/cpp/bindings/pending_associated_remote.h" |
Scott Violet | 318a55f | 2018-03-30 19:08:19 | [diff] [blame] | 14 | #include "printing/buildflags/buildflags.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 16 | namespace content { |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 17 | class RenderFrameHost; |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 18 | class RenderProcessHost; |
Lei Zhang | 60a3b18 | 2022-02-25 22:29:03 | [diff] [blame] | 19 | struct GlobalRenderFrameHostId; |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 20 | } |
| 21 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | namespace printing { |
| 23 | |
[email protected] | e3a7e6f | 2012-09-18 22:44:31 | [diff] [blame] | 24 | // Manages the print commands for a WebContents. |
[email protected] | 1e54c1c | 2013-08-12 17:16:05 | [diff] [blame] | 25 | class PrintViewManager : public PrintViewManagerBase, |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 26 | public content::WebContentsUserData<PrintViewManager> { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | public: |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 28 | PrintViewManager(const PrintViewManager&) = delete; |
| 29 | PrintViewManager& operator=(const PrintViewManager&) = delete; |
| 30 | |
dcheng | df7e44a7 | 2014-10-21 23:50:19 | [diff] [blame] | 31 | ~PrintViewManager() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | |
Robert Sesek | 926bda6 | 2021-08-10 20:59:46 | [diff] [blame] | 33 | static void BindPrintManagerHost( |
| 34 | mojo::PendingAssociatedReceiver<mojom::PrintManagerHost> receiver, |
| 35 | content::RenderFrameHost* rfh); |
| 36 | |
[email protected] | 3121c76b | 2011-07-22 02:34:32 | [diff] [blame] | 37 | // Same as PrintNow(), but for the case where a user prints with the system |
| 38 | // dialog from print preview. |
Cuc Doan | 8c41923 | 2022-01-22 01:32:51 | [diff] [blame] | 39 | // `dialog_shown_callback` is called when the print dialog is shown. |
Lei Zhang | 3398fa5 | 2018-10-31 18:27:11 | [diff] [blame] | 40 | bool PrintForSystemDialogNow(base::OnceClosure dialog_shown_callback); |
[email protected] | 3121c76b | 2011-07-22 02:34:32 | [diff] [blame] | 41 | |
[email protected] | 0996e9b | 2011-08-26 17:59:01 | [diff] [blame] | 42 | // Same as PrintNow(), but for the case where a user press "ctrl+shift+p" to |
[email protected] | e6c97f6d | 2013-08-02 00:47:05 | [diff] [blame] | 43 | // show the native system dialog. This can happen from both initiator and |
[email protected] | c248f555 | 2013-01-26 01:58:49 | [diff] [blame] | 44 | // preview dialog. |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 45 | bool BasicPrint(content::RenderFrameHost* rfh); |
[email protected] | 0996e9b | 2011-08-26 17:59:01 | [diff] [blame] | 46 | |
Jesse Schettler | fd3df7e8 | 2019-09-16 23:23:18 | [diff] [blame] | 47 | // Initiate print preview of the current document and specify whether a |
| 48 | // selection or the entire frame is being printed. |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 49 | bool PrintPreviewNow(content::RenderFrameHost* rfh, bool has_selection); |
[email protected] | 8980e0d | 2011-05-27 19:16:23 | [diff] [blame] | 50 | |
Yuta Hijikata | fd3ad1d | 2025-03-07 02:01:24 | [diff] [blame] | 51 | #if BUILDFLAG(IS_CHROMEOS) |
Jesse Schettler | fd3df7e8 | 2019-09-16 23:23:18 | [diff] [blame] | 52 | // 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 Kim | 52b2da7 | 2019-11-20 00:02:35 | [diff] [blame] | 57 | mojo::PendingAssociatedRemote<mojom::PrintRenderer> print_renderer); |
Lei Zhang | be4236bc | 2023-04-27 18:54:08 | [diff] [blame] | 58 | #endif |
Jesse Schettler | fd3df7e8 | 2019-09-16 23:23:18 | [diff] [blame] | 59 | |
Lei Zhang | 862c6f57a | 2023-06-16 21:44:28 | [diff] [blame] | 60 | // Initiate print preview for the node under the context menu. |
| 61 | void PrintPreviewForNodeUnderContextMenu(content::RenderFrameHost* rfh); |
[email protected] | 9a26bb2 | 2012-01-24 23:50:25 | [diff] [blame] | 62 | |
Lei Zhang | 218f742d0 | 2019-03-05 05:18:40 | [diff] [blame] | 63 | // 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] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 69 | void PrintPreviewDone(); |
| 70 | |
Julie Jeongeun Kim | a75e43f | 2020-08-25 07:21:32 | [diff] [blame] | 71 | // mojom::PrintManagerHost: |
| 72 | void DidShowPrintDialog() override; |
Julie Jeongeun Kim | ac7fed6 | 2021-01-22 08:01:21 | [diff] [blame] | 73 | void SetupScriptedPrintPreview( |
| 74 | SetupScriptedPrintPreviewCallback callback) override; |
Julie Jeongeun Kim | ec9f86b | 2020-12-08 03:32:35 | [diff] [blame] | 75 | void ShowScriptedPrintPreview(bool source_is_modifiable) override; |
Julie Jeongeun Kim | 514a77b | 2021-01-07 06:05:03 | [diff] [blame] | 76 | void RequestPrintPreview(mojom::RequestPrintPreviewParamsPtr params) override; |
Julie Jeongeun Kim | be7f4124 | 2021-01-21 14:48:36 | [diff] [blame] | 77 | void CheckForCancel(int32_t preview_ui_id, |
| 78 | int32_t request_id, |
| 79 | CheckForCancelCallback callback) override; |
Julie Jeongeun Kim | a75e43f | 2020-08-25 07:21:32 | [diff] [blame] | 80 | |
[email protected] | 08ad957 | 2012-07-26 21:03:39 | [diff] [blame] | 81 | // content::WebContentsObserver implementation. |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 82 | void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
sgurun | 356feed5 | 2016-11-16 16:13:06 | [diff] [blame] | 83 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 84 | content::RenderFrameHost* print_preview_rfh() { return print_preview_rfh_; } |
[email protected] | 2e3f457 | 2011-03-25 19:24:47 | [diff] [blame] | 85 | |
Robert Sesek | 926bda6 | 2021-08-10 20:59:46 | [diff] [blame] | 86 | // Sets the target object for BindPrintManagerHost() for tests. |
| 87 | static void SetReceiverImplForTesting(PrintManager* impl); |
| 88 | |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 89 | protected: |
[email protected] | e3a7e6f | 2012-09-18 22:44:31 | [diff] [blame] | 90 | explicit PrintViewManager(content::WebContents* web_contents); |
rbpotter | fdff067 | 2018-04-12 20:46:58 | [diff] [blame] | 91 | |
| 92 | private: |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 93 | friend class content::WebContentsUserData<PrintViewManager>; |
[email protected] | e3a7e6f | 2012-09-18 22:44:31 | [diff] [blame] | 94 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 95 | enum PrintPreviewState { |
| 96 | NOT_PREVIEWING, |
| 97 | USER_INITIATED_PREVIEW, |
| 98 | SCRIPTED_PREVIEW, |
| 99 | }; |
| 100 | |
Jesse Schettler | fd3df7e8 | 2019-09-16 23:23:18 | [diff] [blame] | 101 | // 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 Kim | 52b2da7 | 2019-11-20 00:02:35 | [diff] [blame] | 106 | bool PrintPreview( |
| 107 | content::RenderFrameHost* rfh, |
Yuta Hijikata | fd3ad1d | 2025-03-07 02:01:24 | [diff] [blame] | 108 | #if BUILDFLAG(IS_CHROMEOS) |
Julie Jeongeun Kim | 52b2da7 | 2019-11-20 00:02:35 | [diff] [blame] | 109 | mojo::PendingAssociatedRemote<mojom::PrintRenderer> print_renderer, |
Lei Zhang | be4236bc | 2023-04-27 18:54:08 | [diff] [blame] | 110 | #endif |
Julie Jeongeun Kim | 52b2da7 | 2019-11-20 00:02:35 | [diff] [blame] | 111 | bool has_selection); |
Jesse Schettler | fd3df7e8 | 2019-09-16 23:23:18 | [diff] [blame] | 112 | |
Lei Zhang | 862c6f57a | 2023-06-16 21:44:28 | [diff] [blame] | 113 | // Notify PrintViewManager that print preview is starting in the renderer for |
| 114 | // a particular WebNode. |
| 115 | void PrintPreviewForWebNode(content::RenderFrameHost* rfh); |
| 116 | |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 117 | // Helper method for ShowScriptedPrintPreview(), called from |
| 118 | // RejectPrintPreviewRequestIfRestricted(). Based on value of |
Cuc Doan | 8c41923 | 2022-01-22 01:32:51 | [diff] [blame] | 119 | // `should_proceed`, continues to show the print preview or cancels it. |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 120 | void OnScriptedPrintPreviewCallback(bool source_is_modifiable, |
Lei Zhang | 60a3b18 | 2022-02-25 22:29:03 | [diff] [blame] | 121 | content::GlobalRenderFrameHostId rfh_id, |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 122 | bool should_proceed); |
Aida Zolic | 56c2021 | 2021-08-03 10:44:35 | [diff] [blame] | 123 | |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 124 | // Helper method for RequestPrintPreview(), called from |
| 125 | // RejectPrintPreviewRequestIfRestricted(). Based on value of |
Cuc Doan | 8c41923 | 2022-01-22 01:32:51 | [diff] [blame] | 126 | // `should_proceed`, continues to show the print preview or cancels it. |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 127 | void OnRequestPrintPreviewCallback(mojom::RequestPrintPreviewParamsPtr params, |
Lei Zhang | 60a3b18 | 2022-02-25 22:29:03 | [diff] [blame] | 128 | content::GlobalRenderFrameHostId rfh_id, |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 129 | bool should_proceed); |
Aida Zolic | 56c2021 | 2021-08-03 10:44:35 | [diff] [blame] | 130 | |
Lei Zhang | 218f742d0 | 2019-03-05 05:18:40 | [diff] [blame] | 131 | void MaybeUnblockScriptedPreviewRPH(); |
| 132 | |
Lei Zhang | 86bf104 | 2023-01-18 21:48:43 | [diff] [blame] | 133 | // 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 Kim | 514a77b | 2021-01-07 06:05:03 | [diff] [blame] | 137 | // Checks whether printing is currently restricted and aborts print preview if |
Cuc Doan | 8c41923 | 2022-01-22 01:32:51 | [diff] [blame] | 138 | // needed. Since this check is performed asynchronously, invokes `callback` |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 139 | // with an indicator whether to proceed or not. |
Aida Zolic | 37afb40 | 2021-11-12 11:11:39 | [diff] [blame] | 140 | // Virtual to allow tests to override. |
| 141 | virtual void RejectPrintPreviewRequestIfRestricted( |
Dominique Fauteux-Chapleau | 81980fc | 2022-04-14 20:10:44 | [diff] [blame] | 142 | content::GlobalRenderFrameHostId rfh_id, |
Aida Zolic | 164bbe1 | 2021-10-13 08:03:09 | [diff] [blame] | 143 | base::OnceCallback<void(bool should_proceed)> callback); |
Aida Zolic | 56c2021 | 2021-08-03 10:44:35 | [diff] [blame] | 144 | |
| 145 | // Helper method for RejectPrintPreviewRequestIfRestricted(). Handles any |
| 146 | // tasks that need to be done when the request is rejected due to |
| 147 | // restrictions. |
Lei Zhang | 60a3b18 | 2022-02-25 22:29:03 | [diff] [blame] | 148 | void OnPrintPreviewRequestRejected(content::GlobalRenderFrameHostId rfh_id); |
Julie Jeongeun Kim | 514a77b | 2021-01-07 06:05:03 | [diff] [blame] | 149 | |
Lei Zhang | c326b52f | 2022-06-29 08:47:42 | [diff] [blame] | 150 | // Virtual method that tests can override, in order to avoid actually |
| 151 | // displaying a system print dialog. |
| 152 | virtual void PrintForSystemDialogImpl(); |
| 153 | |
Aida Zolic | 70465be | 2021-09-27 17:50:34 | [diff] [blame] | 154 | // 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 Zhang | 3398fa5 | 2018-10-31 18:27:11 | [diff] [blame] | 161 | base::OnceClosure on_print_dialog_shown_callback_; |
[email protected] | 0b481bf | 2011-06-29 20:33:00 | [diff] [blame] | 162 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 163 | // Current state of print preview for this view. |
Lei Zhang | 3398fa5 | 2018-10-31 18:27:11 | [diff] [blame] | 164 | PrintPreviewState print_preview_state_ = NOT_PREVIEWING; |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 165 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 166 | // The current RFH that is print previewing. It should be a nullptr when |
Cuc Doan | 8c41923 | 2022-01-22 01:32:51 | [diff] [blame] | 167 | // `print_preview_state_` is NOT_PREVIEWING. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 168 | raw_ptr<content::RenderFrameHost> print_preview_rfh_ = nullptr; |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 169 | |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 170 | // Keeps track of the pending callback during scripted print preview. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 171 | raw_ptr<content::RenderProcessHost> scripted_print_preview_rph_ = nullptr; |
[email protected] | a9c3683 | 2011-11-23 08:55:24 | [diff] [blame] | 172 | |
Cuc Doan | 8c41923 | 2022-01-22 01:32:51 | [diff] [blame] | 173 | // True if `scripted_print_preview_rph_` needs to be unblocked. |
Lei Zhang | 218f742d0 | 2019-03-05 05:18:40 | [diff] [blame] | 174 | bool scripted_print_preview_rph_set_blocked_ = false; |
| 175 | |
Kent Tamura | 18c99f9a | 2017-09-25 05:39:32 | [diff] [blame] | 176 | // Indicates whether we're switching from print preview to system dialog. This |
| 177 | // flag is true between PrintForSystemDialogNow() and PrintPreviewDone(). |
Lei Zhang | 3398fa5 | 2018-10-31 18:27:11 | [diff] [blame] | 178 | bool is_switching_to_system_dialog_ = false; |
Kent Tamura | 18c99f9a | 2017-09-25 05:39:32 | [diff] [blame] | 179 | |
François Doray | 4f51d5d | 2018-12-03 22:26:24 | [diff] [blame] | 180 | WEB_CONTENTS_USER_DATA_KEY_DECL(); |
| 181 | |
Lei Zhang | a2cb1fb | 2021-07-28 18:05:14 | [diff] [blame] | 182 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | } // namespace printing |
| 189 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 190 | #endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ |