Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / printing / printing_context.h
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.
4
5 #ifndef PRINTING_PRINTING_CONTEXT_H_
6 #define PRINTING_PRINTING_CONTEXT_H_
7
8 #include <memory>
9 #include <string>
10
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"
20
21 namespace printing {
22
23 class MetafilePlayer;
24 class PrintingContextFactoryForTest;
25
26 #if BUILDFLAG(IS_WIN)
27 class PageSetup;
28 class PrintedPage;
29 #endif
30
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 {
36  public:
37   // Printing context delegate.
38   class Delegate {
39    public:
40     Delegate() {}
41     virtual ~Delegate() {}
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
50   struct PrinterSettings {
51 #if BUILDFLAG(IS_MAC)
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
60 #if BUILDFLAG(IS_WIN)
61     // If showing the system dialog, the number of pages in the to-be-printed
62     // PDF. Only used on Windows.
63     int page_count;
64 #endif
65   };
66
67   PrintingContext(const PrintingContext&) = delete;
68   PrintingContext& operator=(const PrintingContext&) = delete;
69   virtual ~PrintingContext();
70
71   // Callback of AskUserForSettings, used to notify the PrintJobWorker when
72   // print settings are available.
73   using PrintSettingsCallback = base::OnceCallback<void(mojom::ResultCode)>;
74
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,
83                                   bool has_selection,
84                                   bool is_scripted,
85                                   PrintSettingsCallback callback) = 0;
86
87   // Selects the user's default printer and format. Updates the context with the
88   // default device settings.
89   virtual mojom::ResultCode UseDefaultSettings() = 0;
90
91   // Updates the context with PDF printer settings. The PDF settings are
92   // guaranteed to be valid.
93   void UsePdfSettings();
94
95   // Returns paper size to be used for PDF or Cloud Print in device units.
96   virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
97
98   // Updates printer settings.
99   virtual mojom::ResultCode UpdatePrinterSettings(
100       const PrinterSettings& printer_settings) = 0;
101
102   // Updates Print Settings. `job_settings` contains all print job settings
103   // information.
104   mojom::ResultCode UpdatePrintSettings(base::Value::Dict job_settings);
105
106 #if BUILDFLAG(IS_CHROMEOS)
107   // Updates Print Settings.
108   mojom::ResultCode UpdatePrintSettingsFromPOD(
109       std::unique_ptr<PrintSettings> job_settings);
110 #endif
111
112   // Sets the print settings to `settings`.
113   void SetPrintSettings(const PrintSettings& settings);
114
115   // Applies the print settings to this context.  Intended to be used only by
116   // the Print Backend service process.
117   void ApplyPrintSettings(const PrintSettings& settings);
118
119   // Set the printable area in print settings to be the default printable area.
120   // Intended to be used only for virtual printers.
121   void SetDefaultPrintableAreaForVirtualPrinters();
122
123   // Does platform specific setup of the printer before the printing. Signal the
124   // printer that a document is about to be spooled.
125   // Warning: This function enters a message loop. That may cause side effects
126   // like IPC message processing! Some printers have side-effects on this call
127   // like virtual printers that ask the user for the path of the saved document;
128   // for example a PDF printer.
129   virtual mojom::ResultCode NewDocument(
130       const std::u16string& document_name) = 0;
131
132 #if BUILDFLAG(IS_WIN)
133   // Renders a page.
134   virtual mojom::ResultCode RenderPage(const PrintedPage& page,
135                                        const PageSetup& page_setup) = 0;
136 #endif
137
138   // Prints the document contained in `metafile`.
139   virtual mojom::ResultCode PrintDocument(const MetafilePlayer& metafile,
140                                           const PrintSettings& settings,
141                                           uint32_t num_pages) = 0;
142
143   // Closes the printing job. After this call the object is ready to start a new
144   // document.
145   virtual mojom::ResultCode DocumentDone() = 0;
146
147   // Cancels printing. Can be used in a multi-threaded context. Takes effect
148   // immediately.
149   virtual void Cancel() = 0;
150
151   // Releases the native printing context.
152   virtual void ReleaseContext() = 0;
153
154   // Returns the native context used to print.
155   virtual printing::NativeDrawingContext context() const = 0;
156
157 #if BUILDFLAG(IS_WIN)
158   // Initializes with predefined settings.
159   virtual mojom::ResultCode InitWithSettingsForTest(
160       std::unique_ptr<PrintSettings> settings) = 0;
161 #endif
162
163   // Creates an instance of this object.
164   static std::unique_ptr<PrintingContext> Create(Delegate* delegate,
165                                                  bool skip_system_calls);
166
167   // Test method for generating printing contexts for testing.  This overrides
168   // the platform-specific implementations of CreateImpl().
169   static void SetPrintingContextFactoryForTest(
170       PrintingContextFactoryForTest* factory);
171
172   void set_margin_type(mojom::MarginType type);
173   void set_is_modifiable(bool is_modifiable);
174
175   const PrintSettings& settings() const;
176
177   std::unique_ptr<PrintSettings> TakeAndResetSettings();
178
179   bool PrintingAborted() const { return abort_printing_; }
180
181   int job_id() const { return job_id_; }
182
183  protected:
184   explicit PrintingContext(Delegate* delegate);
185
186   // Creates an instance of this object. Implementers of this interface should
187   // implement this method to create an object of their implementation.
188   static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
189                                                      bool skip_system_calls);
190
191   // Reinitializes the settings for object reuse.
192   void ResetSettings();
193
194   // Determine if system calls should be skipped by this instance.
195   bool skip_system_calls() const {
196 #if BUILDFLAG(ENABLE_OOP_PRINTING)
197     return skip_system_calls_;
198 #else
199     return false;
200 #endif
201   }
202
203 #if BUILDFLAG(ENABLE_OOP_PRINTING)
204   // Make the one-way adjustment to have all system calls skipped by this
205   // `PrintingContext` instance.
206   void set_skip_system_calls() { skip_system_calls_ = true; }
207 #endif
208
209   // Does bookkeeping when an error occurs.
210   virtual mojom::ResultCode OnError();
211
212   // Complete print context settings.
213   std::unique_ptr<PrintSettings> settings_;
214
215   // Printing context delegate.
216   const raw_ptr<Delegate> delegate_;
217
218   // Is a print job being done.
219   volatile bool in_print_job_;
220
221   // Did the user cancel the print job.
222   volatile bool abort_printing_;
223
224   // The job id for the current job. The value is 0 if no jobs are active.
225   int job_id_;
226
227  private:
228 #if BUILDFLAG(ENABLE_OOP_PRINTING)
229   // If this instance of PrintingContext should skip making any system calls
230   // to the operating system.
231   bool skip_system_calls_ = false;
232 #endif
233 };
234
235 }  // namespace printing
236
237 #endif  // PRINTING_PRINTING_CONTEXT_H_