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