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