Upload upstream chromium 67.0.3396
[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 "printing/native_drawing_context.h"
15 #include "printing/print_settings.h"
16 #include "ui/gfx/native_widget_types.h"
17
18 namespace base {
19 class DictionaryValue;
20 }
21
22 namespace printing {
23
24 // An abstraction of a printer context, implemented by objects that describe the
25 // user selected printing context. This includes the OS-dependent UI to ask the
26 // user about the print settings. Concrete implementations directly talk to the
27 // printer and manage the document and page breaks.
28 class PRINTING_EXPORT PrintingContext {
29  public:
30   // Printing context delegate.
31   class Delegate {
32    public:
33     Delegate() {}
34     virtual ~Delegate() {}
35
36     // Returns parent view to use for modal dialogs.
37     virtual gfx::NativeView GetParentView() = 0;
38
39     // Returns application locale.
40     virtual std::string GetAppLocale() = 0;
41   };
42
43   // Tri-state result for user behavior-dependent functions.
44   enum Result {
45     OK,
46     CANCEL,
47     FAILED,
48   };
49
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. |ranges| has the new page range settings.
87   Result UpdatePrintSettings(const base::DictionaryValue& 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 base::string16& 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(MarginType type);
128   void set_is_modifiable(bool is_modifiable);
129
130   const PrintSettings& settings() const {
131     return settings_;
132   }
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   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  private:
161   DISALLOW_COPY_AND_ASSIGN(PrintingContext);
162 };
163
164 }  // namespace printing
165
166 #endif  // PRINTING_PRINTING_CONTEXT_H_