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> |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 | 95 |
96 namespace printing { | 96 namespace printing { |
97 | 97 |
98 static const char kCUPSPrinterInfoOpt[] = "printer-info"; | 98 static const char kCUPSPrinterInfoOpt[] = "printer-info"; |
99 static const char kCUPSPrinterStateOpt[] = "printer-state"; | 99 static const char kCUPSPrinterStateOpt[] = "printer-state"; |
100 static const char kCUPSPrinterTypeOpt[] = "printer-type"; | 100 static const char kCUPSPrinterTypeOpt[] = "printer-type"; |
101 static const char kCUPSPrinterMakeModelOpt[] = "printer-make-and-model"; | 101 static const char kCUPSPrinterMakeModelOpt[] = "printer-make-and-model"; |
102 | 102 |
103 class PrintBackendCUPS : public PrintBackend { | 103 class PrintBackendCUPS : public PrintBackend { |
104 public: | 104 public: |
105 PrintBackendCUPS(const GURL& print_server_url, bool blocking); | 105 PrintBackendCUPS(const GURL& print_server_url, |
106 http_encryption_t encryption, bool blocking); | |
Ryan Sleevi
2012/05/17 00:35:27
style nit: put "bool blocking" on a new line, per
| |
106 | 107 |
107 // PrintBackend implementation. | 108 // PrintBackend implementation. |
108 virtual bool EnumeratePrinters(PrinterList* printer_list) OVERRIDE; | 109 virtual bool EnumeratePrinters(PrinterList* printer_list) OVERRIDE; |
109 virtual std::string GetDefaultPrinterName() OVERRIDE; | 110 virtual std::string GetDefaultPrinterName() OVERRIDE; |
110 virtual bool GetPrinterCapsAndDefaults( | 111 virtual bool GetPrinterCapsAndDefaults( |
111 const std::string& printer_name, | 112 const std::string& printer_name, |
112 PrinterCapsAndDefaults* printer_info) OVERRIDE; | 113 PrinterCapsAndDefaults* printer_info) OVERRIDE; |
113 virtual std::string GetPrinterDriverInfo( | 114 virtual std::string GetPrinterDriverInfo( |
114 const std::string& printer_name) OVERRIDE; | 115 const std::string& printer_name) OVERRIDE; |
115 virtual bool IsValidPrinter(const std::string& printer_name) OVERRIDE; | 116 virtual bool IsValidPrinter(const std::string& printer_name) OVERRIDE; |
116 | 117 |
117 protected: | 118 protected: |
118 virtual ~PrintBackendCUPS() {} | 119 virtual ~PrintBackendCUPS() {} |
119 | 120 |
120 private: | 121 private: |
121 // Following functions are wrappers around corresponding CUPS functions. | 122 // Following functions are wrappers around corresponding CUPS functions. |
122 // <functions>2() are called when print server is specified, and plain | 123 // <functions>2() are called when print server is specified, and plain |
123 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT | 124 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT |
124 // in the <functions>2(), it does not work in CUPS prior to 1.4. | 125 // in the <functions>2(), it does not work in CUPS prior to 1.4. |
125 int GetDests(cups_dest_t** dests); | 126 int GetDests(cups_dest_t** dests); |
126 FilePath GetPPD(const char* name); | 127 FilePath GetPPD(const char* name); |
127 | 128 |
128 GURL print_server_url_; | 129 GURL print_server_url_; |
130 http_encryption_t cups_encryption_; | |
129 bool blocking_; | 131 bool blocking_; |
130 }; | 132 }; |
131 | 133 |
132 PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url, bool blocking) | 134 PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url, |
133 : print_server_url_(print_server_url), blocking_(blocking) { | 135 http_encryption_t encryption, |
136 bool blocking) | |
137 : print_server_url_(print_server_url), | |
138 cups_encryption_(encryption), | |
139 blocking_(blocking) { | |
134 } | 140 } |
135 | 141 |
136 bool PrintBackendCUPS::EnumeratePrinters(PrinterList* printer_list) { | 142 bool PrintBackendCUPS::EnumeratePrinters(PrinterList* printer_list) { |
137 DCHECK(printer_list); | 143 DCHECK(printer_list); |
138 printer_list->clear(); | 144 printer_list->clear(); |
139 | 145 |
140 cups_dest_t* destinations = NULL; | 146 cups_dest_t* destinations = NULL; |
141 int num_dests = GetDests(&destinations); | 147 int num_dests = GetDests(&destinations); |
142 if ((num_dests == 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE)) { | 148 if ((num_dests == 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE)) { |
143 VLOG(1) << "CP_CUPS: Error getting printers from CUPS server. Server: " | 149 VLOG(1) << "CP_CUPS: Error getting printers from CUPS server. Server: " |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 } | 274 } |
269 | 275 |
270 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 276 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
271 const DictionaryValue* print_backend_settings) { | 277 const DictionaryValue* print_backend_settings) { |
272 #if !defined(OS_MACOSX) | 278 #if !defined(OS_MACOSX) |
273 // Initialize gcrypt library. | 279 // Initialize gcrypt library. |
274 g_gcrypt_initializer.Get(); | 280 g_gcrypt_initializer.Get(); |
275 #endif | 281 #endif |
276 | 282 |
277 std::string print_server_url_str, cups_blocking; | 283 std::string print_server_url_str, cups_blocking; |
284 int encryption = HTTP_ENCRYPT_NEVER; | |
278 if (print_backend_settings) { | 285 if (print_backend_settings) { |
279 print_backend_settings->GetString(kCUPSPrintServerURL, | 286 print_backend_settings->GetString(kCUPSPrintServerURL, |
280 &print_server_url_str); | 287 &print_server_url_str); |
281 | 288 |
282 print_backend_settings->GetString(kCUPSBlocking, | 289 print_backend_settings->GetString(kCUPSBlocking, |
283 &cups_blocking); | 290 &cups_blocking); |
291 | |
292 print_backend_settings->GetInteger(kCUPSEncryption, &encryption); | |
284 } | 293 } |
285 GURL print_server_url(print_server_url_str.c_str()); | 294 GURL print_server_url(print_server_url_str.c_str()); |
286 return new PrintBackendCUPS(print_server_url, cups_blocking == kValueTrue); | 295 return new PrintBackendCUPS(print_server_url, |
296 static_cast<http_encryption_t>(encryption), | |
297 cups_blocking == kValueTrue); | |
287 } | 298 } |
288 | 299 |
289 int PrintBackendCUPS::GetDests(cups_dest_t** dests) { | 300 int PrintBackendCUPS::GetDests(cups_dest_t** dests) { |
290 if (print_server_url_.is_empty()) { // Use default (local) print server. | 301 if (print_server_url_.is_empty()) { // Use default (local) print server. |
291 return cupsGetDests(dests); | 302 return cupsGetDests(dests); |
292 } else { | 303 } else { |
293 HttpConnectionCUPS http(print_server_url_); | 304 HttpConnectionCUPS http(print_server_url_, cups_encryption_); |
294 http.SetBlocking(blocking_); | 305 http.SetBlocking(blocking_); |
295 return cupsGetDests2(http.http(), dests); | 306 return cupsGetDests2(http.http(), dests); |
296 } | 307 } |
297 } | 308 } |
298 | 309 |
299 FilePath PrintBackendCUPS::GetPPD(const char* name) { | 310 FilePath PrintBackendCUPS::GetPPD(const char* name) { |
300 // cupsGetPPD returns a filename stored in a static buffer in CUPS. | 311 // cupsGetPPD returns a filename stored in a static buffer in CUPS. |
301 // Protect this code with lock. | 312 // Protect this code with lock. |
302 CR_DEFINE_STATIC_LOCAL(base::Lock, ppd_lock, ()); | 313 CR_DEFINE_STATIC_LOCAL(base::Lock, ppd_lock, ()); |
303 base::AutoLock ppd_autolock(ppd_lock); | 314 base::AutoLock ppd_autolock(ppd_lock); |
304 FilePath ppd_path; | 315 FilePath ppd_path; |
305 const char* ppd_file_path = NULL; | 316 const char* ppd_file_path = NULL; |
306 if (print_server_url_.is_empty()) { // Use default (local) print server. | 317 if (print_server_url_.is_empty()) { // Use default (local) print server. |
307 ppd_file_path = cupsGetPPD(name); | 318 ppd_file_path = cupsGetPPD(name); |
308 if (ppd_file_path) | 319 if (ppd_file_path) |
309 ppd_path = FilePath(ppd_file_path); | 320 ppd_path = FilePath(ppd_file_path); |
310 } else { | 321 } else { |
311 // cupsGetPPD2 gets stuck sometimes in an infinite time due to network | 322 // cupsGetPPD2 gets stuck sometimes in an infinite time due to network |
312 // configuration/issues. To prevent that, use non-blocking http connection | 323 // configuration/issues. To prevent that, use non-blocking http connection |
313 // here. | 324 // here. |
314 // Note: After looking at CUPS sources, it looks like non-blocking | 325 // Note: After looking at CUPS sources, it looks like non-blocking |
315 // connection will timeout after 10 seconds of no data period. And it will | 326 // connection will timeout after 10 seconds of no data period. And it will |
316 // return the same way as if data was completely and sucessfully downloaded. | 327 // return the same way as if data was completely and sucessfully downloaded. |
317 HttpConnectionCUPS http(print_server_url_); | 328 HttpConnectionCUPS http(print_server_url_, cups_encryption_); |
318 http.SetBlocking(blocking_); | 329 http.SetBlocking(blocking_); |
319 ppd_file_path = cupsGetPPD2(http.http(), name); | 330 ppd_file_path = cupsGetPPD2(http.http(), name); |
320 // Check if the get full PPD, since non-blocking call may simply return | 331 // Check if the get full PPD, since non-blocking call may simply return |
321 // normally after timeout expired. | 332 // normally after timeout expired. |
322 if (ppd_file_path) { | 333 if (ppd_file_path) { |
323 // There is no reliable way right now to detect full and complete PPD | 334 // There is no reliable way right now to detect full and complete PPD |
324 // get downloaded. If we reach http timeout, it may simply return | 335 // get downloaded. If we reach http timeout, it may simply return |
325 // downloaded part as a full response. It might be good enough to check | 336 // downloaded part as a full response. It might be good enough to check |
326 // http->data_remaining or http->_data_remaining, unfortunately http_t | 337 // http->data_remaining or http->_data_remaining, unfortunately http_t |
327 // is an internal structure and fields are not exposed in CUPS headers. | 338 // is an internal structure and fields are not exposed in CUPS headers. |
(...skipping 10 matching lines...) Expand all Loading... | |
338 << ", HTTP error: " << http_error; | 349 << ", HTTP error: " << http_error; |
339 file_util::Delete(ppd_path, false); | 350 file_util::Delete(ppd_path, false); |
340 ppd_path.clear(); | 351 ppd_path.clear(); |
341 } | 352 } |
342 } | 353 } |
343 } | 354 } |
344 return ppd_path; | 355 return ppd_path; |
345 } | 356 } |
346 | 357 |
347 } // namespace printing | 358 } // namespace printing |
OLD | NEW |