| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| 11 #include <pthread.h> | 11 #include <pthread.h> |
| 12 | 12 |
| 13 #if !defined(OS_MACOSX) | |
| 14 #include <gcrypt.h> | |
| 15 #endif | |
| 16 | |
| 17 #include "base/debug/leak_annotations.h" | 13 #include "base/debug/leak_annotations.h" |
| 18 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 19 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 20 #include "base/logging.h" | 16 #include "base/logging.h" |
| 21 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 23 #include "base/values.h" | 19 #include "base/values.h" |
| 24 #include "printing/backend/cups_helper.h" | 20 #include "printing/backend/cups_helper.h" |
| 25 #include "printing/backend/print_backend_consts.h" | 21 #include "printing/backend/print_backend_consts.h" |
| 26 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 27 | 23 |
| 28 #if !defined(OS_MACOSX) | |
| 29 GCRY_THREAD_OPTION_PTHREAD_IMPL; | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Init GCrypt library (needed for CUPS) using pthreads. | |
| 34 // There exists a bug in CUPS library, where it crashed with: "ath.c:184: | |
| 35 // _gcry_ath_mutex_lock: Assertion `*lock == ((ath_mutex_t) 0)' failed." | |
| 36 // It happened when multiple threads tried printing simultaneously. | |
| 37 // Google search for 'gnutls thread safety' provided a solution that | |
| 38 // initialized gcrypt and gnutls. | |
| 39 | |
| 40 // TODO(phajdan.jr): Remove this after https://bugs.g10code.com/gnupg/issue1197 | |
| 41 // gets fixed on all Linux distros we support (i.e. when they ship libgcrypt | |
| 42 // with the fix). | |
| 43 | |
| 44 // Initially, we linked with -lgnutls and simply called gnutls_global_init(), | |
| 45 // but this did not work well since we build one binary on Ubuntu Hardy and | |
| 46 // expect it to run on many Linux distros. (See http://crbug.com/46954) | |
| 47 // So instead we use dlopen() and dlsym() to dynamically load and call | |
| 48 // gnutls_global_init(). | |
| 49 | |
| 50 class GcryptInitializer { | |
| 51 public: | |
| 52 GcryptInitializer() { | |
| 53 Init(); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 void Init() { | |
| 58 const char* kGnuTlsFiles[] = { | |
| 59 "libgnutls.so.28", | |
| 60 "libgnutls.so.26", | |
| 61 "libgnutls.so", | |
| 62 }; | |
| 63 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); | |
| 64 for (size_t i = 0; i < arraysize(kGnuTlsFiles); ++i) { | |
| 65 void* gnutls_lib = dlopen(kGnuTlsFiles[i], RTLD_NOW); | |
| 66 if (!gnutls_lib) { | |
| 67 VLOG(1) << "Cannot load " << kGnuTlsFiles[i]; | |
| 68 continue; | |
| 69 } | |
| 70 const char* kGnuTlsInitFuncName = "gnutls_global_init"; | |
| 71 int (*pgnutls_global_init)(void) = reinterpret_cast<int(*)()>( | |
| 72 dlsym(gnutls_lib, kGnuTlsInitFuncName)); | |
| 73 if (!pgnutls_global_init) { | |
| 74 VLOG(1) << "Could not find " << kGnuTlsInitFuncName | |
| 75 << " in " << kGnuTlsFiles[i]; | |
| 76 continue; | |
| 77 } | |
| 78 { | |
| 79 // GnuTLS has a genuine small memory leak that is easier to annotate | |
| 80 // than suppress. See http://crbug.com/176888#c7 | |
| 81 // TODO(earthdok): remove this once the leak is fixed. | |
| 82 ANNOTATE_SCOPED_MEMORY_LEAK; | |
| 83 if ((*pgnutls_global_init)() != 0) | |
| 84 LOG(ERROR) << "gnutls_global_init() failed"; | |
| 85 } | |
| 86 return; | |
| 87 } | |
| 88 LOG(ERROR) << "Cannot find libgnutls"; | |
| 89 } | |
| 90 }; | |
| 91 | |
| 92 base::LazyInstance<GcryptInitializer> g_gcrypt_initializer = | |
| 93 LAZY_INSTANCE_INITIALIZER; | |
| 94 | |
| 95 } // namespace | |
| 96 #endif // !defined(OS_MACOSX) | |
| 97 | |
| 98 namespace printing { | 24 namespace printing { |
| 99 | 25 |
| 100 static const char kCUPSPrinterInfoOpt[] = "printer-info"; | 26 static const char kCUPSPrinterInfoOpt[] = "printer-info"; |
| 101 static const char kCUPSPrinterStateOpt[] = "printer-state"; | 27 static const char kCUPSPrinterStateOpt[] = "printer-state"; |
| 102 static const char kCUPSPrinterTypeOpt[] = "printer-type"; | 28 static const char kCUPSPrinterTypeOpt[] = "printer-type"; |
| 103 static const char kCUPSPrinterMakeModelOpt[] = "printer-make-and-model"; | 29 static const char kCUPSPrinterMakeModelOpt[] = "printer-make-and-model"; |
| 104 | 30 |
| 105 class PrintBackendCUPS : public PrintBackend { | 31 class PrintBackendCUPS : public PrintBackend { |
| 106 public: | 32 public: |
| 107 PrintBackendCUPS(const GURL& print_server_url, | 33 PrintBackendCUPS(const GURL& print_server_url, |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 | 215 |
| 290 PrinterList::iterator it; | 216 PrinterList::iterator it; |
| 291 for (it = printer_list.begin(); it != printer_list.end(); ++it) | 217 for (it = printer_list.begin(); it != printer_list.end(); ++it) |
| 292 if (it->printer_name == printer_name) | 218 if (it->printer_name == printer_name) |
| 293 return true; | 219 return true; |
| 294 return false; | 220 return false; |
| 295 } | 221 } |
| 296 | 222 |
| 297 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 223 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
| 298 const base::DictionaryValue* print_backend_settings) { | 224 const base::DictionaryValue* print_backend_settings) { |
| 299 #if !defined(OS_MACOSX) | |
| 300 // Initialize gcrypt library. | |
| 301 g_gcrypt_initializer.Get(); | |
| 302 #endif | |
| 303 | |
| 304 std::string print_server_url_str, cups_blocking; | 225 std::string print_server_url_str, cups_blocking; |
| 305 int encryption = HTTP_ENCRYPT_NEVER; | 226 int encryption = HTTP_ENCRYPT_NEVER; |
| 306 if (print_backend_settings) { | 227 if (print_backend_settings) { |
| 307 print_backend_settings->GetString(kCUPSPrintServerURL, | 228 print_backend_settings->GetString(kCUPSPrintServerURL, |
| 308 &print_server_url_str); | 229 &print_server_url_str); |
| 309 | 230 |
| 310 print_backend_settings->GetString(kCUPSBlocking, | 231 print_backend_settings->GetString(kCUPSBlocking, |
| 311 &cups_blocking); | 232 &cups_blocking); |
| 312 | 233 |
| 313 print_backend_settings->GetInteger(kCUPSEncryption, &encryption); | 234 print_backend_settings->GetInteger(kCUPSEncryption, &encryption); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 << ", HTTP error: " << http_error; | 292 << ", HTTP error: " << http_error; |
| 372 base::DeleteFile(ppd_path, false); | 293 base::DeleteFile(ppd_path, false); |
| 373 ppd_path.clear(); | 294 ppd_path.clear(); |
| 374 } | 295 } |
| 375 } | 296 } |
| 376 } | 297 } |
| 377 return ppd_path; | 298 return ppd_path; |
| 378 } | 299 } |
| 379 | 300 |
| 380 } // namespace printing | 301 } // namespace printing |
| OLD | NEW |