| 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> |
| 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/i18n/file_util_icu.h" | 15 #include "base/i18n/file_util_icu.h" |
| 16 #include "base/i18n/number_formatting.h" | 16 #include "base/i18n/number_formatting.h" |
| 17 #include "base/json/json_reader.h" | 17 #include "base/json/json_reader.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 20 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 22 #include "base/threading/thread_restrictions.h" | 22 #include "base/threading/thread_restrictions.h" |
| 23 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
| 24 #include "base/values.h" | 24 #include "base/values.h" |
| 25 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 26 #include "chrome/browser/platform_util.h" | 26 #include "chrome/browser/platform_util.h" |
| 27 #include "chrome/browser/prefs/pref_service.h" | 27 #include "chrome/browser/prefs/pref_service.h" |
| 28 #include "chrome/browser/printing/background_printing_manager.h" |
| 28 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" | 29 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
| 29 #include "chrome/browser/printing/print_dialog_cloud.h" | 30 #include "chrome/browser/printing/print_dialog_cloud.h" |
| 30 #include "chrome/browser/printing/print_job_manager.h" | 31 #include "chrome/browser/printing/print_job_manager.h" |
| 31 #include "chrome/browser/printing/print_preview_tab_controller.h" | 32 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 32 #include "chrome/browser/printing/print_system_task_proxy.h" | 33 #include "chrome/browser/printing/print_system_task_proxy.h" |
| 33 #include "chrome/browser/printing/print_view_manager.h" | 34 #include "chrome/browser/printing/print_view_manager.h" |
| 34 #include "chrome/browser/printing/printer_manager_dialog.h" | 35 #include "chrome/browser/printing/printer_manager_dialog.h" |
| 35 #include "chrome/browser/profiles/profile.h" | 36 #include "chrome/browser/profiles/profile.h" |
| 37 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 38 #include "chrome/browser/tabs/tab_strip_model.h" |
| 36 #include "chrome/browser/ui/browser_list.h" | 39 #include "chrome/browser/ui/browser_list.h" |
| 37 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 40 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 38 #include "chrome/browser/ui/webui/cloud_print_signin_dialog.h" | 41 #include "chrome/browser/ui/webui/cloud_print_signin_dialog.h" |
| 39 #include "chrome/browser/ui/webui/print_preview_ui.h" | 42 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 40 #include "chrome/common/chrome_paths.h" | 43 #include "chrome/common/chrome_paths.h" |
| 41 #include "chrome/common/pref_names.h" | 44 #include "chrome/common/pref_names.h" |
| 42 #include "chrome/common/print_messages.h" | 45 #include "chrome/common/print_messages.h" |
| 43 #include "content/browser/renderer_host/render_view_host.h" | 46 #include "content/browser/renderer_host/render_view_host.h" |
| 44 #include "content/browser/tab_contents/tab_contents.h" | 47 #include "content/browser/tab_contents/tab_contents.h" |
| 45 #include "content/public/browser/browser_thread.h" | 48 #include "content/public/browser/browser_thread.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 60 | 63 |
| 61 namespace { | 64 namespace { |
| 62 | 65 |
| 63 enum UserActionBuckets { | 66 enum UserActionBuckets { |
| 64 PRINT_TO_PRINTER, | 67 PRINT_TO_PRINTER, |
| 65 PRINT_TO_PDF, | 68 PRINT_TO_PDF, |
| 66 CANCEL, | 69 CANCEL, |
| 67 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, | 70 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, |
| 68 PREVIEW_FAILED, | 71 PREVIEW_FAILED, |
| 69 PREVIEW_STARTED, | 72 PREVIEW_STARTED, |
| 70 INITIATOR_TAB_CRASHED, // UNUSED | 73 INITIATOR_TAB_CRASHED, |
| 71 INITIATOR_TAB_CLOSED, | 74 INITIATOR_TAB_CLOSED, |
| 72 PRINT_WITH_CLOUD_PRINT, | 75 PRINT_WITH_CLOUD_PRINT, |
| 73 USERACTION_BUCKET_BOUNDARY | 76 USERACTION_BUCKET_BOUNDARY |
| 74 }; | 77 }; |
| 75 | 78 |
| 76 enum PrintSettingsBuckets { | 79 enum PrintSettingsBuckets { |
| 77 LANDSCAPE, | 80 LANDSCAPE, |
| 78 PORTRAIT, | 81 PORTRAIT, |
| 79 COLOR, | 82 COLOR, |
| 80 BLACK_AND_WHITE, | 83 BLACK_AND_WHITE, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 if (settings.GetInteger(printing::kSettingDuplexMode, &duplex_mode)) | 169 if (settings.GetInteger(printing::kSettingDuplexMode, &duplex_mode)) |
| 167 ReportPrintSettingHistogram(duplex_mode ? DUPLEX : SIMPLEX); | 170 ReportPrintSettingHistogram(duplex_mode ? DUPLEX : SIMPLEX); |
| 168 | 171 |
| 169 int color_mode; | 172 int color_mode; |
| 170 if (settings.GetInteger(printing::kSettingColor, &color_mode)) { | 173 if (settings.GetInteger(printing::kSettingColor, &color_mode)) { |
| 171 ReportPrintSettingHistogram( | 174 ReportPrintSettingHistogram( |
| 172 printing::isColorModelSelected(color_mode) ? COLOR : BLACK_AND_WHITE); | 175 printing::isColorModelSelected(color_mode) ? COLOR : BLACK_AND_WHITE); |
| 173 } | 176 } |
| 174 } | 177 } |
| 175 | 178 |
| 179 printing::BackgroundPrintingManager* GetBackgroundPrintingManager() { |
| 180 return g_browser_process->background_printing_manager(); |
| 181 } |
| 182 |
| 176 } // namespace | 183 } // namespace |
| 177 | 184 |
| 178 // A Task implementation that stores a PDF file on disk. | 185 // A Task implementation that stores a PDF file on disk. |
| 179 class PrintToPdfTask : public Task { | 186 class PrintToPdfTask : public Task { |
| 180 public: | 187 public: |
| 181 // Takes ownership of |metafile|. | 188 // Takes ownership of |metafile|. |
| 182 PrintToPdfTask(printing::Metafile* metafile, const FilePath& path) | 189 PrintToPdfTask(printing::Metafile* metafile, const FilePath& path) |
| 183 : metafile_(metafile), path_(path) { | 190 : metafile_(metafile), path_(path) { |
| 184 DCHECK(metafile); | 191 DCHECK(metafile); |
| 185 } | 192 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 base::Unretained(this))); | 253 base::Unretained(this))); |
| 247 web_ui_->RegisterMessageCallback("signIn", | 254 web_ui_->RegisterMessageCallback("signIn", |
| 248 base::Bind(&PrintPreviewHandler::HandleSignin, | 255 base::Bind(&PrintPreviewHandler::HandleSignin, |
| 249 base::Unretained(this))); | 256 base::Unretained(this))); |
| 250 web_ui_->RegisterMessageCallback("manageCloudPrinters", | 257 web_ui_->RegisterMessageCallback("manageCloudPrinters", |
| 251 base::Bind(&PrintPreviewHandler::HandleManageCloudPrint, | 258 base::Bind(&PrintPreviewHandler::HandleManageCloudPrint, |
| 252 base::Unretained(this))); | 259 base::Unretained(this))); |
| 253 web_ui_->RegisterMessageCallback("manageLocalPrinters", | 260 web_ui_->RegisterMessageCallback("manageLocalPrinters", |
| 254 base::Bind(&PrintPreviewHandler::HandleManagePrinters, | 261 base::Bind(&PrintPreviewHandler::HandleManagePrinters, |
| 255 base::Unretained(this))); | 262 base::Unretained(this))); |
| 263 web_ui_->RegisterMessageCallback("reloadCrashedInitiatorTab", |
| 264 base::Bind(&PrintPreviewHandler::HandleReloadCrashedInitiatorTab, |
| 265 base::Unretained(this))); |
| 256 web_ui_->RegisterMessageCallback("closePrintPreviewTab", | 266 web_ui_->RegisterMessageCallback("closePrintPreviewTab", |
| 257 base::Bind(&PrintPreviewHandler::HandleClosePreviewTab, | 267 base::Bind(&PrintPreviewHandler::HandleClosePreviewTab, |
| 258 base::Unretained(this))); | 268 base::Unretained(this))); |
| 259 web_ui_->RegisterMessageCallback("hidePreview", | 269 web_ui_->RegisterMessageCallback("hidePreview", |
| 260 base::Bind(&PrintPreviewHandler::HandleHidePreview, | 270 base::Bind(&PrintPreviewHandler::HandleHidePreview, |
| 261 base::Unretained(this))); | 271 base::Unretained(this))); |
| 262 web_ui_->RegisterMessageCallback("cancelPendingPrintRequest", | 272 web_ui_->RegisterMessageCallback("cancelPendingPrintRequest", |
| 263 base::Bind(&PrintPreviewHandler::HandleCancelPendingPrintRequest, | 273 base::Bind(&PrintPreviewHandler::HandleCancelPendingPrintRequest, |
| 264 base::Unretained(this))); | 274 base::Unretained(this))); |
| 265 web_ui_->RegisterMessageCallback("saveLastPrinter", | 275 web_ui_->RegisterMessageCallback("saveLastPrinter", |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // thread. | 315 // thread. |
| 306 settings->SetString(printing::kPreviewUIAddr, | 316 settings->SetString(printing::kPreviewUIAddr, |
| 307 print_preview_ui->GetPrintPreviewUIAddress()); | 317 print_preview_ui->GetPrintPreviewUIAddress()); |
| 308 | 318 |
| 309 // Increment request count. | 319 // Increment request count. |
| 310 ++regenerate_preview_request_count_; | 320 ++regenerate_preview_request_count_; |
| 311 | 321 |
| 312 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 322 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 313 if (!initiator_tab) { | 323 if (!initiator_tab) { |
| 314 ReportUserActionHistogram(INITIATOR_TAB_CLOSED); | 324 ReportUserActionHistogram(INITIATOR_TAB_CLOSED); |
| 315 print_preview_ui->OnClosePrintPreviewTab(); | 325 print_preview_ui->OnInitiatorTabClosed(); |
| 316 return; | 326 return; |
| 317 } | 327 } |
| 318 | 328 |
| 319 // Retrieve the page title and url and send it to the renderer process if | 329 // Retrieve the page title and url and send it to the renderer process if |
| 320 // headers and footers are to be displayed. | 330 // headers and footers are to be displayed. |
| 321 bool display_header_footer = false; | 331 bool display_header_footer = false; |
| 322 if (!settings->GetBoolean(printing::kSettingHeaderFooterEnabled, | 332 if (!settings->GetBoolean(printing::kSettingHeaderFooterEnabled, |
| 323 &display_header_footer)) { | 333 &display_header_footer)) { |
| 324 NOTREACHED(); | 334 NOTREACHED(); |
| 325 } | 335 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 } else if (is_cloud_dialog) { | 425 } else if (is_cloud_dialog) { |
| 416 HandlePrintWithCloudPrint(); | 426 HandlePrintWithCloudPrint(); |
| 417 } else { | 427 } else { |
| 418 ReportPrintSettingsStats(*settings); | 428 ReportPrintSettingsStats(*settings); |
| 419 ReportUserActionHistogram(PRINT_TO_PRINTER); | 429 ReportUserActionHistogram(PRINT_TO_PRINTER); |
| 420 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", | 430 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", |
| 421 GetPageCountFromSettingsDictionary(*settings)); | 431 GetPageCountFromSettingsDictionary(*settings)); |
| 422 | 432 |
| 423 // This tries to activate the initiator tab as well, so do not clear the | 433 // This tries to activate the initiator tab as well, so do not clear the |
| 424 // association with the initiator tab yet. | 434 // association with the initiator tab yet. |
| 425 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 435 HidePreviewTab(); |
| 426 print_preview_ui->OnHidePreviewTab(); | |
| 427 | 436 |
| 428 // Do this so the initiator tab can open a new print preview tab. | 437 // Do this so the initiator tab can open a new print preview tab. |
| 429 ClearInitiatorTabDetails(); | 438 ClearInitiatorTabDetails(); |
| 430 | 439 |
| 431 // The PDF being printed contains only the pages that the user selected, | 440 // The PDF being printed contains only the pages that the user selected, |
| 432 // so ignore the page range and print all pages. | 441 // so ignore the page range and print all pages. |
| 433 settings->Remove(printing::kSettingPageRange, NULL); | 442 settings->Remove(printing::kSettingPageRange, NULL); |
| 434 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); | 443 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); |
| 435 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | 444 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| 436 } | 445 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 460 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | 469 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); |
| 461 FilePath default_filename(print_job_title); | 470 FilePath default_filename(print_job_title); |
| 462 default_filename = | 471 default_filename = |
| 463 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | 472 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); |
| 464 | 473 |
| 465 SelectFile(default_filename); | 474 SelectFile(default_filename); |
| 466 } | 475 } |
| 467 } | 476 } |
| 468 | 477 |
| 469 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { | 478 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { |
| 470 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 479 HidePreviewTab(); |
| 471 print_preview_ui->OnHidePreviewTab(); | |
| 472 } | 480 } |
| 473 | 481 |
| 474 void PrintPreviewHandler::HandleCancelPendingPrintRequest( | 482 void PrintPreviewHandler::HandleCancelPendingPrintRequest( |
| 475 const ListValue* /*args*/) { | 483 const ListValue* /*args*/) { |
| 476 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 484 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 477 if (initiator_tab) { | 485 if (initiator_tab) { |
| 478 ClearInitiatorTabDetails(); | 486 ClearInitiatorTabDetails(); |
| 479 } else { | 487 } else { |
| 480 // Initiator tab does not exists. Get the wrapper contents of current tab. | 488 // Initiator tab does not exists. Get the wrapper contents of current tab. |
| 481 Browser* browser = BrowserList::GetLastActive(); | 489 Browser* browser = BrowserList::GetLastActive(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 // Cancel the pending preview request if exists. | 580 // Cancel the pending preview request if exists. |
| 573 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 581 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
| 574 print_preview_ui->OnCancelPendingPreviewRequest(); | 582 print_preview_ui->OnCancelPendingPreviewRequest(); |
| 575 } | 583 } |
| 576 | 584 |
| 577 void PrintPreviewHandler::HandleManagePrinters(const ListValue* /*args*/) { | 585 void PrintPreviewHandler::HandleManagePrinters(const ListValue* /*args*/) { |
| 578 ++manage_printers_dialog_request_count_; | 586 ++manage_printers_dialog_request_count_; |
| 579 printing::PrinterManagerDialog::ShowPrinterManagerDialog(); | 587 printing::PrinterManagerDialog::ShowPrinterManagerDialog(); |
| 580 } | 588 } |
| 581 | 589 |
| 590 void PrintPreviewHandler::HandleReloadCrashedInitiatorTab( |
| 591 const ListValue* /*args*/) { |
| 592 ReportStats(); |
| 593 ReportUserActionHistogram(INITIATOR_TAB_CRASHED); |
| 594 |
| 595 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 596 if (!initiator_tab) |
| 597 return; |
| 598 |
| 599 TabContents* contents = initiator_tab->tab_contents(); |
| 600 contents->OpenURL(contents->GetURL(), GURL(), CURRENT_TAB, |
| 601 content::PAGE_TRANSITION_RELOAD); |
| 602 ActivateInitiatorTabAndClosePreviewTab(); |
| 603 } |
| 604 |
| 582 void PrintPreviewHandler::HandleClosePreviewTab(const ListValue* /*args*/) { | 605 void PrintPreviewHandler::HandleClosePreviewTab(const ListValue* /*args*/) { |
| 583 ReportStats(); | 606 ReportStats(); |
| 584 ReportUserActionHistogram(CANCEL); | 607 ReportUserActionHistogram(CANCEL); |
| 585 | 608 |
| 586 // Record the number of times the user requests to regenerate preview data | 609 // Record the number of times the user requests to regenerate preview data |
| 587 // before cancelling. | 610 // before cancelling. |
| 588 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforeCancel", | 611 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforeCancel", |
| 589 regenerate_preview_request_count_); | 612 regenerate_preview_request_count_); |
| 613 |
| 614 ActivateInitiatorTabAndClosePreviewTab(); |
| 590 } | 615 } |
| 591 | 616 |
| 592 void PrintPreviewHandler::ReportStats() { | 617 void PrintPreviewHandler::ReportStats() { |
| 593 UMA_HISTOGRAM_COUNTS("PrintPreview.ManagePrinters", | 618 UMA_HISTOGRAM_COUNTS("PrintPreview.ManagePrinters", |
| 594 manage_printers_dialog_request_count_); | 619 manage_printers_dialog_request_count_); |
| 595 } | 620 } |
| 596 | 621 |
| 597 void PrintPreviewHandler::GetNumberFormatAndMeasurementSystem( | 622 void PrintPreviewHandler::GetNumberFormatAndMeasurementSystem( |
| 598 base::DictionaryValue* settings) { | 623 base::DictionaryValue* settings) { |
| 599 | 624 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 } | 681 } |
| 657 web_ui_->CallJavascriptFunction("setInitialSettings", initial_settings); | 682 web_ui_->CallJavascriptFunction("setInitialSettings", initial_settings); |
| 658 } | 683 } |
| 659 | 684 |
| 660 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { | 685 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { |
| 661 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 686 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 662 if (initiator_tab) { | 687 if (initiator_tab) { |
| 663 static_cast<RenderViewHostDelegate*>( | 688 static_cast<RenderViewHostDelegate*>( |
| 664 initiator_tab->tab_contents())->Activate(); | 689 initiator_tab->tab_contents())->Activate(); |
| 665 } | 690 } |
| 666 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 691 ClosePrintPreviewTab(); |
| 667 print_preview_ui->OnClosePrintPreviewTab(); | |
| 668 } | 692 } |
| 669 | 693 |
| 670 void PrintPreviewHandler::SendPrinterCapabilities( | 694 void PrintPreviewHandler::SendPrinterCapabilities( |
| 671 const DictionaryValue& settings_info) { | 695 const DictionaryValue& settings_info) { |
| 672 VLOG(1) << "Get printer capabilities finished"; | 696 VLOG(1) << "Get printer capabilities finished"; |
| 673 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities", | 697 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities", |
| 674 settings_info); | 698 settings_info); |
| 675 } | 699 } |
| 676 | 700 |
| 677 void PrintPreviewHandler::SetupPrinterList(const ListValue& printers) { | 701 void PrintPreviewHandler::SetupPrinterList(const ListValue& printers) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 696 print_preview_ui->GetPrintPreviewDataForIndex( | 720 print_preview_ui->GetPrintPreviewDataForIndex( |
| 697 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); | 721 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); |
| 698 CHECK(data.get()); | 722 CHECK(data.get()); |
| 699 DCHECK_GT(data->size(), 0U); | 723 DCHECK_GT(data->size(), 0U); |
| 700 | 724 |
| 701 string16 print_job_title_utf16 = | 725 string16 print_job_title_utf16 = |
| 702 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); | 726 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); |
| 703 std::string print_job_title = UTF16ToUTF8(print_job_title_utf16); | 727 std::string print_job_title = UTF16ToUTF8(print_job_title_utf16); |
| 704 std::string printer_id; | 728 std::string printer_id; |
| 705 settings.GetString(printing::kSettingCloudPrintId, &printer_id); | 729 settings.GetString(printing::kSettingCloudPrintId, &printer_id); |
| 706 // BASE64 encode the job data. | 730 // BASE64 encode the job data. |
| 707 std::string raw_data(reinterpret_cast<const char*>(data->front()), | 731 std::string raw_data(reinterpret_cast<const char*>(data->front()), |
| 708 data->size()); | 732 data->size()); |
| 709 std::string base64_data; | 733 std::string base64_data; |
| 710 if (!base::Base64Encode(raw_data, &base64_data)) { | 734 if (!base::Base64Encode(raw_data, &base64_data)) { |
| 711 NOTREACHED() << "Base64 encoding PDF data."; | 735 NOTREACHED() << "Base64 encoding PDF data."; |
| 712 } | 736 } |
| 713 | 737 |
| 714 const char boundary[] = "----CloudPrintFormBoundaryjc9wuprokl8i"; | 738 const char boundary[] = "----CloudPrintFormBoundaryjc9wuprokl8i"; |
| 715 const char prolog[] = "--%s\r\n" | 739 const char prolog[] = "--%s\r\n" |
| 716 "Content-Disposition: form-data; name=\"capabilities\"\r\n\r\n%s\r\n" | 740 "Content-Disposition: form-data; name=\"capabilities\"\r\n\r\n%s\r\n" |
| 717 "--%s\r\n" | 741 "--%s\r\n" |
| 718 "Content-Disposition: form-data; name=\"contentType\"\r\n\r\ndataUrl\r\n" | 742 "Content-Disposition: form-data; name=\"contentType\"\r\n\r\ndataUrl\r\n" |
| 719 "--%s\r\n" | 743 "--%s\r\n" |
| 720 "Content-Disposition: form-data; name=\"title\"\r\n\r\n%s\r\n" | 744 "Content-Disposition: form-data; name=\"title\"\r\n\r\n%s\r\n" |
| 721 "--%s\r\n" | 745 "--%s\r\n" |
| 722 "Content-Disposition: form-data; name=\"printerid\"\r\n\r\n%s\r\n" | 746 "Content-Disposition: form-data; name=\"printerid\"\r\n\r\n%s\r\n" |
| 723 "--%s\r\n" | 747 "--%s\r\n" |
| 724 "Content-Disposition: form-data; name=\"content\"\r\n\r\n" | 748 "Content-Disposition: form-data; name=\"content\"\r\n\r\n" |
| 725 "data:application/pdf;base64,%s\r\n" | 749 "data:application/pdf;base64,%s\r\n" |
| 726 "--%s\r\n"; | 750 "--%s\r\n"; |
| 727 | 751 |
| 728 // TODO(abodenha@chromium.org) This implies a large copy operation. | 752 // TODO(abodenha@chromium.org) This implies a large copy operation. |
| 729 // Profile this and optimize if necessary. | 753 // Profile this and optimize if necessary. |
| 730 std::string final_data; | 754 std::string final_data; |
| 731 base::SStringPrintf(&final_data, | 755 base::SStringPrintf(&final_data, |
| 732 prolog, | 756 prolog, |
| 733 boundary, | 757 boundary, |
| 734 print_ticket.c_str(), | 758 print_ticket.c_str(), |
| 735 boundary, | 759 boundary, |
| 736 boundary, | 760 boundary, |
| 737 print_job_title.c_str(), | 761 print_job_title.c_str(), |
| 738 boundary, | 762 boundary, |
| 739 printer_id.c_str(), | 763 printer_id.c_str(), |
| 740 boundary, | 764 boundary, |
| 741 base64_data.c_str(), | 765 base64_data.c_str(), |
| 742 boundary); | 766 boundary); |
| 743 | 767 |
| 744 StringValue data_value(final_data); | 768 StringValue data_value(final_data); |
| 745 | 769 |
| 746 web_ui_->CallJavascriptFunction("printToCloud", | 770 web_ui_->CallJavascriptFunction("printToCloud", |
| 747 data_value); | 771 data_value); |
| 748 } | 772 } |
| 749 | 773 |
| 750 TabContentsWrapper* PrintPreviewHandler::GetInitiatorTab() const { | 774 TabContentsWrapper* PrintPreviewHandler::GetInitiatorTab() const { |
| 751 printing::PrintPreviewTabController* tab_controller = | 775 printing::PrintPreviewTabController* tab_controller = |
| 752 printing::PrintPreviewTabController::GetInstance(); | 776 printing::PrintPreviewTabController::GetInstance(); |
| 753 if (!tab_controller) | 777 if (!tab_controller) |
| 754 return NULL; | 778 return NULL; |
| 755 return tab_controller->GetInitiatorTab(preview_tab_wrapper()); | 779 return tab_controller->GetInitiatorTab(preview_tab_wrapper()); |
| 756 } | 780 } |
| 757 | 781 |
| 782 void PrintPreviewHandler::ClosePrintPreviewTab() { |
| 783 TabContentsWrapper* tab = |
| 784 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); |
| 785 if (!tab) |
| 786 return; |
| 787 Browser* preview_tab_browser = BrowserList::FindBrowserWithID( |
| 788 tab->restore_tab_helper()->window_id().id()); |
| 789 if (!preview_tab_browser) |
| 790 return; |
| 791 TabStripModel* tabstrip = preview_tab_browser->tabstrip_model(); |
| 792 int index = tabstrip->GetIndexOfTabContents(tab); |
| 793 if (index == TabStripModel::kNoTab) |
| 794 return; |
| 795 |
| 796 // Keep print preview tab out of the recently closed tab list, because |
| 797 // re-opening that page will just display a non-functional print preview page. |
| 798 tabstrip->CloseTabContentsAt(index, TabStripModel::CLOSE_NONE); |
| 799 } |
| 800 |
| 758 void PrintPreviewHandler::OnPrintDialogShown() { | 801 void PrintPreviewHandler::OnPrintDialogShown() { |
| 759 ActivateInitiatorTabAndClosePreviewTab(); | 802 ActivateInitiatorTabAndClosePreviewTab(); |
| 760 } | 803 } |
| 761 | 804 |
| 762 void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { | 805 void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { |
| 763 SelectFileDialog::FileTypeInfo file_type_info; | 806 SelectFileDialog::FileTypeInfo file_type_info; |
| 764 file_type_info.extensions.resize(1); | 807 file_type_info.extensions.resize(1); |
| 765 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | 808 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 766 | 809 |
| 767 // Initializing last_saved_path_ if it is not already initialized. | 810 // Initializing last_saved_path_ if it is not already initialized. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 879 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 837 print_to_pdf_path_.reset(); | 880 print_to_pdf_path_.reset(); |
| 838 ActivateInitiatorTabAndClosePreviewTab(); | 881 ActivateInitiatorTabAndClosePreviewTab(); |
| 839 } | 882 } |
| 840 | 883 |
| 841 void PrintPreviewHandler::FileSelectionCanceled(void* params) { | 884 void PrintPreviewHandler::FileSelectionCanceled(void* params) { |
| 842 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 885 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
| 843 print_preview_ui->OnFileSelectionCancelled(); | 886 print_preview_ui->OnFileSelectionCancelled(); |
| 844 } | 887 } |
| 845 | 888 |
| 889 void PrintPreviewHandler::HidePreviewTab() { |
| 890 if (GetBackgroundPrintingManager()->HasPrintPreviewTab(preview_tab_wrapper())) |
| 891 return; |
| 892 GetBackgroundPrintingManager()->OwnPrintPreviewTab(preview_tab_wrapper()); |
| 893 } |
| 894 |
| 846 void PrintPreviewHandler::ClearInitiatorTabDetails() { | 895 void PrintPreviewHandler::ClearInitiatorTabDetails() { |
| 847 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 896 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 848 if (!initiator_tab) | 897 if (!initiator_tab) |
| 849 return; | 898 return; |
| 850 | 899 |
| 851 // We no longer require the initiator tab details. Remove those details | 900 // We no longer require the initiator tab details. Remove those details |
| 852 // associated with the preview tab to allow the initiator tab to create | 901 // associated with the preview tab to allow the initiator tab to create |
| 853 // another preview tab. | 902 // another preview tab. |
| 854 printing::PrintPreviewTabController* tab_controller = | 903 printing::PrintPreviewTabController* tab_controller = |
| 855 printing::PrintPreviewTabController::GetInstance(); | 904 printing::PrintPreviewTabController::GetInstance(); |
| 856 if (tab_controller) | 905 if (tab_controller) |
| 857 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); | 906 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); |
| 858 } | 907 } |
| OLD | NEW |