[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 1 | // Copyright 2014 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 | #include "printing/printing_context_linux.h" |
| 6 | |
| 7 | #include "base/logging.h" |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 8 | #include "base/memory/ptr_util.h" |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 9 | #include "base/values.h" |
| 10 | #include "printing/metafile.h" |
| 11 | #include "printing/print_dialog_gtk_interface.h" |
| 12 | #include "printing/print_job_constants.h" |
| 13 | #include "printing/units.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // Function pointer for creating print dialogs. |callback| is only used when |
| 18 | // |show_dialog| is true. |
| 19 | printing::PrintDialogGtkInterface* (*create_dialog_func_)( |
| 20 | printing::PrintingContextLinux* context) = NULL; |
| 21 | |
| 22 | // Function pointer for determining paper size. |
| 23 | gfx::Size (*get_pdf_paper_size_)( |
| 24 | printing::PrintingContextLinux* context) = NULL; |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | namespace printing { |
| 29 | |
| 30 | // static |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 31 | std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { |
| 32 | return base::WrapUnique(new PrintingContextLinux(delegate)); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 33 | } |
| 34 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 35 | PrintingContextLinux::PrintingContextLinux(Delegate* delegate) |
| 36 | : PrintingContext(delegate), print_dialog_(NULL) { |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | PrintingContextLinux::~PrintingContextLinux() { |
| 40 | ReleaseContext(); |
| 41 | |
| 42 | if (print_dialog_) |
| 43 | print_dialog_->ReleaseDialog(); |
| 44 | } |
| 45 | |
| 46 | // static |
| 47 | void PrintingContextLinux::SetCreatePrintDialogFunction( |
| 48 | PrintDialogGtkInterface* (*create_dialog_func)( |
| 49 | PrintingContextLinux* context)) { |
| 50 | DCHECK(create_dialog_func); |
| 51 | DCHECK(!create_dialog_func_); |
| 52 | create_dialog_func_ = create_dialog_func; |
| 53 | } |
| 54 | |
| 55 | // static |
| 56 | void PrintingContextLinux::SetPdfPaperSizeFunction( |
| 57 | gfx::Size (*get_pdf_paper_size)(PrintingContextLinux* context)) { |
| 58 | DCHECK(get_pdf_paper_size); |
| 59 | DCHECK(!get_pdf_paper_size_); |
| 60 | get_pdf_paper_size_ = get_pdf_paper_size; |
| 61 | } |
| 62 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 63 | void PrintingContextLinux::PrintDocument(const MetafilePlayer& metafile) { |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 64 | DCHECK(print_dialog_); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 65 | print_dialog_->PrintDocument(metafile, document_name_); |
| 66 | } |
| 67 | |
| 68 | void PrintingContextLinux::AskUserForSettings( |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 69 | int max_pages, |
| 70 | bool has_selection, |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 71 | bool is_scripted, |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 72 | const PrintSettingsCallback& callback) { |
[email protected] | bdf12cb7 | 2014-02-07 22:48:08 | [diff] [blame] | 73 | if (!print_dialog_) { |
| 74 | // Can only get here if the renderer is sending bad messages. |
| 75 | // http://crbug.com/341777 |
| 76 | NOTREACHED(); |
| 77 | callback.Run(FAILED); |
| 78 | return; |
| 79 | } |
| 80 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 81 | print_dialog_->ShowDialog( |
| 82 | delegate_->GetParentView(), has_selection, callback); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | PrintingContext::Result PrintingContextLinux::UseDefaultSettings() { |
| 86 | DCHECK(!in_print_job_); |
| 87 | |
| 88 | ResetSettings(); |
| 89 | |
| 90 | if (!create_dialog_func_) |
| 91 | return OK; |
| 92 | |
| 93 | if (!print_dialog_) { |
| 94 | print_dialog_ = create_dialog_func_(this); |
| 95 | print_dialog_->AddRefToDialog(); |
| 96 | } |
| 97 | print_dialog_->UseDefaultSettings(); |
| 98 | |
| 99 | return OK; |
| 100 | } |
| 101 | |
| 102 | gfx::Size PrintingContextLinux::GetPdfPaperSizeDeviceUnits() { |
| 103 | if (get_pdf_paper_size_) |
| 104 | return get_pdf_paper_size_(this); |
| 105 | |
| 106 | return gfx::Size(); |
| 107 | } |
| 108 | |
| 109 | PrintingContext::Result PrintingContextLinux::UpdatePrinterSettings( |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 110 | bool external_preview, |
vitalybuka | 95fa3c9 | 2015-05-05 03:03:32 | [diff] [blame] | 111 | bool show_system_dialog, |
| 112 | int page_count) { |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 113 | DCHECK(!show_system_dialog); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 114 | DCHECK(!in_print_job_); |
| 115 | DCHECK(!external_preview) << "Not implemented"; |
| 116 | |
| 117 | if (!create_dialog_func_) |
| 118 | return OK; |
| 119 | |
| 120 | if (!print_dialog_) { |
| 121 | print_dialog_ = create_dialog_func_(this); |
| 122 | print_dialog_->AddRefToDialog(); |
| 123 | } |
| 124 | |
| 125 | if (!print_dialog_->UpdateSettings(&settings_)) |
| 126 | return OnError(); |
| 127 | |
| 128 | return OK; |
| 129 | } |
| 130 | |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 131 | void PrintingContextLinux::InitWithSettings(const PrintSettings& settings) { |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 132 | DCHECK(!in_print_job_); |
| 133 | |
| 134 | settings_ = settings; |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | PrintingContext::Result PrintingContextLinux::NewDocument( |
| 138 | const base::string16& document_name) { |
| 139 | DCHECK(!in_print_job_); |
| 140 | in_print_job_ = true; |
| 141 | |
| 142 | document_name_ = document_name; |
| 143 | |
| 144 | return OK; |
| 145 | } |
| 146 | |
| 147 | PrintingContext::Result PrintingContextLinux::NewPage() { |
| 148 | if (abort_printing_) |
| 149 | return CANCEL; |
| 150 | DCHECK(in_print_job_); |
| 151 | |
| 152 | // Intentional No-op. |
| 153 | |
| 154 | return OK; |
| 155 | } |
| 156 | |
| 157 | PrintingContext::Result PrintingContextLinux::PageDone() { |
| 158 | if (abort_printing_) |
| 159 | return CANCEL; |
| 160 | DCHECK(in_print_job_); |
| 161 | |
| 162 | // Intentional No-op. |
| 163 | |
| 164 | return OK; |
| 165 | } |
| 166 | |
| 167 | PrintingContext::Result PrintingContextLinux::DocumentDone() { |
| 168 | if (abort_printing_) |
| 169 | return CANCEL; |
| 170 | DCHECK(in_print_job_); |
| 171 | |
| 172 | ResetSettings(); |
| 173 | return OK; |
| 174 | } |
| 175 | |
| 176 | void PrintingContextLinux::Cancel() { |
| 177 | abort_printing_ = true; |
| 178 | in_print_job_ = false; |
| 179 | } |
| 180 | |
| 181 | void PrintingContextLinux::ReleaseContext() { |
| 182 | // Intentional No-op. |
| 183 | } |
| 184 | |
tfarina | 876d1e0 | 2016-10-11 23:12:53 | [diff] [blame] | 185 | skia::NativeDrawingContext PrintingContextLinux::context() const { |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 186 | // Intentional No-op. |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | } // namespace printing |