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.
5 #ifndef PRINTING_PRINTING_CONTEXT_H_
6 #define PRINTING_PRINTING_CONTEXT_H_
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"
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 {
27 // Printing context delegate.
31 virtual ~Delegate() {}
33 // Returns parent view to use for modal dialogs.
34 virtual gfx::NativeView GetParentView() = 0;
36 // Returns application locale.
37 virtual std::string GetAppLocale() = 0;
40 // Tri-state result for user behavior-dependent functions.
47 virtual ~PrintingContext();
49 // Callback of AskUserForSettings, used to notify the PrintJobWorker when
50 // print settings are available.
51 using PrintSettingsCallback = base::OnceCallback<void(Result)>;
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,
63 PrintSettingsCallback callback) = 0;
65 // Selects the user's default printer and format. Updates the context with the
66 // default device settings.
67 virtual Result UseDefaultSettings() = 0;
69 // Updates the context with PDF printer settings.
70 Result UsePdfSettings();
72 // Returns paper size to be used for PDF or Cloud Print in device units.
73 virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
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,
82 // Updates Print Settings. |job_settings| contains all print job
83 // settings information.
84 Result UpdatePrintSettings(base::Value job_settings);
86 #if defined(OS_CHROMEOS)
87 // Updates Print Settings.
88 Result UpdatePrintSettingsFromPOD(
89 std::unique_ptr<PrintSettings> job_settings);
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;
100 // Starts a new page.
101 virtual Result NewPage() = 0;
103 // Closes the printed page.
104 virtual Result PageDone() = 0;
106 // Closes the printing job. After this call the object is ready to start a new
108 virtual Result DocumentDone() = 0;
110 // Cancels printing. Can be used in a multi-threaded context. Takes effect
112 virtual void Cancel() = 0;
114 // Releases the native printing context.
115 virtual void ReleaseContext() = 0;
117 // Returns the native context used to print.
118 virtual printing::NativeDrawingContext context() const = 0;
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);
124 void set_margin_type(MarginType type);
125 void set_is_modifiable(bool is_modifiable);
127 const PrintSettings& settings() const {
131 int job_id() const { return job_id_; }
134 explicit PrintingContext(Delegate* delegate);
136 // Reinitializes the settings for object reuse.
137 void ResetSettings();
139 // Does bookkeeping when an error occurs.
140 PrintingContext::Result OnError();
142 // Complete print context settings.
143 PrintSettings settings_;
145 // Printing context delegate.
146 Delegate* const delegate_;
148 // Is a print job being done.
149 volatile bool in_print_job_;
151 // Did the user cancel the print job.
152 volatile bool abort_printing_;
154 // The job id for the current job. The value is 0 if no jobs are active.
158 DISALLOW_COPY_AND_ASSIGN(PrintingContext);
161 } // namespace printing
163 #endif // PRINTING_PRINTING_CONTEXT_H_