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/cups_helper.h" | 5 #include "printing/backend/cups_helper.h" |
6 | 6 |
7 #include <cups/ppd.h> | 7 #include <cups/ppd.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
10 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
11 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
12 #include "base/logging.h" | 15 #include "base/logging.h" |
13 #include "base/path_service.h" | 16 #include "base/path_service.h" |
14 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
16 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
17 #include "base/values.h" | 20 #include "base/values.h" |
18 #include "printing/backend/print_backend.h" | 21 #include "printing/backend/print_backend.h" |
19 #include "printing/backend/print_backend_consts.h" | 22 #include "printing/backend/print_backend_consts.h" |
(...skipping 13 matching lines...) Expand all Loading... |
33 const char kDraftGray[] = "Draft.Gray"; | 36 const char kDraftGray[] = "Draft.Gray"; |
34 const char kHighGray[] = "High.Gray"; | 37 const char kHighGray[] = "High.Gray"; |
35 | 38 |
36 const char kDuplex[] = "Duplex"; | 39 const char kDuplex[] = "Duplex"; |
37 const char kDuplexNone[] = "None"; | 40 const char kDuplexNone[] = "None"; |
38 const char kPageSize[] = "PageSize"; | 41 const char kPageSize[] = "PageSize"; |
39 | 42 |
40 const double kMicronsPerPoint = 10.0f * kHundrethsMMPerInch / kPointsPerInch; | 43 const double kMicronsPerPoint = 10.0f * kHundrethsMMPerInch / kPointsPerInch; |
41 | 44 |
42 void ParseLpOptions(const base::FilePath& filepath, | 45 void ParseLpOptions(const base::FilePath& filepath, |
43 const std::string& printer_name, | 46 base::StringPiece printer_name, |
44 int* num_options, cups_option_t** options) { | 47 int* num_options, |
| 48 cups_option_t** options) { |
45 std::string content; | 49 std::string content; |
46 if (!base::ReadFileToString(filepath, &content)) | 50 if (!base::ReadFileToString(filepath, &content)) |
47 return; | 51 return; |
48 | 52 |
49 const char kDest[] = "dest"; | 53 const char kDest[] = "dest"; |
50 const char kDefault[] = "default"; | 54 const char kDefault[] = "default"; |
51 const size_t kDestLen = sizeof(kDest) - 1; | 55 const size_t kDestLen = sizeof(kDest) - 1; |
52 const size_t kDefaultLen = sizeof(kDefault) - 1; | 56 const size_t kDefaultLen = sizeof(kDefault) - 1; |
53 | 57 |
54 for (base::StringPiece line : | 58 for (base::StringPiece line : |
(...skipping 24 matching lines...) Expand all Loading... |
79 continue; | 83 continue; |
80 | 84 |
81 if (!base::EqualsCaseInsensitiveASCII(printer_name, name)) | 85 if (!base::EqualsCaseInsensitiveASCII(printer_name, name)) |
82 continue; // This is not the required printer. | 86 continue; // This is not the required printer. |
83 | 87 |
84 line = line.substr(space_found + 1); | 88 line = line.substr(space_found + 1); |
85 // Remove extra spaces. | 89 // Remove extra spaces. |
86 line = base::TrimWhitespaceASCII(line, base::TRIM_ALL); | 90 line = base::TrimWhitespaceASCII(line, base::TRIM_ALL); |
87 if (line.empty()) | 91 if (line.empty()) |
88 continue; | 92 continue; |
| 93 |
89 // Parse the selected printer custom options. Need to pass a | 94 // Parse the selected printer custom options. Need to pass a |
90 // null-terminated string. | 95 // null-terminated string. |
91 *num_options = cupsParseOptions(line.as_string().c_str(), 0, options); | 96 *num_options = cupsParseOptions(line.as_string().c_str(), 0, options); |
92 } | 97 } |
93 } | 98 } |
94 | 99 |
95 void MarkLpOptions(const std::string& printer_name, ppd_file_t** ppd) { | 100 void MarkLpOptions(base::StringPiece printer_name, ppd_file_t** ppd) { |
96 cups_option_t* options = NULL; | 101 cups_option_t* options = nullptr; |
97 int num_options = 0; | 102 int num_options = 0; |
98 | 103 |
99 const char kSystemLpOptionPath[] = "/etc/cups/lpoptions"; | 104 const char kSystemLpOptionPath[] = "/etc/cups/lpoptions"; |
100 const char kUserLpOptionPath[] = ".cups/lpoptions"; | 105 const char kUserLpOptionPath[] = ".cups/lpoptions"; |
101 | 106 |
102 std::vector<base::FilePath> file_locations; | 107 std::vector<base::FilePath> file_locations; |
103 file_locations.push_back(base::FilePath(kSystemLpOptionPath)); | 108 file_locations.push_back(base::FilePath(kSystemLpOptionPath)); |
104 base::FilePath homedir; | 109 base::FilePath homedir; |
105 PathService::Get(base::DIR_HOME, &homedir); | 110 PathService::Get(base::DIR_HOME, &homedir); |
106 file_locations.push_back(base::FilePath(homedir.Append(kUserLpOptionPath))); | 111 file_locations.push_back(base::FilePath(homedir.Append(kUserLpOptionPath))); |
107 | 112 |
108 for (std::vector<base::FilePath>::const_iterator it = file_locations.begin(); | 113 for (const base::FilePath& location : file_locations) { |
109 it != file_locations.end(); ++it) { | |
110 num_options = 0; | 114 num_options = 0; |
111 options = NULL; | 115 options = nullptr; |
112 ParseLpOptions(*it, printer_name, &num_options, &options); | 116 ParseLpOptions(location, printer_name, &num_options, &options); |
113 if (num_options > 0 && options) { | 117 if (num_options > 0 && options) { |
114 cupsMarkOptions(*ppd, num_options, options); | 118 cupsMarkOptions(*ppd, num_options, options); |
115 cupsFreeOptions(num_options, options); | 119 cupsFreeOptions(num_options, options); |
116 } | 120 } |
117 } | 121 } |
118 } | 122 } |
119 | 123 |
120 bool GetBasicColorModelSettings(ppd_file_t* ppd, | 124 bool GetBasicColorModelSettings(ppd_file_t* ppd, |
121 ColorModel* color_model_for_black, | 125 ColorModel* color_model_for_black, |
122 ColorModel* color_model_for_color, | 126 ColorModel* color_model_for_color, |
123 bool* color_is_default) { | 127 bool* color_is_default) { |
124 ppd_option_t* color_model = ppdFindOption(ppd, kColorModel); | 128 ppd_option_t* color_model = ppdFindOption(ppd, kColorModel); |
125 if (!color_model) | 129 if (!color_model) |
126 return false; | 130 return false; |
127 | 131 |
128 if (ppdFindChoice(color_model, printing::kBlack)) | 132 if (ppdFindChoice(color_model, kBlack)) |
129 *color_model_for_black = printing::BLACK; | 133 *color_model_for_black = BLACK; |
130 else if (ppdFindChoice(color_model, printing::kGray)) | 134 else if (ppdFindChoice(color_model, kGray)) |
131 *color_model_for_black = printing::GRAY; | 135 *color_model_for_black = GRAY; |
132 else if (ppdFindChoice(color_model, printing::kGrayscale)) | 136 else if (ppdFindChoice(color_model, kGrayscale)) |
133 *color_model_for_black = printing::GRAYSCALE; | 137 *color_model_for_black = GRAYSCALE; |
134 | 138 |
135 if (ppdFindChoice(color_model, printing::kColor)) | 139 if (ppdFindChoice(color_model, kColor)) |
136 *color_model_for_color = printing::COLOR; | 140 *color_model_for_color = COLOR; |
137 else if (ppdFindChoice(color_model, printing::kCMYK)) | 141 else if (ppdFindChoice(color_model, kCMYK)) |
138 *color_model_for_color = printing::CMYK; | 142 *color_model_for_color = CMYK; |
139 else if (ppdFindChoice(color_model, printing::kRGB)) | 143 else if (ppdFindChoice(color_model, kRGB)) |
140 *color_model_for_color = printing::RGB; | 144 *color_model_for_color = RGB; |
141 else if (ppdFindChoice(color_model, printing::kRGBA)) | 145 else if (ppdFindChoice(color_model, kRGBA)) |
142 *color_model_for_color = printing::RGBA; | 146 *color_model_for_color = RGBA; |
143 else if (ppdFindChoice(color_model, printing::kRGB16)) | 147 else if (ppdFindChoice(color_model, kRGB16)) |
144 *color_model_for_color = printing::RGB16; | 148 *color_model_for_color = RGB16; |
145 else if (ppdFindChoice(color_model, printing::kCMY)) | 149 else if (ppdFindChoice(color_model, kCMY)) |
146 *color_model_for_color = printing::CMY; | 150 *color_model_for_color = CMY; |
147 else if (ppdFindChoice(color_model, printing::kKCMY)) | 151 else if (ppdFindChoice(color_model, kKCMY)) |
148 *color_model_for_color = printing::KCMY; | 152 *color_model_for_color = KCMY; |
149 else if (ppdFindChoice(color_model, printing::kCMY_K)) | 153 else if (ppdFindChoice(color_model, kCMY_K)) |
150 *color_model_for_color = printing::CMY_K; | 154 *color_model_for_color = CMY_K; |
151 | 155 |
152 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorModel); | 156 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorModel); |
153 if (!marked_choice) | 157 if (!marked_choice) |
154 marked_choice = ppdFindChoice(color_model, color_model->defchoice); | 158 marked_choice = ppdFindChoice(color_model, color_model->defchoice); |
155 | 159 |
156 if (marked_choice) { | 160 if (marked_choice) { |
157 *color_is_default = | 161 *color_is_default = |
158 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, | 162 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && |
159 printing::kBlack) && | 163 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kGray) && |
160 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, | 164 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kGrayscale); |
161 printing::kGray) && | |
162 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, | |
163 printing::kGrayscale); | |
164 } | 165 } |
165 return true; | 166 return true; |
166 } | 167 } |
167 | 168 |
168 bool GetPrintOutModeColorSettings(ppd_file_t* ppd, | 169 bool GetPrintOutModeColorSettings(ppd_file_t* ppd, |
169 ColorModel* color_model_for_black, | 170 ColorModel* color_model_for_black, |
170 ColorModel* color_model_for_color, | 171 ColorModel* color_model_for_color, |
171 bool* color_is_default) { | 172 bool* color_is_default) { |
172 ppd_option_t* printout_mode = ppdFindOption(ppd, kPrintoutMode); | 173 ppd_option_t* printout_mode = ppdFindOption(ppd, kPrintoutMode); |
173 if (!printout_mode) | 174 if (!printout_mode) |
174 return false; | 175 return false; |
175 | 176 |
176 *color_model_for_color = printing::PRINTOUTMODE_NORMAL; | 177 *color_model_for_color = PRINTOUTMODE_NORMAL; |
177 *color_model_for_black = printing::PRINTOUTMODE_NORMAL; | 178 *color_model_for_black = PRINTOUTMODE_NORMAL; |
178 | 179 |
179 // Check to see if NORMAL_GRAY value is supported by PrintoutMode. | 180 // Check to see if NORMAL_GRAY value is supported by PrintoutMode. |
180 // If NORMAL_GRAY is not supported, NORMAL value is used to | 181 // If NORMAL_GRAY is not supported, NORMAL value is used to |
181 // represent grayscale. If NORMAL_GRAY is supported, NORMAL is used to | 182 // represent grayscale. If NORMAL_GRAY is supported, NORMAL is used to |
182 // represent color. | 183 // represent color. |
183 if (ppdFindChoice(printout_mode, printing::kNormalGray)) | 184 if (ppdFindChoice(printout_mode, kNormalGray)) |
184 *color_model_for_black = printing::PRINTOUTMODE_NORMAL_GRAY; | 185 *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; |
185 | 186 |
186 // Get the default marked choice to identify the default color setting | 187 // Get the default marked choice to identify the default color setting |
187 // value. | 188 // value. |
188 ppd_choice_t* printout_mode_choice = ppdFindMarkedChoice(ppd, kPrintoutMode); | 189 ppd_choice_t* printout_mode_choice = ppdFindMarkedChoice(ppd, kPrintoutMode); |
189 if (!printout_mode_choice) { | 190 if (!printout_mode_choice) { |
190 printout_mode_choice = ppdFindChoice(printout_mode, | 191 printout_mode_choice = ppdFindChoice(printout_mode, |
191 printout_mode->defchoice); | 192 printout_mode->defchoice); |
192 } | 193 } |
193 if (printout_mode_choice) { | 194 if (printout_mode_choice) { |
194 if (base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, | 195 if (base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, |
195 printing::kNormalGray) || | 196 kNormalGray) || |
196 base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, | 197 base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, |
197 kHighGray) || | 198 kHighGray) || |
198 base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, | 199 base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, |
199 kDraftGray)) { | 200 kDraftGray)) { |
200 *color_model_for_black = printing::PRINTOUTMODE_NORMAL_GRAY; | 201 *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; |
201 *color_is_default = false; | 202 *color_is_default = false; |
202 } | 203 } |
203 } | 204 } |
204 return true; | 205 return true; |
205 } | 206 } |
206 | 207 |
207 bool GetColorModeSettings(ppd_file_t* ppd, | 208 bool GetColorModeSettings(ppd_file_t* ppd, |
208 ColorModel* color_model_for_black, | 209 ColorModel* color_model_for_black, |
209 ColorModel* color_model_for_color, | 210 ColorModel* color_model_for_color, |
210 bool* color_is_default) { | 211 bool* color_is_default) { |
211 // Samsung printers use "ColorMode" attribute in their ppds. | 212 // Samsung printers use "ColorMode" attribute in their ppds. |
212 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode); | 213 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode); |
213 if (!color_mode_option) | 214 if (!color_mode_option) |
214 return false; | 215 return false; |
215 | 216 |
216 if (ppdFindChoice(color_mode_option, printing::kColor)) | 217 if (ppdFindChoice(color_mode_option, kColor)) |
217 *color_model_for_color = printing::COLORMODE_COLOR; | 218 *color_model_for_color = COLORMODE_COLOR; |
218 | 219 |
219 if (ppdFindChoice(color_mode_option, printing::kMonochrome)) | 220 if (ppdFindChoice(color_mode_option, kMonochrome)) |
220 *color_model_for_black = printing::COLORMODE_MONOCHROME; | 221 *color_model_for_black = COLORMODE_MONOCHROME; |
221 | 222 |
222 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); | 223 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
223 if (!mode_choice) { | 224 if (!mode_choice) { |
224 mode_choice = ppdFindChoice(color_mode_option, | 225 mode_choice = ppdFindChoice(color_mode_option, |
225 color_mode_option->defchoice); | 226 color_mode_option->defchoice); |
226 } | 227 } |
227 | 228 |
228 if (mode_choice) { | 229 if (mode_choice) { |
229 *color_is_default = base::EqualsCaseInsensitiveASCII( | 230 *color_is_default = |
230 mode_choice->choice, printing::kColor); | 231 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); |
231 } | 232 } |
232 return true; | 233 return true; |
233 } | 234 } |
234 | 235 |
235 bool GetHPColorSettings(ppd_file_t* ppd, | 236 bool GetHPColorSettings(ppd_file_t* ppd, |
236 ColorModel* color_model_for_black, | 237 ColorModel* color_model_for_black, |
237 ColorModel* color_model_for_color, | 238 ColorModel* color_model_for_color, |
238 bool* color_is_default) { | 239 bool* color_is_default) { |
239 // HP printers use "Color/Color Model" attribute in their ppds. | 240 // HP printers use "Color/Color Model" attribute in their ppds. |
240 ppd_option_t* color_mode_option = ppdFindOption(ppd, printing::kColor); | 241 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColor); |
241 if (!color_mode_option) | 242 if (!color_mode_option) |
242 return false; | 243 return false; |
243 | 244 |
244 if (ppdFindChoice(color_mode_option, printing::kColor)) | 245 if (ppdFindChoice(color_mode_option, kColor)) |
245 *color_model_for_color = printing::HP_COLOR_COLOR; | 246 *color_model_for_color = HP_COLOR_COLOR; |
246 if (ppdFindChoice(color_mode_option, printing::kBlack)) | 247 if (ppdFindChoice(color_mode_option, kBlack)) |
247 *color_model_for_black = printing::HP_COLOR_BLACK; | 248 *color_model_for_black = HP_COLOR_BLACK; |
248 | 249 |
249 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); | 250 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
250 if (!mode_choice) { | 251 if (!mode_choice) { |
251 mode_choice = ppdFindChoice(color_mode_option, | 252 mode_choice = ppdFindChoice(color_mode_option, |
252 color_mode_option->defchoice); | 253 color_mode_option->defchoice); |
253 } | 254 } |
254 if (mode_choice) { | 255 if (mode_choice) { |
255 *color_is_default = base::EqualsCaseInsensitiveASCII( | 256 *color_is_default = |
256 mode_choice->choice, printing::kColor); | 257 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); |
257 } | 258 } |
258 return true; | 259 return true; |
259 } | 260 } |
260 | 261 |
261 bool GetProcessColorModelSettings(ppd_file_t* ppd, | 262 bool GetProcessColorModelSettings(ppd_file_t* ppd, |
262 ColorModel* color_model_for_black, | 263 ColorModel* color_model_for_black, |
263 ColorModel* color_model_for_color, | 264 ColorModel* color_model_for_color, |
264 bool* color_is_default) { | 265 bool* color_is_default) { |
265 // Canon printers use "ProcessColorModel" attribute in their ppds. | 266 // Canon printers use "ProcessColorModel" attribute in their ppds. |
266 ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel); | 267 ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel); |
267 if (!color_mode_option) | 268 if (!color_mode_option) |
268 return false; | 269 return false; |
269 | 270 |
270 if (ppdFindChoice(color_mode_option, printing::kRGB)) | 271 if (ppdFindChoice(color_mode_option, kRGB)) |
271 *color_model_for_color = printing::PROCESSCOLORMODEL_RGB; | 272 *color_model_for_color = PROCESSCOLORMODEL_RGB; |
272 else if (ppdFindChoice(color_mode_option, printing::kCMYK)) | 273 else if (ppdFindChoice(color_mode_option, kCMYK)) |
273 *color_model_for_color = printing::PROCESSCOLORMODEL_CMYK; | 274 *color_model_for_color = PROCESSCOLORMODEL_CMYK; |
274 | 275 |
275 if (ppdFindChoice(color_mode_option, printing::kGreyscale)) | 276 if (ppdFindChoice(color_mode_option, kGreyscale)) |
276 *color_model_for_black = printing::PROCESSCOLORMODEL_GREYSCALE; | 277 *color_model_for_black = PROCESSCOLORMODEL_GREYSCALE; |
277 | 278 |
278 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kProcessColorModel); | 279 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kProcessColorModel); |
279 if (!mode_choice) { | 280 if (!mode_choice) { |
280 mode_choice = ppdFindChoice(color_mode_option, | 281 mode_choice = ppdFindChoice(color_mode_option, |
281 color_mode_option->defchoice); | 282 color_mode_option->defchoice); |
282 } | 283 } |
283 | 284 |
284 if (mode_choice) { | 285 if (mode_choice) { |
285 *color_is_default = !base::EqualsCaseInsensitiveASCII( | 286 *color_is_default = |
286 mode_choice->choice, printing::kGreyscale); | 287 !base::EqualsCaseInsensitiveASCII(mode_choice->choice, kGreyscale); |
287 } | 288 } |
288 return true; | 289 return true; |
289 } | 290 } |
290 | 291 |
291 bool GetColorModelSettings(ppd_file_t* ppd, | 292 bool GetColorModelSettings(ppd_file_t* ppd, |
292 ColorModel* cm_black, | 293 ColorModel* cm_black, |
293 ColorModel* cm_color, | 294 ColorModel* cm_color, |
294 bool* is_color) { | 295 bool* is_color) { |
295 bool is_color_device = false; | 296 bool is_color_device = false; |
296 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, NULL); | 297 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, nullptr); |
297 if (attr && attr->value) | 298 if (attr && attr->value) |
298 is_color_device = ppd->color_device; | 299 is_color_device = ppd->color_device; |
299 | 300 |
300 *is_color = is_color_device; | 301 *is_color = is_color_device; |
301 return (is_color_device && | 302 return (is_color_device && |
302 GetBasicColorModelSettings(ppd, cm_black, cm_color, is_color)) || | 303 GetBasicColorModelSettings(ppd, cm_black, cm_color, is_color)) || |
303 GetPrintOutModeColorSettings(ppd, cm_black, cm_color, is_color) || | 304 GetPrintOutModeColorSettings(ppd, cm_black, cm_color, is_color) || |
304 GetColorModeSettings(ppd, cm_black, cm_color, is_color) || | 305 GetColorModeSettings(ppd, cm_black, cm_color, is_color) || |
305 GetHPColorSettings(ppd, cm_black, cm_color, is_color) || | 306 GetHPColorSettings(ppd, cm_black, cm_color, is_color) || |
306 GetProcessColorModelSettings(ppd, cm_black, cm_color, is_color); | 307 GetProcessColorModelSettings(ppd, cm_black, cm_color, is_color); |
307 } | 308 } |
308 | 309 |
309 // Default port for IPP print servers. | 310 // Default port for IPP print servers. |
310 const int kDefaultIPPServerPort = 631; | 311 const int kDefaultIPPServerPort = 631; |
311 | 312 |
312 } // namespace | 313 } // namespace |
313 | 314 |
314 // Helper wrapper around http_t structure, with connection and cleanup | 315 // Helper wrapper around http_t structure, with connection and cleanup |
315 // functionality. | 316 // functionality. |
316 HttpConnectionCUPS::HttpConnectionCUPS(const GURL& print_server_url, | 317 HttpConnectionCUPS::HttpConnectionCUPS(const GURL& print_server_url, |
317 http_encryption_t encryption) | 318 http_encryption_t encryption) |
318 : http_(NULL) { | 319 : http_(nullptr) { |
319 // If we have an empty url, use default print server. | 320 // If we have an empty url, use default print server. |
320 if (print_server_url.is_empty()) | 321 if (print_server_url.is_empty()) |
321 return; | 322 return; |
322 | 323 |
323 int port = print_server_url.IntPort(); | 324 int port = print_server_url.IntPort(); |
324 if (port == url::PORT_UNSPECIFIED) | 325 if (port == url::PORT_UNSPECIFIED) |
325 port = kDefaultIPPServerPort; | 326 port = kDefaultIPPServerPort; |
326 | 327 |
327 http_ = httpConnectEncrypt(print_server_url.host().c_str(), port, encryption); | 328 http_ = httpConnectEncrypt(print_server_url.host().c_str(), port, encryption); |
328 if (http_ == NULL) { | 329 if (!http_) { |
329 LOG(ERROR) << "CP_CUPS: Failed connecting to print server: " | 330 LOG(ERROR) << "CP_CUPS: Failed connecting to print server: " |
330 << print_server_url; | 331 << print_server_url; |
331 } | 332 } |
332 } | 333 } |
333 | 334 |
334 HttpConnectionCUPS::~HttpConnectionCUPS() { | 335 HttpConnectionCUPS::~HttpConnectionCUPS() { |
335 if (http_ != NULL) | 336 if (http_) |
336 httpClose(http_); | 337 httpClose(http_); |
337 } | 338 } |
338 | 339 |
339 void HttpConnectionCUPS::SetBlocking(bool blocking) { | 340 void HttpConnectionCUPS::SetBlocking(bool blocking) { |
340 httpBlocking(http_, blocking ? 1 : 0); | 341 httpBlocking(http_, blocking ? 1 : 0); |
341 } | 342 } |
342 | 343 |
343 http_t* HttpConnectionCUPS::http() { | 344 http_t* HttpConnectionCUPS::http() { |
344 return http_; | 345 return http_; |
345 } | 346 } |
346 | 347 |
347 bool ParsePpdCapabilities( | 348 bool ParsePpdCapabilities(base::StringPiece printer_name, |
348 const std::string& printer_name, | 349 base::StringPiece printer_capabilities, |
349 const std::string& printer_capabilities, | 350 PrinterSemanticCapsAndDefaults* printer_info) { |
350 PrinterSemanticCapsAndDefaults* printer_info) { | |
351 base::FilePath ppd_file_path; | 351 base::FilePath ppd_file_path; |
352 if (!base::CreateTemporaryFile(&ppd_file_path)) | 352 if (!base::CreateTemporaryFile(&ppd_file_path)) |
353 return false; | 353 return false; |
354 | 354 |
355 int data_size = printer_capabilities.length(); | 355 int data_size = printer_capabilities.length(); |
356 if (data_size != base::WriteFile( | 356 if (data_size != base::WriteFile( |
357 ppd_file_path, | 357 ppd_file_path, |
358 printer_capabilities.data(), | 358 printer_capabilities.data(), |
359 data_size)) { | 359 data_size)) { |
360 base::DeleteFile(ppd_file_path, false); | 360 base::DeleteFile(ppd_file_path, false); |
361 return false; | 361 return false; |
362 } | 362 } |
363 | 363 |
364 ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str()); | 364 ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str()); |
365 if (!ppd) { | 365 if (!ppd) { |
366 int line = 0; | 366 int line = 0; |
367 ppd_status_t ppd_status = ppdLastError(&line); | 367 ppd_status_t ppd_status = ppdLastError(&line); |
368 LOG(ERROR) << "Failed to open PDD file: error " << ppd_status << " at line " | 368 LOG(ERROR) << "Failed to open PDD file: error " << ppd_status << " at line " |
369 << line << ", " << ppdErrorString(ppd_status); | 369 << line << ", " << ppdErrorString(ppd_status); |
370 return false; | 370 return false; |
371 } | 371 } |
372 ppdMarkDefaults(ppd); | 372 ppdMarkDefaults(ppd); |
373 MarkLpOptions(printer_name, &ppd); | 373 MarkLpOptions(printer_name, &ppd); |
374 | 374 |
375 printing::PrinterSemanticCapsAndDefaults caps; | 375 PrinterSemanticCapsAndDefaults caps; |
376 caps.collate_capable = true; | 376 caps.collate_capable = true; |
377 caps.collate_default = true; | 377 caps.collate_default = true; |
378 caps.copies_capable = true; | 378 caps.copies_capable = true; |
379 | 379 |
380 ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex); | 380 ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex); |
381 if (!duplex_choice) { | 381 if (!duplex_choice) { |
382 ppd_option_t* option = ppdFindOption(ppd, kDuplex); | 382 ppd_option_t* option = ppdFindOption(ppd, kDuplex); |
383 if (option) | 383 if (option) |
384 duplex_choice = ppdFindChoice(option, option->defchoice); | 384 duplex_choice = ppdFindChoice(option, option->defchoice); |
385 } | 385 } |
386 | 386 |
387 if (duplex_choice) { | 387 if (duplex_choice) { |
388 caps.duplex_capable = true; | 388 caps.duplex_capable = true; |
389 if (!base::EqualsCaseInsensitiveASCII(duplex_choice->choice, kDuplexNone)) | 389 if (!base::EqualsCaseInsensitiveASCII(duplex_choice->choice, kDuplexNone)) |
390 caps.duplex_default = printing::LONG_EDGE; | 390 caps.duplex_default = LONG_EDGE; |
391 else | 391 else |
392 caps.duplex_default = printing::SIMPLEX; | 392 caps.duplex_default = SIMPLEX; |
393 } | 393 } |
394 | 394 |
395 bool is_color = false; | 395 bool is_color = false; |
396 ColorModel cm_color = UNKNOWN_COLOR_MODEL, cm_black = UNKNOWN_COLOR_MODEL; | 396 ColorModel cm_color = UNKNOWN_COLOR_MODEL, cm_black = UNKNOWN_COLOR_MODEL; |
397 if (!GetColorModelSettings(ppd, &cm_black, &cm_color, &is_color)) { | 397 if (!GetColorModelSettings(ppd, &cm_black, &cm_color, &is_color)) { |
398 VLOG(1) << "Unknown printer color model"; | 398 VLOG(1) << "Unknown printer color model"; |
399 } | 399 } |
400 | 400 |
401 caps.color_changeable = ((cm_color != UNKNOWN_COLOR_MODEL) && | 401 caps.color_changeable = ((cm_color != UNKNOWN_COLOR_MODEL) && |
402 (cm_black != UNKNOWN_COLOR_MODEL) && | 402 (cm_black != UNKNOWN_COLOR_MODEL) && |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 } | 434 } |
435 | 435 |
436 ppdClose(ppd); | 436 ppdClose(ppd); |
437 base::DeleteFile(ppd_file_path, false); | 437 base::DeleteFile(ppd_file_path, false); |
438 | 438 |
439 *printer_info = caps; | 439 *printer_info = caps; |
440 return true; | 440 return true; |
441 } | 441 } |
442 | 442 |
443 } // namespace printing | 443 } // namespace printing |
OLD | NEW |