Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/scoped_ptr.h" | 22 #include "base/scoped_ptr.h" |
| 23 #include "base/string_number_conversions.h" | 23 #include "base/string_number_conversions.h" |
| 24 #include "base/string_util.h" | 24 #include "base/string_util.h" |
| 25 #include "base/task.h" | 25 #include "base/task.h" |
| 26 #include "base/values.h" | 26 #include "base/values.h" |
| 27 #include "base/utf_string_conversions.h" | 27 #include "base/utf_string_conversions.h" |
| 28 #include "chrome/service/cloud_print/cloud_print_consts.h" | 28 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 29 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 29 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 30 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
| 31 #include "grit/generated_resources.h" | |
| 31 #include "printing/backend/cups_helper.h" | 32 #include "printing/backend/cups_helper.h" |
| 32 #include "printing/backend/print_backend.h" | 33 #include "printing/backend/print_backend.h" |
| 33 #include "printing/backend/print_backend_consts.h" | 34 #include "printing/backend/print_backend_consts.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | |
| 34 | 36 |
| 35 namespace { | 37 namespace { |
| 36 static const char kCUPSPrinterInfoOpt[] = "printer-info"; | 38 static const char kCUPSPrinterInfoOpt[] = "printer-info"; |
| 37 static const char kCUPSPrinterStateOpt[] = "printer-state"; | 39 static const char kCUPSPrinterStateOpt[] = "printer-state"; |
| 38 static const char kCUPSPrintServerURLs[] = "print_server_urls"; | 40 static const char kCUPSPrintServerURLs[] = "print_server_urls"; |
| 39 static const char kCUPSUpdateTimeoutMs[] = "update_timeout_ms"; | 41 static const char kCUPSUpdateTimeoutMs[] = "update_timeout_ms"; |
| 40 | 42 |
| 41 // Default port for IPP print servers. | 43 // Default port for IPP print servers. |
| 42 static const int kDefaultIPPServerPort = 631; | 44 static const int kDefaultIPPServerPort = 631; |
| 43 | 45 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 66 CapsMap caps_cache; | 68 CapsMap caps_cache; |
| 67 }; | 69 }; |
| 68 | 70 |
| 69 class PrintSystemCUPS : public PrintSystem { | 71 class PrintSystemCUPS : public PrintSystem { |
| 70 public: | 72 public: |
| 71 explicit PrintSystemCUPS(const DictionaryValue* print_system_settings); | 73 explicit PrintSystemCUPS(const DictionaryValue* print_system_settings); |
| 72 | 74 |
| 73 // PrintSystem implementation. | 75 // PrintSystem implementation. |
| 74 virtual PrintSystemResult Init(); | 76 virtual PrintSystemResult Init(); |
| 75 | 77 |
| 76 virtual void EnumeratePrinters(printing::PrinterList* printer_list); | 78 virtual PrintSystem::PrintSystemResult EnumeratePrinters( |
| 79 printing::PrinterList* printer_list); | |
| 77 | 80 |
| 78 virtual void GetPrinterCapsAndDefaults( | 81 virtual void GetPrinterCapsAndDefaults( |
| 79 const std::string& printer_name, | 82 const std::string& printer_name, |
| 80 PrinterCapsAndDefaultsCallback* callback); | 83 PrinterCapsAndDefaultsCallback* callback); |
| 81 | 84 |
| 82 virtual bool IsValidPrinter(const std::string& printer_name); | 85 virtual bool IsValidPrinter(const std::string& printer_name); |
| 83 | 86 |
| 84 virtual bool ValidatePrintTicket(const std::string& printer_name, | 87 virtual bool ValidatePrintTicket(const std::string& printer_name, |
| 85 const std::string& print_ticket_data); | 88 const std::string& print_ticket_data); |
| 86 | 89 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 const std::string& printer_name, | 147 const std::string& printer_name, |
| 145 const printing::PrinterCapsAndDefaults& printer_info); | 148 const printing::PrinterCapsAndDefaults& printer_info); |
| 146 | 149 |
| 147 // PrintServerList contains information about all print servers and backends | 150 // PrintServerList contains information about all print servers and backends |
| 148 // this proxy is connected to. | 151 // this proxy is connected to. |
| 149 typedef std::list<PrintServerInfoCUPS> PrintServerList; | 152 typedef std::list<PrintServerInfoCUPS> PrintServerList; |
| 150 PrintServerList print_servers_; | 153 PrintServerList print_servers_; |
| 151 | 154 |
| 152 int update_timeout_; | 155 int update_timeout_; |
| 153 bool initialized_; | 156 bool initialized_; |
| 157 // A comma separated string of print server names for which enumeration of | |
| 158 // printers failed. This is used to prepare a diagnostic error message. | |
| 159 std::string enum_failed_server_list_; | |
|
gene1
2011/01/24 19:46:47
Can we keep it as a bool flag?
I understand that i
Scott Byer
2011/01/24 20:02:07
Since it's not being used, how about making it a b
| |
| 154 }; | 160 }; |
| 155 | 161 |
| 156 class PrintServerWatcherCUPS | 162 class PrintServerWatcherCUPS |
| 157 : public PrintSystem::PrintServerWatcher { | 163 : public PrintSystem::PrintServerWatcher { |
| 158 public: | 164 public: |
| 159 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) | 165 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) |
| 160 : print_system_(print_system), | 166 : print_system_(print_system), |
| 161 delegate_(NULL) { | 167 delegate_(NULL) { |
| 162 } | 168 } |
| 163 ~PrintServerWatcherCUPS() { | 169 ~PrintServerWatcherCUPS() { |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 } | 411 } |
| 406 | 412 |
| 407 PrintSystem::PrintSystemResult PrintSystemCUPS::Init() { | 413 PrintSystem::PrintSystemResult PrintSystemCUPS::Init() { |
| 408 UpdatePrinters(); | 414 UpdatePrinters(); |
| 409 initialized_ = true; | 415 initialized_ = true; |
| 410 return PrintSystemResult(true, std::string()); | 416 return PrintSystemResult(true, std::string()); |
| 411 } | 417 } |
| 412 | 418 |
| 413 void PrintSystemCUPS::UpdatePrinters() { | 419 void PrintSystemCUPS::UpdatePrinters() { |
| 414 PrintServerList::iterator it; | 420 PrintServerList::iterator it; |
| 421 enum_failed_server_list_.clear(); | |
| 415 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { | 422 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { |
| 416 it->backend->EnumeratePrinters(&it->printers); | 423 if (!it->backend->EnumeratePrinters(&it->printers)) { |
| 424 enum_failed_server_list_.append(it->url.spec()); | |
| 425 enum_failed_server_list_.append(","); | |
| 426 } | |
| 417 it->caps_cache.clear(); | 427 it->caps_cache.clear(); |
| 418 printing::PrinterList::iterator printer_it; | 428 printing::PrinterList::iterator printer_it; |
| 419 for (printer_it = it->printers.begin(); | 429 for (printer_it = it->printers.begin(); |
| 420 printer_it != it->printers.end(); ++printer_it) { | 430 printer_it != it->printers.end(); ++printer_it) { |
| 421 printer_it->printer_name = MakeFullPrinterName(it->url, | 431 printer_it->printer_name = MakeFullPrinterName(it->url, |
| 422 printer_it->printer_name); | 432 printer_it->printer_name); |
| 423 } | 433 } |
| 424 VLOG(1) << "CUPS: Updated printer list for url: " << it->url | 434 VLOG(1) << "CUPS: Updated printer list for url: " << it->url |
| 425 << " Number of printers: " << it->printers.size(); | 435 << " Number of printers: " << it->printers.size(); |
| 426 } | 436 } |
| 427 | 437 |
| 428 // Schedule next update. | 438 // Schedule next update. |
| 429 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 439 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 430 NewRunnableMethod(this, &PrintSystemCUPS::UpdatePrinters), | 440 NewRunnableMethod(this, &PrintSystemCUPS::UpdatePrinters), |
| 431 GetUpdateTimeoutMs()); | 441 GetUpdateTimeoutMs()); |
| 432 } | 442 } |
| 433 | 443 |
| 434 void PrintSystemCUPS::EnumeratePrinters(printing::PrinterList* printer_list) { | 444 PrintSystem::PrintSystemResult PrintSystemCUPS::EnumeratePrinters( |
| 445 printing::PrinterList* printer_list) { | |
| 435 DCHECK(initialized_); | 446 DCHECK(initialized_); |
| 436 printer_list->clear(); | 447 printer_list->clear(); |
| 437 PrintServerList::iterator it; | 448 PrintServerList::iterator it; |
| 438 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { | 449 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { |
| 439 printer_list->insert(printer_list->end(), | 450 printer_list->insert(printer_list->end(), |
| 440 it->printers.begin(), it->printers.end()); | 451 it->printers.begin(), it->printers.end()); |
| 441 } | 452 } |
| 442 VLOG(1) << "CUPS: Total " << printer_list->size() << " printers enumerated."; | 453 VLOG(1) << "CUPS: Total " << printer_list->size() << " printers enumerated."; |
| 454 if (!enum_failed_server_list_.empty()) { | |
| 455 // The enumeration failed on some servers. | |
| 456 // TODO(sanjeevr): Maybe some day we want to report the actual server names. | |
| 457 std::string message = l10n_util::GetStringUTF8(IDS_CLOUD_PRINT_ENUM_FAILED); | |
| 458 return PrintSystemResult(false, message); | |
| 459 } | |
| 460 return PrintSystemResult(true, std::string()); | |
| 443 } | 461 } |
| 444 | 462 |
| 445 void PrintSystemCUPS::GetPrinterCapsAndDefaults( | 463 void PrintSystemCUPS::GetPrinterCapsAndDefaults( |
| 446 const std::string& printer_name, | 464 const std::string& printer_name, |
| 447 PrinterCapsAndDefaultsCallback* callback) { | 465 PrinterCapsAndDefaultsCallback* callback) { |
| 448 printing::PrinterCapsAndDefaults printer_info; | 466 printing::PrinterCapsAndDefaults printer_info; |
| 449 bool succeeded = GetPrinterCapsAndDefaults(printer_name, &printer_info); | 467 bool succeeded = GetPrinterCapsAndDefaults(printer_name, &printer_info); |
| 450 MessageLoop::current()->PostTask( | 468 MessageLoop::current()->PostTask( |
| 451 FROM_HERE, | 469 FROM_HERE, |
| 452 NewRunnableFunction(&PrintSystemCUPS::RunCapsCallback, | 470 NewRunnableFunction(&PrintSystemCUPS::RunCapsCallback, |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 766 void PrintSystemCUPS::RunCapsCallback( | 784 void PrintSystemCUPS::RunCapsCallback( |
| 767 PrinterCapsAndDefaultsCallback* callback, | 785 PrinterCapsAndDefaultsCallback* callback, |
| 768 bool succeeded, | 786 bool succeeded, |
| 769 const std::string& printer_name, | 787 const std::string& printer_name, |
| 770 const printing::PrinterCapsAndDefaults& printer_info) { | 788 const printing::PrinterCapsAndDefaults& printer_info) { |
| 771 callback->Run(succeeded, printer_name, printer_info); | 789 callback->Run(succeeded, printer_name, printer_info); |
| 772 delete callback; | 790 delete callback; |
| 773 } | 791 } |
| 774 | 792 |
| 775 } // namespace cloud_print | 793 } // namespace cloud_print |
| OLD | NEW |