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/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"
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 {
28 // Printing context delegate.
32 virtual ~Delegate() {}
34 // Returns parent view to use for modal dialogs.
35 virtual gfx::NativeView GetParentView() = 0;
37 // Returns application locale.
38 virtual std::string GetAppLocale() = 0;
41 // Tri-state result for user behavior-dependent functions.
48 PrintingContext(const PrintingContext&) = delete;
49 PrintingContext& operator=(const PrintingContext&) = delete;
50 virtual ~PrintingContext();
52 // Callback of AskUserForSettings, used to notify the PrintJobWorker when
53 // print settings are available.
54 using PrintSettingsCallback = base::OnceCallback<void(Result)>;
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,
66 PrintSettingsCallback callback) = 0;
68 // Selects the user's default printer and format. Updates the context with the
69 // default device settings.
70 virtual Result UseDefaultSettings() = 0;
72 // Updates the context with PDF printer settings.
73 Result UsePdfSettings();
75 // Returns paper size to be used for PDF or Cloud Print in device units.
76 virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
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,
85 // Updates Print Settings. `job_settings` contains all print job
86 // settings information.
87 Result UpdatePrintSettings(base::Value job_settings);
89 #if defined(OS_CHROMEOS)
90 // Updates Print Settings.
91 Result UpdatePrintSettingsFromPOD(
92 std::unique_ptr<PrintSettings> job_settings);
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;
103 // Starts a new page.
104 virtual Result NewPage() = 0;
106 // Closes the printed page.
107 virtual Result PageDone() = 0;
109 // Closes the printing job. After this call the object is ready to start a new
111 virtual Result DocumentDone() = 0;
113 // Cancels printing. Can be used in a multi-threaded context. Takes effect
115 virtual void Cancel() = 0;
117 // Releases the native printing context.
118 virtual void ReleaseContext() = 0;
120 // Returns the native context used to print.
121 virtual printing::NativeDrawingContext context() const = 0;
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);
127 void set_margin_type(mojom::MarginType type);
128 void set_is_modifiable(bool is_modifiable);
130 const PrintSettings& settings() const;
132 std::unique_ptr<PrintSettings> TakeAndResetSettings();
134 int job_id() const { return job_id_; }
137 explicit PrintingContext(Delegate* delegate);
139 // Reinitializes the settings for object reuse.
140 void ResetSettings();
142 // Does bookkeeping when an error occurs.
143 PrintingContext::Result OnError();
145 // Complete print context settings.
146 std::unique_ptr<PrintSettings> settings_;
148 // Printing context delegate.
149 Delegate* const delegate_;
151 // Is a print job being done.
152 volatile bool in_print_job_;
154 // Did the user cancel the print job.
155 volatile bool abort_printing_;
157 // The job id for the current job. The value is 0 if no jobs are active.
161 } // namespace printing
163 #endif // PRINTING_PRINTING_CONTEXT_H_