Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2011 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 | |
sverrir@google.com | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #ifndef PRINTING_PRINTING_CONTEXT_H_ |
| 6 | #define PRINTING_PRINTING_CONTEXT_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 8 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Avi Drissman | 821ca309 | 2023-01-11 22:42:15 | [diff] [blame] | 11 | #include "base/functional/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 13 | #include "base/values.h" |
Lei Zhang | d687961 | 2019-02-07 18:45:53 | [diff] [blame] | 14 | #include "build/build_config.h" |
Alan Screen | e9ed54a | 2021-11-24 18:22:46 | [diff] [blame] | 15 | #include "printing/buildflags/buildflags.h" |
Julie Jeongeun Kim | cb00d1ab | 2020-08-14 07:43:32 | [diff] [blame] | 16 | #include "printing/mojom/print.mojom.h" |
Thiago Farina | 45e31953 | 2017-03-23 02:44:49 | [diff] [blame] | 17 | #include "printing/native_drawing_context.h" |
sverrir@google.com | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 18 | #include "printing/print_settings.h" |
sail@chromium.org | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 19 | #include "ui/gfx/native_widget_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | |
| 21 | namespace printing { |
| 22 | |
Alan Screen | 4e284e9 | 2021-12-04 00:38:29 | [diff] [blame] | 23 | class MetafilePlayer; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 24 | class PrintingContextFactoryForTest; |
| 25 | |
Xiaohan Wang | e3d00dd6 | 2022-01-08 02:33:28 | [diff] [blame] | 26 | #if BUILDFLAG(IS_WIN) |
Alan Screen | 4e284e9 | 2021-12-04 00:38:29 | [diff] [blame] | 27 | class PageSetup; |
| 28 | class PrintedPage; |
| 29 | #endif |
| 30 | |
jhawkins@chromium.org | 51e8d93 | 2010-10-06 22:21:17 | [diff] [blame] | 31 | // An abstraction of a printer context, implemented by objects that describe the |
| 32 | // user selected printing context. This includes the OS-dependent UI to ask the |
| 33 | // user about the print settings. Concrete implementations directly talk to the |
| 34 | // printer and manage the document and page breaks. |
Alan Screen | 9347cae | 2021-04-27 18:11:24 | [diff] [blame] | 35 | class COMPONENT_EXPORT(PRINTING) PrintingContext { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | public: |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 37 | // Printing context delegate. |
| 38 | class Delegate { |
| 39 | public: |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 40 | Delegate() {} |
| 41 | virtual ~Delegate() {} |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 42 | |
| 43 | // Returns parent view to use for modal dialogs. |
| 44 | virtual gfx::NativeView GetParentView() = 0; |
| 45 | |
| 46 | // Returns application locale. |
| 47 | virtual std::string GetAppLocale() = 0; |
| 48 | }; |
| 49 | |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 50 | struct PrinterSettings { |
Xiaohan Wang | e3d00dd6 | 2022-01-08 02:33:28 | [diff] [blame] | 51 | #if BUILDFLAG(IS_MAC) |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 52 | // True if the to-be-printed PDF is going to be opened in external |
| 53 | // preview. Used by macOS only to open Preview.app. |
| 54 | bool external_preview; |
| 55 | #endif |
| 56 | |
| 57 | // Whether to show the system dialog. |
| 58 | bool show_system_dialog; |
| 59 | |
Xiaohan Wang | e3d00dd6 | 2022-01-08 02:33:28 | [diff] [blame] | 60 | #if BUILDFLAG(IS_WIN) |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 61 | // If showing the system dialog, the number of pages in the to-be-printed |
Lei Zhang | f8b5b15 | 2021-11-16 01:10:25 | [diff] [blame] | 62 | // PDF. Only used on Windows. |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 63 | int page_count; |
Lei Zhang | f8b5b15 | 2021-11-16 01:10:25 | [diff] [blame] | 64 | #endif |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 65 | }; |
| 66 | |
Alan Screen | 96a12395 | 2023-11-06 21:53:16 | [diff] [blame] | 67 | enum class ProcessBehavior { |
| 68 | // Out-of-process support is disabled. All platform printing calls are |
| 69 | // performed in the browser process. |
| 70 | kOopDisabled, |
| 71 | #if BUILDFLAG(ENABLE_OOP_PRINTING) |
| 72 | // Out-of-process support is enabled. This is for `PrintingContext` |
| 73 | // objects which exist in the PrintBackend service. These objects make |
| 74 | // platform printing calls. |
| 75 | kOopEnabledPerformSystemCalls, |
| 76 | // Out-of-process support is enabled. This is for `PrintingContext` |
| 77 | // objects which exist in the browser process. These objects normally skip |
| 78 | // doing platform printing calls, deferring to an associated |
| 79 | // `PrintingContext` that is running in a PrintBackend service (i.e., |
| 80 | // deferring to a `PrintingContext` with `kOopEnabledPerformSystemCalls`). |
| 81 | // An exception to deferring platform calls in this case is for platforms |
| 82 | // that cannot display a system print dialog from a PrintBackend service. |
| 83 | // On such platforms the relevant calls to invoke a system print dialog |
| 84 | // are still made from the browser process. |
| 85 | kOopEnabledSkipSystemCalls, |
| 86 | #endif |
| 87 | }; |
| 88 | |
Alan Screen | 348530fe | 2024-01-20 02:47:14 | [diff] [blame] | 89 | // Value returned by `job_id()` when there is no active print job or the |
| 90 | // platform/test does not expose an underlying job ID for extra management. |
| 91 | static constexpr int kNoPrintJobId = 0; |
| 92 | |
Lei Zhang | dd3e9b0 | 2020-08-19 23:04:42 | [diff] [blame] | 93 | PrintingContext(const PrintingContext&) = delete; |
| 94 | PrintingContext& operator=(const PrintingContext&) = delete; |
jhawkins@chromium.org | 51e8d93 | 2010-10-06 22:21:17 | [diff] [blame] | 95 | virtual ~PrintingContext(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | |
jhawkins@chromium.org | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 97 | // Callback of AskUserForSettings, used to notify the PrintJobWorker when |
| 98 | // print settings are available. |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 99 | using PrintSettingsCallback = base::OnceCallback<void(mojom::ResultCode)>; |
jhawkins@chromium.org | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 100 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | // Asks the user what printer and format should be used to print. Updates the |
jhawkins@chromium.org | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 102 | // context with the select device settings. The result of the call is returned |
| 103 | // in the callback. This is necessary for Linux, which only has an |
| 104 | // asynchronous printing API. |
Daniel Hosseinian | 3553e27 | 2021-04-24 00:51:18 | [diff] [blame] | 105 | // On Android, when `is_scripted` is true, calling it initiates a full |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 106 | // printing flow from the framework's PrintManager. |
| 107 | // (see https://codereview.chromium.org/740983002/) |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 108 | virtual void AskUserForSettings(int max_pages, |
jhawkins@chromium.org | 51e8d93 | 2010-10-06 22:21:17 | [diff] [blame] | 109 | bool has_selection, |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 110 | bool is_scripted, |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 111 | PrintSettingsCallback callback) = 0; |
thestig@chromium.org | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 112 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | // Selects the user's default printer and format. Updates the context with the |
| 114 | // default device settings. |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 115 | virtual mojom::ResultCode UseDefaultSettings() = 0; |
sanjeevr@chromium.org | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 116 | |
Lei Zhang | d32b55d | 2022-05-12 18:52:32 | [diff] [blame] | 117 | // Updates the context with PDF printer settings. The PDF settings are |
| 118 | // guaranteed to be valid. |
| 119 | void UsePdfSettings(); |
alekseys@chromium.org | 2dafb8b | 2014-05-29 16:32:48 | [diff] [blame] | 120 | |
vitalybuka@chromium.org | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 121 | // Returns paper size to be used for PDF or Cloud Print in device units. |
| 122 | virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0; |
| 123 | |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 124 | // Updates printer settings. |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 125 | virtual mojom::ResultCode UpdatePrinterSettings( |
| 126 | const PrinterSettings& printer_settings) = 0; |
aayushkumar@chromium.org | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 127 | |
Lei Zhang | 4bc85297 | 2022-04-12 20:22:19 | [diff] [blame] | 128 | // Updates Print Settings. `job_settings` contains all print job settings |
| 129 | // information. |
| 130 | mojom::ResultCode UpdatePrintSettings(base::Value::Dict job_settings); |
kmadhusu@chromium.org | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 131 | |
Xiaohan Wang | e3d00dd6 | 2022-01-08 02:33:28 | [diff] [blame] | 132 | #if BUILDFLAG(IS_CHROMEOS) |
Vladislav Kuzkokov | e219910 | 2018-02-20 16:25:13 | [diff] [blame] | 133 | // Updates Print Settings. |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 134 | mojom::ResultCode UpdatePrintSettingsFromPOD( |
Vladislav Kuzkokov | e219910 | 2018-02-20 16:25:13 | [diff] [blame] | 135 | std::unique_ptr<PrintSettings> job_settings); |
| 136 | #endif |
| 137 | |
Andy Phan | 83a2c25 | 2023-04-13 22:29:40 | [diff] [blame] | 138 | // Sets the print settings to `settings`. |
| 139 | void SetPrintSettings(const PrintSettings& settings); |
| 140 | |
Andy Phan | 83a2c25 | 2023-04-13 22:29:40 | [diff] [blame] | 141 | // Set the printable area in print settings to be the default printable area. |
| 142 | // Intended to be used only for virtual printers. |
| 143 | void SetDefaultPrintableAreaForVirtualPrinters(); |
| 144 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | // Does platform specific setup of the printer before the printing. Signal the |
| 146 | // printer that a document is about to be spooled. |
| 147 | // Warning: This function enters a message loop. That may cause side effects |
| 148 | // like IPC message processing! Some printers have side-effects on this call |
| 149 | // like virtual printers that ask the user for the path of the saved document; |
| 150 | // for example a PDF printer. |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 151 | virtual mojom::ResultCode NewDocument( |
| 152 | const std::u16string& document_name) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | |
Xiaohan Wang | e3d00dd6 | 2022-01-08 02:33:28 | [diff] [blame] | 154 | #if BUILDFLAG(IS_WIN) |
Alan Screen | 4e284e9 | 2021-12-04 00:38:29 | [diff] [blame] | 155 | // Renders a page. |
| 156 | virtual mojom::ResultCode RenderPage(const PrintedPage& page, |
| 157 | const PageSetup& page_setup) = 0; |
| 158 | #endif |
| 159 | |
Alan Screen | 4e284e9 | 2021-12-04 00:38:29 | [diff] [blame] | 160 | // Prints the document contained in `metafile`. |
| 161 | virtual mojom::ResultCode PrintDocument(const MetafilePlayer& metafile, |
| 162 | const PrintSettings& settings, |
| 163 | uint32_t num_pages) = 0; |
| 164 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | // Closes the printing job. After this call the object is ready to start a new |
| 166 | // document. |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 167 | virtual mojom::ResultCode DocumentDone() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | |
sverrir@google.com | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 169 | // Cancels printing. Can be used in a multi-threaded context. Takes effect |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | // immediately. |
jhawkins@chromium.org | 51e8d93 | 2010-10-06 22:21:17 | [diff] [blame] | 171 | virtual void Cancel() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | |
jhawkins@chromium.org | 51e8d93 | 2010-10-06 22:21:17 | [diff] [blame] | 173 | // Releases the native printing context. |
| 174 | virtual void ReleaseContext() = 0; |
| 175 | |
| 176 | // Returns the native context used to print. |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 177 | virtual printing::NativeDrawingContext context() const = 0; |
jhawkins@chromium.org | 51e8d93 | 2010-10-06 22:21:17 | [diff] [blame] | 178 | |
Xiaohan Wang | e3d00dd6 | 2022-01-08 02:33:28 | [diff] [blame] | 179 | #if BUILDFLAG(IS_WIN) |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 180 | // Initializes with predefined settings. |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 181 | virtual mojom::ResultCode InitWithSettingsForTest( |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 182 | std::unique_ptr<PrintSettings> settings) = 0; |
| 183 | #endif |
| 184 | |
| 185 | // Creates an instance of this object. |
Alan Screen | 96a12395 | 2023-11-06 21:53:16 | [diff] [blame] | 186 | static std::unique_ptr<PrintingContext> Create( |
| 187 | Delegate* delegate, |
| 188 | ProcessBehavior process_behavior); |
jhawkins@chromium.org | 51e8d93 | 2010-10-06 22:21:17 | [diff] [blame] | 189 | |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 190 | // Test method for generating printing contexts for testing. This overrides |
| 191 | // the platform-specific implementations of CreateImpl(). |
| 192 | static void SetPrintingContextFactoryForTest( |
| 193 | PrintingContextFactoryForTest* factory); |
| 194 | |
Alan Screen | 96a12395 | 2023-11-06 21:53:16 | [diff] [blame] | 195 | // Determine process behavior, which can determine if system calls should be |
| 196 | // made and if certain extra code paths should be followed to support |
| 197 | // out-of-process printing. |
| 198 | ProcessBehavior process_behavior() const { return process_behavior_; } |
| 199 | |
Julie Jeongeun Kim | cb00d1ab | 2020-08-14 07:43:32 | [diff] [blame] | 200 | void set_margin_type(mojom::MarginType type); |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 201 | void set_is_modifiable(bool is_modifiable); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 203 | const PrintSettings& settings() const; |
| 204 | |
Vladislav Kuzkokov | 56e6bbbd | 2019-08-21 18:48:18 | [diff] [blame] | 205 | std::unique_ptr<PrintSettings> TakeAndResetSettings(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | |
Alan Screen | 46d031eb | 2022-01-05 23:15:29 | [diff] [blame] | 207 | bool PrintingAborted() const { return abort_printing_; } |
| 208 | |
skau | 3fadb49 | 2017-02-15 19:23:37 | [diff] [blame] | 209 | int job_id() const { return job_id_; } |
| 210 | |
Alan Screen | 8740c25 | 2024-01-20 02:47:14 | [diff] [blame] | 211 | #if BUILDFLAG(ENABLE_OOP_PRINTING) |
| 212 | // Override the job ID for this context. Can only be called to update the |
| 213 | // value for a `PrintingContext` in the browser process with a value that was |
| 214 | // determined by a PrintBackend service. |
| 215 | void SetJobId(int job_id); |
| 216 | #endif |
| 217 | |
Vladislav Kuzkokov | 7511633 | 2019-08-20 15:40:14 | [diff] [blame] | 218 | protected: |
Alan Screen | 96a12395 | 2023-11-06 21:53:16 | [diff] [blame] | 219 | PrintingContext(Delegate* delegate, ProcessBehavior process_behavior); |
Vladislav Kuzkokov | 7511633 | 2019-08-20 15:40:14 | [diff] [blame] | 220 | |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 221 | // Creates an instance of this object. Implementers of this interface should |
| 222 | // implement this method to create an object of their implementation. |
Alan Screen | 96a12395 | 2023-11-06 21:53:16 | [diff] [blame] | 223 | static std::unique_ptr<PrintingContext> CreateImpl( |
| 224 | Delegate* delegate, |
| 225 | ProcessBehavior process_behavior); |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 226 | |
Vladislav Kuzkokov | 56e6bbbd | 2019-08-21 18:48:18 | [diff] [blame] | 227 | // Reinitializes the settings for object reuse. |
| 228 | void ResetSettings(); |
| 229 | |
sverrir@google.com | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 230 | // Does bookkeeping when an error occurs. |
Alan Screen | 39279cf0 | 2021-09-22 07:16:41 | [diff] [blame] | 231 | virtual mojom::ResultCode OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | // Complete print context settings. |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 234 | std::unique_ptr<PrintSettings> settings_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 236 | // Printing context delegate. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 237 | const raw_ptr<Delegate> delegate_; |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 238 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 239 | // Is a print job being done. |
| 240 | volatile bool in_print_job_; |
| 241 | |
| 242 | // Did the user cancel the print job. |
| 243 | volatile bool abort_printing_; |
| 244 | |
Alan Screen | 348530fe | 2024-01-20 02:47:14 | [diff] [blame] | 245 | // The job id for the current job used by the underlying platform. |
| 246 | // The value is `kNoPrintJobId` if no jobs are active or if the platform |
| 247 | // or test does not require passing such an ID for extra print job |
| 248 | // management. |
| 249 | int job_id_ = kNoPrintJobId; |
Alan Screen | e9ed54a | 2021-11-24 18:22:46 | [diff] [blame] | 250 | |
| 251 | private: |
Alan Screen | 96a12395 | 2023-11-06 21:53:16 | [diff] [blame] | 252 | const ProcessBehavior process_behavior_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | } // namespace printing |
| 256 | |
sverrir@google.com | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 257 | #endif // PRINTING_PRINTING_CONTEXT_H_ |