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 "chrome/browser/ui/webui/print_preview/print_preview_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
(...skipping 27 matching lines...) Expand all Loading... |
38 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 38 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
39 #include "chrome/browser/signin/signin_manager_factory.h" | 39 #include "chrome/browser/signin/signin_manager_factory.h" |
40 #include "chrome/browser/ui/browser_finder.h" | 40 #include "chrome/browser/ui/browser_finder.h" |
41 #include "chrome/browser/ui/browser_tabstrip.h" | 41 #include "chrome/browser/ui/browser_tabstrip.h" |
42 #include "chrome/browser/ui/chrome_select_file_policy.h" | 42 #include "chrome/browser/ui/chrome_select_file_policy.h" |
43 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 43 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
44 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 44 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
45 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | 45 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
46 #include "chrome/common/chrome_paths.h" | 46 #include "chrome/common/chrome_paths.h" |
47 #include "chrome/common/chrome_switches.h" | 47 #include "chrome/common/chrome_switches.h" |
| 48 #include "chrome/common/cloud_print/cloud_print_cdd_conversion.h" |
48 #include "chrome/common/cloud_print/cloud_print_constants.h" | 49 #include "chrome/common/cloud_print/cloud_print_constants.h" |
49 #include "chrome/common/crash_keys.h" | 50 #include "chrome/common/crash_keys.h" |
50 #include "chrome/common/pref_names.h" | 51 #include "chrome/common/pref_names.h" |
51 #include "chrome/common/print_messages.h" | 52 #include "chrome/common/print_messages.h" |
52 #include "components/cloud_devices/common/cloud_device_description.h" | 53 #include "components/cloud_devices/common/cloud_device_description.h" |
53 #include "components/cloud_devices/common/cloud_devices_urls.h" | 54 #include "components/cloud_devices/common/cloud_devices_urls.h" |
54 #include "components/cloud_devices/common/printer_description.h" | 55 #include "components/cloud_devices/common/printer_description.h" |
55 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 56 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
56 #include "components/signin/core/browser/signin_manager.h" | 57 #include "components/signin/core/browser/signin_manager.h" |
57 #include "components/signin/core/browser/signin_manager_base.h" | 58 #include "components/signin/core/browser/signin_manager_base.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 #endif | 183 #endif |
183 // Name of a dictionary field holding the state of selection for document. | 184 // Name of a dictionary field holding the state of selection for document. |
184 const char kDocumentHasSelection[] = "documentHasSelection"; | 185 const char kDocumentHasSelection[] = "documentHasSelection"; |
185 | 186 |
186 // Id of the predefined PDF printer. | 187 // Id of the predefined PDF printer. |
187 const char kLocalPdfPrinterId[] = "Save as PDF"; | 188 const char kLocalPdfPrinterId[] = "Save as PDF"; |
188 | 189 |
189 // Additional printer capability setting keys. | 190 // Additional printer capability setting keys. |
190 const char kPrinterId[] = "printerId"; | 191 const char kPrinterId[] = "printerId"; |
191 const char kPrinterCapabilities[] = "capabilities"; | 192 const char kPrinterCapabilities[] = "capabilities"; |
192 const char kDisableColorOption[] = "disableColorOption"; | |
193 const char kSetDuplexAsDefault[] = "setDuplexAsDefault"; | |
194 const char kPrinterDefaultDuplexValue[] = "printerDefaultDuplexValue"; | |
195 #if defined(USE_CUPS) | 193 #if defined(USE_CUPS) |
196 const char kCUPSsColorModel[] = "cupsColorModel"; | 194 const char kCUPSsColorModel[] = "cupsColorModel"; |
197 const char kCUPSsBWModel[] = "cupsBWModel"; | 195 const char kCUPSsBWModel[] = "cupsBWModel"; |
198 #endif | 196 #endif |
199 | 197 |
200 // Get the print job settings dictionary from |args|. The caller takes | 198 // Get the print job settings dictionary from |args|. The caller takes |
201 // ownership of the returned DictionaryValue. Returns NULL on failure. | 199 // ownership of the returned DictionaryValue. Returns NULL on failure. |
202 base::DictionaryValue* GetSettingsDictionary(const base::ListValue* args) { | 200 base::DictionaryValue* GetSettingsDictionary(const base::ListValue* args) { |
203 std::string json_str; | 201 std::string json_str; |
204 if (!args->GetString(0, &json_str)) { | 202 if (!args->GetString(0, &json_str)) { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 for (size_t i = 0; i < arraysize(kPdfMedia); ++i) { | 340 for (size_t i = 0; i < arraysize(kPdfMedia); ++i) { |
343 Media media_option(kPdfMedia[i]); | 341 Media media_option(kPdfMedia[i]); |
344 media.AddDefaultOption(media_option, | 342 media.AddDefaultOption(media_option, |
345 default_media.type == media_option.type); | 343 default_media.type == media_option.type); |
346 } | 344 } |
347 media.SaveTo(&description); | 345 media.SaveTo(&description); |
348 | 346 |
349 return scoped_ptr<base::DictionaryValue>(description.root().DeepCopy()); | 347 return scoped_ptr<base::DictionaryValue>(description.root().DeepCopy()); |
350 } | 348 } |
351 | 349 |
| 350 scoped_ptr<base::DictionaryValue> GetLocalPrinterCapabilitiesOnFileThread( |
| 351 const std::string& printer_name) { |
| 352 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 353 |
| 354 scoped_refptr<printing::PrintBackend> print_backend( |
| 355 printing::PrintBackend::CreateInstance(NULL)); |
| 356 |
| 357 VLOG(1) << "Get printer capabilities start for " << printer_name; |
| 358 crash_keys::ScopedPrinterInfo crash_key( |
| 359 print_backend->GetPrinterDriverInfo(printer_name)); |
| 360 |
| 361 if (!print_backend->IsValidPrinter(printer_name)) { |
| 362 LOG(WARNING) << "Invalid printer " << printer_name; |
| 363 return scoped_ptr<base::DictionaryValue>(); |
| 364 } |
| 365 |
| 366 printing::PrinterSemanticCapsAndDefaults info; |
| 367 if (!print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, &info)) { |
| 368 LOG(WARNING) << "Failed to get capabilities for " << printer_name; |
| 369 return scoped_ptr<base::DictionaryValue>(); |
| 370 } |
| 371 |
| 372 scoped_ptr<base::DictionaryValue> description( |
| 373 cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info)); |
| 374 if (!description) { |
| 375 LOG(WARNING) << "Failed to convert capabilities for " << printer_name; |
| 376 return scoped_ptr<base::DictionaryValue>(); |
| 377 } |
| 378 |
| 379 #if defined(USE_CUPS) |
| 380 // TODO(alekseys): Use CUSTOM_COLOR/MONOCHROME instead. |
| 381 description->SetInteger(kCUPSsColorModel, info.color_model); |
| 382 description->SetInteger(kCUPSsBWModel, info.bw_model); |
| 383 #endif |
| 384 return description.Pass(); |
| 385 } |
| 386 |
352 void EnumeratePrintersOnFileThread(base::ListValue* printers) { | 387 void EnumeratePrintersOnFileThread(base::ListValue* printers) { |
353 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 388 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
354 | 389 |
355 scoped_refptr<printing::PrintBackend> print_backend( | 390 scoped_refptr<printing::PrintBackend> print_backend( |
356 printing::PrintBackend::CreateInstance(NULL)); | 391 printing::PrintBackend::CreateInstance(NULL)); |
357 | 392 |
358 VLOG(1) << "Enumerate printers start"; | 393 VLOG(1) << "Enumerate printers start"; |
359 printing::PrinterList printer_list; | 394 printing::PrinterList printer_list; |
360 print_backend->EnumeratePrinters(&printer_list); | 395 print_backend->EnumeratePrinters(&printer_list); |
361 | 396 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 GetPrinterCapabilitiesFailureCallback; | 438 GetPrinterCapabilitiesFailureCallback; |
404 | 439 |
405 void GetPrinterCapabilitiesOnFileThread( | 440 void GetPrinterCapabilitiesOnFileThread( |
406 const std::string& printer_name, | 441 const std::string& printer_name, |
407 const std::string& locale, | 442 const std::string& locale, |
408 const GetPrinterCapabilitiesSuccessCallback& success_cb, | 443 const GetPrinterCapabilitiesSuccessCallback& success_cb, |
409 const GetPrinterCapabilitiesFailureCallback& failure_cb) { | 444 const GetPrinterCapabilitiesFailureCallback& failure_cb) { |
410 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 445 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
411 DCHECK(!printer_name.empty()); | 446 DCHECK(!printer_name.empty()); |
412 | 447 |
413 // Special case for PDF printer. | 448 scoped_ptr<base::DictionaryValue> printer_capabilities( |
414 if (printer_name == kLocalPdfPrinterId) { | 449 printer_name == kLocalPdfPrinterId ? |
415 scoped_ptr<base::DictionaryValue> printer_info(new base::DictionaryValue); | 450 GetPdfCapabilitiesOnFileThread(locale) : |
416 printer_info->SetString(kPrinterId, printer_name); | 451 GetLocalPrinterCapabilitiesOnFileThread(printer_name)); |
417 printer_info->Set(kPrinterCapabilities, | 452 if (!printer_capabilities) { |
418 GetPdfCapabilitiesOnFileThread(locale).release()); | |
419 BrowserThread::PostTask( | |
420 BrowserThread::UI, FROM_HERE, | |
421 base::Bind(success_cb, base::Owned(printer_info.release()))); | |
422 return; | |
423 } | |
424 | |
425 scoped_refptr<printing::PrintBackend> print_backend( | |
426 printing::PrintBackend::CreateInstance(NULL)); | |
427 | |
428 VLOG(1) << "Get printer capabilities start for " << printer_name; | |
429 crash_keys::ScopedPrinterInfo crash_key( | |
430 print_backend->GetPrinterDriverInfo(printer_name)); | |
431 | |
432 if (!print_backend->IsValidPrinter(printer_name)) { | |
433 // TODO(gene): Notify explicitly if printer is not valid, instead of | |
434 // failed to get capabilities. | |
435 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 453 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
436 base::Bind(failure_cb, printer_name)); | 454 base::Bind(failure_cb, printer_name)); |
437 return; | 455 return; |
438 } | 456 } |
439 | 457 |
440 printing::PrinterSemanticCapsAndDefaults info; | 458 scoped_ptr<base::DictionaryValue> printer_info(new base::DictionaryValue); |
441 if (!print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, &info)) { | 459 printer_info->SetString(kPrinterId, printer_name); |
442 LOG(WARNING) << "Failed to get capabilities for " << printer_name; | 460 printer_info->Set(kPrinterCapabilities, printer_capabilities.release()); |
443 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
444 base::Bind(failure_cb, printer_name)); | |
445 return; | |
446 } | |
447 | |
448 scoped_ptr<base::DictionaryValue> settings_info(new base::DictionaryValue); | |
449 settings_info->SetString(kPrinterId, printer_name); | |
450 settings_info->SetBoolean(kDisableColorOption, !info.color_changeable); | |
451 settings_info->SetBoolean(printing::kSettingSetColorAsDefault, | |
452 info.color_default); | |
453 #if defined(USE_CUPS) | |
454 settings_info->SetInteger(kCUPSsColorModel, info.color_model); | |
455 settings_info->SetInteger(kCUPSsBWModel, info.bw_model); | |
456 #endif | |
457 | |
458 // TODO(gene): Make new capabilities format for Print Preview | |
459 // that will suit semantic capabilities better. | |
460 // Refactor pld API code below | |
461 bool default_duplex = info.duplex_capable ? | |
462 (info.duplex_default != printing::SIMPLEX) : false; | |
463 int duplex_value = info.duplex_capable ? | |
464 printing::LONG_EDGE : printing::UNKNOWN_DUPLEX_MODE; | |
465 settings_info->SetBoolean(kSetDuplexAsDefault, default_duplex); | |
466 settings_info->SetInteger(kPrinterDefaultDuplexValue, duplex_value); | |
467 | 461 |
468 BrowserThread::PostTask( | 462 BrowserThread::PostTask( |
469 BrowserThread::UI, FROM_HERE, | 463 BrowserThread::UI, FROM_HERE, |
470 base::Bind(success_cb, base::Owned(settings_info.release()))); | 464 base::Bind(success_cb, base::Owned(printer_info.release()))); |
471 } | 465 } |
472 | 466 |
473 base::LazyInstance<printing::StickySettings> g_sticky_settings = | 467 base::LazyInstance<printing::StickySettings> g_sticky_settings = |
474 LAZY_INSTANCE_INITIALIZER; | 468 LAZY_INSTANCE_INITIALIZER; |
475 | 469 |
476 printing::StickySettings* GetStickySettings() { | 470 printing::StickySettings* GetStickySettings() { |
477 return g_sticky_settings.Pointer(); | 471 return g_sticky_settings.Pointer(); |
478 } | 472 } |
479 | 473 |
480 } // namespace | 474 } // namespace |
(...skipping 932 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1413 *data = tmp_data; | 1407 *data = tmp_data; |
1414 *title = print_preview_ui->initiator_title(); | 1408 *title = print_preview_ui->initiator_title(); |
1415 return true; | 1409 return true; |
1416 } | 1410 } |
1417 | 1411 |
1418 #if defined(USE_CUPS) | 1412 #if defined(USE_CUPS) |
1419 void PrintPreviewHandler::SaveCUPSColorSetting( | 1413 void PrintPreviewHandler::SaveCUPSColorSetting( |
1420 const base::DictionaryValue* settings) { | 1414 const base::DictionaryValue* settings) { |
1421 cups_printer_color_models_.reset(new CUPSPrinterColorModels); | 1415 cups_printer_color_models_.reset(new CUPSPrinterColorModels); |
1422 settings->GetString(kPrinterId, &cups_printer_color_models_->printer_name); | 1416 settings->GetString(kPrinterId, &cups_printer_color_models_->printer_name); |
1423 settings->GetInteger( | 1417 const base::DictionaryValue* capabilities = NULL; |
| 1418 if (!settings->GetDictionary(kPrinterCapabilities, &capabilities) || |
| 1419 !capabilities) { |
| 1420 NOTREACHED(); |
| 1421 return; |
| 1422 } |
| 1423 capabilities->GetInteger( |
1424 kCUPSsColorModel, | 1424 kCUPSsColorModel, |
1425 reinterpret_cast<int*>(&cups_printer_color_models_->color_model)); | 1425 reinterpret_cast<int*>(&cups_printer_color_models_->color_model)); |
1426 settings->GetInteger( | 1426 capabilities->GetInteger( |
1427 kCUPSsBWModel, | 1427 kCUPSsBWModel, |
1428 reinterpret_cast<int*>(&cups_printer_color_models_->bw_model)); | 1428 reinterpret_cast<int*>(&cups_printer_color_models_->bw_model)); |
1429 } | 1429 } |
1430 | 1430 |
1431 void PrintPreviewHandler::ConvertColorSettingToCUPSColorModel( | 1431 void PrintPreviewHandler::ConvertColorSettingToCUPSColorModel( |
1432 base::DictionaryValue* settings) const { | 1432 base::DictionaryValue* settings) const { |
1433 if (!cups_printer_color_models_) | 1433 if (!cups_printer_color_models_) |
1434 return; | 1434 return; |
1435 | 1435 |
1436 // Sanity check the printer name. | 1436 // Sanity check the printer name. |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1655 printer_value->SetString("name", description.name); | 1655 printer_value->SetString("name", description.name); |
1656 printer_value->SetBoolean("hasLocalPrinting", has_local_printing); | 1656 printer_value->SetBoolean("hasLocalPrinting", has_local_printing); |
1657 printer_value->SetBoolean( | 1657 printer_value->SetBoolean( |
1658 "isUnregistered", | 1658 "isUnregistered", |
1659 description.id.empty() && | 1659 description.id.empty() && |
1660 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)); | 1660 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)); |
1661 printer_value->SetString("cloudID", description.id); | 1661 printer_value->SetString("cloudID", description.id); |
1662 } | 1662 } |
1663 | 1663 |
1664 #endif | 1664 #endif |
OLD | NEW |