| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/service/cloud_print/print_system.h" | 5 #include "chrome/service/cloud_print/print_system.h" |
| 6 | 6 |
| 7 #include <cups/cups.h> | 7 #include <cups/cups.h> |
| 8 #include <dlfcn.h> | 8 #include <dlfcn.h> |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include <pthread.h> | 10 #include <pthread.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <list> | 13 #include <list> |
| 14 #include <map> | 14 #include <map> |
| 15 | 15 |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/json/json_reader.h" | 18 #include "base/json/json_reader.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/md5.h" | 20 #include "base/md5.h" |
| 21 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
| 22 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
| 23 #include "base/rand_util.h" | 23 #include "base/rand_util.h" |
| 24 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
| 25 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
| 26 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 27 #include "base/values.h" | 27 #include "base/values.h" |
| 28 #include "chrome/common/child_process_logging.h" | |
| 29 #include "chrome/common/cloud_print/cloud_print_constants.h" | 28 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 29 #include "chrome/common/crash_keys.h" |
| 30 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 30 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 31 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
| 32 #include "printing/backend/cups_helper.h" | 32 #include "printing/backend/cups_helper.h" |
| 33 #include "printing/backend/print_backend.h" | 33 #include "printing/backend/print_backend.h" |
| 34 #include "printing/backend/print_backend_consts.h" | 34 #include "printing/backend/print_backend_consts.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 36 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 : printer_name_(printer_name), | 257 : printer_name_(printer_name), |
| 258 delegate_(NULL), | 258 delegate_(NULL), |
| 259 print_system_(print_system) { | 259 print_system_(print_system) { |
| 260 } | 260 } |
| 261 | 261 |
| 262 // PrintSystem::PrinterWatcher implementation. | 262 // PrintSystem::PrinterWatcher implementation. |
| 263 virtual bool StartWatching( | 263 virtual bool StartWatching( |
| 264 PrintSystem::PrinterWatcher::Delegate* delegate) OVERRIDE{ | 264 PrintSystem::PrinterWatcher::Delegate* delegate) OVERRIDE{ |
| 265 scoped_refptr<printing::PrintBackend> print_backend( | 265 scoped_refptr<printing::PrintBackend> print_backend( |
| 266 printing::PrintBackend::CreateInstance(NULL)); | 266 printing::PrintBackend::CreateInstance(NULL)); |
| 267 child_process_logging::ScopedPrinterInfoSetter prn_info( | 267 crash_keys::ScopedPrinterInfo crash_key( |
| 268 print_backend->GetPrinterDriverInfo(printer_name_)); | 268 print_backend->GetPrinterDriverInfo(printer_name_)); |
| 269 if (delegate_ != NULL) | 269 if (delegate_ != NULL) |
| 270 StopWatching(); | 270 StopWatching(); |
| 271 delegate_ = delegate; | 271 delegate_ = delegate; |
| 272 settings_hash_ = GetSettingsHash(); | 272 settings_hash_ = GetSettingsHash(); |
| 273 // Schedule next job status update. | 273 // Schedule next job status update. |
| 274 base::MessageLoop::current()->PostDelayedTask( | 274 base::MessageLoop::current()->PostDelayedTask( |
| 275 FROM_HERE, | 275 FROM_HERE, |
| 276 base::Bind(&PrinterWatcherCUPS::JobStatusUpdate, this), | 276 base::Bind(&PrinterWatcherCUPS::JobStatusUpdate, this), |
| 277 base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds)); | 277 base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds)); |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 return false; | 588 return false; |
| 589 | 589 |
| 590 PrintServerInfoCUPS::CapsMap::iterator caps_it = | 590 PrintServerInfoCUPS::CapsMap::iterator caps_it = |
| 591 server_info->caps_cache.find(printer_name); | 591 server_info->caps_cache.find(printer_name); |
| 592 if (caps_it != server_info->caps_cache.end()) { | 592 if (caps_it != server_info->caps_cache.end()) { |
| 593 *printer_info = caps_it->second; | 593 *printer_info = caps_it->second; |
| 594 return true; | 594 return true; |
| 595 } | 595 } |
| 596 | 596 |
| 597 // TODO(gene): Retry multiple times in case of error. | 597 // TODO(gene): Retry multiple times in case of error. |
| 598 child_process_logging::ScopedPrinterInfoSetter prn_info( | 598 crash_keys::ScopedPrinterInfo crash_key( |
| 599 server_info->backend->GetPrinterDriverInfo(short_printer_name)); | 599 server_info->backend->GetPrinterDriverInfo(short_printer_name)); |
| 600 if (!server_info->backend->GetPrinterCapsAndDefaults(short_printer_name, | 600 if (!server_info->backend->GetPrinterCapsAndDefaults(short_printer_name, |
| 601 printer_info) ) { | 601 printer_info) ) { |
| 602 return false; | 602 return false; |
| 603 } | 603 } |
| 604 | 604 |
| 605 server_info->caps_cache[printer_name] = *printer_info; | 605 server_info->caps_cache[printer_name] = *printer_info; |
| 606 return true; | 606 return true; |
| 607 } | 607 } |
| 608 | 608 |
| 609 bool PrintSystemCUPS::GetJobDetails(const std::string& printer_name, | 609 bool PrintSystemCUPS::GetJobDetails(const std::string& printer_name, |
| 610 PlatformJobId job_id, | 610 PlatformJobId job_id, |
| 611 PrintJobDetails *job_details) { | 611 PrintJobDetails *job_details) { |
| 612 DCHECK(initialized_); | 612 DCHECK(initialized_); |
| 613 DCHECK(job_details); | 613 DCHECK(job_details); |
| 614 | 614 |
| 615 std::string short_printer_name; | 615 std::string short_printer_name; |
| 616 PrintServerInfoCUPS* server_info = | 616 PrintServerInfoCUPS* server_info = |
| 617 FindServerByFullName(printer_name, &short_printer_name); | 617 FindServerByFullName(printer_name, &short_printer_name); |
| 618 if (!server_info) | 618 if (!server_info) |
| 619 return false; | 619 return false; |
| 620 | 620 |
| 621 child_process_logging::ScopedPrinterInfoSetter prn_info( | 621 crash_keys::ScopedPrinterInfo crash_key( |
| 622 server_info->backend->GetPrinterDriverInfo(short_printer_name)); | 622 server_info->backend->GetPrinterDriverInfo(short_printer_name)); |
| 623 cups_job_t* jobs = NULL; | 623 cups_job_t* jobs = NULL; |
| 624 int num_jobs = GetJobs(&jobs, server_info->url, cups_encryption_, | 624 int num_jobs = GetJobs(&jobs, server_info->url, cups_encryption_, |
| 625 short_printer_name.c_str(), 1, -1); | 625 short_printer_name.c_str(), 1, -1); |
| 626 bool error = (num_jobs == 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE); | 626 bool error = (num_jobs == 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE); |
| 627 if (error) { | 627 if (error) { |
| 628 VLOG(1) << "CP_CUPS: Error getting jobs from CUPS server" | 628 VLOG(1) << "CP_CUPS: Error getting jobs from CUPS server" |
| 629 << ", printer name:" << printer_name | 629 << ", printer name:" << printer_name |
| 630 << ", error: " << static_cast<int>(cupsLastError()); | 630 << ", error: " << static_cast<int>(cupsLastError()); |
| 631 return false; | 631 return false; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 771 bool* dry_run) { | 771 bool* dry_run) { |
| 772 DCHECK(initialized_); | 772 DCHECK(initialized_); |
| 773 VLOG(1) << "CP_CUPS: Spooling print job, printer name: " << printer_name; | 773 VLOG(1) << "CP_CUPS: Spooling print job, printer name: " << printer_name; |
| 774 | 774 |
| 775 std::string short_printer_name; | 775 std::string short_printer_name; |
| 776 PrintServerInfoCUPS* server_info = | 776 PrintServerInfoCUPS* server_info = |
| 777 FindServerByFullName(printer_name, &short_printer_name); | 777 FindServerByFullName(printer_name, &short_printer_name); |
| 778 if (!server_info) | 778 if (!server_info) |
| 779 return false; | 779 return false; |
| 780 | 780 |
| 781 child_process_logging::ScopedPrinterInfoSetter prn_info( | 781 crash_keys::ScopedPrinterInfo crash_key( |
| 782 server_info->backend->GetPrinterDriverInfo(printer_name)); | 782 server_info->backend->GetPrinterDriverInfo(printer_name)); |
| 783 | 783 |
| 784 // We need to store options as char* string for the duration of the | 784 // We need to store options as char* string for the duration of the |
| 785 // cupsPrintFile2 call. We'll use map here to store options, since | 785 // cupsPrintFile2 call. We'll use map here to store options, since |
| 786 // Dictionary value from JSON parser returns wchat_t. | 786 // Dictionary value from JSON parser returns wchat_t. |
| 787 std::map<std::string, std::string> options; | 787 std::map<std::string, std::string> options; |
| 788 bool res = ParsePrintTicket(print_ticket, &options); | 788 bool res = ParsePrintTicket(print_ticket, &options); |
| 789 DCHECK(res); // If print ticket is invalid we still print using defaults. | 789 DCHECK(res); // If print ticket is invalid we still print using defaults. |
| 790 | 790 |
| 791 // Check if this is a dry run (test) job. | 791 // Check if this is a dry run (test) job. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 867 | 867 |
| 868 void PrintSystemCUPS::RunCapsCallback( | 868 void PrintSystemCUPS::RunCapsCallback( |
| 869 const PrinterCapsAndDefaultsCallback& callback, | 869 const PrinterCapsAndDefaultsCallback& callback, |
| 870 bool succeeded, | 870 bool succeeded, |
| 871 const std::string& printer_name, | 871 const std::string& printer_name, |
| 872 const printing::PrinterCapsAndDefaults& printer_info) { | 872 const printing::PrinterCapsAndDefaults& printer_info) { |
| 873 callback.Run(succeeded, printer_name, printer_info); | 873 callback.Run(succeeded, printer_name, printer_info); |
| 874 } | 874 } |
| 875 | 875 |
| 876 } // namespace cloud_print | 876 } // namespace cloud_print |
| OLD | NEW |