OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview_handler.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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 #include "chrome/common/chrome_paths.h" | 43 #include "chrome/common/chrome_paths.h" |
44 #include "chrome/common/pref_names.h" | 44 #include "chrome/common/pref_names.h" |
45 #include "chrome/common/print_messages.h" | 45 #include "chrome/common/print_messages.h" |
46 #include "content/browser/renderer_host/render_view_host.h" | 46 #include "content/browser/renderer_host/render_view_host.h" |
47 #include "content/browser/tab_contents/tab_contents.h" | 47 #include "content/browser/tab_contents/tab_contents.h" |
48 #include "content/public/browser/browser_thread.h" | 48 #include "content/public/browser/browser_thread.h" |
49 #include "printing/backend/print_backend.h" | 49 #include "printing/backend/print_backend.h" |
50 #include "printing/metafile.h" | 50 #include "printing/metafile.h" |
51 #include "printing/metafile_impl.h" | 51 #include "printing/metafile_impl.h" |
52 #include "printing/page_range.h" | 52 #include "printing/page_range.h" |
| 53 #include "printing/page_size_margins.h" |
53 #include "printing/print_settings.h" | 54 #include "printing/print_settings.h" |
54 #include "unicode/ulocdata.h" | 55 #include "unicode/ulocdata.h" |
55 | 56 |
56 #if !defined(OS_CHROMEOS) | 57 #if !defined(OS_CHROMEOS) |
57 #include "base/command_line.h" | 58 #include "base/command_line.h" |
58 #include "chrome/common/chrome_switches.h" | 59 #include "chrome/common/chrome_switches.h" |
59 #endif | 60 #endif |
60 | 61 |
61 using content::BrowserThread; | 62 using content::BrowserThread; |
62 | 63 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 }; | 200 }; |
200 | 201 |
201 // static | 202 // static |
202 FilePath* PrintPreviewHandler::last_saved_path_ = NULL; | 203 FilePath* PrintPreviewHandler::last_saved_path_ = NULL; |
203 std::string* PrintPreviewHandler::last_used_printer_cloud_print_data_ = NULL; | 204 std::string* PrintPreviewHandler::last_used_printer_cloud_print_data_ = NULL; |
204 std::string* PrintPreviewHandler::last_used_printer_name_ = NULL; | 205 std::string* PrintPreviewHandler::last_used_printer_name_ = NULL; |
205 printing::ColorModels PrintPreviewHandler::last_used_color_model_ = | 206 printing::ColorModels PrintPreviewHandler::last_used_color_model_ = |
206 printing::UNKNOWN_COLOR_MODEL; | 207 printing::UNKNOWN_COLOR_MODEL; |
207 printing::MarginType PrintPreviewHandler::last_used_margins_type_ = | 208 printing::MarginType PrintPreviewHandler::last_used_margins_type_ = |
208 printing::DEFAULT_MARGINS; | 209 printing::DEFAULT_MARGINS; |
| 210 printing::PageSizeMargins* |
| 211 PrintPreviewHandler::last_used_page_size_margins_ = NULL; |
209 | 212 |
210 PrintPreviewHandler::PrintPreviewHandler() | 213 PrintPreviewHandler::PrintPreviewHandler() |
211 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), | 214 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), |
212 regenerate_preview_request_count_(0), | 215 regenerate_preview_request_count_(0), |
213 manage_printers_dialog_request_count_(0), | 216 manage_printers_dialog_request_count_(0), |
214 reported_failed_preview_(false), | 217 reported_failed_preview_(false), |
215 has_logged_printers_count_(false) { | 218 has_logged_printers_count_(false) { |
216 ReportUserActionHistogram(PREVIEW_STARTED); | 219 ReportUserActionHistogram(PREVIEW_STARTED); |
217 } | 220 } |
218 | 221 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 base::Unretained(this))); | 260 base::Unretained(this))); |
258 web_ui_->RegisterMessageCallback("hidePreview", | 261 web_ui_->RegisterMessageCallback("hidePreview", |
259 base::Bind(&PrintPreviewHandler::HandleHidePreview, | 262 base::Bind(&PrintPreviewHandler::HandleHidePreview, |
260 base::Unretained(this))); | 263 base::Unretained(this))); |
261 web_ui_->RegisterMessageCallback("cancelPendingPrintRequest", | 264 web_ui_->RegisterMessageCallback("cancelPendingPrintRequest", |
262 base::Bind(&PrintPreviewHandler::HandleCancelPendingPrintRequest, | 265 base::Bind(&PrintPreviewHandler::HandleCancelPendingPrintRequest, |
263 base::Unretained(this))); | 266 base::Unretained(this))); |
264 web_ui_->RegisterMessageCallback("saveLastPrinter", | 267 web_ui_->RegisterMessageCallback("saveLastPrinter", |
265 base::Bind(&PrintPreviewHandler::HandleSaveLastPrinter, | 268 base::Bind(&PrintPreviewHandler::HandleSaveLastPrinter, |
266 base::Unretained(this))); | 269 base::Unretained(this))); |
267 web_ui_->RegisterMessageCallback("getInitiatorTabTitle", | 270 web_ui_->RegisterMessageCallback("getInitialSettings", |
268 base::Bind(&PrintPreviewHandler::HandleGetInitiatorTabTitle, | 271 base::Bind(&PrintPreviewHandler::HandleGetInitialSettings, |
269 base::Unretained(this))); | 272 base::Unretained(this))); |
270 web_ui_->RegisterMessageCallback("getNumberFormatAndMeasurementSystem", | |
271 base::Bind( | |
272 &PrintPreviewHandler::HandleGetNumberFormatAndMeasurementSystem, | |
273 base::Unretained(this))); | |
274 } | 273 } |
275 | 274 |
276 TabContentsWrapper* PrintPreviewHandler::preview_tab_wrapper() const { | 275 TabContentsWrapper* PrintPreviewHandler::preview_tab_wrapper() const { |
277 return TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); | 276 return TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); |
278 } | 277 } |
279 TabContents* PrintPreviewHandler::preview_tab() const { | 278 TabContents* PrintPreviewHandler::preview_tab() const { |
280 return web_ui_->tab_contents(); | 279 return web_ui_->tab_contents(); |
281 } | 280 } |
282 | 281 |
283 void PrintPreviewHandler::HandleGetDefaultPrinter(const ListValue* /*args*/) { | 282 void PrintPreviewHandler::HandleGetDefaultPrinter(const ListValue* /*args*/) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 if (!settings.get()) | 389 if (!settings.get()) |
391 return; | 390 return; |
392 | 391 |
393 // Storing last used color model. | 392 // Storing last used color model. |
394 int color_model; | 393 int color_model; |
395 if (!settings->GetInteger(printing::kSettingColor, &color_model)) | 394 if (!settings->GetInteger(printing::kSettingColor, &color_model)) |
396 color_model = printing::GRAY; | 395 color_model = printing::GRAY; |
397 last_used_color_model_ = static_cast<printing::ColorModels>(color_model); | 396 last_used_color_model_ = static_cast<printing::ColorModels>(color_model); |
398 | 397 |
399 // Storing last used margin settings. | 398 // Storing last used margin settings. |
400 int margin_type; | 399 bool is_modifiable; |
401 if (!settings->GetInteger(printing::kSettingMarginsType, &margin_type)) | 400 settings->GetBoolean(printing::kSettingPreviewModifiable, &is_modifiable); |
402 margin_type = printing::DEFAULT_MARGINS; | 401 if (is_modifiable) { |
403 last_used_margins_type_ = static_cast<printing::MarginType>(margin_type); | 402 int margin_type; |
| 403 if (!settings->GetInteger(printing::kSettingMarginsType, &margin_type)) |
| 404 margin_type = printing::DEFAULT_MARGINS; |
| 405 last_used_margins_type_ = static_cast<printing::MarginType>(margin_type); |
| 406 if (last_used_margins_type_ == printing::CUSTOM_MARGINS) { |
| 407 if (!last_used_page_size_margins_) |
| 408 last_used_page_size_margins_ = new printing::PageSizeMargins(); |
| 409 getCustomMarginsFromJobSettings(*settings, last_used_page_size_margins_); |
| 410 } |
| 411 } |
404 | 412 |
405 bool print_to_pdf = false; | 413 bool print_to_pdf = false; |
406 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 414 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
407 | 415 |
408 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); | 416 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); |
409 | 417 |
410 bool is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); | 418 bool is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); |
411 bool is_cloud_dialog = false; | 419 bool is_cloud_dialog = false; |
412 settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog); | 420 settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog); |
413 if (is_cloud_printer) { | 421 if (is_cloud_printer) { |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 regenerate_preview_request_count_); | 614 regenerate_preview_request_count_); |
607 | 615 |
608 ActivateInitiatorTabAndClosePreviewTab(); | 616 ActivateInitiatorTabAndClosePreviewTab(); |
609 } | 617 } |
610 | 618 |
611 void PrintPreviewHandler::ReportStats() { | 619 void PrintPreviewHandler::ReportStats() { |
612 UMA_HISTOGRAM_COUNTS("PrintPreview.ManagePrinters", | 620 UMA_HISTOGRAM_COUNTS("PrintPreview.ManagePrinters", |
613 manage_printers_dialog_request_count_); | 621 manage_printers_dialog_request_count_); |
614 } | 622 } |
615 | 623 |
616 void PrintPreviewHandler::HandleGetInitiatorTabTitle( | 624 void PrintPreviewHandler::GetNumberFormatAndMeasurementSystem( |
617 const ListValue* /*args*/) { | 625 string16* number_format, int* measurement_system) { |
618 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | |
619 base::StringValue tab_title(print_preview_ui->initiator_tab_title()); | |
620 web_ui_->CallJavascriptFunction("setInitiatorTabTitle", tab_title); | |
621 } | |
622 | |
623 void PrintPreviewHandler::HandleGetNumberFormatAndMeasurementSystem( | |
624 const ListValue* /*args*/) { | |
625 | 626 |
626 // Getting the measurement system based on the locale. | 627 // Getting the measurement system based on the locale. |
627 UErrorCode errorCode = U_ZERO_ERROR; | 628 UErrorCode errorCode = U_ZERO_ERROR; |
628 const char* locale = g_browser_process->GetApplicationLocale().c_str(); | 629 const char* locale = g_browser_process->GetApplicationLocale().c_str(); |
629 UMeasurementSystem measurement_system = | 630 UMeasurementSystem system = ulocdata_getMeasurementSystem(locale, &errorCode); |
630 ulocdata_getMeasurementSystem(locale, &errorCode); | 631 if (errorCode > U_ZERO_ERROR || system == UMS_LIMIT) |
631 if (errorCode > U_ZERO_ERROR || measurement_system == UMS_LIMIT) | 632 system = UMS_SI; |
632 measurement_system = UMS_SI; | |
633 | 633 |
634 // Getting the number formatting based on the locale. | 634 // Getting the number formatting based on the locale. |
635 StringValue number_format(base::FormatDouble(123456.78, 2)); | 635 *measurement_system = system; |
| 636 *number_format = base::FormatDouble(123456.78, 2); |
| 637 } |
| 638 |
| 639 void PrintPreviewHandler::GetLastUsedMarginSettings( |
| 640 printing::MarginType* margin_type, base::DictionaryValue* custom_margins) { |
| 641 *margin_type = PrintPreviewHandler::last_used_margins_type_; |
| 642 if (last_used_page_size_margins_) { |
| 643 custom_margins->SetDouble(printing::kSettingMarginTop, |
| 644 last_used_page_size_margins_->margin_top); |
| 645 custom_margins->SetDouble(printing::kSettingMarginBottom, |
| 646 last_used_page_size_margins_->margin_bottom); |
| 647 custom_margins->SetDouble(printing::kSettingMarginLeft, |
| 648 last_used_page_size_margins_->margin_left); |
| 649 custom_margins->SetDouble(printing::kSettingMarginRight, |
| 650 last_used_page_size_margins_->margin_right); |
| 651 } |
| 652 } |
| 653 |
| 654 void PrintPreviewHandler::HandleGetInitialSettings(const ListValue* /*args*/) { |
| 655 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
| 656 base::StringValue tab_title(print_preview_ui->initiator_tab_title()); |
| 657 |
| 658 string16 number_format; |
| 659 int measurement_system = -1; |
| 660 GetNumberFormatAndMeasurementSystem(&number_format, &measurement_system); |
| 661 |
| 662 printing::MarginType margin_type; |
| 663 base::DictionaryValue custom_margins; |
| 664 GetLastUsedMarginSettings(&margin_type, &custom_margins); |
| 665 |
| 666 base::FundamentalValue m_type(margin_type); |
| 667 base::StringValue format(number_format); |
636 base::FundamentalValue system(measurement_system); | 668 base::FundamentalValue system(measurement_system); |
| 669 base::FundamentalValue modifiable(print_preview_ui->is_modifiable()); |
637 | 670 |
638 web_ui_->CallJavascriptFunction( | 671 std::vector<const base::Value*> args; |
639 "print_preview.setNumberFormatAndMeasurementSystem", | 672 args.push_back(&tab_title); |
640 number_format, | 673 args.push_back(&format); |
641 system); | 674 args.push_back(&system); |
| 675 args.push_back(&m_type); |
| 676 args.push_back(&custom_margins); |
| 677 args.push_back(&modifiable); |
| 678 web_ui_->CallJavascriptFunction("setInitialSettings", args); |
642 } | 679 } |
643 | 680 |
644 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { | 681 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { |
645 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 682 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
646 if (initiator_tab) { | 683 if (initiator_tab) { |
647 static_cast<RenderViewHostDelegate*>( | 684 static_cast<RenderViewHostDelegate*>( |
648 initiator_tab->tab_contents())->Activate(); | 685 initiator_tab->tab_contents())->Activate(); |
649 } | 686 } |
650 ClosePrintPreviewTab(); | 687 ClosePrintPreviewTab(); |
651 } | 688 } |
652 | 689 |
653 void PrintPreviewHandler::SendPrinterCapabilities( | 690 void PrintPreviewHandler::SendPrinterCapabilities( |
654 const DictionaryValue& settings_info) { | 691 const DictionaryValue& settings_info) { |
655 VLOG(1) << "Get printer capabilities finished"; | 692 VLOG(1) << "Get printer capabilities finished"; |
656 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities", | 693 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities", |
657 settings_info); | 694 settings_info); |
658 } | 695 } |
659 | 696 |
660 void PrintPreviewHandler::SendDefaultPrinter( | 697 void PrintPreviewHandler::SendDefaultPrinter( |
661 const StringValue& default_printer, | 698 const StringValue& default_printer, |
662 const StringValue& cloud_print_data) { | 699 const StringValue& cloud_print_data) { |
663 base::FundamentalValue margins_type( | 700 web_ui_->CallJavascriptFunction( |
664 PrintPreviewHandler::last_used_margins_type_); | 701 "setDefaultPrinter", default_printer, cloud_print_data); |
665 web_ui_->CallJavascriptFunction("setDefaultPrinter", | |
666 default_printer, | |
667 cloud_print_data, | |
668 margins_type); | |
669 } | 702 } |
670 | 703 |
671 void PrintPreviewHandler::SetupPrinterList(const ListValue& printers) { | 704 void PrintPreviewHandler::SetupPrinterList(const ListValue& printers) { |
672 SendCloudPrintEnabled(); | 705 SendCloudPrintEnabled(); |
673 web_ui_->CallJavascriptFunction("setPrinters", printers); | 706 web_ui_->CallJavascriptFunction("setPrinters", printers); |
674 } | 707 } |
675 | 708 |
676 void PrintPreviewHandler::SendCloudPrintEnabled() { | 709 void PrintPreviewHandler::SendCloudPrintEnabled() { |
677 Profile* profile = BrowserList::GetLastActive()->profile(); | 710 Profile* profile = BrowserList::GetLastActive()->profile(); |
678 PrefService* prefs = profile->GetPrefs(); | 711 PrefService* prefs = profile->GetPrefs(); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
868 return; | 901 return; |
869 | 902 |
870 // We no longer require the initiator tab details. Remove those details | 903 // We no longer require the initiator tab details. Remove those details |
871 // associated with the preview tab to allow the initiator tab to create | 904 // associated with the preview tab to allow the initiator tab to create |
872 // another preview tab. | 905 // another preview tab. |
873 printing::PrintPreviewTabController* tab_controller = | 906 printing::PrintPreviewTabController* tab_controller = |
874 printing::PrintPreviewTabController::GetInstance(); | 907 printing::PrintPreviewTabController::GetInstance(); |
875 if (tab_controller) | 908 if (tab_controller) |
876 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); | 909 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); |
877 } | 910 } |
OLD | NEW |