1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef PRINTING_PRINTING_CONTEXT_H_
6 #define PRINTING_PRINTING_CONTEXT_H_
11 #include "base/functional/callback.h"
12 #include "base/memory/raw_ptr.h"
13 #include "base/values.h"
14 #include "build/build_config.h"
15 #include "printing/buildflags/buildflags.h"
16 #include "printing/mojom/print.mojom.h"
17 #include "printing/native_drawing_context.h"
18 #include "printing/print_settings.h"
19 #include "ui/gfx/native_widget_types.h"
24 class PrintingContextFactoryForTest;
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.
35 class COMPONENT_EXPORT(PRINTING) PrintingContext {
37 // Printing context delegate.
41 virtual ~Delegate() {}
43 // Returns parent view to use for modal dialogs.
44 virtual gfx::NativeView GetParentView() = 0;
46 // Returns application locale.
47 virtual std::string GetAppLocale() = 0;
50 struct PrinterSettings {
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;
57 // Whether to show the system dialog.
58 bool show_system_dialog;
61 // If showing the system dialog, the number of pages in the to-be-printed
62 // PDF. Only used on Windows.
67 PrintingContext(const PrintingContext&) = delete;
68 PrintingContext& operator=(const PrintingContext&) = delete;
69 virtual ~PrintingContext();
71 // Callback of AskUserForSettings, used to notify the PrintJobWorker when
72 // print settings are available.
73 using PrintSettingsCallback = base::OnceCallback<void(mojom::ResultCode)>;
75 // Asks the user what printer and format should be used to print. Updates the
76 // context with the select device settings. The result of the call is returned
77 // in the callback. This is necessary for Linux, which only has an
78 // asynchronous printing API.
79 // On Android, when `is_scripted` is true, calling it initiates a full
80 // printing flow from the framework's PrintManager.
81 // (see https://codereview.chromium.org/740983002/)
82 virtual void AskUserForSettings(int max_pages,
85 PrintSettingsCallback callback) = 0;
87 // Selects the user's default printer and format. Updates the context with the
88 // default device settings.
89 virtual mojom::ResultCode UseDefaultSettings() = 0;
91 // Updates the context with PDF printer settings. The PDF settings are
92 // guaranteed to be valid.
93 void UsePdfSettings();
95 // Returns paper size to be used for PDF or Cloud Print in device units.
96 virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
98 // Updates printer settings.
99 virtual mojom::ResultCode UpdatePrinterSettings(
100 const PrinterSettings& printer_settings) = 0;
102 // Updates Print Settings. `job_settings` contains all print job settings
104 mojom::ResultCode UpdatePrintSettings(base::Value::Dict job_settings);
106 #if BUILDFLAG(IS_CHROMEOS)
107 // Updates Print Settings.
108 mojom::ResultCode UpdatePrintSettingsFromPOD(
109 std::unique_ptr<PrintSettings> job_settings);
112 // Sets the print settings to `settings`.
113 void SetPrintSettings(const PrintSettings& settings);
115 // Set the printable area in print settings to be the default printable area.
116 // Intended to be used only for virtual printers.
117 void SetDefaultPrintableAreaForVirtualPrinters();
119 // Does platform specific setup of the printer before the printing. Signal the
120 // printer that a document is about to be spooled.
121 // Warning: This function enters a message loop. That may cause side effects
122 // like IPC message processing! Some printers have side-effects on this call
123 // like virtual printers that ask the user for the path of the saved document;
124 // for example a PDF printer.
125 virtual mojom::ResultCode NewDocument(
126 const std::u16string& document_name) = 0;
128 #if BUILDFLAG(IS_WIN)
130 virtual mojom::ResultCode RenderPage(const PrintedPage& page,
131 const PageSetup& page_setup) = 0;
134 // Prints the document contained in `metafile`.
135 virtual mojom::ResultCode PrintDocument(const MetafilePlayer& metafile,
136 const PrintSettings& settings,
137 uint32_t num_pages) = 0;
139 // Closes the printing job. After this call the object is ready to start a new
141 virtual mojom::ResultCode DocumentDone() = 0;
143 // Cancels printing. Can be used in a multi-threaded context. Takes effect
145 virtual void Cancel() = 0;
147 // Releases the native printing context.
148 virtual void ReleaseContext() = 0;
150 // Returns the native context used to print.
151 virtual printing::NativeDrawingContext context() const = 0;
153 #if BUILDFLAG(IS_WIN)
154 // Initializes with predefined settings.
155 virtual mojom::ResultCode InitWithSettingsForTest(
156 std::unique_ptr<PrintSettings> settings) = 0;
159 // Creates an instance of this object.
160 static std::unique_ptr<PrintingContext> Create(Delegate* delegate,
161 bool skip_system_calls);
163 // Test method for generating printing contexts for testing. This overrides
164 // the platform-specific implementations of CreateImpl().
165 static void SetPrintingContextFactoryForTest(
166 PrintingContextFactoryForTest* factory);
168 void set_margin_type(mojom::MarginType type);
169 void set_is_modifiable(bool is_modifiable);
171 const PrintSettings& settings() const;
173 std::unique_ptr<PrintSettings> TakeAndResetSettings();
175 bool PrintingAborted() const { return abort_printing_; }
177 int job_id() const { return job_id_; }
180 explicit PrintingContext(Delegate* delegate);
182 // Creates an instance of this object. Implementers of this interface should
183 // implement this method to create an object of their implementation.
184 static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
185 bool skip_system_calls);
187 // Reinitializes the settings for object reuse.
188 void ResetSettings();
190 // Determine if system calls should be skipped by this instance.
191 bool skip_system_calls() const {
192 #if BUILDFLAG(ENABLE_OOP_PRINTING)
193 return skip_system_calls_;
199 #if BUILDFLAG(ENABLE_OOP_PRINTING)
200 // Make the one-way adjustment to have all system calls skipped by this
201 // `PrintingContext` instance.
202 void set_skip_system_calls() { skip_system_calls_ = true; }
205 // Does bookkeeping when an error occurs.
206 virtual mojom::ResultCode OnError();
208 // Complete print context settings.
209 std::unique_ptr<PrintSettings> settings_;
211 // Printing context delegate.
212 const raw_ptr<Delegate> delegate_;
214 // Is a print job being done.
215 volatile bool in_print_job_;
217 // Did the user cancel the print job.
218 volatile bool abort_printing_;
220 // The job id for the current job. The value is 0 if no jobs are active.
224 #if BUILDFLAG(ENABLE_OOP_PRINTING)
225 // If this instance of PrintingContext should skip making any system calls
226 // to the operating system.
227 bool skip_system_calls_ = false;
231 } // namespace printing
233 #endif // PRINTING_PRINTING_CONTEXT_H_