Upload upstream chromium 120.0.6099.5
[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   // 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();
118
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;
127
128 #if BUILDFLAG(IS_WIN)
129   // Renders a page.
130   virtual mojom::ResultCode RenderPage(const PrintedPage& page,
131                                        const PageSetup& page_setup) = 0;
132 #endif
133
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;
138
139   // Closes the printing job. After this call the object is ready to start a new
140   // document.
141   virtual mojom::ResultCode DocumentDone() = 0;
142
143   // Cancels printing. Can be used in a multi-threaded context. Takes effect
144   // immediately.
145   virtual void Cancel() = 0;
146
147   // Releases the native printing context.
148   virtual void ReleaseContext() = 0;
149
150   // Returns the native context used to print.
151   virtual printing::NativeDrawingContext context() const = 0;
152
153 #if BUILDFLAG(IS_WIN)
154   // Initializes with predefined settings.
155   virtual mojom::ResultCode InitWithSettingsForTest(
156       std::unique_ptr<PrintSettings> settings) = 0;
157 #endif
158
159   // Creates an instance of this object.
160   static std::unique_ptr<PrintingContext> Create(Delegate* delegate,
161                                                  bool skip_system_calls);
162
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);
167
168   void set_margin_type(mojom::MarginType type);
169   void set_is_modifiable(bool is_modifiable);
170
171   const PrintSettings& settings() const;
172
173   std::unique_ptr<PrintSettings> TakeAndResetSettings();
174
175   bool PrintingAborted() const { return abort_printing_; }
176
177   int job_id() const { return job_id_; }
178
179  protected:
180   explicit PrintingContext(Delegate* delegate);
181
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);
186
187   // Reinitializes the settings for object reuse.
188   void ResetSettings();
189
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_;
194 #else
195     return false;
196 #endif
197   }
198
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; }
203 #endif
204
205   // Does bookkeeping when an error occurs.
206   virtual mojom::ResultCode OnError();
207
208   // Complete print context settings.
209   std::unique_ptr<PrintSettings> settings_;
210
211   // Printing context delegate.
212   const raw_ptr<Delegate> delegate_;
213
214   // Is a print job being done.
215   volatile bool in_print_job_;
216
217   // Did the user cancel the print job.
218   volatile bool abort_printing_;
219
220   // The job id for the current job. The value is 0 if no jobs are active.
221   int job_id_;
222
223  private:
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;
228 #endif
229 };
230
231 }  // namespace printing
232
233 #endif  // PRINTING_PRINTING_CONTEXT_H_