[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #ifndef PRINTING_PRINTING_CONTEXT_H_ |
| 6 | #define PRINTING_PRINTING_CONTEXT_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 8 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 11 | #include "base/callback.h" |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 896d161f | 2013-06-11 22:52:24 | [diff] [blame] | 13 | #include "base/strings/string16.h" |
Thiago Farina | 45e31953 | 2017-03-23 02:44:49 | [diff] [blame] | 14 | #include "printing/native_drawing_context.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 15 | #include "printing/print_settings.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 16 | #include "ui/gfx/native_widget_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 18 | namespace base { |
[email protected] | 89f5aa8c | 2011-03-21 20:58:44 | [diff] [blame] | 19 | class DictionaryValue; |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 20 | } |
[email protected] | 89f5aa8c | 2011-03-21 20:58:44 | [diff] [blame] | 21 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | namespace printing { |
| 23 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 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. |
[email protected] | 69f5b1e6 | 2011-09-01 06:34:04 | [diff] [blame] | 28 | class PRINTING_EXPORT PrintingContext { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | public: |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 30 | // Printing context delegate. |
| 31 | class Delegate { |
| 32 | public: |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 33 | Delegate() {} |
| 34 | virtual ~Delegate() {} |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | // Tri-state result for user behavior-dependent functions. |
| 44 | enum Result { |
| 45 | OK, |
| 46 | CANCEL, |
| 47 | FAILED, |
| 48 | }; |
| 49 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 50 | virtual ~PrintingContext(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 52 | // Callback of AskUserForSettings, used to notify the PrintJobWorker when |
| 53 | // print settings are available. |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 54 | using PrintSettingsCallback = base::OnceCallback<void(Result)>; |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 55 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | // Asks the user what printer and format should be used to print. Updates the |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 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. |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 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/) |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 63 | virtual void AskUserForSettings(int max_pages, |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 64 | bool has_selection, |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 65 | bool is_scripted, |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 66 | PrintSettingsCallback callback) = 0; |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 67 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | // Selects the user's default printer and format. Updates the context with the |
| 69 | // default device settings. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 70 | virtual Result UseDefaultSettings() = 0; |
[email protected] | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 71 | |
[email protected] | 2dafb8b | 2014-05-29 16:32:48 | [diff] [blame] | 72 | // Updates the context with PDF printer settings. |
| 73 | Result UsePdfSettings(); |
| 74 | |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 75 | // Returns paper size to be used for PDF or Cloud Print in device units. |
| 76 | virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0; |
| 77 | |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 78 | // Updates printer settings. |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 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. |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 81 | virtual Result UpdatePrinterSettings(bool external_preview, |
vitalybuka | 95fa3c9 | 2015-05-05 03:03:32 | [diff] [blame] | 82 | bool show_system_dialog, |
| 83 | int page_count) = 0; |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 84 | |
| 85 | // Updates Print Settings. |job_settings| contains all print job |
| 86 | // settings information. |ranges| has the new page range settings. |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 87 | Result UpdatePrintSettings(const base::DictionaryValue& job_settings); |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 88 | |
Vladislav Kuzkokov | e219910 | 2018-02-20 16:25:13 | [diff] [blame] | 89 | #if defined(OS_CHROMEOS) |
| 90 | // Updates Print Settings. |
| 91 | Result UpdatePrintSettingsFromPOD( |
| 92 | std::unique_ptr<PrintSettings> job_settings); |
| 93 | #endif |
| 94 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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. |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 101 | virtual Result NewDocument(const base::string16& document_name) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | |
| 103 | // Starts a new page. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 104 | virtual Result NewPage() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | |
| 106 | // Closes the printed page. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 107 | virtual Result PageDone() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | |
| 109 | // Closes the printing job. After this call the object is ready to start a new |
| 110 | // document. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 111 | virtual Result DocumentDone() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 113 | // Cancels printing. Can be used in a multi-threaded context. Takes effect |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | // immediately. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 115 | virtual void Cancel() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 117 | // Releases the native printing context. |
| 118 | virtual void ReleaseContext() = 0; |
| 119 | |
| 120 | // Returns the native context used to print. |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 121 | virtual printing::NativeDrawingContext context() const = 0; |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 122 | |
| 123 | // Creates an instance of this object. Implementers of this interface should |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 124 | // implement this method to create an object of their implementation. |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 125 | static std::unique_ptr<PrintingContext> Create(Delegate* delegate); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 126 | |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 127 | void set_margin_type(MarginType type); |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 128 | void set_is_modifiable(bool is_modifiable); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | |
| 130 | const PrintSettings& settings() const { |
| 131 | return settings_; |
| 132 | } |
| 133 | |
skau | 3fadb49 | 2017-02-15 19:23:37 | [diff] [blame] | 134 | int job_id() const { return job_id_; } |
| 135 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 136 | protected: |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 137 | explicit PrintingContext(Delegate* delegate); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 138 | |
| 139 | // Reinitializes the settings for object reuse. |
| 140 | void ResetSettings(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 142 | // Does bookkeeping when an error occurs. |
| 143 | PrintingContext::Result OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | // Complete print context settings. |
| 146 | PrintSettings settings_; |
| 147 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 148 | // Printing context delegate. |
Lei Zhang | 9aeaf60d | 2017-11-22 18:29:44 | [diff] [blame] | 149 | Delegate* const delegate_; |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 150 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
skau | 3fadb49 | 2017-02-15 19:23:37 | [diff] [blame] | 157 | // The job id for the current job. The value is 0 if no jobs are active. |
| 158 | int job_id_; |
| 159 | |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 160 | private: |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 161 | DISALLOW_COPY_AND_ASSIGN(PrintingContext); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | } // namespace printing |
| 165 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 166 | #endif // PRINTING_PRINTING_CONTEXT_H_ |