Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(511)

Side by Side Diff: chrome/browser/printing/print_system_task_proxy.cc

Issue 10905006: Get semantic capabilities from Print Backend. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/printing/print_system_task_proxy.h" 5 #include "chrome/browser/printing/print_system_task_proxy.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 116
117 namespace { 117 namespace {
118 118
119 const char kPrinterId[] = "printerId"; 119 const char kPrinterId[] = "printerId";
120 const char kDisableColorOption[] = "disableColorOption"; 120 const char kDisableColorOption[] = "disableColorOption";
121 const char kSetDuplexAsDefault[] = "setDuplexAsDefault"; 121 const char kSetDuplexAsDefault[] = "setDuplexAsDefault";
122 const char kPrinterColorModelForBlack[] = "printerColorModelForBlack"; 122 const char kPrinterColorModelForBlack[] = "printerColorModelForBlack";
123 const char kPrinterColorModelForColor[] = "printerColorModelForColor"; 123 const char kPrinterColorModelForColor[] = "printerColorModelForColor";
124 const char kPrinterDefaultDuplexValue[] = "printerDefaultDuplexValue"; 124 const char kPrinterDefaultDuplexValue[] = "printerDefaultDuplexValue";
125 125
126 #if defined(OS_WIN)
127 const char kPskColor[] = "psk:Color";
128 const char kPskGray[] = "psk:Grayscale";
129 const char kPskMonochrome[] = "psk:Monochrome";
130 const char kPskDuplexFeature[] = "psk:JobDuplexAllDocumentsContiguously";
131 const char kPskTwoSided[] = "psk:TwoSided";
132 #elif defined(USE_CUPS)
133 const char kColorDevice[] = "ColorDevice";
134 const char kColorModel[] = "ColorModel";
135 const char kColorMode[] = "ColorMode";
136 const char kProcessColorModel[] = "ProcessColorModel";
137 const char kPrintoutMode[] = "PrintoutMode";
138 const char kDraftGray[] = "Draft.Gray";
139 const char kHighGray[] = "High.Gray";
140
141 const char kDuplex[] = "Duplex";
142 const char kDuplexNone[] = "None";
143
144 bool getBasicColorModelSettings(
145 ppd_file_t* ppd, int* color_model_for_black, int* color_model_for_color,
146 bool* color_is_default) {
147 ppd_option_t* color_model = ppdFindOption(ppd, kColorModel);
148 if (!color_model)
149 return false;
150
151 if (ppdFindChoice(color_model, printing::kBlack))
152 *color_model_for_black = printing::BLACK;
153 else if (ppdFindChoice(color_model, printing::kGray))
154 *color_model_for_black = printing::GRAY;
155 else if (ppdFindChoice(color_model, printing::kGrayscale))
156 *color_model_for_black = printing::GRAYSCALE;
157
158 if (ppdFindChoice(color_model, printing::kColor))
159 *color_model_for_color = printing::COLOR;
160 else if (ppdFindChoice(color_model, printing::kCMYK))
161 *color_model_for_color = printing::CMYK;
162 else if (ppdFindChoice(color_model, printing::kRGB))
163 *color_model_for_color = printing::RGB;
164 else if (ppdFindChoice(color_model, printing::kRGBA))
165 *color_model_for_color = printing::RGBA;
166 else if (ppdFindChoice(color_model, printing::kRGB16))
167 *color_model_for_color = printing::RGB16;
168 else if (ppdFindChoice(color_model, printing::kCMY))
169 *color_model_for_color = printing::CMY;
170 else if (ppdFindChoice(color_model, printing::kKCMY))
171 *color_model_for_color = printing::KCMY;
172 else if (ppdFindChoice(color_model, printing::kCMY_K))
173 *color_model_for_color = printing::CMY_K;
174
175 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorModel);
176 if (!marked_choice)
177 marked_choice = ppdFindChoice(color_model, color_model->defchoice);
178
179 if (marked_choice) {
180 *color_is_default =
181 (base::strcasecmp(marked_choice->choice, printing::kBlack) != 0) &&
182 (base::strcasecmp(marked_choice->choice, printing::kGray) != 0);
183 }
184 return true;
185 }
186
187 bool getPrintOutModeColorSettings(
188 ppd_file_t* ppd, int* color_model_for_black, int* color_model_for_color,
189 bool* color_is_default) {
190 ppd_option_t* printout_mode = ppdFindOption(ppd, kPrintoutMode);
191 if (!printout_mode)
192 return false;
193
194 *color_model_for_color = printing::PRINTOUTMODE_NORMAL;
195 *color_model_for_black = printing::PRINTOUTMODE_NORMAL;
196
197 // Check to see if NORMAL_GRAY value is supported by PrintoutMode.
198 // If NORMAL_GRAY is not supported, NORMAL value is used to
199 // represent grayscale. If NORMAL_GRAY is supported, NORMAL is used to
200 // represent color.
201 if (ppdFindChoice(printout_mode, printing::kNormalGray))
202 *color_model_for_black = printing::PRINTOUTMODE_NORMAL_GRAY;
203
204 // Get the default marked choice to identify the default color setting
205 // value.
206 ppd_choice_t* printout_mode_choice = ppdFindMarkedChoice(ppd, kPrintoutMode);
207 if (!printout_mode_choice) {
208 printout_mode_choice = ppdFindChoice(printout_mode,
209 printout_mode->defchoice);
210 }
211 if (printout_mode_choice) {
212 if ((base::strcasecmp(printout_mode_choice->choice,
213 printing::kNormalGray) == 0) ||
214 (base::strcasecmp(printout_mode_choice->choice, kHighGray) == 0) ||
215 (base::strcasecmp(printout_mode_choice->choice, kDraftGray) == 0)) {
216 *color_model_for_black = printing::PRINTOUTMODE_NORMAL_GRAY;
217 *color_is_default = false;
218 }
219 }
220 return true;
221 }
222
223 bool getColorModeSettings(
224 ppd_file_t* ppd, int* color_model_for_black, int* color_model_for_color,
225 bool* color_is_default) {
226 // Samsung printers use "ColorMode" attribute in their ppds.
227 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode);
228 if (!color_mode_option)
229 return false;
230
231 if (ppdFindChoice(color_mode_option, printing::kColor))
232 *color_model_for_color = printing::COLORMODE_COLOR;
233
234 if (ppdFindChoice(color_mode_option, printing::kMonochrome))
235 *color_model_for_black = printing::COLORMODE_MONOCHROME;
236
237 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode);
238 if (!mode_choice) {
239 mode_choice = ppdFindChoice(color_mode_option,
240 color_mode_option->defchoice);
241 }
242
243 if (mode_choice) {
244 *color_is_default =
245 (base::strcasecmp(mode_choice->choice, printing::kColor) == 0);
246 }
247 return true;
248 }
249
250 bool getHPColorSettings(
251 ppd_file_t* ppd, int* color_model_for_black, int* color_model_for_color,
252 bool* color_is_default) {
253 // HP printers use "Color/Color Model" attribute in their ppds.
254 ppd_option_t* color_mode_option = ppdFindOption(ppd, printing::kColor);
255 if (!color_mode_option)
256 return false;
257
258 if (ppdFindChoice(color_mode_option, printing::kColor))
259 *color_model_for_color = printing::HP_COLOR_COLOR;
260 if (ppdFindChoice(color_mode_option, printing::kBlack))
261 *color_model_for_black = printing::HP_COLOR_BLACK;
262
263 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode);
264 if (!mode_choice) {
265 mode_choice = ppdFindChoice(color_mode_option,
266 color_mode_option->defchoice);
267 }
268 if (mode_choice) {
269 *color_is_default =
270 (base::strcasecmp(mode_choice->choice, printing::kColor) == 0);
271 }
272 return true;
273 }
274
275 bool getProcessColorModelSettings(
276 ppd_file_t* ppd, int* color_model_for_black, int* color_model_for_color,
277 bool* color_is_default) {
278 // Canon printers use "ProcessColorModel" attribute in their ppds.
279 ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel);
280 if (!color_mode_option)
281 return false;
282
283 if (ppdFindChoice(color_mode_option, printing::kRGB))
284 *color_model_for_color = printing::PROCESSCOLORMODEL_RGB;
285 else if (ppdFindChoice(color_mode_option, printing::kCMYK))
286 *color_model_for_color = printing::PROCESSCOLORMODEL_CMYK;
287
288 if (ppdFindChoice(color_mode_option, printing::kGreyscale))
289 *color_model_for_black = printing::PROCESSCOLORMODEL_GREYSCALE;
290
291 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kProcessColorModel);
292 if (!mode_choice) {
293 mode_choice = ppdFindChoice(color_mode_option,
294 color_mode_option->defchoice);
295 }
296
297 if (mode_choice) {
298 *color_is_default =
299 (base::strcasecmp(mode_choice->choice, printing::kGreyscale) != 0);
300 }
301 return true;
302 }
303 #endif
304
305 } // namespace 126 } // namespace
306 127
307 PrintSystemTaskProxy::PrintSystemTaskProxy( 128 PrintSystemTaskProxy::PrintSystemTaskProxy(
308 const base::WeakPtr<PrintPreviewHandler>& handler, 129 const base::WeakPtr<PrintPreviewHandler>& handler,
309 printing::PrintBackend* print_backend, 130 printing::PrintBackend* print_backend,
310 bool has_logged_printers_count) 131 bool has_logged_printers_count)
311 : handler_(handler), 132 : handler_(handler),
312 print_backend_(print_backend), 133 print_backend_(print_backend),
313 has_logged_printers_count_(has_logged_printers_count) { 134 has_logged_printers_count_(has_logged_printers_count) {
314 } 135 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 BrowserThread::UI, FROM_HERE, 192 BrowserThread::UI, FROM_HERE,
372 base::Bind(&PrintSystemTaskProxy::SetupPrinterList, this, printers)); 193 base::Bind(&PrintSystemTaskProxy::SetupPrinterList, this, printers));
373 } 194 }
374 195
375 void PrintSystemTaskProxy::SetupPrinterList(ListValue* printers) { 196 void PrintSystemTaskProxy::SetupPrinterList(ListValue* printers) {
376 if (handler_) 197 if (handler_)
377 handler_->SetupPrinterList(*printers); 198 handler_->SetupPrinterList(*printers);
378 delete printers; 199 delete printers;
379 } 200 }
380 201
381 bool PrintSystemTaskProxy::ParsePrinterCapabilities(
382 const printing::PrinterCapsAndDefaults& printer_info,
383 const std::string& printer_name,
384 bool* set_color_as_default,
385 int* printer_color_space_for_color,
386 int* printer_color_space_for_black,
387 bool* set_duplex_as_default,
388 int* default_duplex_setting_value) {
389 #if defined(USE_CUPS)
390 FilePath ppd_file_path;
391 if (!file_util::CreateTemporaryFile(&ppd_file_path))
392 return false;
393
394 int data_size = printer_info.printer_capabilities.length();
395 if (data_size != file_util::WriteFile(
396 ppd_file_path,
397 printer_info.printer_capabilities.data(),
398 data_size)) {
399 file_util::Delete(ppd_file_path, false);
400 return false;
401 }
402
403 ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str());
404 if (ppd) {
405 #if !defined(OS_MACOSX)
406 printing_internal::mark_lpoptions(printer_name, &ppd);
407 #endif
408 ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex);
409 if (!duplex_choice) {
410 ppd_option_t* option = ppdFindOption(ppd, kDuplex);
411 if (option)
412 duplex_choice = ppdFindChoice(option, option->defchoice);
413 }
414
415 if (duplex_choice) {
416 if (base::strcasecmp(duplex_choice->choice, kDuplexNone) != 0) {
417 *set_duplex_as_default = true;
418 *default_duplex_setting_value = printing::LONG_EDGE;
419 } else {
420 *default_duplex_setting_value = printing::SIMPLEX;
421 }
422 }
423
424 bool is_color_device = false;
425 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, NULL);
426 if (attr && attr->value)
427 is_color_device = ppd->color_device;
428 *set_color_as_default = is_color_device;
429
430 if (!((is_color_device && getBasicColorModelSettings(
431 ppd, printer_color_space_for_black,
432 printer_color_space_for_color, set_color_as_default)) ||
433 getPrintOutModeColorSettings(
434 ppd, printer_color_space_for_black,
435 printer_color_space_for_color, set_color_as_default) ||
436 getColorModeSettings(
437 ppd, printer_color_space_for_black,
438 printer_color_space_for_color, set_color_as_default) ||
439 getHPColorSettings(
440 ppd, printer_color_space_for_black,
441 printer_color_space_for_color, set_color_as_default) ||
442 getProcessColorModelSettings(
443 ppd, printer_color_space_for_black,
444 printer_color_space_for_color, set_color_as_default))) {
445 VLOG(1) << "Unknown printer color model";
446 }
447 ppdClose(ppd);
448 }
449 file_util::Delete(ppd_file_path, false);
450 return true;
451
452 #elif defined(OS_WIN)
453
454 // According to XPS 1.0 spec, only color printers have psk:Color.
455 // Therefore we don't need to parse the whole XML file, we just need to
456 // search the string. The spec can be found at:
457 // http://msdn.microsoft.com/en-us/windows/hardware/gg463431.
458 if (printer_info.printer_capabilities.find(kPskColor) != std::string::npos)
459 *printer_color_space_for_color = printing::COLOR;
460
461 if ((printer_info.printer_capabilities.find(kPskGray) !=
462 std::string::npos) ||
463 (printer_info.printer_capabilities.find(kPskMonochrome) !=
464 std::string::npos)) {
465 *printer_color_space_for_black = printing::GRAY;
466 }
467 *set_color_as_default =
468 (printer_info.printer_defaults.find(kPskColor) != std::string::npos);
469
470 *set_duplex_as_default =
471 (printer_info.printer_defaults.find(kPskDuplexFeature) !=
472 std::string::npos) &&
473 (printer_info.printer_defaults.find(kPskTwoSided) !=
474 std::string::npos);
475
476 if (printer_info.printer_defaults.find(kPskDuplexFeature) !=
477 std::string::npos) {
478 if (printer_info.printer_defaults.find(kPskTwoSided) !=
479 std::string::npos) {
480 *default_duplex_setting_value = printing::LONG_EDGE;
481 } else {
482 *default_duplex_setting_value = printing::SIMPLEX;
483 }
484 }
485 return true;
486
487 #else
488
489 NOTIMPLEMENTED();
490 return false;
491
492 #endif // defined(OS_WIN)
493 }
494
495
496 void PrintSystemTaskProxy::GetPrinterCapabilities( 202 void PrintSystemTaskProxy::GetPrinterCapabilities(
497 const std::string& printer_name) { 203 const std::string& printer_name) {
498 VLOG(1) << "Get printer capabilities start for " << printer_name; 204 VLOG(1) << "Get printer capabilities start for " << printer_name;
499 child_process_logging::ScopedPrinterInfoSetter prn_info( 205 child_process_logging::ScopedPrinterInfoSetter prn_info(
500 print_backend_->GetPrinterDriverInfo(printer_name)); 206 print_backend_->GetPrinterDriverInfo(printer_name));
501 207
502 if (!print_backend_->IsValidPrinter(printer_name)) { 208 if (!print_backend_->IsValidPrinter(printer_name)) {
209 // TODO(gene): Notify explicitly if printer is not valid, instead of
210 // failed to get capabilities.
503 BrowserThread::PostTask( 211 BrowserThread::PostTask(
504 BrowserThread::UI, FROM_HERE, 212 BrowserThread::UI, FROM_HERE,
505 base::Bind(&PrintSystemTaskProxy::SendFailedToGetPrinterCapabilities, 213 base::Bind(&PrintSystemTaskProxy::SendFailedToGetPrinterCapabilities,
506 this, printer_name)); 214 this, printer_name));
507 return; 215 return;
508 } 216 }
509 217
510 bool set_color_as_default = false; 218 printing::PrinterSemanticCapsAndDefaults info;
511 bool set_duplex_as_default = false; 219 if (!print_backend_->GetPrinterSemanticCapsAndDefaults(printer_name, &info)) {
512 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL;
513 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL;
514 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE;
515 bool disable_color_options = false;
516
517 printing::PrinterCapsAndDefaults info;
518 if (print_backend_->GetPrinterCapsAndDefaults(printer_name, &info) &&
519 ParsePrinterCapabilities(info,
520 printer_name,
521 &set_color_as_default,
522 &printer_color_space_for_color,
523 &printer_color_space_for_black,
524 &set_duplex_as_default,
525 &default_duplex_setting_value)) {
526 disable_color_options = (!printer_color_space_for_color ||
527 !printer_color_space_for_black ||
528 (printer_color_space_for_color ==
529 printer_color_space_for_black));
530 } else {
531 VLOG(1) << "Failed to get capabilities for " << printer_name; 220 VLOG(1) << "Failed to get capabilities for " << printer_name;
221 BrowserThread::PostTask(
222 BrowserThread::UI, FROM_HERE,
223 base::Bind(&PrintSystemTaskProxy::SendFailedToGetPrinterCapabilities,
224 this, printer_name));
225 return;
532 } 226 }
533 227
534 DictionaryValue settings_info; 228 DictionaryValue settings_info;
535 settings_info.SetString(kPrinterId, printer_name); 229 settings_info.SetString(kPrinterId, printer_name);
536 settings_info.SetBoolean(kDisableColorOption, disable_color_options); 230 settings_info.SetBoolean(kDisableColorOption, !info.color_capable);
537 if (printer_color_space_for_color == printing::UNKNOWN_COLOR_MODEL) 231 settings_info.SetBoolean(printing::kSettingSetColorAsDefault,
538 printer_color_space_for_color = printing::COLOR; 232 info.color_default);
233 // Old API, ugly code :(
234 if (info.duplex_capable) {
235 settings_info.SetBoolean(kSetDuplexAsDefault,
236 info.duplex_default != printing::SIMPLEX);
237 settings_info.SetInteger(kPrinterDefaultDuplexValue,
238 printing::LONG_EDGE);
239 } else {
240 settings_info.SetBoolean(kSetDuplexAsDefault, false);
241 settings_info.SetInteger(kPrinterDefaultDuplexValue,
242 printing::UNKNOWN_DUPLEX_MODE);
243 }
539 244
540 if (printer_color_space_for_black == printing::UNKNOWN_COLOR_MODEL)
541 printer_color_space_for_black = printing::GRAY;
542
543 settings_info.SetBoolean(printing::kSettingSetColorAsDefault,
544 set_color_as_default);
545 settings_info.SetBoolean(kSetDuplexAsDefault, set_duplex_as_default);
546 settings_info.SetInteger(kPrinterColorModelForColor,
547 printer_color_space_for_color);
548 settings_info.SetInteger(kPrinterColorModelForBlack,
549 printer_color_space_for_black);
550 settings_info.SetInteger(kPrinterDefaultDuplexValue,
551 default_duplex_setting_value);
552 BrowserThread::PostTask( 245 BrowserThread::PostTask(
553 BrowserThread::UI, FROM_HERE, 246 BrowserThread::UI, FROM_HERE,
554 base::Bind(&PrintSystemTaskProxy::SendPrinterCapabilities, this, 247 base::Bind(&PrintSystemTaskProxy::SendPrinterCapabilities, this,
555 settings_info.DeepCopy())); 248 settings_info.DeepCopy()));
556 } 249 }
557 250
558 void PrintSystemTaskProxy::SendPrinterCapabilities( 251 void PrintSystemTaskProxy::SendPrinterCapabilities(
559 DictionaryValue* settings_info) { 252 DictionaryValue* settings_info) {
560 if (handler_) 253 if (handler_)
561 handler_->SendPrinterCapabilities(*settings_info); 254 handler_->SendPrinterCapabilities(*settings_info);
562 delete settings_info; 255 delete settings_info;
563 } 256 }
564 257
565 void PrintSystemTaskProxy::SendFailedToGetPrinterCapabilities( 258 void PrintSystemTaskProxy::SendFailedToGetPrinterCapabilities(
566 const std::string& printer_name) { 259 const std::string& printer_name) {
567 if (handler_) 260 if (handler_)
568 handler_->SendFailedToGetPrinterCapabilities(printer_name); 261 handler_->SendFailedToGetPrinterCapabilities(printer_name);
569 } 262 }
OLDNEW
« no previous file with comments | « no previous file | chrome/renderer/print_web_view_helper.cc » ('j') | chrome/renderer/print_web_view_helper.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698