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

Side by Side Diff: chrome/browser/ui/webui/print_preview/print_preview_handler.cc

Issue 324523002: Generalize printer color model handling, get rid of CUPS specific case. Enable supported paper szes… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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/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
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
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);
Vitaly Buka (NO REVIEWS) 2014/06/06 17:19:25 why do you need {} here
Aleksey Shlyapnikov 2014/06/06 18:21:20 To limit standard_color scope. On 2014/06/06 17:1
Vitaly Buka (NO REVIEWS) 2014/06/07 23:42:56 What is a point? Usually we don't do so if no part
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
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
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
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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 const std::string& access_token) { 1204 const std::string& access_token) {
1222 VLOG(1) << "Get getAccessToken finished"; 1205 VLOG(1) << "Get getAccessToken finished";
1223 web_ui()->CallJavascriptFunction("onDidGetAccessToken", 1206 web_ui()->CallJavascriptFunction("onDidGetAccessToken",
1224 base::StringValue(type), 1207 base::StringValue(type),
1225 base::StringValue(access_token)); 1208 base::StringValue(access_token));
1226 } 1209 }
1227 1210
1228 void PrintPreviewHandler::SendPrinterCapabilities( 1211 void PrintPreviewHandler::SendPrinterCapabilities(
1229 const base::DictionaryValue* settings_info) { 1212 const base::DictionaryValue* settings_info) {
1230 VLOG(1) << "Get printer capabilities finished"; 1213 VLOG(1) << "Get printer capabilities finished";
1231
1232 #if defined(USE_CUPS)
1233 SaveCUPSColorSetting(settings_info);
1234 #endif
1235
1236 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", 1214 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities",
1237 *settings_info); 1215 *settings_info);
1238 } 1216 }
1239 1217
1240 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities( 1218 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities(
1241 const std::string& printer_name) { 1219 const std::string& printer_name) {
1242 VLOG(1) << "Get printer capabilities failed"; 1220 VLOG(1) << "Get printer capabilities failed";
1243 base::StringValue printer_name_value(printer_name); 1221 base::StringValue printer_name_value(printer_name);
1244 web_ui()->CallJavascriptFunction("failedToGetPrinterCapabilities", 1222 web_ui()->CallJavascriptFunction("failedToGetPrinterCapabilities",
1245 printer_name_value); 1223 printer_name_value);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 // Nothing to print, no preview available. 1380 // Nothing to print, no preview available.
1403 return false; 1381 return false;
1404 } 1382 }
1405 DCHECK(tmp_data->size() && tmp_data->front()); 1383 DCHECK(tmp_data->size() && tmp_data->front());
1406 1384
1407 *data = tmp_data; 1385 *data = tmp_data;
1408 *title = print_preview_ui->initiator_title(); 1386 *title = print_preview_ui->initiator_title();
1409 return true; 1387 return true;
1410 } 1388 }
1411 1389
1412 #if defined(USE_CUPS)
1413 void PrintPreviewHandler::SaveCUPSColorSetting(
1414 const base::DictionaryValue* settings) {
1415 cups_printer_color_models_.reset(new CUPSPrinterColorModels);
1416 settings->GetString(kPrinterId, &cups_printer_color_models_->printer_name);
1417 const base::DictionaryValue* capabilities = NULL;
1418 if (!settings->GetDictionary(kPrinterCapabilities, &capabilities) ||
1419 !capabilities) {
1420 NOTREACHED();
1421 return;
1422 }
1423 capabilities->GetInteger(
1424 kCUPSsColorModel,
1425 reinterpret_cast<int*>(&cups_printer_color_models_->color_model));
1426 capabilities->GetInteger(
1427 kCUPSsBWModel,
1428 reinterpret_cast<int*>(&cups_printer_color_models_->bw_model));
1429 }
1430
1431 void PrintPreviewHandler::ConvertColorSettingToCUPSColorModel(
1432 base::DictionaryValue* settings) const {
1433 if (!cups_printer_color_models_)
1434 return;
1435
1436 // Sanity check the printer name.
1437 std::string printer_name;
1438 if (!settings->GetString(printing::kSettingDeviceName, &printer_name) ||
1439 printer_name != cups_printer_color_models_->printer_name) {
1440 NOTREACHED();
1441 return;
1442 }
1443
1444 int color;
1445 if (!settings->GetInteger(printing::kSettingColor, &color)) {
1446 NOTREACHED();
1447 return;
1448 }
1449
1450 if (color == printing::GRAY) {
1451 if (cups_printer_color_models_->bw_model != printing::UNKNOWN_COLOR_MODEL) {
1452 settings->SetInteger(printing::kSettingColor,
1453 cups_printer_color_models_->bw_model);
1454 }
1455 return;
1456 }
1457
1458 printing::ColorModel color_model = cups_printer_color_models_->color_model;
1459 if (color_model != printing::UNKNOWN_COLOR_MODEL)
1460 settings->SetInteger(printing::kSettingColor, color_model);
1461 }
1462
1463 #endif
1464
1465 #if defined(ENABLE_SERVICE_DISCOVERY) 1390 #if defined(ENABLE_SERVICE_DISCOVERY)
1466 void PrintPreviewHandler::LocalPrinterChanged( 1391 void PrintPreviewHandler::LocalPrinterChanged(
1467 bool added, 1392 bool added,
1468 const std::string& name, 1393 const std::string& name,
1469 bool has_local_printing, 1394 bool has_local_printing,
1470 const local_discovery::DeviceDescription& description) { 1395 const local_discovery::DeviceDescription& description) {
1471 CommandLine* command_line = CommandLine::ForCurrentProcess(); 1396 CommandLine* command_line = CommandLine::ForCurrentProcess();
1472 if (has_local_printing || 1397 if (has_local_printing ||
1473 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)) { 1398 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)) {
1474 base::DictionaryValue info; 1399 base::DictionaryValue info;
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1655 printer_value->SetString("name", description.name); 1580 printer_value->SetString("name", description.name);
1656 printer_value->SetBoolean("hasLocalPrinting", has_local_printing); 1581 printer_value->SetBoolean("hasLocalPrinting", has_local_printing);
1657 printer_value->SetBoolean( 1582 printer_value->SetBoolean(
1658 "isUnregistered", 1583 "isUnregistered",
1659 description.id.empty() && 1584 description.id.empty() &&
1660 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)); 1585 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos));
1661 printer_value->SetString("cloudID", description.id); 1586 printer_value->SetString("cloudID", description.id);
1662 } 1587 }
1663 1588
1664 #endif 1589 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698