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> |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 return notify_delete_ && printer_enum_succeeded_; | 126 return notify_delete_ && printer_enum_succeeded_; |
127 } | 127 } |
128 | 128 |
129 private: | 129 private: |
130 virtual ~PrintSystemCUPS() {} | 130 virtual ~PrintSystemCUPS() {} |
131 | 131 |
132 // Following functions are wrappers around corresponding CUPS functions. | 132 // Following functions are wrappers around corresponding CUPS functions. |
133 // <functions>2() are called when print server is specified, and plain | 133 // <functions>2() are called when print server is specified, and plain |
134 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT | 134 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT |
135 // in the <functions>2(), it does not work in CUPS prior to 1.4. | 135 // in the <functions>2(), it does not work in CUPS prior to 1.4. |
136 int GetJobs(cups_job_t** jobs, const GURL& url, const char* name, | 136 int GetJobs(cups_job_t** jobs, const GURL& url, |
137 http_encryption_t encryption, const char* name, | |
137 int myjobs, int whichjobs); | 138 int myjobs, int whichjobs); |
138 int PrintFile(const GURL& url, const char* name, const char* filename, | 139 int PrintFile(const GURL& url, http_encryption_t encryption, |
140 const char* name, const char* filename, | |
139 const char* title, int num_options, cups_option_t* options); | 141 const char* title, int num_options, cups_option_t* options); |
Ryan Sleevi
2012/05/17 00:35:27
nit: these too
| |
140 | 142 |
141 void InitPrintBackends(const DictionaryValue* print_system_settings); | 143 void InitPrintBackends(const DictionaryValue* print_system_settings); |
142 void AddPrintServer(const std::string& url); | 144 void AddPrintServer(const std::string& url); |
143 | 145 |
144 void UpdatePrinters(); | 146 void UpdatePrinters(); |
145 | 147 |
146 // Full name contains print server url:port and printer name. Short name | 148 // Full name contains print server url:port and printer name. Short name |
147 // is the name of the printer in the CUPS server. | 149 // is the name of the printer in the CUPS server. |
148 std::string MakeFullPrinterName(const GURL& url, | 150 std::string MakeFullPrinterName(const GURL& url, |
149 const std::string& short_printer_name); | 151 const std::string& short_printer_name); |
150 PrintServerInfoCUPS* FindServerByFullName( | 152 PrintServerInfoCUPS* FindServerByFullName( |
151 const std::string& full_printer_name, std::string* short_printer_name); | 153 const std::string& full_printer_name, std::string* short_printer_name); |
152 | 154 |
153 // Helper method to invoke a PrinterCapsAndDefaultsCallback. | 155 // Helper method to invoke a PrinterCapsAndDefaultsCallback. |
154 static void RunCapsCallback( | 156 static void RunCapsCallback( |
155 const PrinterCapsAndDefaultsCallback& callback, | 157 const PrinterCapsAndDefaultsCallback& callback, |
156 bool succeeded, | 158 bool succeeded, |
157 const std::string& printer_name, | 159 const std::string& printer_name, |
158 const printing::PrinterCapsAndDefaults& printer_info); | 160 const printing::PrinterCapsAndDefaults& printer_info); |
159 | 161 |
160 // PrintServerList contains information about all print servers and backends | 162 // PrintServerList contains information about all print servers and backends |
161 // this proxy is connected to. | 163 // this proxy is connected to. |
162 typedef std::list<PrintServerInfoCUPS> PrintServerList; | 164 typedef std::list<PrintServerInfoCUPS> PrintServerList; |
163 PrintServerList print_servers_; | 165 PrintServerList print_servers_; |
164 | 166 |
165 base::TimeDelta update_timeout_; | 167 base::TimeDelta update_timeout_; |
166 bool initialized_; | 168 bool initialized_; |
167 bool printer_enum_succeeded_; | 169 bool printer_enum_succeeded_; |
168 bool notify_delete_; | 170 bool notify_delete_; |
171 http_encryption_t cups_encryption_; | |
169 }; | 172 }; |
170 | 173 |
171 class PrintServerWatcherCUPS | 174 class PrintServerWatcherCUPS |
172 : public PrintSystem::PrintServerWatcher { | 175 : public PrintSystem::PrintServerWatcher { |
173 public: | 176 public: |
174 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) | 177 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) |
175 : print_system_(print_system), | 178 : print_system_(print_system), |
176 delegate_(NULL) { | 179 delegate_(NULL) { |
177 } | 180 } |
178 | 181 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 scoped_refptr<PrintSystemCUPS> print_system_; | 402 scoped_refptr<PrintSystemCUPS> print_system_; |
400 | 403 |
401 DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS); | 404 DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS); |
402 }; | 405 }; |
403 | 406 |
404 PrintSystemCUPS::PrintSystemCUPS(const DictionaryValue* print_system_settings) | 407 PrintSystemCUPS::PrintSystemCUPS(const DictionaryValue* print_system_settings) |
405 : update_timeout_(base::TimeDelta::FromMinutes( | 408 : update_timeout_(base::TimeDelta::FromMinutes( |
406 kCheckForPrinterUpdatesMinutes)), | 409 kCheckForPrinterUpdatesMinutes)), |
407 initialized_(false), | 410 initialized_(false), |
408 printer_enum_succeeded_(false), | 411 printer_enum_succeeded_(false), |
409 notify_delete_(true) { | 412 notify_delete_(true), |
413 cups_encryption_(HTTP_ENCRYPT_NEVER) { | |
410 if (print_system_settings) { | 414 if (print_system_settings) { |
411 int timeout; | 415 int timeout; |
412 if (print_system_settings->GetInteger(kCUPSUpdateTimeoutMs, &timeout)) | 416 if (print_system_settings->GetInteger(kCUPSUpdateTimeoutMs, &timeout)) |
413 update_timeout_ = base::TimeDelta::FromMilliseconds(timeout); | 417 update_timeout_ = base::TimeDelta::FromMilliseconds(timeout); |
414 | 418 |
419 int encryption; | |
420 if (print_system_settings->GetInteger(kCUPSEncryption, &encryption)) | |
421 cups_encryption_ = | |
422 static_cast<http_encryption_t>(encryption); | |
423 | |
415 bool notify_delete = true; | 424 bool notify_delete = true; |
416 if (print_system_settings->GetBoolean(kCUPSNotifyDelete, ¬ify_delete)) | 425 if (print_system_settings->GetBoolean(kCUPSNotifyDelete, ¬ify_delete)) |
417 notify_delete_ = notify_delete; | 426 notify_delete_ = notify_delete; |
418 } | 427 } |
419 | 428 |
420 InitPrintBackends(print_system_settings); | 429 InitPrintBackends(print_system_settings); |
421 } | 430 } |
422 | 431 |
423 void PrintSystemCUPS::InitPrintBackends( | 432 void PrintSystemCUPS::InitPrintBackends( |
424 const DictionaryValue* print_system_settings) { | 433 const DictionaryValue* print_system_settings) { |
(...skipping 16 matching lines...) Expand all Loading... | |
441 if (url.empty()) | 450 if (url.empty()) |
442 LOG(WARNING) << "No print server specified. Using default print server."; | 451 LOG(WARNING) << "No print server specified. Using default print server."; |
443 | 452 |
444 // Get Print backend for the specific print server. | 453 // Get Print backend for the specific print server. |
445 DictionaryValue backend_settings; | 454 DictionaryValue backend_settings; |
446 backend_settings.SetString(kCUPSPrintServerURL, url); | 455 backend_settings.SetString(kCUPSPrintServerURL, url); |
447 | 456 |
448 // Make CUPS requests non-blocking. | 457 // Make CUPS requests non-blocking. |
449 backend_settings.SetString(kCUPSBlocking, kValueFalse); | 458 backend_settings.SetString(kCUPSBlocking, kValueFalse); |
450 | 459 |
460 // Set encryption for backend. | |
461 backend_settings.SetInteger(kCUPSEncryption, cups_encryption_); | |
462 | |
451 PrintServerInfoCUPS print_server; | 463 PrintServerInfoCUPS print_server; |
452 print_server.backend = | 464 print_server.backend = |
453 printing::PrintBackend::CreateInstance(&backend_settings); | 465 printing::PrintBackend::CreateInstance(&backend_settings); |
454 print_server.url = GURL(url.c_str()); | 466 print_server.url = GURL(url.c_str()); |
455 | 467 |
456 print_servers_.push_back(print_server); | 468 print_servers_.push_back(print_server); |
457 } | 469 } |
458 | 470 |
459 PrintSystem::PrintSystemResult PrintSystemCUPS::Init() { | 471 PrintSystem::PrintSystemResult PrintSystemCUPS::Init() { |
460 UpdatePrinters(); | 472 UpdatePrinters(); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
583 | 595 |
584 std::string short_printer_name; | 596 std::string short_printer_name; |
585 PrintServerInfoCUPS* server_info = | 597 PrintServerInfoCUPS* server_info = |
586 FindServerByFullName(printer_name, &short_printer_name); | 598 FindServerByFullName(printer_name, &short_printer_name); |
587 if (!server_info) | 599 if (!server_info) |
588 return false; | 600 return false; |
589 | 601 |
590 child_process_logging::ScopedPrinterInfoSetter prn_info( | 602 child_process_logging::ScopedPrinterInfoSetter prn_info( |
591 server_info->backend->GetPrinterDriverInfo(short_printer_name)); | 603 server_info->backend->GetPrinterDriverInfo(short_printer_name)); |
592 cups_job_t* jobs = NULL; | 604 cups_job_t* jobs = NULL; |
593 int num_jobs = GetJobs(&jobs, server_info->url, | 605 int num_jobs = GetJobs(&jobs, server_info->url, cups_encryption_, |
594 short_printer_name.c_str(), 1, -1); | 606 short_printer_name.c_str(), 1, -1); |
595 bool error = (num_jobs == 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE); | 607 bool error = (num_jobs == 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE); |
596 if (error) { | 608 if (error) { |
597 VLOG(1) << "CP_CUPS: Error getting jobs from CUPS server. Printer:" | 609 VLOG(1) << "CP_CUPS: Error getting jobs from CUPS server. Printer:" |
598 << printer_name | 610 << printer_name |
599 << " Error: " | 611 << " Error: " |
600 << static_cast<int>(cupsLastError()); | 612 << static_cast<int>(cupsLastError()); |
601 return false; | 613 return false; |
602 } | 614 } |
603 | 615 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
706 std::string id("CP_PROXY_"); | 718 std::string id("CP_PROXY_"); |
707 id += base::Uint64ToString(base::RandUint64()); | 719 id += base::Uint64ToString(base::RandUint64()); |
708 return id; | 720 return id; |
709 } | 721 } |
710 | 722 |
711 scoped_refptr<PrintSystem> PrintSystem::CreateInstance( | 723 scoped_refptr<PrintSystem> PrintSystem::CreateInstance( |
712 const DictionaryValue* print_system_settings) { | 724 const DictionaryValue* print_system_settings) { |
713 return new PrintSystemCUPS(print_system_settings); | 725 return new PrintSystemCUPS(print_system_settings); |
714 } | 726 } |
715 | 727 |
716 int PrintSystemCUPS::PrintFile(const GURL& url, const char* name, | 728 int PrintSystemCUPS::PrintFile(const GURL& url, http_encryption_t encryption, |
717 const char* filename, const char* title, | 729 const char* name, const char* filename, |
718 int num_options, cups_option_t* options) { | 730 const char* title, int num_options, |
731 cups_option_t* options) { | |
719 if (url.is_empty()) { // Use default (local) print server. | 732 if (url.is_empty()) { // Use default (local) print server. |
720 return cupsPrintFile(name, filename, title, num_options, options); | 733 return cupsPrintFile(name, filename, title, num_options, options); |
721 } else { | 734 } else { |
722 printing::HttpConnectionCUPS http(url); | 735 printing::HttpConnectionCUPS http(url, encryption); |
723 http.SetBlocking(false); | 736 http.SetBlocking(false); |
724 return cupsPrintFile2(http.http(), name, filename, | 737 return cupsPrintFile2(http.http(), name, filename, |
725 title, num_options, options); | 738 title, num_options, options); |
726 } | 739 } |
727 } | 740 } |
728 | 741 |
729 int PrintSystemCUPS::GetJobs(cups_job_t** jobs, const GURL& url, | 742 int PrintSystemCUPS::GetJobs(cups_job_t** jobs, const GURL& url, |
743 http_encryption_t encryption, | |
730 const char* name, int myjobs, int whichjobs) { | 744 const char* name, int myjobs, int whichjobs) { |
731 if (url.is_empty()) { // Use default (local) print server. | 745 if (url.is_empty()) { // Use default (local) print server. |
732 return cupsGetJobs(jobs, name, myjobs, whichjobs); | 746 return cupsGetJobs(jobs, name, myjobs, whichjobs); |
733 } else { | 747 } else { |
734 printing::HttpConnectionCUPS http(url); | 748 printing::HttpConnectionCUPS http(url, encryption); |
735 http.SetBlocking(false); | 749 http.SetBlocking(false); |
736 return cupsGetJobs2(http.http(), jobs, name, myjobs, whichjobs); | 750 return cupsGetJobs2(http.http(), jobs, name, myjobs, whichjobs); |
737 } | 751 } |
738 } | 752 } |
739 | 753 |
740 PlatformJobId PrintSystemCUPS::SpoolPrintJob( | 754 PlatformJobId PrintSystemCUPS::SpoolPrintJob( |
741 const std::string& print_ticket, | 755 const std::string& print_ticket, |
742 const FilePath& print_data_file_path, | 756 const FilePath& print_data_file_path, |
743 const std::string& print_data_mime_type, | 757 const std::string& print_data_mime_type, |
744 const std::string& printer_name, | 758 const std::string& printer_name, |
(...skipping 30 matching lines...) Expand all Loading... | |
775 std::map<std::string, std::string>::iterator it; | 789 std::map<std::string, std::string>::iterator it; |
776 | 790 |
777 for (it = options.begin(); it != options.end(); ++it) { | 791 for (it = options.begin(); it != options.end(); ++it) { |
778 cups_option_t opt; | 792 cups_option_t opt; |
779 opt.name = const_cast<char*>(it->first.c_str()); | 793 opt.name = const_cast<char*>(it->first.c_str()); |
780 opt.value = const_cast<char*>(it->second.c_str()); | 794 opt.value = const_cast<char*>(it->second.c_str()); |
781 cups_options.push_back(opt); | 795 cups_options.push_back(opt); |
782 } | 796 } |
783 | 797 |
784 int job_id = PrintFile(server_info->url, | 798 int job_id = PrintFile(server_info->url, |
799 cups_encryption_, | |
785 short_printer_name.c_str(), | 800 short_printer_name.c_str(), |
786 print_data_file_path.value().c_str(), | 801 print_data_file_path.value().c_str(), |
787 job_title.c_str(), | 802 job_title.c_str(), |
788 cups_options.size(), | 803 cups_options.size(), |
789 &(cups_options[0])); | 804 &(cups_options[0])); |
790 | 805 |
791 VLOG(1) << "CP_CUPS: Job spooled, id: " << job_id; | 806 VLOG(1) << "CP_CUPS: Job spooled, id: " << job_id; |
792 | 807 |
793 return job_id; | 808 return job_id; |
794 } | 809 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
837 | 852 |
838 void PrintSystemCUPS::RunCapsCallback( | 853 void PrintSystemCUPS::RunCapsCallback( |
839 const PrinterCapsAndDefaultsCallback& callback, | 854 const PrinterCapsAndDefaultsCallback& callback, |
840 bool succeeded, | 855 bool succeeded, |
841 const std::string& printer_name, | 856 const std::string& printer_name, |
842 const printing::PrinterCapsAndDefaults& printer_info) { | 857 const printing::PrinterCapsAndDefaults& printer_info) { |
843 callback.Run(succeeded, printer_name, printer_info); | 858 callback.Run(succeeded, printer_name, printer_info); |
844 } | 859 } |
845 | 860 |
846 } // namespace cloud_print | 861 } // namespace cloud_print |
OLD | NEW |