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 "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"
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 {
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 virtual ~PrintingContext();
50 // Callback of AskUserForSettings, used to notify the PrintJobWorker when
51 // print settings are available.
52 using PrintSettingsCallback = base::OnceCallback<void(Result)>;
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,
64 PrintSettingsCallback callback) = 0;
66 // Selects the user's default printer and format. Updates the context with the
67 // default device settings.
68 virtual Result UseDefaultSettings() = 0;
70 // Updates the context with PDF printer settings.
71 Result UsePdfSettings();
73 // Returns paper size to be used for PDF or Cloud Print in device units.
74 virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
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,
83 // Updates Print Settings. |job_settings| contains all print job
84 // settings information.
85 Result UpdatePrintSettings(base::Value job_settings);
87 #if defined(OS_CHROMEOS)
88 // Updates Print Settings.
89 Result UpdatePrintSettingsFromPOD(
90 std::unique_ptr<PrintSettings> job_settings);
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;
101 // Starts a new page.
102 virtual Result NewPage() = 0;
104 // Closes the printed page.
105 virtual Result PageDone() = 0;
107 // Closes the printing job. After this call the object is ready to start a new
109 virtual Result DocumentDone() = 0;
111 // Cancels printing. Can be used in a multi-threaded context. Takes effect
113 virtual void Cancel() = 0;
115 // Releases the native printing context.
116 virtual void ReleaseContext() = 0;
118 // Returns the native context used to print.
119 virtual printing::NativeDrawingContext context() const = 0;
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);
125 void set_margin_type(MarginType type);
126 void set_is_modifiable(bool is_modifiable);
128 const PrintSettings& settings() const { return settings_; }
130 int job_id() const { return job_id_; }
133 explicit PrintingContext(Delegate* delegate);
135 // Reinitializes the settings for object reuse.
136 void ResetSettings();
138 // Does bookkeeping when an error occurs.
139 PrintingContext::Result OnError();
141 // Complete print context settings.
142 PrintSettings settings_;
144 // Printing context delegate.
145 Delegate* const delegate_;
147 // Is a print job being done.
148 volatile bool in_print_job_;
150 // Did the user cancel the print job.
151 volatile bool abort_printing_;
153 // The job id for the current job. The value is 0 if no jobs are active.
157 DISALLOW_COPY_AND_ASSIGN(PrintingContext);
160 } // namespace printing
162 #endif // PRINTING_PRINTING_CONTEXT_H_