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