OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "printing/backend/print_backend.h" | 5 #include "printing/backend/print_backend.h" |
6 | 6 |
7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #if !defined(OS_MACOSX) | 9 #if !defined(OS_MACOSX) |
10 #include <gcrypt.h> | 10 #include <gcrypt.h> |
11 #endif | 11 #endif |
12 #include <pthread.h> | 12 #include <pthread.h> |
13 | 13 |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
21 #include "printing/backend/cups_helper.h" | 21 #include "printing/backend/cups_helper.h" |
22 #include "printing/backend/print_backend_consts.h" | 22 #include "printing/backend/print_backend_consts.h" |
23 | 23 |
24 #if !defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
25 // Mac still builds against the 10.5 SDK, which does not have this in cups.h. | |
Mark Mentovai
2011/08/08 21:02:07
Don’t do this. You need more #if:
#if defined(OS_
Lei Zhang
2011/08/08 22:04:02
Mark: Thanks, more #ifs added.
On 2011/08/08 21:0
| |
26 #define CUPS_PRINTER_SCANNER 0x2000000 | |
27 #else | |
25 GCRY_THREAD_OPTION_PTHREAD_IMPL; | 28 GCRY_THREAD_OPTION_PTHREAD_IMPL; |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 // Init GCrypt library (needed for CUPS) using pthreads. | 32 // Init GCrypt library (needed for CUPS) using pthreads. |
30 // There exists a bug in CUPS library, where it crashed with: "ath.c:184: | 33 // There exists a bug in CUPS library, where it crashed with: "ath.c:184: |
31 // _gcry_ath_mutex_lock: Assertion `*lock == ((ath_mutex_t) 0)' failed." | 34 // _gcry_ath_mutex_lock: Assertion `*lock == ((ath_mutex_t) 0)' failed." |
32 // It happened when multiple threads tried printing simultaneously. | 35 // It happened when multiple threads tried printing simultaneously. |
33 // Google search for 'gnutls thread safety' provided a solution that | 36 // Google search for 'gnutls thread safety' provided a solution that |
34 // initialized gcrypt and gnutls. | 37 // initialized gcrypt and gnutls. |
(...skipping 29 matching lines...) Expand all Loading... | |
64 } | 67 } |
65 if ((*pgnutls_global_init)() != 0) | 68 if ((*pgnutls_global_init)() != 0) |
66 LOG(ERROR) << "Gnutls initialization failed"; | 69 LOG(ERROR) << "Gnutls initialization failed"; |
67 } | 70 } |
68 }; | 71 }; |
69 | 72 |
70 static base::LazyInstance<GcryptInitializer> g_gcrypt_initializer( | 73 static base::LazyInstance<GcryptInitializer> g_gcrypt_initializer( |
71 base::LINKER_INITIALIZED); | 74 base::LINKER_INITIALIZED); |
72 | 75 |
73 } // namespace | 76 } // namespace |
74 #endif | 77 #endif // defined(OS_MACOSX) |
75 | 78 |
76 namespace printing { | 79 namespace printing { |
77 | 80 |
78 static const char kCUPSPrinterInfoOpt[] = "printer-info"; | 81 static const char kCUPSPrinterInfoOpt[] = "printer-info"; |
79 static const char kCUPSPrinterStateOpt[] = "printer-state"; | 82 static const char kCUPSPrinterStateOpt[] = "printer-state"; |
83 static const char kCUPSPrinterTypeOpt[] = "printer-type"; | |
80 | 84 |
81 class PrintBackendCUPS : public PrintBackend { | 85 class PrintBackendCUPS : public PrintBackend { |
82 public: | 86 public: |
83 PrintBackendCUPS(const GURL& print_server_url, bool blocking); | 87 PrintBackendCUPS(const GURL& print_server_url, bool blocking); |
84 virtual ~PrintBackendCUPS() {} | 88 virtual ~PrintBackendCUPS() {} |
85 | 89 |
86 // PrintBackend implementation. | 90 // PrintBackend implementation. |
87 virtual bool EnumeratePrinters(PrinterList* printer_list); | 91 virtual bool EnumeratePrinters(PrinterList* printer_list); |
88 | 92 |
89 virtual std::string GetDefaultPrinterName(); | 93 virtual std::string GetDefaultPrinterName(); |
(...skipping 29 matching lines...) Expand all Loading... | |
119 VLOG(1) << "CP_CUPS: Error getting printers from CUPS server. Server: " | 123 VLOG(1) << "CP_CUPS: Error getting printers from CUPS server. Server: " |
120 << print_server_url_ | 124 << print_server_url_ |
121 << " Error: " | 125 << " Error: " |
122 << static_cast<int>(cupsLastError()); | 126 << static_cast<int>(cupsLastError()); |
123 return false; | 127 return false; |
124 } | 128 } |
125 | 129 |
126 for (int printer_index = 0; printer_index < num_dests; printer_index++) { | 130 for (int printer_index = 0; printer_index < num_dests; printer_index++) { |
127 const cups_dest_t& printer = destinations[printer_index]; | 131 const cups_dest_t& printer = destinations[printer_index]; |
128 | 132 |
133 // CUPS can have 'printers' that are actually scanners. (not MFC) | |
134 // At least on Mac. Check for scanners and skip them. | |
135 const char* type_str = cupsGetOption(kCUPSPrinterTypeOpt, | |
136 printer.num_options, printer.options); | |
137 if (type_str != NULL) { | |
138 int type; | |
139 if (base::StringToInt(type_str, &type) && (type & CUPS_PRINTER_SCANNER)) | |
140 continue; | |
141 } | |
142 | |
129 PrinterBasicInfo printer_info; | 143 PrinterBasicInfo printer_info; |
130 printer_info.printer_name = printer.name; | 144 printer_info.printer_name = printer.name; |
131 printer_info.is_default = printer.is_default; | 145 printer_info.is_default = printer.is_default; |
132 | 146 |
133 const char* info = cupsGetOption(kCUPSPrinterInfoOpt, | 147 const char* info = cupsGetOption(kCUPSPrinterInfoOpt, |
134 printer.num_options, printer.options); | 148 printer.num_options, printer.options); |
135 if (info != NULL) | 149 if (info != NULL) |
136 printer_info.printer_description = info; | 150 printer_info.printer_description = info; |
137 | 151 |
138 const char* state = cupsGetOption(kCUPSPrinterStateOpt, | 152 const char* state = cupsGetOption(kCUPSPrinterStateOpt, |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 << ", HTTP error: " << http_error; | 292 << ", HTTP error: " << http_error; |
279 file_util::Delete(ppd_path, false); | 293 file_util::Delete(ppd_path, false); |
280 ppd_path.clear(); | 294 ppd_path.clear(); |
281 } | 295 } |
282 } | 296 } |
283 } | 297 } |
284 return ppd_path; | 298 return ppd_path; |
285 } | 299 } |
286 | 300 |
287 } // namespace printing | 301 } // namespace printing |
OLD | NEW |