| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 class PrintSystemCUPS : public PrintSystem { | 70 class PrintSystemCUPS : public PrintSystem { |
| 71 public: | 71 public: |
| 72 explicit PrintSystemCUPS(const DictionaryValue* print_system_settings); | 72 explicit PrintSystemCUPS(const DictionaryValue* print_system_settings); |
| 73 | 73 |
| 74 // PrintSystem implementation. | 74 // PrintSystem implementation. |
| 75 virtual void Init(); | 75 virtual void Init(); |
| 76 | 76 |
| 77 virtual void EnumeratePrinters(printing::PrinterList* printer_list); | 77 virtual void EnumeratePrinters(printing::PrinterList* printer_list); |
| 78 | 78 |
| 79 virtual bool GetPrinterCapsAndDefaults( | 79 virtual void GetPrinterCapsAndDefaults( |
| 80 const std::string& printer_name, | 80 const std::string& printer_name, |
| 81 printing::PrinterCapsAndDefaults* printer_info); | 81 PrinterCapsAndDefaultsCallback* callback); |
| 82 | 82 |
| 83 virtual bool IsValidPrinter(const std::string& printer_name); | 83 virtual bool IsValidPrinter(const std::string& printer_name); |
| 84 | 84 |
| 85 virtual bool ValidatePrintTicket(const std::string& printer_name, | 85 virtual bool ValidatePrintTicket(const std::string& printer_name, |
| 86 const std::string& print_ticket_data); | 86 const std::string& print_ticket_data); |
| 87 | 87 |
| 88 virtual bool GetJobDetails(const std::string& printer_name, | 88 virtual bool GetJobDetails(const std::string& printer_name, |
| 89 PlatformJobId job_id, | 89 PlatformJobId job_id, |
| 90 PrintJobDetails *job_details); | 90 PrintJobDetails *job_details); |
| 91 | 91 |
| 92 virtual PrintSystem::PrintServerWatcher* CreatePrintServerWatcher(); | 92 virtual PrintSystem::PrintServerWatcher* CreatePrintServerWatcher(); |
| 93 virtual PrintSystem::PrinterWatcher* CreatePrinterWatcher( | 93 virtual PrintSystem::PrinterWatcher* CreatePrinterWatcher( |
| 94 const std::string& printer_name); | 94 const std::string& printer_name); |
| 95 virtual PrintSystem::JobSpooler* CreateJobSpooler(); | 95 virtual PrintSystem::JobSpooler* CreateJobSpooler(); |
| 96 | 96 |
| 97 // Helper functions. | 97 // Helper functions. |
| 98 PlatformJobId SpoolPrintJob(const std::string& print_ticket, | 98 PlatformJobId SpoolPrintJob(const std::string& print_ticket, |
| 99 const FilePath& print_data_file_path, | 99 const FilePath& print_data_file_path, |
| 100 const std::string& print_data_mime_type, | 100 const std::string& print_data_mime_type, |
| 101 const std::string& printer_name, | 101 const std::string& printer_name, |
| 102 const std::string& job_title, | 102 const std::string& job_title, |
| 103 const std::vector<std::string>& tags, | 103 const std::vector<std::string>& tags, |
| 104 bool* dry_run); | 104 bool* dry_run); |
| 105 bool GetPrinterInfo(const std::string& printer_name, | 105 bool GetPrinterInfo(const std::string& printer_name, |
| 106 printing::PrinterBasicInfo* info); | 106 printing::PrinterBasicInfo* info); |
| 107 bool ParsePrintTicket(const std::string& print_ticket, | 107 bool ParsePrintTicket(const std::string& print_ticket, |
| 108 std::map<std::string, std::string>* options); | 108 std::map<std::string, std::string>* options); |
| 109 | 109 |
| 110 // Synchronous version of GetPrinterCapsAndDefaults. |
| 111 bool GetPrinterCapsAndDefaults( |
| 112 const std::string& printer_name, |
| 113 printing::PrinterCapsAndDefaults* printer_info); |
| 114 |
| 110 int GetUpdateTimeoutMs() const { | 115 int GetUpdateTimeoutMs() const { |
| 111 return update_timeout_; | 116 return update_timeout_; |
| 112 } | 117 } |
| 113 | 118 |
| 114 private: | 119 private: |
| 115 // Following functions are wrappers around corresponding CUPS functions. | 120 // Following functions are wrappers around corresponding CUPS functions. |
| 116 // <functions>2() are called when print server is specified, and plain | 121 // <functions>2() are called when print server is specified, and plain |
| 117 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT | 122 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT |
| 118 // in the <functions>2(), it does not work in CUPS prior to 1.4. | 123 // in the <functions>2(), it does not work in CUPS prior to 1.4. |
| 119 int GetJobs(cups_job_t** jobs, const GURL& url, const char* name, | 124 int GetJobs(cups_job_t** jobs, const GURL& url, const char* name, |
| 120 int myjobs, int whichjobs); | 125 int myjobs, int whichjobs); |
| 121 int PrintFile(const GURL& url, const char* name, const char* filename, | 126 int PrintFile(const GURL& url, const char* name, const char* filename, |
| 122 const char* title, int num_options, cups_option_t* options); | 127 const char* title, int num_options, cups_option_t* options); |
| 123 | 128 |
| 124 void InitPrintBackends(const DictionaryValue* print_system_settings); | 129 void InitPrintBackends(const DictionaryValue* print_system_settings); |
| 125 void AddPrintServer(const std::string& url); | 130 void AddPrintServer(const std::string& url); |
| 126 | 131 |
| 127 void UpdatePrinters(); | 132 void UpdatePrinters(); |
| 128 | 133 |
| 129 // Full name contains print server url:port and printer name. Short name | 134 // Full name contains print server url:port and printer name. Short name |
| 130 // is the name of the printer in the CUPS server. | 135 // is the name of the printer in the CUPS server. |
| 131 std::string MakeFullPrinterName(const GURL& url, | 136 std::string MakeFullPrinterName(const GURL& url, |
| 132 const std::string& short_printer_name); | 137 const std::string& short_printer_name); |
| 133 PrintServerInfoCUPS* FindServerByFullName( | 138 PrintServerInfoCUPS* FindServerByFullName( |
| 134 const std::string& full_printer_name, std::string* short_printer_name); | 139 const std::string& full_printer_name, std::string* short_printer_name); |
| 135 | 140 |
| 141 // Helper method to invoke a PrinterCapsAndDefaultsCallback. |
| 142 static void RunCapsCallback( |
| 143 PrinterCapsAndDefaultsCallback* callback, |
| 144 bool succeeded, |
| 145 const std::string& printer_name, |
| 146 const printing::PrinterCapsAndDefaults& printer_info); |
| 147 |
| 136 // PrintServerList contains information about all print servers and backends | 148 // PrintServerList contains information about all print servers and backends |
| 137 // this proxy is connected to. | 149 // this proxy is connected to. |
| 138 typedef std::list<PrintServerInfoCUPS> PrintServerList; | 150 typedef std::list<PrintServerInfoCUPS> PrintServerList; |
| 139 PrintServerList print_servers_; | 151 PrintServerList print_servers_; |
| 140 | 152 |
| 141 int update_timeout_; | 153 int update_timeout_; |
| 142 bool initialized_; | 154 bool initialized_; |
| 143 }; | 155 }; |
| 144 | 156 |
| 145 class PrintServerWatcherCUPS | 157 class PrintServerWatcherCUPS |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 DCHECK(initialized_); | 435 DCHECK(initialized_); |
| 424 printer_list->clear(); | 436 printer_list->clear(); |
| 425 PrintServerList::iterator it; | 437 PrintServerList::iterator it; |
| 426 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { | 438 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { |
| 427 printer_list->insert(printer_list->end(), | 439 printer_list->insert(printer_list->end(), |
| 428 it->printers.begin(), it->printers.end()); | 440 it->printers.begin(), it->printers.end()); |
| 429 } | 441 } |
| 430 VLOG(1) << "CUPS: Total " << printer_list->size() << " printers enumerated."; | 442 VLOG(1) << "CUPS: Total " << printer_list->size() << " printers enumerated."; |
| 431 } | 443 } |
| 432 | 444 |
| 433 bool PrintSystemCUPS::GetPrinterCapsAndDefaults( | 445 void PrintSystemCUPS::GetPrinterCapsAndDefaults( |
| 434 const std::string& printer_name, | 446 const std::string& printer_name, |
| 435 printing::PrinterCapsAndDefaults* printer_info) { | 447 PrinterCapsAndDefaultsCallback* callback) { |
| 436 DCHECK(initialized_); | 448 printing::PrinterCapsAndDefaults printer_info; |
| 437 std::string short_printer_name; | 449 bool succeeded = GetPrinterCapsAndDefaults(printer_name, &printer_info); |
| 438 PrintServerInfoCUPS* server_info = | 450 MessageLoop::current()->PostTask( |
| 439 FindServerByFullName(printer_name, &short_printer_name); | 451 FROM_HERE, |
| 440 if (!server_info) | 452 NewRunnableFunction(&PrintSystemCUPS::RunCapsCallback, |
| 441 return false; | 453 callback, |
| 442 | 454 succeeded, |
| 443 PrintServerInfoCUPS::CapsMap::iterator caps_it = | 455 printer_name, |
| 444 server_info->caps_cache.find(printer_name); | 456 printer_info)); |
| 445 if (caps_it != server_info->caps_cache.end()) { | |
| 446 *printer_info = caps_it->second; | |
| 447 return true; | |
| 448 } | |
| 449 | |
| 450 // TODO(gene): Retry multiple times in case of error. | |
| 451 if (!server_info->backend->GetPrinterCapsAndDefaults(short_printer_name, | |
| 452 printer_info) ) { | |
| 453 return false; | |
| 454 } | |
| 455 | |
| 456 server_info->caps_cache[printer_name] = *printer_info; | |
| 457 return true; | |
| 458 } | 457 } |
| 459 | 458 |
| 460 bool PrintSystemCUPS::IsValidPrinter(const std::string& printer_name) { | 459 bool PrintSystemCUPS::IsValidPrinter(const std::string& printer_name) { |
| 461 return GetPrinterInfo(printer_name, NULL); | 460 return GetPrinterInfo(printer_name, NULL); |
| 462 } | 461 } |
| 463 | 462 |
| 464 bool PrintSystemCUPS::ValidatePrintTicket(const std::string& printer_name, | 463 bool PrintSystemCUPS::ValidatePrintTicket(const std::string& printer_name, |
| 465 const std::string& print_ticket_data) { | 464 const std::string& print_ticket_data) { |
| 466 DCHECK(initialized_); | 465 DCHECK(initialized_); |
| 467 scoped_ptr<Value> ticket_value(base::JSONReader::Read(print_ticket_data, | 466 scoped_ptr<Value> ticket_value(base::JSONReader::Read(print_ticket_data, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 485 const std::string& key = *it; | 484 const std::string& key = *it; |
| 486 std::string value; | 485 std::string value; |
| 487 if (ticket_dict->GetString(key, &value)) { | 486 if (ticket_dict->GetString(key, &value)) { |
| 488 (*options)[key] = value; | 487 (*options)[key] = value; |
| 489 } | 488 } |
| 490 } | 489 } |
| 491 | 490 |
| 492 return true; | 491 return true; |
| 493 } | 492 } |
| 494 | 493 |
| 494 bool PrintSystemCUPS::GetPrinterCapsAndDefaults( |
| 495 const std::string& printer_name, |
| 496 printing::PrinterCapsAndDefaults* printer_info) { |
| 497 DCHECK(initialized_); |
| 498 std::string short_printer_name; |
| 499 PrintServerInfoCUPS* server_info = |
| 500 FindServerByFullName(printer_name, &short_printer_name); |
| 501 if (!server_info) |
| 502 return false; |
| 503 |
| 504 PrintServerInfoCUPS::CapsMap::iterator caps_it = |
| 505 server_info->caps_cache.find(printer_name); |
| 506 if (caps_it != server_info->caps_cache.end()) { |
| 507 *printer_info = caps_it->second; |
| 508 return true; |
| 509 } |
| 510 |
| 511 // TODO(gene): Retry multiple times in case of error. |
| 512 if (!server_info->backend->GetPrinterCapsAndDefaults(short_printer_name, |
| 513 printer_info) ) { |
| 514 return false; |
| 515 } |
| 516 |
| 517 server_info->caps_cache[printer_name] = *printer_info; |
| 518 return true; |
| 519 } |
| 520 |
| 495 bool PrintSystemCUPS::GetJobDetails(const std::string& printer_name, | 521 bool PrintSystemCUPS::GetJobDetails(const std::string& printer_name, |
| 496 PlatformJobId job_id, | 522 PlatformJobId job_id, |
| 497 PrintJobDetails *job_details) { | 523 PrintJobDetails *job_details) { |
| 498 DCHECK(initialized_); | 524 DCHECK(initialized_); |
| 499 DCHECK(job_details); | 525 DCHECK(job_details); |
| 500 | 526 |
| 501 std::string short_printer_name; | 527 std::string short_printer_name; |
| 502 PrintServerInfoCUPS* server_info = | 528 PrintServerInfoCUPS* server_info = |
| 503 FindServerByFullName(printer_name, &short_printer_name); | 529 FindServerByFullName(printer_name, &short_printer_name); |
| 504 if (!server_info) | 530 if (!server_info) |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 730 if (cur_server == server) { | 756 if (cur_server == server) { |
| 731 *short_printer_name = full_printer_name.substr(separator + 1); | 757 *short_printer_name = full_printer_name.substr(separator + 1); |
| 732 return &(*it); | 758 return &(*it); |
| 733 } | 759 } |
| 734 } | 760 } |
| 735 | 761 |
| 736 LOG(WARNING) << "Server not found for printer: " << full_printer_name; | 762 LOG(WARNING) << "Server not found for printer: " << full_printer_name; |
| 737 return NULL; | 763 return NULL; |
| 738 } | 764 } |
| 739 | 765 |
| 766 void PrintSystemCUPS::RunCapsCallback( |
| 767 PrinterCapsAndDefaultsCallback* callback, |
| 768 bool succeeded, |
| 769 const std::string& printer_name, |
| 770 const printing::PrinterCapsAndDefaults& printer_info) { |
| 771 callback->Run(succeeded, printer_name, printer_info); |
| 772 delete callback; |
| 773 } |
| 774 |
| 740 } // namespace cloud_print | 775 } // namespace cloud_print |
| OLD | NEW |