| 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_ui.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 59 |
| 60 namespace { | 60 namespace { |
| 61 | 61 |
| 62 #if defined(OS_MACOSX) | 62 #if defined(OS_MACOSX) |
| 63 // U+0028 U+21E7 U+2318 U+0050 U+0029 in UTF8 | 63 // U+0028 U+21E7 U+2318 U+0050 U+0029 in UTF8 |
| 64 const char kBasicPrintShortcut[] = "\x28\xE2\x8c\xA5\xE2\x8C\x98\x50\x29"; | 64 const char kBasicPrintShortcut[] = "\x28\xE2\x8c\xA5\xE2\x8C\x98\x50\x29"; |
| 65 #elif !defined(OS_CHROMEOS) | 65 #elif !defined(OS_CHROMEOS) |
| 66 const char kBasicPrintShortcut[] = "(Ctrl+Shift+P)"; | 66 const char kBasicPrintShortcut[] = "(Ctrl+Shift+P)"; |
| 67 #endif | 67 #endif |
| 68 | 68 |
| 69 PrintPreviewUI::TestingDelegate* g_testing_delegate = nullptr; |
| 70 |
| 69 // Thread-safe wrapper around a std::map to keep track of mappings from | 71 // Thread-safe wrapper around a std::map to keep track of mappings from |
| 70 // PrintPreviewUI IDs to most recent print preview request IDs. | 72 // PrintPreviewUI IDs to most recent print preview request IDs. |
| 71 class PrintPreviewRequestIdMapWithLock { | 73 class PrintPreviewRequestIdMapWithLock { |
| 72 public: | 74 public: |
| 73 PrintPreviewRequestIdMapWithLock() {} | 75 PrintPreviewRequestIdMapWithLock() {} |
| 74 ~PrintPreviewRequestIdMapWithLock() {} | 76 ~PrintPreviewRequestIdMapWithLock() {} |
| 75 | 77 |
| 76 // Gets the value for |preview_id|. | 78 // Gets the value for |preview_id|. |
| 77 // Returns true and sets |out_value| on success. | 79 // Returns true and sets |out_value| on success. |
| 78 bool Get(int32_t preview_id, int* out_value) { | 80 bool Get(int32_t preview_id, int* out_value) { |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 bool cups_and_md_settings_enabled = | 429 bool cups_and_md_settings_enabled = |
| 428 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 430 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 429 ::switches::kDisableNativeCups); | 431 ::switches::kDisableNativeCups); |
| 430 source->AddBoolean("showLocalManageButton", cups_and_md_settings_enabled); | 432 source->AddBoolean("showLocalManageButton", cups_and_md_settings_enabled); |
| 431 #else | 433 #else |
| 432 source->AddBoolean("showLocalManageButton", true); | 434 source->AddBoolean("showLocalManageButton", true); |
| 433 #endif | 435 #endif |
| 434 return source; | 436 return source; |
| 435 } | 437 } |
| 436 | 438 |
| 437 PrintPreviewUI::TestingDelegate* g_testing_delegate = NULL; | |
| 438 | |
| 439 } // namespace | 439 } // namespace |
| 440 | 440 |
| 441 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui) | 441 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui) |
| 442 : ConstrainedWebDialogUI(web_ui), | 442 : ConstrainedWebDialogUI(web_ui), |
| 443 initial_preview_start_time_(base::TimeTicks::Now()), | 443 initial_preview_start_time_(base::TimeTicks::Now()), |
| 444 id_(g_print_preview_ui_id_map.Get().Add(this)), | 444 id_(g_print_preview_ui_id_map.Get().Add(this)), |
| 445 handler_(nullptr), | 445 handler_(nullptr), |
| 446 source_is_modifiable_(true), | 446 source_is_modifiable_(true), |
| 447 source_has_selection_(false), | 447 source_has_selection_(false), |
| 448 print_selection_only_(false), | 448 print_selection_only_(false), |
| 449 dialog_closed_(false) { | 449 dialog_closed_(false) { |
| 450 // Set up the chrome://print/ data source. | 450 // Set up the chrome://print/ data source. |
| 451 Profile* profile = Profile::FromWebUI(web_ui); | 451 Profile* profile = Profile::FromWebUI(web_ui); |
| 452 content::WebUIDataSource::Add(profile, CreatePrintPreviewUISource()); | 452 content::WebUIDataSource::Add(profile, CreatePrintPreviewUISource()); |
| 453 | 453 |
| 454 // Set up the chrome://theme/ source. | 454 // Set up the chrome://theme/ source. |
| 455 content::URLDataSource::Add(profile, new ThemeSource(profile)); | 455 content::URLDataSource::Add(profile, new ThemeSource(profile)); |
| 456 | 456 |
| 457 auto handler = base::MakeUnique<PrintPreviewHandler>(); | 457 auto handler = base::MakeUnique<PrintPreviewHandler>(); |
| 458 handler_ = handler.get(); | 458 handler_ = handler.get(); |
| 459 web_ui->AddMessageHandler(std::move(handler)); | 459 web_ui->AddMessageHandler(std::move(handler)); |
| 460 web_ui->AddMessageHandler(base::MakeUnique<MetricsHandler>()); | 460 web_ui->AddMessageHandler(base::MakeUnique<MetricsHandler>()); |
| 461 | 461 |
| 462 g_print_preview_request_id_map.Get().Set(id_, -1); | 462 g_print_preview_request_id_map.Get().Set(id_, -1); |
| 463 } | 463 } |
| 464 | 464 |
| 465 PrintPreviewUI::~PrintPreviewUI() { | 465 PrintPreviewUI::~PrintPreviewUI() { |
| 466 print_preview_data_service()->RemoveEntry(id_); | 466 PrintPreviewDataService::GetInstance()->RemoveEntry(id_); |
| 467 g_print_preview_request_id_map.Get().Erase(id_); | 467 g_print_preview_request_id_map.Get().Erase(id_); |
| 468 g_print_preview_ui_id_map.Get().Remove(id_); | 468 g_print_preview_ui_id_map.Get().Remove(id_); |
| 469 } | 469 } |
| 470 | 470 |
| 471 void PrintPreviewUI::GetPrintPreviewDataForIndex( | 471 void PrintPreviewUI::GetPrintPreviewDataForIndex( |
| 472 int index, | 472 int index, |
| 473 scoped_refptr<base::RefCountedBytes>* data) { | 473 scoped_refptr<base::RefCountedBytes>* data) const { |
| 474 print_preview_data_service()->GetDataEntry(id_, index, data); | 474 PrintPreviewDataService::GetInstance()->GetDataEntry(id_, index, data); |
| 475 } | 475 } |
| 476 | 476 |
| 477 void PrintPreviewUI::SetPrintPreviewDataForIndex( | 477 void PrintPreviewUI::SetPrintPreviewDataForIndex( |
| 478 int index, | 478 int index, |
| 479 scoped_refptr<base::RefCountedBytes> data) { | 479 scoped_refptr<base::RefCountedBytes> data) { |
| 480 print_preview_data_service()->SetDataEntry(id_, index, std::move(data)); | 480 PrintPreviewDataService::GetInstance()->SetDataEntry(id_, index, |
| 481 std::move(data)); |
| 481 } | 482 } |
| 482 | 483 |
| 483 void PrintPreviewUI::ClearAllPreviewData() { | 484 void PrintPreviewUI::ClearAllPreviewData() { |
| 484 print_preview_data_service()->RemoveEntry(id_); | 485 PrintPreviewDataService::GetInstance()->RemoveEntry(id_); |
| 485 } | 486 } |
| 486 | 487 |
| 487 int PrintPreviewUI::GetAvailableDraftPageCount() { | 488 int PrintPreviewUI::GetAvailableDraftPageCount() const { |
| 488 return print_preview_data_service()->GetAvailableDraftPageCount(id_); | 489 return PrintPreviewDataService::GetInstance()->GetAvailableDraftPageCount( |
| 490 id_); |
| 489 } | 491 } |
| 490 | 492 |
| 491 void PrintPreviewUI::SetInitiatorTitle( | 493 void PrintPreviewUI::SetInitiatorTitle( |
| 492 const base::string16& job_title) { | 494 const base::string16& job_title) { |
| 493 initiator_title_ = job_title; | 495 initiator_title_ = job_title; |
| 494 } | 496 } |
| 495 | 497 |
| 496 // static | 498 // static |
| 497 void PrintPreviewUI::SetInitialParams( | 499 void PrintPreviewUI::SetInitialParams( |
| 498 content::WebContents* print_preview_dialog, | 500 content::WebContents* print_preview_dialog, |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 | 636 |
| 635 void PrintPreviewUI::OnPrintPreviewFailed() { | 637 void PrintPreviewUI::OnPrintPreviewFailed() { |
| 636 handler_->OnPrintPreviewFailed(); | 638 handler_->OnPrintPreviewFailed(); |
| 637 web_ui()->CallJavascriptFunctionUnsafe("printPreviewFailed"); | 639 web_ui()->CallJavascriptFunctionUnsafe("printPreviewFailed"); |
| 638 } | 640 } |
| 639 | 641 |
| 640 void PrintPreviewUI::OnInvalidPrinterSettings() { | 642 void PrintPreviewUI::OnInvalidPrinterSettings() { |
| 641 web_ui()->CallJavascriptFunctionUnsafe("invalidPrinterSettings"); | 643 web_ui()->CallJavascriptFunctionUnsafe("invalidPrinterSettings"); |
| 642 } | 644 } |
| 643 | 645 |
| 644 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { | |
| 645 return PrintPreviewDataService::GetInstance(); | |
| 646 } | |
| 647 | |
| 648 void PrintPreviewUI::OnHidePreviewDialog() { | 646 void PrintPreviewUI::OnHidePreviewDialog() { |
| 649 WebContents* preview_dialog = web_ui()->GetWebContents(); | 647 WebContents* preview_dialog = web_ui()->GetWebContents(); |
| 650 printing::BackgroundPrintingManager* background_printing_manager = | 648 printing::BackgroundPrintingManager* background_printing_manager = |
| 651 g_browser_process->background_printing_manager(); | 649 g_browser_process->background_printing_manager(); |
| 652 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog)) | 650 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog)) |
| 653 return; | 651 return; |
| 654 | 652 |
| 655 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); | 653 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); |
| 656 if (!delegate) | 654 if (!delegate) |
| 657 return; | 655 return; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 } | 693 } |
| 696 | 694 |
| 697 void PrintPreviewUI::SetSelectedFileForTesting(const base::FilePath& path) { | 695 void PrintPreviewUI::SetSelectedFileForTesting(const base::FilePath& path) { |
| 698 handler_->FileSelected(path, 0, NULL); | 696 handler_->FileSelected(path, 0, NULL); |
| 699 } | 697 } |
| 700 | 698 |
| 701 void PrintPreviewUI::SetPdfSavedClosureForTesting( | 699 void PrintPreviewUI::SetPdfSavedClosureForTesting( |
| 702 const base::Closure& closure) { | 700 const base::Closure& closure) { |
| 703 handler_->SetPdfSavedClosureForTesting(closure); | 701 handler_->SetPdfSavedClosureForTesting(closure); |
| 704 } | 702 } |
| OLD | NEW |