| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/printing/print_dialog_gtk.h" | 5 #include "chrome/browser/printing/print_dialog_gtk.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <gtk/gtkpagesetupunixdialog.h> | 8 #include <gtk/gtkpagesetupunixdialog.h> |
| 9 #include <gtk/gtkprintjob.h> | 9 #include <gtk/gtkprintjob.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| 11 #include <sys/types.h> | 11 #include <sys/types.h> |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/file_util_proxy.h" | 17 #include "base/file_util_proxy.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/synchronization/waitable_event.h" | |
| 20 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 21 #include "chrome/browser/ui/browser_list.h" | 20 #include "chrome/browser/ui/browser_list.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "printing/metafile.h" | 22 #include "printing/metafile.h" |
| 24 #include "printing/print_job_constants.h" | 23 #include "printing/print_job_constants.h" |
| 25 #include "printing/print_settings_initializer_gtk.h" | 24 #include "printing/print_settings_initializer_gtk.h" |
| 26 | 25 |
| 27 using printing::PageRanges; | 26 using printing::PageRanges; |
| 28 using printing::PrintSettings; | 27 using printing::PrintSettings; |
| 29 | 28 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 g_object_unref(page_setup_); | 125 g_object_unref(page_setup_); |
| 127 page_setup_ = NULL; | 126 page_setup_ = NULL; |
| 128 } | 127 } |
| 129 if (printer_) { | 128 if (printer_) { |
| 130 g_object_unref(printer_); | 129 g_object_unref(printer_); |
| 131 printer_ = NULL; | 130 printer_ = NULL; |
| 132 } | 131 } |
| 133 } | 132 } |
| 134 | 133 |
| 135 void PrintDialogGtk::UseDefaultSettings() { | 134 void PrintDialogGtk::UseDefaultSettings() { |
| 136 DCHECK(!save_document_event_.get()); | |
| 137 DCHECK(!page_setup_); | 135 DCHECK(!page_setup_); |
| 138 | 136 |
| 139 // |gtk_settings_| is a new object. | 137 // |gtk_settings_| is a new object. |
| 140 gtk_settings_ = gtk_print_settings_new(); | 138 gtk_settings_ = gtk_print_settings_new(); |
| 141 | 139 |
| 142 scoped_ptr<GtkPrinterList> printer_list(new GtkPrinterList); | 140 scoped_ptr<GtkPrinterList> printer_list(new GtkPrinterList); |
| 143 printer_ = printer_list->default_printer(); | 141 printer_ = printer_list->default_printer(); |
| 144 if (printer_) { | 142 if (printer_) { |
| 145 g_object_ref(printer_); | 143 g_object_ref(printer_); |
| 146 gtk_print_settings_set_printer(gtk_settings_, | 144 gtk_print_settings_set_printer(gtk_settings_, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 gtk_settings_, | 207 gtk_settings_, |
| 210 landscape ? GTK_PAGE_ORIENTATION_LANDSCAPE : | 208 landscape ? GTK_PAGE_ORIENTATION_LANDSCAPE : |
| 211 GTK_PAGE_ORIENTATION_PORTRAIT); | 209 GTK_PAGE_ORIENTATION_PORTRAIT); |
| 212 | 210 |
| 213 InitPrintSettings(ranges); | 211 InitPrintSettings(ranges); |
| 214 return true; | 212 return true; |
| 215 } | 213 } |
| 216 | 214 |
| 217 void PrintDialogGtk::ShowDialog( | 215 void PrintDialogGtk::ShowDialog( |
| 218 PrintingContextCairo::PrintSettingsCallback* callback) { | 216 PrintingContextCairo::PrintSettingsCallback* callback) { |
| 219 DCHECK(!save_document_event_.get()); | |
| 220 | |
| 221 callback_ = callback; | 217 callback_ = callback; |
| 222 | 218 |
| 223 GtkWindow* parent = BrowserList::GetLastActive()->window()->GetNativeHandle(); | 219 GtkWindow* parent = BrowserList::GetLastActive()->window()->GetNativeHandle(); |
| 224 // TODO(estade): We need a window title here. | 220 // TODO(estade): We need a window title here. |
| 225 dialog_ = gtk_print_unix_dialog_new(NULL, parent); | 221 dialog_ = gtk_print_unix_dialog_new(NULL, parent); |
| 226 g_signal_connect(dialog_, "delete-event", | 222 g_signal_connect(dialog_, "delete-event", |
| 227 G_CALLBACK(gtk_widget_hide_on_delete), NULL); | 223 G_CALLBACK(gtk_widget_hide_on_delete), NULL); |
| 228 | 224 |
| 229 | 225 |
| 230 // Set modal so user cannot focus the same tab and press print again. | 226 // Set modal so user cannot focus the same tab and press print again. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 249 } | 245 } |
| 250 | 246 |
| 251 void PrintDialogGtk::PrintDocument(const printing::Metafile* metafile, | 247 void PrintDialogGtk::PrintDocument(const printing::Metafile* metafile, |
| 252 const string16& document_name) { | 248 const string16& document_name) { |
| 253 // This runs on the print worker thread, does not block the UI thread. | 249 // This runs on the print worker thread, does not block the UI thread. |
| 254 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 250 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 255 | 251 |
| 256 // The document printing tasks can outlive the PrintingContext that created | 252 // The document printing tasks can outlive the PrintingContext that created |
| 257 // this dialog. | 253 // this dialog. |
| 258 AddRef(); | 254 AddRef(); |
| 259 DCHECK(!save_document_event_.get()); | |
| 260 save_document_event_.reset(new base::WaitableEvent(false, false)); | |
| 261 | 255 |
| 262 BrowserThread::PostTask( | 256 bool error = false; |
| 263 BrowserThread::FILE, FROM_HERE, | 257 if (!file_util::CreateTemporaryFile(&path_to_pdf_)) { |
| 264 NewRunnableMethod(this, | 258 LOG(ERROR) << "Creating temporary file failed"; |
| 265 &PrintDialogGtk::SaveDocumentToDisk, | 259 error = true; |
| 266 metafile, | 260 } |
| 267 document_name)); | 261 |
| 268 // Wait for SaveDocumentToDisk() to finish. | 262 if (!error && !metafile->SaveTo(path_to_pdf_)) { |
| 269 save_document_event_->Wait(); | 263 LOG(ERROR) << "Saving metafile failed"; |
| 264 file_util::Delete(path_to_pdf_, false); |
| 265 error = true; |
| 266 } |
| 267 |
| 268 if (error) { |
| 269 // Matches AddRef() above. |
| 270 Release(); |
| 271 } else { |
| 272 // No errors, continue printing. |
| 273 BrowserThread::PostTask( |
| 274 BrowserThread::UI, FROM_HERE, |
| 275 NewRunnableMethod(this, |
| 276 &PrintDialogGtk::SendDocumentToPrinter, |
| 277 document_name)); |
| 278 } |
| 270 } | 279 } |
| 271 | 280 |
| 272 void PrintDialogGtk::AddRefToDialog() { | 281 void PrintDialogGtk::AddRefToDialog() { |
| 273 AddRef(); | 282 AddRef(); |
| 274 } | 283 } |
| 275 | 284 |
| 276 void PrintDialogGtk::ReleaseDialog() { | 285 void PrintDialogGtk::ReleaseDialog() { |
| 277 Release(); | 286 Release(); |
| 278 } | 287 } |
| 279 | 288 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 callback_ = NULL; | 337 callback_ = NULL; |
| 329 return; | 338 return; |
| 330 } | 339 } |
| 331 case GTK_RESPONSE_APPLY: | 340 case GTK_RESPONSE_APPLY: |
| 332 default: { | 341 default: { |
| 333 NOTREACHED(); | 342 NOTREACHED(); |
| 334 } | 343 } |
| 335 } | 344 } |
| 336 } | 345 } |
| 337 | 346 |
| 338 void PrintDialogGtk::SaveDocumentToDisk(const printing::Metafile* metafile, | |
| 339 const string16& document_name) { | |
| 340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 341 | |
| 342 bool error = false; | |
| 343 if (!file_util::CreateTemporaryFile(&path_to_pdf_)) { | |
| 344 LOG(ERROR) << "Creating temporary file failed"; | |
| 345 error = true; | |
| 346 } | |
| 347 | |
| 348 if (!error && !metafile->SaveTo(path_to_pdf_)) { | |
| 349 LOG(ERROR) << "Saving metafile failed"; | |
| 350 file_util::Delete(path_to_pdf_, false); | |
| 351 error = true; | |
| 352 } | |
| 353 | |
| 354 // Done saving, let PrintDialogGtk::PrintDocument() continue. | |
| 355 save_document_event_->Signal(); | |
| 356 | |
| 357 if (error) { | |
| 358 // Matches AddRef() in PrintDocument(); | |
| 359 Release(); | |
| 360 } else { | |
| 361 // No errors, continue printing. | |
| 362 BrowserThread::PostTask( | |
| 363 BrowserThread::UI, FROM_HERE, | |
| 364 NewRunnableMethod(this, | |
| 365 &PrintDialogGtk::SendDocumentToPrinter, | |
| 366 document_name)); | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 void PrintDialogGtk::SendDocumentToPrinter(const string16& document_name) { | 347 void PrintDialogGtk::SendDocumentToPrinter(const string16& document_name) { |
| 371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 372 | 349 |
| 373 // If |printer_| is NULL then somehow the GTK printer list changed out under | 350 // If |printer_| is NULL then somehow the GTK printer list changed out under |
| 374 // us. In which case, just bail out. | 351 // us. In which case, just bail out. |
| 375 if (!printer_) { | 352 if (!printer_) { |
| 376 // Matches AddRef() in PrintDocument(); | 353 // Matches AddRef() in PrintDocument(); |
| 377 Release(); | 354 Release(); |
| 378 return; | 355 return; |
| 379 } | 356 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 407 // Printing finished. Matches AddRef() in PrintDocument(); | 384 // Printing finished. Matches AddRef() in PrintDocument(); |
| 408 Release(); | 385 Release(); |
| 409 } | 386 } |
| 410 | 387 |
| 411 void PrintDialogGtk::InitPrintSettings(const PageRanges& page_ranges) { | 388 void PrintDialogGtk::InitPrintSettings(const PageRanges& page_ranges) { |
| 412 PrintSettings settings; | 389 PrintSettings settings; |
| 413 printing::PrintSettingsInitializerGtk::InitPrintSettings( | 390 printing::PrintSettingsInitializerGtk::InitPrintSettings( |
| 414 gtk_settings_, page_setup_, page_ranges, false, &settings); | 391 gtk_settings_, page_setup_, page_ranges, false, &settings); |
| 415 context_->InitWithSettings(settings); | 392 context_->InitWithSettings(settings); |
| 416 } | 393 } |
| OLD | NEW |