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/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 // Applies the print settings to this context. Intended to be used only by
113 // the Print Backend service process.
114 void ApplyPrintSettings(const PrintSettings& settings);
116 // Does platform specific setup of the printer before the printing. Signal the
117 // printer that a document is about to be spooled.
118 // Warning: This function enters a message loop. That may cause side effects
119 // like IPC message processing! Some printers have side-effects on this call
120 // like virtual printers that ask the user for the path of the saved document;
121 // for example a PDF printer.
122 virtual mojom::ResultCode NewDocument(
123 const std::u16string& document_name) = 0;
125 #if BUILDFLAG(IS_WIN)
127 virtual mojom::ResultCode RenderPage(const PrintedPage& page,
128 const PageSetup& page_setup) = 0;
131 // Prints the document contained in `metafile`.
132 virtual mojom::ResultCode PrintDocument(const MetafilePlayer& metafile,
133 const PrintSettings& settings,
134 uint32_t num_pages) = 0;
136 // Closes the printing job. After this call the object is ready to start a new
138 virtual mojom::ResultCode DocumentDone() = 0;
140 // Cancels printing. Can be used in a multi-threaded context. Takes effect
142 virtual void Cancel() = 0;
144 // Releases the native printing context.
145 virtual void ReleaseContext() = 0;
147 // Returns the native context used to print.
148 virtual printing::NativeDrawingContext context() const = 0;
150 #if BUILDFLAG(IS_WIN)
151 // Initializes with predefined settings.
152 virtual mojom::ResultCode InitWithSettingsForTest(
153 std::unique_ptr<PrintSettings> settings) = 0;
156 // Creates an instance of this object.
157 static std::unique_ptr<PrintingContext> Create(Delegate* delegate,
158 bool skip_system_calls);
160 // Test method for generating printing contexts for testing. This overrides
161 // the platform-specific implementations of CreateImpl().
162 static void SetPrintingContextFactoryForTest(
163 PrintingContextFactoryForTest* factory);
165 void set_margin_type(mojom::MarginType type);
166 void set_is_modifiable(bool is_modifiable);
168 const PrintSettings& settings() const;
170 std::unique_ptr<PrintSettings> TakeAndResetSettings();
172 bool PrintingAborted() const { return abort_printing_; }
174 int job_id() const { return job_id_; }
177 explicit PrintingContext(Delegate* delegate);
179 // Creates an instance of this object. Implementers of this interface should
180 // implement this method to create an object of their implementation.
181 static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
182 bool skip_system_calls);
184 // Reinitializes the settings for object reuse.
185 void ResetSettings();
187 // Determine if system calls should be skipped by this instance.
188 bool skip_system_calls() const {
189 #if BUILDFLAG(ENABLE_OOP_PRINTING)
190 return skip_system_calls_;
196 #if BUILDFLAG(ENABLE_OOP_PRINTING)
197 // Make the one-way adjustment to have all system calls skipped by this
198 // `PrintingContext` instance.
199 void set_skip_system_calls() { skip_system_calls_ = true; }
202 // Does bookkeeping when an error occurs.
203 virtual mojom::ResultCode OnError();
205 void SetDefaultPrintableAreaForVirtualPrinters();
207 // Complete print context settings.
208 std::unique_ptr<PrintSettings> settings_;
210 // Printing context delegate.
211 const raw_ptr<Delegate> delegate_;
213 // Is a print job being done.
214 volatile bool in_print_job_;
216 // Did the user cancel the print job.
217 volatile bool abort_printing_;
219 // The job id for the current job. The value is 0 if no jobs are active.
223 #if BUILDFLAG(ENABLE_OOP_PRINTING)
224 // If this instance of PrintingContext should skip making any system calls
225 // to the operating system.
226 bool skip_system_calls_ = false;
230 } // namespace printing
232 #endif // PRINTING_PRINTING_CONTEXT_H_