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