| 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> |
| 11 | 11 |
| 12 #include "base/base64.h" | 12 #include "base/base64.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/i18n/file_util_icu.h" | 16 #include "base/i18n/file_util_icu.h" |
| 17 #include "base/i18n/number_formatting.h" | 17 #include "base/i18n/number_formatting.h" |
| 18 #include "base/json/json_reader.h" | 18 #include "base/json/json_reader.h" |
| 19 #include "base/lazy_instance.h" | 19 #include "base/lazy_instance.h" |
| 20 #include "base/memory/linked_ptr.h" | 20 #include "base/memory/linked_ptr.h" |
| 21 #include "base/memory/ref_counted_memory.h" | 21 #include "base/memory/ref_counted_memory.h" |
| 22 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
| 23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 24 #include "base/prefs/pref_service.h" | 24 #include "base/prefs/pref_service.h" |
| 25 #include "base/strings/string_number_conversions.h" | |
| 26 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 27 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
| 28 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
| 29 #include "base/values.h" | 28 #include "base/values.h" |
| 30 #include "chrome/browser/browser_process.h" | 29 #include "chrome/browser/browser_process.h" |
| 31 #include "chrome/browser/platform_util.h" | 30 #include "chrome/browser/platform_util.h" |
| 32 #include "chrome/browser/printing/print_dialog_cloud.h" | 31 #include "chrome/browser/printing/print_dialog_cloud.h" |
| 33 #include "chrome/browser/printing/print_error_dialog.h" | 32 #include "chrome/browser/printing/print_error_dialog.h" |
| 34 #include "chrome/browser/printing/print_job_manager.h" | 33 #include "chrome/browser/printing/print_job_manager.h" |
| 35 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 34 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 #endif | 183 #endif |
| 185 // 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. |
| 186 const char kDocumentHasSelection[] = "documentHasSelection"; | 185 const char kDocumentHasSelection[] = "documentHasSelection"; |
| 187 | 186 |
| 188 // Id of the predefined PDF printer. | 187 // Id of the predefined PDF printer. |
| 189 const char kLocalPdfPrinterId[] = "Save as PDF"; | 188 const char kLocalPdfPrinterId[] = "Save as PDF"; |
| 190 | 189 |
| 191 // Additional printer capability setting keys. | 190 // Additional printer capability setting keys. |
| 192 const char kPrinterId[] = "printerId"; | 191 const char kPrinterId[] = "printerId"; |
| 193 const char kPrinterCapabilities[] = "capabilities"; | 192 const char kPrinterCapabilities[] = "capabilities"; |
| 193 #if defined(USE_CUPS) |
| 194 const char kCUPSsColorModel[] = "cupsColorModel"; |
| 195 const char kCUPSsBWModel[] = "cupsBWModel"; |
| 196 #endif |
| 194 | 197 |
| 195 // Get the print job settings dictionary from |args|. The caller takes | 198 // Get the print job settings dictionary from |args|. The caller takes |
| 196 // ownership of the returned DictionaryValue. Returns NULL on failure. | 199 // ownership of the returned DictionaryValue. Returns NULL on failure. |
| 197 base::DictionaryValue* GetSettingsDictionary(const base::ListValue* args) { | 200 base::DictionaryValue* GetSettingsDictionary(const base::ListValue* args) { |
| 198 std::string json_str; | 201 std::string json_str; |
| 199 if (!args->GetString(0, &json_str)) { | 202 if (!args->GetString(0, &json_str)) { |
| 200 NOTREACHED() << "Could not read JSON argument"; | 203 NOTREACHED() << "Could not read JSON argument"; |
| 201 return NULL; | 204 return NULL; |
| 202 } | 205 } |
| 203 if (json_str.empty()) { | 206 if (json_str.empty()) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 cloud_devices::CloudDeviceDescription description; | 310 cloud_devices::CloudDeviceDescription description; |
| 308 using namespace cloud_devices::printer; | 311 using namespace cloud_devices::printer; |
| 309 | 312 |
| 310 OrientationCapability orientation; | 313 OrientationCapability orientation; |
| 311 orientation.AddOption(cloud_devices::printer::PORTRAIT); | 314 orientation.AddOption(cloud_devices::printer::PORTRAIT); |
| 312 orientation.AddOption(cloud_devices::printer::LANDSCAPE); | 315 orientation.AddOption(cloud_devices::printer::LANDSCAPE); |
| 313 orientation.AddDefaultOption(AUTO_ORIENTATION, true); | 316 orientation.AddDefaultOption(AUTO_ORIENTATION, true); |
| 314 orientation.SaveTo(&description); | 317 orientation.SaveTo(&description); |
| 315 | 318 |
| 316 ColorCapability color; | 319 ColorCapability color; |
| 317 { | 320 color.AddDefaultOption(Color(STANDARD_COLOR), true); |
| 318 Color standard_color(STANDARD_COLOR); | |
| 319 standard_color.vendor_id = base::IntToString(printing::COLOR); | |
| 320 color.AddDefaultOption(standard_color, true); | |
| 321 } | |
| 322 color.SaveTo(&description); | 321 color.SaveTo(&description); |
| 323 | 322 |
| 324 static const cloud_devices::printer::MediaType kPdfMedia[] = { | 323 static const cloud_devices::printer::MediaType kPdfMedia[] = { |
| 325 ISO_A4, | 324 ISO_A4, |
| 326 ISO_A3, | 325 ISO_A3, |
| 327 NA_LETTER, | 326 NA_LETTER, |
| 328 NA_LEGAL, | 327 NA_LEGAL, |
| 329 NA_LEDGER | 328 NA_LEDGER |
| 330 }; | 329 }; |
| 331 const gfx::Size default_media_size = GetDefaultPdfMediaSizeMicrons(); | 330 const gfx::Size default_media_size = GetDefaultPdfMediaSizeMicrons(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 return scoped_ptr<base::DictionaryValue>(); | 369 return scoped_ptr<base::DictionaryValue>(); |
| 371 } | 370 } |
| 372 | 371 |
| 373 scoped_ptr<base::DictionaryValue> description( | 372 scoped_ptr<base::DictionaryValue> description( |
| 374 cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info)); | 373 cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info)); |
| 375 if (!description) { | 374 if (!description) { |
| 376 LOG(WARNING) << "Failed to convert capabilities for " << printer_name; | 375 LOG(WARNING) << "Failed to convert capabilities for " << printer_name; |
| 377 return scoped_ptr<base::DictionaryValue>(); | 376 return scoped_ptr<base::DictionaryValue>(); |
| 378 } | 377 } |
| 379 | 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 |
| 380 return description.Pass(); | 384 return description.Pass(); |
| 381 } | 385 } |
| 382 | 386 |
| 383 void EnumeratePrintersOnFileThread(base::ListValue* printers) { | 387 void EnumeratePrintersOnFileThread(base::ListValue* printers) { |
| 384 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 388 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 385 | 389 |
| 386 scoped_refptr<printing::PrintBackend> print_backend( | 390 scoped_refptr<printing::PrintBackend> print_backend( |
| 387 printing::PrintBackend::CreateInstance(NULL)); | 391 printing::PrintBackend::CreateInstance(NULL)); |
| 388 | 392 |
| 389 VLOG(1) << "Enumerate printers start"; | 393 VLOG(1) << "Enumerate printers start"; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 | 466 |
| 463 base::LazyInstance<printing::StickySettings> g_sticky_settings = | 467 base::LazyInstance<printing::StickySettings> g_sticky_settings = |
| 464 LAZY_INSTANCE_INITIALIZER; | 468 LAZY_INSTANCE_INITIALIZER; |
| 465 | 469 |
| 466 printing::StickySettings* GetStickySettings() { | 470 printing::StickySettings* GetStickySettings() { |
| 467 return g_sticky_settings.Pointer(); | 471 return g_sticky_settings.Pointer(); |
| 468 } | 472 } |
| 469 | 473 |
| 470 } // namespace | 474 } // namespace |
| 471 | 475 |
| 476 #if defined(USE_CUPS) |
| 477 struct PrintPreviewHandler::CUPSPrinterColorModels { |
| 478 std::string printer_name; |
| 479 printing::ColorModel color_model; |
| 480 printing::ColorModel bw_model; |
| 481 }; |
| 482 #endif |
| 483 |
| 472 class PrintPreviewHandler::AccessTokenService | 484 class PrintPreviewHandler::AccessTokenService |
| 473 : public OAuth2TokenService::Consumer { | 485 : public OAuth2TokenService::Consumer { |
| 474 public: | 486 public: |
| 475 explicit AccessTokenService(PrintPreviewHandler* handler) | 487 explicit AccessTokenService(PrintPreviewHandler* handler) |
| 476 : OAuth2TokenService::Consumer("print_preview"), | 488 : OAuth2TokenService::Consumer("print_preview"), |
| 477 handler_(handler) { | 489 handler_(handler) { |
| 478 } | 490 } |
| 479 | 491 |
| 480 void RequestToken(const std::string& type) { | 492 void RequestToken(const std::string& type) { |
| 481 if (requests_.find(type) != requests_.end()) | 493 if (requests_.find(type) != requests_.end()) |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 861 // Do this so the initiator can open a new print preview dialog, while the | 873 // Do this so the initiator can open a new print preview dialog, while the |
| 862 // current print preview dialog is still handling its print job. | 874 // current print preview dialog is still handling its print job. |
| 863 ClearInitiatorDetails(); | 875 ClearInitiatorDetails(); |
| 864 | 876 |
| 865 // The PDF being printed contains only the pages that the user selected, | 877 // The PDF being printed contains only the pages that the user selected, |
| 866 // so ignore the page range and print all pages. | 878 // so ignore the page range and print all pages. |
| 867 settings->Remove(printing::kSettingPageRange, NULL); | 879 settings->Remove(printing::kSettingPageRange, NULL); |
| 868 // Reset selection only flag for the same reason. | 880 // Reset selection only flag for the same reason. |
| 869 settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly, false); | 881 settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly, false); |
| 870 | 882 |
| 883 #if defined(USE_CUPS) |
| 884 if (!open_pdf_in_preview) // We can get here even for cloud printers. |
| 885 ConvertColorSettingToCUPSColorModel(settings.get()); |
| 886 #endif |
| 887 |
| 871 // Set ID to know whether printing is for preview. | 888 // Set ID to know whether printing is for preview. |
| 872 settings->SetInteger(printing::kPreviewUIID, | 889 settings->SetInteger(printing::kPreviewUIID, |
| 873 print_preview_ui->GetIDForPrintPreviewUI()); | 890 print_preview_ui->GetIDForPrintPreviewUI()); |
| 874 RenderViewHost* rvh = preview_web_contents()->GetRenderViewHost(); | 891 RenderViewHost* rvh = preview_web_contents()->GetRenderViewHost(); |
| 875 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->GetRoutingID(), | 892 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->GetRoutingID(), |
| 876 *settings)); | 893 *settings)); |
| 877 | 894 |
| 878 // For all other cases above, the preview dialog will stay open until the | 895 // For all other cases above, the preview dialog will stay open until the |
| 879 // printing has finished. Then the dialog closes and PrintPreviewDone() gets | 896 // printing has finished. Then the dialog closes and PrintPreviewDone() gets |
| 880 // called. In the case below, since the preview dialog will be hidden and | 897 // called. In the case below, since the preview dialog will be hidden and |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1205 const std::string& access_token) { | 1222 const std::string& access_token) { |
| 1206 VLOG(1) << "Get getAccessToken finished"; | 1223 VLOG(1) << "Get getAccessToken finished"; |
| 1207 web_ui()->CallJavascriptFunction("onDidGetAccessToken", | 1224 web_ui()->CallJavascriptFunction("onDidGetAccessToken", |
| 1208 base::StringValue(type), | 1225 base::StringValue(type), |
| 1209 base::StringValue(access_token)); | 1226 base::StringValue(access_token)); |
| 1210 } | 1227 } |
| 1211 | 1228 |
| 1212 void PrintPreviewHandler::SendPrinterCapabilities( | 1229 void PrintPreviewHandler::SendPrinterCapabilities( |
| 1213 const base::DictionaryValue* settings_info) { | 1230 const base::DictionaryValue* settings_info) { |
| 1214 VLOG(1) << "Get printer capabilities finished"; | 1231 VLOG(1) << "Get printer capabilities finished"; |
| 1232 |
| 1233 #if defined(USE_CUPS) |
| 1234 SaveCUPSColorSetting(settings_info); |
| 1235 #endif |
| 1236 |
| 1215 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", | 1237 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", |
| 1216 *settings_info); | 1238 *settings_info); |
| 1217 } | 1239 } |
| 1218 | 1240 |
| 1219 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities( | 1241 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities( |
| 1220 const std::string& printer_name) { | 1242 const std::string& printer_name) { |
| 1221 VLOG(1) << "Get printer capabilities failed"; | 1243 VLOG(1) << "Get printer capabilities failed"; |
| 1222 base::StringValue printer_name_value(printer_name); | 1244 base::StringValue printer_name_value(printer_name); |
| 1223 web_ui()->CallJavascriptFunction("failedToGetPrinterCapabilities", | 1245 web_ui()->CallJavascriptFunction("failedToGetPrinterCapabilities", |
| 1224 printer_name_value); | 1246 printer_name_value); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1381 // Nothing to print, no preview available. | 1403 // Nothing to print, no preview available. |
| 1382 return false; | 1404 return false; |
| 1383 } | 1405 } |
| 1384 DCHECK(tmp_data->size() && tmp_data->front()); | 1406 DCHECK(tmp_data->size() && tmp_data->front()); |
| 1385 | 1407 |
| 1386 *data = tmp_data; | 1408 *data = tmp_data; |
| 1387 *title = print_preview_ui->initiator_title(); | 1409 *title = print_preview_ui->initiator_title(); |
| 1388 return true; | 1410 return true; |
| 1389 } | 1411 } |
| 1390 | 1412 |
| 1413 #if defined(USE_CUPS) |
| 1414 void PrintPreviewHandler::SaveCUPSColorSetting( |
| 1415 const base::DictionaryValue* settings) { |
| 1416 cups_printer_color_models_.reset(new CUPSPrinterColorModels); |
| 1417 settings->GetString(kPrinterId, &cups_printer_color_models_->printer_name); |
| 1418 const base::DictionaryValue* capabilities = NULL; |
| 1419 if (!settings->GetDictionary(kPrinterCapabilities, &capabilities) || |
| 1420 !capabilities) { |
| 1421 NOTREACHED(); |
| 1422 return; |
| 1423 } |
| 1424 capabilities->GetInteger( |
| 1425 kCUPSsColorModel, |
| 1426 reinterpret_cast<int*>(&cups_printer_color_models_->color_model)); |
| 1427 capabilities->GetInteger( |
| 1428 kCUPSsBWModel, |
| 1429 reinterpret_cast<int*>(&cups_printer_color_models_->bw_model)); |
| 1430 } |
| 1431 |
| 1432 void PrintPreviewHandler::ConvertColorSettingToCUPSColorModel( |
| 1433 base::DictionaryValue* settings) const { |
| 1434 if (!cups_printer_color_models_) |
| 1435 return; |
| 1436 |
| 1437 // Sanity check the printer name. |
| 1438 std::string printer_name; |
| 1439 if (!settings->GetString(printing::kSettingDeviceName, &printer_name) || |
| 1440 printer_name != cups_printer_color_models_->printer_name) { |
| 1441 NOTREACHED(); |
| 1442 return; |
| 1443 } |
| 1444 |
| 1445 int color; |
| 1446 if (!settings->GetInteger(printing::kSettingColor, &color)) { |
| 1447 NOTREACHED(); |
| 1448 return; |
| 1449 } |
| 1450 |
| 1451 if (color == printing::GRAY) { |
| 1452 if (cups_printer_color_models_->bw_model != printing::UNKNOWN_COLOR_MODEL) { |
| 1453 settings->SetInteger(printing::kSettingColor, |
| 1454 cups_printer_color_models_->bw_model); |
| 1455 } |
| 1456 return; |
| 1457 } |
| 1458 |
| 1459 printing::ColorModel color_model = cups_printer_color_models_->color_model; |
| 1460 if (color_model != printing::UNKNOWN_COLOR_MODEL) |
| 1461 settings->SetInteger(printing::kSettingColor, color_model); |
| 1462 } |
| 1463 |
| 1464 #endif // defined(USE_CUPS) |
| 1465 |
| 1391 #if defined(ENABLE_SERVICE_DISCOVERY) | 1466 #if defined(ENABLE_SERVICE_DISCOVERY) |
| 1392 void PrintPreviewHandler::LocalPrinterChanged( | 1467 void PrintPreviewHandler::LocalPrinterChanged( |
| 1393 bool added, | 1468 bool added, |
| 1394 const std::string& name, | 1469 const std::string& name, |
| 1395 bool has_local_printing, | 1470 bool has_local_printing, |
| 1396 const local_discovery::DeviceDescription& description) { | 1471 const local_discovery::DeviceDescription& description) { |
| 1397 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 1472 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 1398 if (has_local_printing || | 1473 if (has_local_printing || |
| 1399 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)) { | 1474 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)) { |
| 1400 base::DictionaryValue info; | 1475 base::DictionaryValue info; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1581 printer_value->SetString("name", description.name); | 1656 printer_value->SetString("name", description.name); |
| 1582 printer_value->SetBoolean("hasLocalPrinting", has_local_printing); | 1657 printer_value->SetBoolean("hasLocalPrinting", has_local_printing); |
| 1583 printer_value->SetBoolean( | 1658 printer_value->SetBoolean( |
| 1584 "isUnregistered", | 1659 "isUnregistered", |
| 1585 description.id.empty() && | 1660 description.id.empty() && |
| 1586 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)); | 1661 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)); |
| 1587 printer_value->SetString("cloudID", description.id); | 1662 printer_value->SetString("cloudID", description.id); |
| 1588 } | 1663 } |
| 1589 | 1664 |
| 1590 #endif // defined(ENABLE_SERVICE_DISCOVERY) | 1665 #endif // defined(ENABLE_SERVICE_DISCOVERY) |
| OLD | NEW |