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