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" 8 #include "base/values.h" 9 #include "printing/metafile.h" 10 #include "printing/print_dialog_gtk_interface.h" 11 #include "printing/print_job_constants.h" 12 #include "printing/units.h" 13 14 namespace { 15 16 // Function pointer for creating print dialogs. |callback| is only used when 17 // |show_dialog| is true. 18 printing::PrintDialogGtkInterface* (*create_dialog_func_)( 19 printing::PrintingContextLinux* context) = NULL; 20 21 // Function pointer for determining paper size. 22 gfx::Size (*get_pdf_paper_size_)( 23 printing::PrintingContextLinux* context) = NULL; 24 25 } // namespace 26 27 namespace printing { 28 29 // static 30 scoped_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { 31 return make_scoped_ptr<PrintingContext>(new PrintingContextLinux(delegate)); 32 } 33 34 PrintingContextLinux::PrintingContextLinux(Delegate* delegate) 35 : PrintingContext(delegate), print_dialog_(NULL) { 36 } 37 38 PrintingContextLinux::~PrintingContextLinux() { 39 ReleaseContext(); 40 41 if (print_dialog_) 42 print_dialog_->ReleaseDialog(); 43 } 44 45 // static 46 void PrintingContextLinux::SetCreatePrintDialogFunction( 47 PrintDialogGtkInterface* (*create_dialog_func)( 48 PrintingContextLinux* context)) { 49 DCHECK(create_dialog_func); 50 DCHECK(!create_dialog_func_); 51 create_dialog_func_ = create_dialog_func; 52 } 53 54 // static 55 void PrintingContextLinux::SetPdfPaperSizeFunction( 56 gfx::Size (*get_pdf_paper_size)(PrintingContextLinux* context)) { 57 DCHECK(get_pdf_paper_size); 58 DCHECK(!get_pdf_paper_size_); 59 get_pdf_paper_size_ = get_pdf_paper_size; 60 } 61 62 void PrintingContextLinux::PrintDocument(const MetafilePlayer& metafile) { 63 DCHECK(print_dialog_); 64 print_dialog_->PrintDocument(metafile, document_name_); 65 } 66 67 void PrintingContextLinux::AskUserForSettings( 68 int max_pages, 69 bool has_selection, 70 const PrintSettingsCallback& callback) { 71 if (!print_dialog_) { 72 // Can only get here if the renderer is sending bad messages. 73 // http://crbug.com/341777 74 NOTREACHED(); 75 callback.Run(FAILED); 76 return; 77 } 78 79 print_dialog_->ShowDialog( 80 delegate_->GetParentView(), has_selection, callback); 81 } 82 83 PrintingContext::Result PrintingContextLinux::UseDefaultSettings() { 84 DCHECK(!in_print_job_); 85 86 ResetSettings(); 87 88 if (!create_dialog_func_) 89 return OK; 90 91 if (!print_dialog_) { 92 print_dialog_ = create_dialog_func_(this); 93 print_dialog_->AddRefToDialog(); 94 } 95 print_dialog_->UseDefaultSettings(); 96 97 return OK; 98 } 99 100 gfx::Size PrintingContextLinux::GetPdfPaperSizeDeviceUnits() { 101 if (get_pdf_paper_size_) 102 return get_pdf_paper_size_(this); 103 104 return gfx::Size(); 105 } 106 107 PrintingContext::Result PrintingContextLinux::UpdatePrinterSettings( 108 bool external_preview, 109 bool show_system_dialog) { 110 DCHECK(!show_system_dialog); 111 DCHECK(!in_print_job_); 112 DCHECK(!external_preview) << "Not implemented"; 113 114 if (!create_dialog_func_) 115 return OK; 116 117 if (!print_dialog_) { 118 print_dialog_ = create_dialog_func_(this); 119 print_dialog_->AddRefToDialog(); 120 } 121 122 if (!print_dialog_->UpdateSettings(&settings_)) 123 return OnError(); 124 125 return OK; 126 } 127 128 PrintingContext::Result PrintingContextLinux::InitWithSettings( 129 const PrintSettings& settings) { 130 DCHECK(!in_print_job_); 131 132 settings_ = settings; 133 134 return OK; 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 185 gfx::NativeDrawingContext PrintingContextLinux::context() const { 186 // Intentional No-op. 187 return NULL; 188 } 189 190 } // namespace printing 191