Chromium Code Reviews| 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_cups.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <pthread.h> | 9 #include <pthread.h> |
| 10 #include <string> | |
|
Lei Zhang
2016/06/28 22:27:35
Chromium code separates C headers from C++ headers
skau
2016/06/28 23:58:19
Done.
| |
| 10 | 11 |
| 11 #include "base/debug/leak_annotations.h" | 12 #include "base/debug/leak_annotations.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 17 #include "base/values.h" | 18 #include "base/values.h" |
| 18 #include "printing/backend/cups_helper.h" | 19 #include "printing/backend/cups_helper.h" |
| 19 #include "printing/backend/print_backend_consts.h" | 20 #include "printing/backend/print_backend_consts.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 // Store printer options. | 62 // Store printer options. |
| 62 for (int opt_index = 0; opt_index < printer.num_options; ++opt_index) { | 63 for (int opt_index = 0; opt_index < printer.num_options; ++opt_index) { |
| 63 printer_info->options[printer.options[opt_index].name] = | 64 printer_info->options[printer.options[opt_index].name] = |
| 64 printer.options[opt_index].value; | 65 printer.options[opt_index].value; |
| 65 } | 66 } |
| 66 return true; | 67 return true; |
| 67 } | 68 } |
| 68 | 69 |
| 69 } // namespace | 70 } // namespace |
| 70 | 71 |
| 71 class PrintBackendCUPS : public PrintBackend { | |
| 72 public: | |
| 73 PrintBackendCUPS(const GURL& print_server_url, | |
| 74 http_encryption_t encryption, bool blocking); | |
| 75 | |
| 76 private: | |
| 77 ~PrintBackendCUPS() override {} | |
| 78 | |
| 79 // PrintBackend implementation. | |
| 80 bool EnumeratePrinters(PrinterList* printer_list) override; | |
| 81 std::string GetDefaultPrinterName() override; | |
| 82 bool GetPrinterBasicInfo(const std::string& printer_name, | |
| 83 PrinterBasicInfo* printer_info) override; | |
| 84 bool GetPrinterSemanticCapsAndDefaults( | |
| 85 const std::string& printer_name, | |
| 86 PrinterSemanticCapsAndDefaults* printer_info) override; | |
| 87 bool GetPrinterCapsAndDefaults(const std::string& printer_name, | |
| 88 PrinterCapsAndDefaults* printer_info) override; | |
| 89 std::string GetPrinterDriverInfo(const std::string& printer_name) override; | |
| 90 bool IsValidPrinter(const std::string& printer_name) override; | |
| 91 | |
| 92 // The following functions are wrappers around corresponding CUPS functions. | |
| 93 // <functions>2() are called when print server is specified, and plain version | |
| 94 // in another case. There is an issue specifying CUPS_HTTP_DEFAULT in the | |
| 95 // functions>2(), it does not work in CUPS prior to 1.4. | |
| 96 int GetDests(cups_dest_t** dests); | |
| 97 base::FilePath GetPPD(const char* name); | |
| 98 | |
| 99 // Wrapper around cupsGetNamedDest(). Returned result should be freed with | |
| 100 // cupsFreeDests(). | |
| 101 cups_dest_t* GetNamedDest(const std::string& printer_name); | |
| 102 | |
| 103 GURL print_server_url_; | |
| 104 http_encryption_t cups_encryption_; | |
| 105 bool blocking_; | |
| 106 }; | |
| 107 | |
| 108 PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url, | 72 PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url, |
| 109 http_encryption_t encryption, | 73 http_encryption_t encryption, |
| 110 bool blocking) | 74 bool blocking) |
| 111 : print_server_url_(print_server_url), | 75 : print_server_url_(print_server_url), |
| 112 cups_encryption_(encryption), | 76 cups_encryption_(encryption), |
| 113 blocking_(blocking) { | 77 blocking_(blocking) { |
| 114 } | 78 } |
| 115 | 79 |
| 116 bool PrintBackendCUPS::EnumeratePrinters(PrinterList* printer_list) { | 80 bool PrintBackendCUPS::EnumeratePrinters(PrinterList* printer_list) { |
| 117 DCHECK(printer_list); | 81 DCHECK(printer_list); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 | 187 |
| 224 bool PrintBackendCUPS::IsValidPrinter(const std::string& printer_name) { | 188 bool PrintBackendCUPS::IsValidPrinter(const std::string& printer_name) { |
| 225 cups_dest_t* dest = GetNamedDest(printer_name); | 189 cups_dest_t* dest = GetNamedDest(printer_name); |
| 226 if (!dest) | 190 if (!dest) |
| 227 return false; | 191 return false; |
| 228 | 192 |
| 229 cupsFreeDests(1, dest); | 193 cupsFreeDests(1, dest); |
| 230 return true; | 194 return true; |
| 231 } | 195 } |
| 232 | 196 |
| 197 #if !defined(OS_CHROMEOS) | |
| 233 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 198 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
| 234 const base::DictionaryValue* print_backend_settings) { | 199 const base::DictionaryValue* print_backend_settings) { |
| 235 std::string print_server_url_str, cups_blocking; | 200 std::string print_server_url_str, cups_blocking; |
| 236 int encryption = HTTP_ENCRYPT_NEVER; | 201 int encryption = HTTP_ENCRYPT_NEVER; |
| 237 if (print_backend_settings) { | 202 if (print_backend_settings) { |
| 238 print_backend_settings->GetString(kCUPSPrintServerURL, | 203 print_backend_settings->GetString(kCUPSPrintServerURL, |
| 239 &print_server_url_str); | 204 &print_server_url_str); |
| 240 | 205 |
| 241 print_backend_settings->GetString(kCUPSBlocking, | 206 print_backend_settings->GetString(kCUPSBlocking, |
| 242 &cups_blocking); | 207 &cups_blocking); |
| 243 | 208 |
| 244 print_backend_settings->GetInteger(kCUPSEncryption, &encryption); | 209 print_backend_settings->GetInteger(kCUPSEncryption, &encryption); |
| 245 } | 210 } |
| 246 GURL print_server_url(print_server_url_str.c_str()); | 211 GURL print_server_url(print_server_url_str.c_str()); |
| 247 return new PrintBackendCUPS(print_server_url, | 212 return new PrintBackendCUPS(print_server_url, |
| 248 static_cast<http_encryption_t>(encryption), | 213 static_cast<http_encryption_t>(encryption), |
| 249 cups_blocking == kValueTrue); | 214 cups_blocking == kValueTrue); |
| 250 } | 215 } |
| 216 #endif // !defined(OS_CHROMEOS) | |
| 251 | 217 |
| 252 int PrintBackendCUPS::GetDests(cups_dest_t** dests) { | 218 int PrintBackendCUPS::GetDests(cups_dest_t** dests) { |
| 253 if (print_server_url_.is_empty()) { // Use default (local) print server. | 219 if (print_server_url_.is_empty()) { // Use default (local) print server. |
| 254 // GnuTLS has a genuine small memory leak that is easier to annotate | 220 // GnuTLS has a genuine small memory leak that is easier to annotate |
| 255 // than suppress. See http://crbug.com/176888#c7 | 221 // than suppress. See http://crbug.com/176888#c7 |
| 256 // In theory any CUPS function can trigger this leak, but in | 222 // In theory any CUPS function can trigger this leak, but in |
| 257 // PrintBackendCUPS, this is the most likely spot. | 223 // PrintBackendCUPS, this is the most likely spot. |
| 258 // TODO(eugenis): remove this once the leak is fixed. | 224 // TODO(eugenis): remove this once the leak is fixed. |
| 259 ANNOTATE_SCOPED_MEMORY_LEAK; | 225 ANNOTATE_SCOPED_MEMORY_LEAK; |
| 260 return cupsGetDests(dests); | 226 return cupsGetDests(dests); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 // Use default (local) print server. | 284 // Use default (local) print server. |
| 319 if (print_server_url_.is_empty()) | 285 if (print_server_url_.is_empty()) |
| 320 return cupsGetNamedDest(CUPS_HTTP_DEFAULT, printer_name.c_str(), nullptr); | 286 return cupsGetNamedDest(CUPS_HTTP_DEFAULT, printer_name.c_str(), nullptr); |
| 321 | 287 |
| 322 HttpConnectionCUPS http(print_server_url_, cups_encryption_); | 288 HttpConnectionCUPS http(print_server_url_, cups_encryption_); |
| 323 http.SetBlocking(blocking_); | 289 http.SetBlocking(blocking_); |
| 324 return cupsGetNamedDest(http.http(), printer_name.c_str(), nullptr); | 290 return cupsGetNamedDest(http.http(), printer_name.c_str(), nullptr); |
| 325 } | 291 } |
| 326 | 292 |
| 327 } // namespace printing | 293 } // namespace printing |
| OLD | NEW |