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 "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> |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 77 |
78 static const char kCUPSPrinterInfoOpt[] = "printer-info"; | 78 static const char kCUPSPrinterInfoOpt[] = "printer-info"; |
79 static const char kCUPSPrinterStateOpt[] = "printer-state"; | 79 static const char kCUPSPrinterStateOpt[] = "printer-state"; |
80 | 80 |
81 class PrintBackendCUPS : public PrintBackend { | 81 class PrintBackendCUPS : public PrintBackend { |
82 public: | 82 public: |
83 PrintBackendCUPS(const GURL& print_server_url, bool blocking); | 83 PrintBackendCUPS(const GURL& print_server_url, bool blocking); |
84 virtual ~PrintBackendCUPS() {} | 84 virtual ~PrintBackendCUPS() {} |
85 | 85 |
86 // PrintBackend implementation. | 86 // PrintBackend implementation. |
87 virtual void EnumeratePrinters(PrinterList* printer_list); | 87 virtual bool EnumeratePrinters(PrinterList* printer_list); |
88 | 88 |
89 virtual bool GetPrinterCapsAndDefaults(const std::string& printer_name, | 89 virtual bool GetPrinterCapsAndDefaults(const std::string& printer_name, |
90 PrinterCapsAndDefaults* printer_info); | 90 PrinterCapsAndDefaults* printer_info); |
91 | 91 |
92 virtual bool IsValidPrinter(const std::string& printer_name); | 92 virtual bool IsValidPrinter(const std::string& printer_name); |
93 | 93 |
94 private: | 94 private: |
95 // Following functions are wrappers around corresponding CUPS functions. | 95 // Following functions are wrappers around corresponding CUPS functions. |
96 // <functions>2() are called when print server is specified, and plain | 96 // <functions>2() are called when print server is specified, and plain |
97 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT | 97 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT |
98 // in the <functions>2(), it does not work in CUPS prior to 1.4. | 98 // in the <functions>2(), it does not work in CUPS prior to 1.4. |
99 int GetDests(cups_dest_t** dests); | 99 int GetDests(cups_dest_t** dests); |
100 FilePath GetPPD(const char* name); | 100 FilePath GetPPD(const char* name); |
101 | 101 |
102 GURL print_server_url_; | 102 GURL print_server_url_; |
103 bool blocking_; | 103 bool blocking_; |
104 }; | 104 }; |
105 | 105 |
106 PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url, bool blocking) | 106 PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url, bool blocking) |
107 : print_server_url_(print_server_url), blocking_(blocking) { | 107 : print_server_url_(print_server_url), blocking_(blocking) { |
108 } | 108 } |
109 | 109 |
110 void PrintBackendCUPS::EnumeratePrinters(PrinterList* printer_list) { | 110 bool PrintBackendCUPS::EnumeratePrinters(PrinterList* printer_list) { |
111 DCHECK(printer_list); | 111 DCHECK(printer_list); |
112 printer_list->clear(); | 112 printer_list->clear(); |
113 | 113 |
114 cups_dest_t* destinations = NULL; | 114 cups_dest_t* destinations = NULL; |
115 int num_dests = GetDests(&destinations); | 115 int num_dests = GetDests(&destinations); |
| 116 // TODO(gene): Figure out how to get an error code from cupsGetDests so we can |
| 117 // differentiate between the enumeration failing and there being 0 printers. |
116 | 118 |
117 for (int printer_index = 0; printer_index < num_dests; printer_index++) { | 119 for (int printer_index = 0; printer_index < num_dests; printer_index++) { |
118 const cups_dest_t& printer = destinations[printer_index]; | 120 const cups_dest_t& printer = destinations[printer_index]; |
119 | 121 |
120 PrinterBasicInfo printer_info; | 122 PrinterBasicInfo printer_info; |
121 printer_info.printer_name = printer.name; | 123 printer_info.printer_name = printer.name; |
122 | 124 |
123 const char* info = cupsGetOption(kCUPSPrinterInfoOpt, | 125 const char* info = cupsGetOption(kCUPSPrinterInfoOpt, |
124 printer.num_options, printer.options); | 126 printer.num_options, printer.options); |
125 if (info != NULL) | 127 if (info != NULL) |
126 printer_info.printer_description = info; | 128 printer_info.printer_description = info; |
127 | 129 |
128 const char* state = cupsGetOption(kCUPSPrinterStateOpt, | 130 const char* state = cupsGetOption(kCUPSPrinterStateOpt, |
129 printer.num_options, printer.options); | 131 printer.num_options, printer.options); |
130 if (state != NULL) | 132 if (state != NULL) |
131 base::StringToInt(state, &printer_info.printer_status); | 133 base::StringToInt(state, &printer_info.printer_status); |
132 | 134 |
133 // Store printer options. | 135 // Store printer options. |
134 for (int opt_index = 0; opt_index < printer.num_options; opt_index++) { | 136 for (int opt_index = 0; opt_index < printer.num_options; opt_index++) { |
135 printer_info.options[printer.options[opt_index].name] = | 137 printer_info.options[printer.options[opt_index].name] = |
136 printer.options[opt_index].value; | 138 printer.options[opt_index].value; |
137 } | 139 } |
138 | 140 |
139 printer_list->push_back(printer_info); | 141 printer_list->push_back(printer_info); |
140 } | 142 } |
141 | 143 |
142 cupsFreeDests(num_dests, destinations); | 144 cupsFreeDests(num_dests, destinations); |
143 | 145 |
144 VLOG(1) << "CUPS: Enumerated " << printer_list->size() << " printers."; | 146 VLOG(1) << "CUPS: Enumerated " << printer_list->size() << " printers."; |
| 147 return true; |
145 } | 148 } |
146 | 149 |
147 bool PrintBackendCUPS::GetPrinterCapsAndDefaults( | 150 bool PrintBackendCUPS::GetPrinterCapsAndDefaults( |
148 const std::string& printer_name, | 151 const std::string& printer_name, |
149 PrinterCapsAndDefaults* printer_info) { | 152 PrinterCapsAndDefaults* printer_info) { |
150 DCHECK(printer_info); | 153 DCHECK(printer_info); |
151 | 154 |
152 VLOG(1) << "CUPS: Getting Caps and Defaults for: " << printer_name; | 155 VLOG(1) << "CUPS: Getting Caps and Defaults for: " << printer_name; |
153 | 156 |
154 FilePath ppd_path(GetPPD(printer_name.c_str())); | 157 FilePath ppd_path(GetPPD(printer_name.c_str())); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 << ", content length: " << content_len; | 261 << ", content length: " << content_len; |
259 file_util::Delete(ppd_path, false); | 262 file_util::Delete(ppd_path, false); |
260 ppd_path.clear(); | 263 ppd_path.clear(); |
261 } | 264 } |
262 } | 265 } |
263 } | 266 } |
264 return ppd_path; | 267 return ppd_path; |
265 } | 268 } |
266 | 269 |
267 } // namespace printing | 270 } // namespace printing |
OLD | NEW |