[go: nahoru, domu]

blob: 63f170c95050416c595e62f4c460c4cd6b7dbd1c [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:281// Copyright 2011 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
sverrir@google.com8ff1d422009-07-07 21:31:395#ifndef PRINTING_PRINTING_CONTEXT_H_
6#define PRINTING_PRINTING_CONTEXT_H_
initial.commit09911bf2008-07-26 23:55:297
dchengc3df9ba2016-04-07 23:09:328#include <memory>
initial.commit09911bf2008-07-26 23:55:299#include <string>
10
Avi Drissman821ca3092023-01-11 22:42:1511#include "base/functional/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Vladislav Kuzkokov490ab0c2019-01-16 11:21:5713#include "base/values.h"
Lei Zhangd6879612019-02-07 18:45:5314#include "build/build_config.h"
Alan Screene9ed54a2021-11-24 18:22:4615#include "printing/buildflags/buildflags.h"
Julie Jeongeun Kimcb00d1ab2020-08-14 07:43:3216#include "printing/mojom/print.mojom.h"
Thiago Farina45e319532017-03-23 02:44:4917#include "printing/native_drawing_context.h"
sverrir@google.com8ff1d422009-07-07 21:31:3918#include "printing/print_settings.h"
sail@chromium.org08397d52011-02-05 01:53:3819#include "ui/gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2920
21namespace printing {
22
Alan Screen4e284e92021-12-04 00:38:2923class MetafilePlayer;
Alan Screenf2fbeb7a2021-08-28 05:27:3824class PrintingContextFactoryForTest;
25
Xiaohan Wange3d00dd62022-01-08 02:33:2826#if BUILDFLAG(IS_WIN)
Alan Screen4e284e92021-12-04 00:38:2927class PageSetup;
28class PrintedPage;
29#endif
30
jhawkins@chromium.org51e8d932010-10-06 22:21:1731// 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 Screen9347cae2021-04-27 18:11:2435class COMPONENT_EXPORT(PRINTING) PrintingContext {
initial.commit09911bf2008-07-26 23:55:2936 public:
Vitaly Bukabd7c9812014-08-26 08:57:5437 // Printing context delegate.
38 class Delegate {
39 public:
thestige85e6b62016-08-25 00:00:0640 Delegate() {}
41 virtual ~Delegate() {}
Vitaly Bukabd7c9812014-08-26 08:57:5442
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 Zhanga3fdad442021-11-15 23:55:3350 struct PrinterSettings {
Xiaohan Wange3d00dd62022-01-08 02:33:2851#if BUILDFLAG(IS_MAC)
Lei Zhanga3fdad442021-11-15 23:55:3352 // 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 Wange3d00dd62022-01-08 02:33:2860#if BUILDFLAG(IS_WIN)
Lei Zhanga3fdad442021-11-15 23:55:3361 // If showing the system dialog, the number of pages in the to-be-printed
Lei Zhangf8b5b152021-11-16 01:10:2562 // PDF. Only used on Windows.
Lei Zhanga3fdad442021-11-15 23:55:3363 int page_count;
Lei Zhangf8b5b152021-11-16 01:10:2564#endif
Lei Zhanga3fdad442021-11-15 23:55:3365 };
66
Alan Screen96a123952023-11-06 21:53:1667 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 Screen348530fe2024-01-20 02:47:1489 // 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 Zhangdd3e9b02020-08-19 23:04:4293 PrintingContext(const PrintingContext&) = delete;
94 PrintingContext& operator=(const PrintingContext&) = delete;
jhawkins@chromium.org51e8d932010-10-06 22:21:1795 virtual ~PrintingContext();
initial.commit09911bf2008-07-26 23:55:2996
jhawkins@chromium.orgb7191422010-09-21 19:18:0597 // Callback of AskUserForSettings, used to notify the PrintJobWorker when
98 // print settings are available.
Alan Screen202e49a2021-09-21 06:34:2899 using PrintSettingsCallback = base::OnceCallback<void(mojom::ResultCode)>;
jhawkins@chromium.orgb7191422010-09-21 19:18:05100
initial.commit09911bf2008-07-26 23:55:29101 // Asks the user what printer and format should be used to print. Updates the
jhawkins@chromium.orgb7191422010-09-21 19:18:05102 // 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 Hosseinian3553e272021-04-24 00:51:18105 // On Android, when `is_scripted` is true, calling it initiates a full
dgn4c172eea2014-12-15 21:11:23106 // printing flow from the framework's PrintManager.
107 // (see https://codereview.chromium.org/740983002/)
Vitaly Bukabd7c9812014-08-26 08:57:54108 virtual void AskUserForSettings(int max_pages,
jhawkins@chromium.org51e8d932010-10-06 22:21:17109 bool has_selection,
dgn4c172eea2014-12-15 21:11:23110 bool is_scripted,
Vladislav Kuzkokov48ceab22018-02-14 16:29:28111 PrintSettingsCallback callback) = 0;
thestig@chromium.orgd8254622010-08-13 19:15:46112
initial.commit09911bf2008-07-26 23:55:29113 // Selects the user's default printer and format. Updates the context with the
114 // default device settings.
Alan Screen202e49a2021-09-21 06:34:28115 virtual mojom::ResultCode UseDefaultSettings() = 0;
sanjeevr@chromium.org38bba4f2010-03-12 05:29:07116
Lei Zhangd32b55d2022-05-12 18:52:32117 // Updates the context with PDF printer settings. The PDF settings are
118 // guaranteed to be valid.
119 void UsePdfSettings();
alekseys@chromium.org2dafb8b2014-05-29 16:32:48120
vitalybuka@chromium.org4c9054b2013-11-04 18:34:29121 // Returns paper size to be used for PDF or Cloud Print in device units.
122 virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
123
vitalybuka@chromium.orge5324b52013-10-29 03:16:37124 // Updates printer settings.
Lei Zhanga3fdad442021-11-15 23:55:33125 virtual mojom::ResultCode UpdatePrinterSettings(
126 const PrinterSettings& printer_settings) = 0;
aayushkumar@chromium.org55b23a02011-08-17 23:09:36127
Lei Zhang4bc852972022-04-12 20:22:19128 // Updates Print Settings. `job_settings` contains all print job settings
129 // information.
130 mojom::ResultCode UpdatePrintSettings(base::Value::Dict job_settings);
kmadhusu@chromium.org7868ecab2011-03-05 00:12:53131
Xiaohan Wange3d00dd62022-01-08 02:33:28132#if BUILDFLAG(IS_CHROMEOS)
Vladislav Kuzkokove2199102018-02-20 16:25:13133 // Updates Print Settings.
Alan Screen202e49a2021-09-21 06:34:28134 mojom::ResultCode UpdatePrintSettingsFromPOD(
Vladislav Kuzkokove2199102018-02-20 16:25:13135 std::unique_ptr<PrintSettings> job_settings);
136#endif
137
Andy Phan83a2c252023-04-13 22:29:40138 // Sets the print settings to `settings`.
139 void SetPrintSettings(const PrintSettings& settings);
140
Andy Phan83a2c252023-04-13 22:29:40141 // 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.commit09911bf2008-07-26 23:55:29145 // 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 Screen202e49a2021-09-21 06:34:28151 virtual mojom::ResultCode NewDocument(
152 const std::u16string& document_name) = 0;
initial.commit09911bf2008-07-26 23:55:29153
Xiaohan Wange3d00dd62022-01-08 02:33:28154#if BUILDFLAG(IS_WIN)
Alan Screen4e284e92021-12-04 00:38:29155 // Renders a page.
156 virtual mojom::ResultCode RenderPage(const PrintedPage& page,
157 const PageSetup& page_setup) = 0;
158#endif
159
Alan Screen4e284e92021-12-04 00:38:29160 // 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.commit09911bf2008-07-26 23:55:29165 // Closes the printing job. After this call the object is ready to start a new
166 // document.
Alan Screen202e49a2021-09-21 06:34:28167 virtual mojom::ResultCode DocumentDone() = 0;
initial.commit09911bf2008-07-26 23:55:29168
sverrir@google.com8ff1d422009-07-07 21:31:39169 // Cancels printing. Can be used in a multi-threaded context. Takes effect
initial.commit09911bf2008-07-26 23:55:29170 // immediately.
jhawkins@chromium.org51e8d932010-10-06 22:21:17171 virtual void Cancel() = 0;
initial.commit09911bf2008-07-26 23:55:29172
jhawkins@chromium.org51e8d932010-10-06 22:21:17173 // Releases the native printing context.
174 virtual void ReleaseContext() = 0;
175
176 // Returns the native context used to print.
Nico Weber8e559562017-10-03 01:25:26177 virtual printing::NativeDrawingContext context() const = 0;
jhawkins@chromium.org51e8d932010-10-06 22:21:17178
Xiaohan Wange3d00dd62022-01-08 02:33:28179#if BUILDFLAG(IS_WIN)
Alan Screenf2fbeb7a2021-08-28 05:27:38180 // Initializes with predefined settings.
Alan Screen202e49a2021-09-21 06:34:28181 virtual mojom::ResultCode InitWithSettingsForTest(
Alan Screenf2fbeb7a2021-08-28 05:27:38182 std::unique_ptr<PrintSettings> settings) = 0;
183#endif
184
185 // Creates an instance of this object.
Alan Screen96a123952023-11-06 21:53:16186 static std::unique_ptr<PrintingContext> Create(
187 Delegate* delegate,
188 ProcessBehavior process_behavior);
jhawkins@chromium.org51e8d932010-10-06 22:21:17189
Alan Screenf2fbeb7a2021-08-28 05:27:38190 // 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 Screen96a123952023-11-06 21:53:16195 // 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 Kimcb00d1ab2020-08-14 07:43:32200 void set_margin_type(mojom::MarginType type);
thestige85e6b62016-08-25 00:00:06201 void set_is_modifiable(bool is_modifiable);
initial.commit09911bf2008-07-26 23:55:29202
Vladislav Kuzkokov19998222019-08-12 14:26:09203 const PrintSettings& settings() const;
204
Vladislav Kuzkokov56e6bbbd2019-08-21 18:48:18205 std::unique_ptr<PrintSettings> TakeAndResetSettings();
initial.commit09911bf2008-07-26 23:55:29206
Alan Screen46d031eb2022-01-05 23:15:29207 bool PrintingAborted() const { return abort_printing_; }
208
skau3fadb492017-02-15 19:23:37209 int job_id() const { return job_id_; }
210
Alan Screen8740c252024-01-20 02:47:14211#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 Kuzkokov75116332019-08-20 15:40:14218 protected:
Alan Screen96a123952023-11-06 21:53:16219 PrintingContext(Delegate* delegate, ProcessBehavior process_behavior);
Vladislav Kuzkokov75116332019-08-20 15:40:14220
Alan Screenf2fbeb7a2021-08-28 05:27:38221 // Creates an instance of this object. Implementers of this interface should
222 // implement this method to create an object of their implementation.
Alan Screen96a123952023-11-06 21:53:16223 static std::unique_ptr<PrintingContext> CreateImpl(
224 Delegate* delegate,
225 ProcessBehavior process_behavior);
Alan Screenf2fbeb7a2021-08-28 05:27:38226
Vladislav Kuzkokov56e6bbbd2019-08-21 18:48:18227 // Reinitializes the settings for object reuse.
228 void ResetSettings();
229
sverrir@google.comc8ad40c2009-06-08 17:05:21230 // Does bookkeeping when an error occurs.
Alan Screen39279cf02021-09-22 07:16:41231 virtual mojom::ResultCode OnError();
initial.commit09911bf2008-07-26 23:55:29232
initial.commit09911bf2008-07-26 23:55:29233 // Complete print context settings.
Vladislav Kuzkokov19998222019-08-12 14:26:09234 std::unique_ptr<PrintSettings> settings_;
initial.commit09911bf2008-07-26 23:55:29235
Vitaly Bukabd7c9812014-08-26 08:57:54236 // Printing context delegate.
Keishi Hattori0e45c022021-11-27 09:25:52237 const raw_ptr<Delegate> delegate_;
Vitaly Bukabd7c9812014-08-26 08:57:54238
initial.commit09911bf2008-07-26 23:55:29239 // 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 Screen348530fe2024-01-20 02:47:14245 // 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 Screene9ed54a2021-11-24 18:22:46250
251 private:
Alan Screen96a123952023-11-06 21:53:16252 const ProcessBehavior process_behavior_;
initial.commit09911bf2008-07-26 23:55:29253};
254
255} // namespace printing
256
sverrir@google.com8ff1d422009-07-07 21:31:39257#endif // PRINTING_PRINTING_CONTEXT_H_