Upload upstream chromium 76.0.3809.146
[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/macros.h"
13 #include "base/strings/string16.h"
14 #include "base/values.h"
15 #include "build/build_config.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 PRINTING_EXPORT 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   virtual ~PrintingContext();
49
50   // Callback of AskUserForSettings, used to notify the PrintJobWorker when
51   // print settings are available.
52   using PrintSettingsCallback = base::OnceCallback<void(Result)>;
53
54   // Asks the user what printer and format should be used to print. Updates the
55   // context with the select device settings. The result of the call is returned
56   // in the callback. This is necessary for Linux, which only has an
57   // asynchronous printing API.
58   // On Android, when |is_scripted| is true, calling it initiates a full
59   // printing flow from the framework's PrintManager.
60   // (see https://codereview.chromium.org/740983002/)
61   virtual void AskUserForSettings(int max_pages,
62                                   bool has_selection,
63                                   bool is_scripted,
64                                   PrintSettingsCallback callback) = 0;
65
66   // Selects the user's default printer and format. Updates the context with the
67   // default device settings.
68   virtual Result UseDefaultSettings() = 0;
69
70   // Updates the context with PDF printer settings.
71   Result UsePdfSettings();
72
73   // Returns paper size to be used for PDF or Cloud Print in device units.
74   virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
75
76   // Updates printer settings.
77   // |external_preview| is true if pdf is going to be opened in external
78   // preview. Used by MacOS only now to open Preview.app.
79   virtual Result UpdatePrinterSettings(bool external_preview,
80                                        bool show_system_dialog,
81                                        int page_count) = 0;
82
83   // Updates Print Settings. |job_settings| contains all print job
84   // settings information.
85   Result UpdatePrintSettings(base::Value job_settings);
86
87 #if defined(OS_CHROMEOS)
88   // Updates Print Settings.
89   Result UpdatePrintSettingsFromPOD(
90       std::unique_ptr<PrintSettings> job_settings);
91 #endif
92
93   // Does platform specific setup of the printer before the printing. Signal the
94   // printer that a document is about to be spooled.
95   // Warning: This function enters a message loop. That may cause side effects
96   // like IPC message processing! Some printers have side-effects on this call
97   // like virtual printers that ask the user for the path of the saved document;
98   // for example a PDF printer.
99   virtual Result NewDocument(const base::string16& document_name) = 0;
100
101   // Starts a new page.
102   virtual Result NewPage() = 0;
103
104   // Closes the printed page.
105   virtual Result PageDone() = 0;
106
107   // Closes the printing job. After this call the object is ready to start a new
108   // document.
109   virtual Result DocumentDone() = 0;
110
111   // Cancels printing. Can be used in a multi-threaded context. Takes effect
112   // immediately.
113   virtual void Cancel() = 0;
114
115   // Releases the native printing context.
116   virtual void ReleaseContext() = 0;
117
118   // Returns the native context used to print.
119   virtual printing::NativeDrawingContext context() const = 0;
120
121   // Creates an instance of this object. Implementers of this interface should
122   // implement this method to create an object of their implementation.
123   static std::unique_ptr<PrintingContext> Create(Delegate* delegate);
124
125   void set_margin_type(MarginType type);
126   void set_is_modifiable(bool is_modifiable);
127
128   const PrintSettings& settings() const { return settings_; }
129
130   int job_id() const { return job_id_; }
131
132  protected:
133   explicit PrintingContext(Delegate* delegate);
134
135   // Reinitializes the settings for object reuse.
136   void ResetSettings();
137
138   // Does bookkeeping when an error occurs.
139   PrintingContext::Result OnError();
140
141   // Complete print context settings.
142   PrintSettings settings_;
143
144   // Printing context delegate.
145   Delegate* const delegate_;
146
147   // Is a print job being done.
148   volatile bool in_print_job_;
149
150   // Did the user cancel the print job.
151   volatile bool abort_printing_;
152
153   // The job id for the current job. The value is 0 if no jobs are active.
154   int job_id_;
155
156  private:
157   DISALLOW_COPY_AND_ASSIGN(PrintingContext);
158 };
159
160 }  // namespace printing
161
162 #endif  // PRINTING_PRINTING_CONTEXT_H_