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 bool printer_enum_succeeded_; |
154 }; | 158 }; |
155 | 159 |
156 class PrintServerWatcherCUPS | 160 class PrintServerWatcherCUPS |
157 : public PrintSystem::PrintServerWatcher { | 161 : public PrintSystem::PrintServerWatcher { |
158 public: | 162 public: |
159 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) | 163 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) |
160 : print_system_(print_system), | 164 : print_system_(print_system), |
161 delegate_(NULL) { | 165 delegate_(NULL) { |
162 } | 166 } |
163 ~PrintServerWatcherCUPS() { | 167 ~PrintServerWatcherCUPS() { |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 delegate->OnJobSpoolSucceeded(job_id); | 355 delegate->OnJobSpoolSucceeded(job_id); |
352 else | 356 else |
353 delegate->OnJobSpoolFailed(); | 357 delegate->OnJobSpoolFailed(); |
354 } | 358 } |
355 private: | 359 private: |
356 scoped_refptr<PrintSystemCUPS> print_system_; | 360 scoped_refptr<PrintSystemCUPS> print_system_; |
357 DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS); | 361 DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS); |
358 }; | 362 }; |
359 | 363 |
360 PrintSystemCUPS::PrintSystemCUPS(const DictionaryValue* print_system_settings) | 364 PrintSystemCUPS::PrintSystemCUPS(const DictionaryValue* print_system_settings) |
361 : update_timeout_(kCheckForPrinterUpdatesMs), initialized_(false) { | 365 : update_timeout_(kCheckForPrinterUpdatesMs), |
| 366 initialized_(false), |
| 367 printer_enum_succeeded_(false) { |
362 if (print_system_settings) { | 368 if (print_system_settings) { |
363 int timeout; | 369 int timeout; |
364 if (print_system_settings->GetInteger(kCUPSUpdateTimeoutMs, &timeout)) | 370 if (print_system_settings->GetInteger(kCUPSUpdateTimeoutMs, &timeout)) |
365 update_timeout_ = timeout; | 371 update_timeout_ = timeout; |
366 } | 372 } |
367 | 373 |
368 InitPrintBackends(print_system_settings); | 374 InitPrintBackends(print_system_settings); |
369 } | 375 } |
370 | 376 |
371 void PrintSystemCUPS::InitPrintBackends( | 377 void PrintSystemCUPS::InitPrintBackends( |
(...skipping 33 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 printer_enum_succeeded_ = true; |
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 printer_enum_succeeded_ = false; |
417 it->caps_cache.clear(); | 425 it->caps_cache.clear(); |
418 printing::PrinterList::iterator printer_it; | 426 printing::PrinterList::iterator printer_it; |
419 for (printer_it = it->printers.begin(); | 427 for (printer_it = it->printers.begin(); |
420 printer_it != it->printers.end(); ++printer_it) { | 428 printer_it != it->printers.end(); ++printer_it) { |
421 printer_it->printer_name = MakeFullPrinterName(it->url, | 429 printer_it->printer_name = MakeFullPrinterName(it->url, |
422 printer_it->printer_name); | 430 printer_it->printer_name); |
423 } | 431 } |
424 VLOG(1) << "CUPS: Updated printer list for url: " << it->url | 432 VLOG(1) << "CUPS: Updated printer list for url: " << it->url |
425 << " Number of printers: " << it->printers.size(); | 433 << " Number of printers: " << it->printers.size(); |
426 } | 434 } |
427 | 435 |
428 // Schedule next update. | 436 // Schedule next update. |
429 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 437 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
430 NewRunnableMethod(this, &PrintSystemCUPS::UpdatePrinters), | 438 NewRunnableMethod(this, &PrintSystemCUPS::UpdatePrinters), |
431 GetUpdateTimeoutMs()); | 439 GetUpdateTimeoutMs()); |
432 } | 440 } |
433 | 441 |
434 void PrintSystemCUPS::EnumeratePrinters(printing::PrinterList* printer_list) { | 442 PrintSystem::PrintSystemResult PrintSystemCUPS::EnumeratePrinters( |
| 443 printing::PrinterList* printer_list) { |
435 DCHECK(initialized_); | 444 DCHECK(initialized_); |
436 printer_list->clear(); | 445 printer_list->clear(); |
437 PrintServerList::iterator it; | 446 PrintServerList::iterator it; |
438 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { | 447 for (it = print_servers_.begin(); it != print_servers_.end(); ++it) { |
439 printer_list->insert(printer_list->end(), | 448 printer_list->insert(printer_list->end(), |
440 it->printers.begin(), it->printers.end()); | 449 it->printers.begin(), it->printers.end()); |
441 } | 450 } |
442 VLOG(1) << "CUPS: Total " << printer_list->size() << " printers enumerated."; | 451 VLOG(1) << "CUPS: Total " << printer_list->size() << " printers enumerated."; |
| 452 // TODO(sanjeevr): Maybe some day we want to report the actual server names |
| 453 // for which the enumeration failed. |
| 454 return PrintSystemResult(printer_enum_succeeded_, std::string()); |
443 } | 455 } |
444 | 456 |
445 void PrintSystemCUPS::GetPrinterCapsAndDefaults( | 457 void PrintSystemCUPS::GetPrinterCapsAndDefaults( |
446 const std::string& printer_name, | 458 const std::string& printer_name, |
447 PrinterCapsAndDefaultsCallback* callback) { | 459 PrinterCapsAndDefaultsCallback* callback) { |
448 printing::PrinterCapsAndDefaults printer_info; | 460 printing::PrinterCapsAndDefaults printer_info; |
449 bool succeeded = GetPrinterCapsAndDefaults(printer_name, &printer_info); | 461 bool succeeded = GetPrinterCapsAndDefaults(printer_name, &printer_info); |
450 MessageLoop::current()->PostTask( | 462 MessageLoop::current()->PostTask( |
451 FROM_HERE, | 463 FROM_HERE, |
452 NewRunnableFunction(&PrintSystemCUPS::RunCapsCallback, | 464 NewRunnableFunction(&PrintSystemCUPS::RunCapsCallback, |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 void PrintSystemCUPS::RunCapsCallback( | 778 void PrintSystemCUPS::RunCapsCallback( |
767 PrinterCapsAndDefaultsCallback* callback, | 779 PrinterCapsAndDefaultsCallback* callback, |
768 bool succeeded, | 780 bool succeeded, |
769 const std::string& printer_name, | 781 const std::string& printer_name, |
770 const printing::PrinterCapsAndDefaults& printer_info) { | 782 const printing::PrinterCapsAndDefaults& printer_info) { |
771 callback->Run(succeeded, printer_name, printer_info); | 783 callback->Run(succeeded, printer_name, printer_info); |
772 delete callback; | 784 delete callback; |
773 } | 785 } |
774 | 786 |
775 } // namespace cloud_print | 787 } // namespace cloud_print |
OLD | NEW |