Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / printing / printing_context.h
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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/values.h"
13 #include "build/build_config.h"
14 #include "build/chromeos_buildflags.h"
15 #include "printing/mojom/print.mojom.h"
16 #include "printing/native_drawing_context.h"
17 #include "printing/print_settings.h"
18 #include "ui/gfx/native_widget_types.h"
19
20 namespace printing {
21
22 // An abstraction of a printer context, implemented by objects that describe the
23 // user selected printing context. This includes the OS-dependent UI to ask the
24 // user about the print settings. Concrete implementations directly talk to the
25 // printer and manage the document and page breaks.
26 class COMPONENT_EXPORT(PRINTING) PrintingContext {
27  public:
28   // Printing context delegate.
29   class Delegate {
30    public:
31     Delegate() {}
32     virtual ~Delegate() {}
33
34     // Returns parent view to use for modal dialogs.
35     virtual gfx::NativeView GetParentView() = 0;
36
37     // Returns application locale.
38     virtual std::string GetAppLocale() = 0;
39   };
40
41   // Tri-state result for user behavior-dependent functions.
42   enum Result {
43     OK,
44     CANCEL,
45     FAILED,
46   };
47
48   PrintingContext(const PrintingContext&) = delete;
49   PrintingContext& operator=(const PrintingContext&) = delete;
50   virtual ~PrintingContext();
51
52   // Callback of AskUserForSettings, used to notify the PrintJobWorker when
53   // print settings are available.
54   using PrintSettingsCallback = base::OnceCallback<void(Result)>;
55
56   // Asks the user what printer and format should be used to print. Updates the
57   // context with the select device settings. The result of the call is returned
58   // in the callback. This is necessary for Linux, which only has an
59   // asynchronous printing API.
60   // On Android, when `is_scripted` is true, calling it initiates a full
61   // printing flow from the framework's PrintManager.
62   // (see https://codereview.chromium.org/740983002/)
63   virtual void AskUserForSettings(int max_pages,
64                                   bool has_selection,
65                                   bool is_scripted,
66                                   PrintSettingsCallback callback) = 0;
67
68   // Selects the user's default printer and format. Updates the context with the
69   // default device settings.
70   virtual Result UseDefaultSettings() = 0;
71
72   // Updates the context with PDF printer settings.
73   Result UsePdfSettings();
74
75   // Returns paper size to be used for PDF or Cloud Print in device units.
76   virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
77
78   // Updates printer settings.
79   // `external_preview` is true if pdf is going to be opened in external
80   // preview. Used by MacOS only now to open Preview.app.
81   virtual Result UpdatePrinterSettings(bool external_preview,
82                                        bool show_system_dialog,
83                                        int page_count) = 0;
84
85   // Updates Print Settings. `job_settings` contains all print job
86   // settings information.
87   Result UpdatePrintSettings(base::Value job_settings);
88
89 #if defined(OS_CHROMEOS)
90   // Updates Print Settings.
91   Result UpdatePrintSettingsFromPOD(
92       std::unique_ptr<PrintSettings> job_settings);
93 #endif
94
95   // Does platform specific setup of the printer before the printing. Signal the
96   // printer that a document is about to be spooled.
97   // Warning: This function enters a message loop. That may cause side effects
98   // like IPC message processing! Some printers have side-effects on this call
99   // like virtual printers that ask the user for the path of the saved document;
100   // for example a PDF printer.
101   virtual Result NewDocument(const std::u16string& document_name) = 0;
102
103   // Starts a new page.
104   virtual Result NewPage() = 0;
105
106   // Closes the printed page.
107   virtual Result PageDone() = 0;
108
109   // Closes the printing job. After this call the object is ready to start a new
110   // document.
111   virtual Result DocumentDone() = 0;
112
113   // Cancels printing. Can be used in a multi-threaded context. Takes effect
114   // immediately.
115   virtual void Cancel() = 0;
116
117   // Releases the native printing context.
118   virtual void ReleaseContext() = 0;
119
120   // Returns the native context used to print.
121   virtual printing::NativeDrawingContext context() const = 0;
122
123   // Creates an instance of this object. Implementers of this interface should
124   // implement this method to create an object of their implementation.
125   static std::unique_ptr<PrintingContext> Create(Delegate* delegate);
126
127   void set_margin_type(mojom::MarginType type);
128   void set_is_modifiable(bool is_modifiable);
129
130   const PrintSettings& settings() const;
131
132   std::unique_ptr<PrintSettings> TakeAndResetSettings();
133
134   int job_id() const { return job_id_; }
135
136  protected:
137   explicit PrintingContext(Delegate* delegate);
138
139   // Reinitializes the settings for object reuse.
140   void ResetSettings();
141
142   // Does bookkeeping when an error occurs.
143   PrintingContext::Result OnError();
144
145   // Complete print context settings.
146   std::unique_ptr<PrintSettings> settings_;
147
148   // Printing context delegate.
149   Delegate* const delegate_;
150
151   // Is a print job being done.
152   volatile bool in_print_job_;
153
154   // Did the user cancel the print job.
155   volatile bool abort_printing_;
156
157   // The job id for the current job. The value is 0 if no jobs are active.
158   int job_id_;
159 };
160
161 }  // namespace printing
162
163 #endif  // PRINTING_PRINTING_CONTEXT_H_