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

Side by Side Diff: chrome/browser/resources/print_preview/print_preview.js

Issue 2948723002: Print Preview: Change print to cr.sendWithPromise (Closed)
Patch Set: Address comments and add close_ param Created 3 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
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 // TODO(rltoscano): Move data/* into print_preview.data namespace 5 // TODO(rltoscano): Move data/* into print_preview.data namespace
6 6
7 // <include src="component.js"> 7 // <include src="component.js">
8 // <include src="print_preview_focus_manager.js"> 8 // <include src="print_preview_focus_manager.js">
9 // 9 //
10 10
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 /** @override */ 345 /** @override */
346 enterDocument: function() { 346 enterDocument: function() {
347 // Native layer events. 347 // Native layer events.
348 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); 348 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget();
349 this.tracker.add( 349 this.tracker.add(
350 nativeLayerEventTarget, 350 nativeLayerEventTarget,
351 print_preview.NativeLayer.EventType.CLOUD_PRINT_ENABLE, 351 print_preview.NativeLayer.EventType.CLOUD_PRINT_ENABLE,
352 this.onCloudPrintEnable_.bind(this)); 352 this.onCloudPrintEnable_.bind(this));
353 this.tracker.add( 353 this.tracker.add(
354 nativeLayerEventTarget, 354 nativeLayerEventTarget,
355 print_preview.NativeLayer.EventType.PRINT_TO_CLOUD,
356 this.onPrintToCloud_.bind(this));
357 this.tracker.add(
358 nativeLayerEventTarget,
359 print_preview.NativeLayer.EventType.FILE_SELECTION_CANCEL,
360 this.onFileSelectionCancel_.bind(this));
361 this.tracker.add(
362 nativeLayerEventTarget,
363 print_preview.NativeLayer.EventType.FILE_SELECTION_COMPLETE,
364 this.onFileSelectionComplete_.bind(this));
365 this.tracker.add(
366 nativeLayerEventTarget,
367 print_preview.NativeLayer.EventType.SETTINGS_INVALID, 355 print_preview.NativeLayer.EventType.SETTINGS_INVALID,
368 this.onSettingsInvalid_.bind(this)); 356 this.onSettingsInvalid_.bind(this));
369 this.tracker.add( 357 this.tracker.add(
370 nativeLayerEventTarget, 358 nativeLayerEventTarget,
371 print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS, 359 print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS,
372 this.onPrintPresetOptionsFromDocument_.bind(this)); 360 this.onPrintPresetOptionsFromDocument_.bind(this));
373 this.tracker.add( 361 this.tracker.add(
374 nativeLayerEventTarget, 362 nativeLayerEventTarget,
375 print_preview.NativeLayer.EventType.PAGE_COUNT_READY, 363 print_preview.NativeLayer.EventType.PAGE_COUNT_READY,
376 this.onPageCountReady_.bind(this)); 364 this.onPageCountReady_.bind(this));
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 } else if ( 525 } else if (
538 this.destinationStore_.selectedDestination.id == 526 this.destinationStore_.selectedDestination.id ==
539 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) { 527 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) {
540 this.uiState_ = PrintPreviewUiState_.FILE_SELECTION; 528 this.uiState_ = PrintPreviewUiState_.FILE_SELECTION;
541 } else { 529 } else {
542 this.uiState_ = PrintPreviewUiState_.PRINTING; 530 this.uiState_ = PrintPreviewUiState_.PRINTING;
543 } 531 }
544 this.setIsEnabled_(false); 532 this.setIsEnabled_(false);
545 this.printHeader_.isCancelButtonEnabled = true; 533 this.printHeader_.isCancelButtonEnabled = true;
546 var printAttemptResult = this.printIfReady_(); 534 var printAttemptResult = this.printIfReady_();
547 if (printAttemptResult == print_preview.PrintAttemptResult_.PRINTED || 535 if (printAttemptResult ==
548 printAttemptResult == 536 print_preview.PrintAttemptResult_.READY_WAITING_FOR_PREVIEW) {
549 print_preview.PrintAttemptResult_.READY_WAITING_FOR_PREVIEW) {
550 if ((this.destinationStore_.selectedDestination.isLocal && 537 if ((this.destinationStore_.selectedDestination.isLocal &&
551 !this.destinationStore_.selectedDestination.isPrivet && 538 !this.destinationStore_.selectedDestination.isPrivet &&
552 !this.destinationStore_.selectedDestination.isExtension && 539 !this.destinationStore_.selectedDestination.isExtension &&
553 this.destinationStore_.selectedDestination.id != 540 this.destinationStore_.selectedDestination.id !=
554 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) || 541 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) ||
555 this.uiState_ == PrintPreviewUiState_.OPENING_PDF_PREVIEW) { 542 this.uiState_ == PrintPreviewUiState_.OPENING_PDF_PREVIEW) {
556 // Hide the dialog for now. The actual print command will be issued 543 // Hide the dialog for now. The actual print command will be issued
557 // when the preview generation is done. 544 // when the preview generation is done.
558 this.nativeLayer_.startHideDialog(); 545 this.nativeLayer_.startHideDialog();
559 } 546 }
(...skipping 23 matching lines...) Expand all
583 this.printTicketStore_.isTicketValid(), 570 this.printTicketStore_.isTicketValid(),
584 'Trying to print with invalid ticket'); 571 'Trying to print with invalid ticket');
585 if (getIsVisible(this.moreSettings_.getElement())) { 572 if (getIsVisible(this.moreSettings_.getElement())) {
586 new print_preview.PrintSettingsUiMetricsContext().record( 573 new print_preview.PrintSettingsUiMetricsContext().record(
587 this.moreSettings_.isExpanded ? 574 this.moreSettings_.isExpanded ?
588 print_preview.Metrics.PrintSettingsUiBucket 575 print_preview.Metrics.PrintSettingsUiBucket
589 .PRINT_WITH_SETTINGS_EXPANDED : 576 .PRINT_WITH_SETTINGS_EXPANDED :
590 print_preview.Metrics.PrintSettingsUiBucket 577 print_preview.Metrics.PrintSettingsUiBucket
591 .PRINT_WITH_SETTINGS_COLLAPSED); 578 .PRINT_WITH_SETTINGS_COLLAPSED);
592 } 579 }
593 this.nativeLayer_.startPrint( 580 var destination = assert(this.destinationStore_.selectedDestination);
594 assert(this.destinationStore_.selectedDestination), 581 var whenPrintDone = this.nativeLayer_.print(
595 this.printTicketStore_, this.cloudPrintInterface_, this.documentInfo_, 582 destination, this.printTicketStore_, this.cloudPrintInterface_,
583 this.documentInfo_,
596 this.uiState_ == PrintPreviewUiState_.OPENING_PDF_PREVIEW, 584 this.uiState_ == PrintPreviewUiState_.OPENING_PDF_PREVIEW,
597 this.showSystemDialogBeforeNextPrint_); 585 this.showSystemDialogBeforeNextPrint_);
586
587 if (!destination.isLocal) {
588 // Cloud print resolves when print data is returned to submit to cloud
589 // print, or if setings are invalid.
590 whenPrintDone.then(
591 this.onPrintToCloud_.bind(this),
592 this.onSettingsInvalid_.bind(this));
593 } else if (destination.isPrivet || destination.isExtension) {
594 // Privet and extension resolve when printing is complete or if there
595 // is an error printing.
596 whenPrintDone.then(
597 this.close_.bind(this, false),
598 this.onPrintFailed_.bind(this));
599 } else if (
600 destination.id ==
601 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) {
602 // Save as PDF resolves when file selection is completed or cancelled.
603 whenPrintDone.then(
604 this.onFileSelectionComplete_.bind(this),
605 this.onFileSelectionCancel_.bind(this));
606 } else { // standard local printer
607 var boundHideDialog = function () {
608 this.nativeLayer_.startHideDialog();
609 }.bind(this);
610 // Local printers resolve when print is started. Hide the dialog.
611 whenPrintDone.then(boundHideDialog, boundHideDialog);
612 }
613
598 this.showSystemDialogBeforeNextPrint_ = false; 614 this.showSystemDialogBeforeNextPrint_ = false;
599 return print_preview.PrintAttemptResult_.PRINTED; 615 return print_preview.PrintAttemptResult_.PRINTED;
600 }, 616 },
601 617
602 /** 618 /**
603 * Closes the print preview. 619 * Closes the print preview.
620 * @param {boolean} isCancel Whether this was called due to the user
621 * closing the dialog without printing.
604 * @private 622 * @private
605 */ 623 */
606 close_: function() { 624 close_: function(isCancel) {
607 this.exitDocument(); 625 this.exitDocument();
608 this.uiState_ = PrintPreviewUiState_.CLOSING; 626 this.uiState_ = PrintPreviewUiState_.CLOSING;
609 this.nativeLayer_.startCloseDialog(); 627 this.nativeLayer_.startCloseDialog(isCancel);
610 }, 628 },
611 629
612 /** 630 /**
613 * Opens the native system print dialog after disabling all controls. 631 * Opens the native system print dialog after disabling all controls.
614 * @private 632 * @private
615 */ 633 */
616 openSystemPrintDialog_: function() { 634 openSystemPrintDialog_: function() {
617 if (!this.shouldShowSystemDialogLink_()) 635 if (!this.shouldShowSystemDialogLink_())
618 return; 636 return;
619 if ($('system-dialog-link').classList.contains('disabled')) 637 if ($('system-dialog-link').classList.contains('disabled'))
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 this.destinationStore_.setCloudPrintInterface(this.cloudPrintInterface_); 718 this.destinationStore_.setCloudPrintInterface(this.cloudPrintInterface_);
701 this.invitationStore_.setCloudPrintInterface(this.cloudPrintInterface_); 719 this.invitationStore_.setCloudPrintInterface(this.cloudPrintInterface_);
702 if (this.destinationSearch_.getIsVisible()) { 720 if (this.destinationSearch_.getIsVisible()) {
703 this.destinationStore_.startLoadCloudDestinations(); 721 this.destinationStore_.startLoadCloudDestinations();
704 this.invitationStore_.startLoadingInvitations(); 722 this.invitationStore_.startLoadingInvitations();
705 } 723 }
706 }, 724 },
707 725
708 /** 726 /**
709 * Called from the native layer when ready to print to Google Cloud Print. 727 * Called from the native layer when ready to print to Google Cloud Print.
710 * @param {Event} event Contains the body to send in the HTTP request. 728 * @param {string} data The body to send in the HTTP request.
711 * @private 729 * @private
712 */ 730 */
713 onPrintToCloud_: function(event) { 731 onPrintToCloud_: function(data) {
714 assert( 732 assert(
715 this.uiState_ == PrintPreviewUiState_.PRINTING, 733 this.uiState_ == PrintPreviewUiState_.PRINTING,
716 'Document ready to be sent to the cloud when not in printing ' + 734 'Document ready to be sent to the cloud when not in printing ' +
717 'state: ' + this.uiState_); 735 'state: ' + this.uiState_);
718 assert( 736 assert(
719 this.cloudPrintInterface_ != null, 737 this.cloudPrintInterface_ != null,
720 'Google Cloud Print is not enabled'); 738 'Google Cloud Print is not enabled');
721 assert(this.destinationStore_.selectedDestination != null); 739 assert(this.destinationStore_.selectedDestination != null);
722 this.cloudPrintInterface_.submit( 740 this.cloudPrintInterface_.submit(
723 this.destinationStore_.selectedDestination, this.printTicketStore_, 741 this.destinationStore_.selectedDestination, this.printTicketStore_,
724 this.documentInfo_, event.data); 742 this.documentInfo_, data);
725 }, 743 },
726 744
727 /** 745 /**
728 * Called from the native layer when the user cancels the save-to-pdf file 746 * Called from the native layer when the user cancels the save-to-pdf file
729 * selection dialog. 747 * selection dialog.
730 * @private 748 * @private
731 */ 749 */
732 onFileSelectionCancel_: function() { 750 onFileSelectionCancel_: function() {
733 assert( 751 assert(
734 this.uiState_ == PrintPreviewUiState_.FILE_SELECTION, 752 this.uiState_ == PrintPreviewUiState_.FILE_SELECTION,
(...skipping 20 matching lines...) Expand all
755 /** 773 /**
756 * Called after successfully submitting a job to Google Cloud Print. 774 * Called after successfully submitting a job to Google Cloud Print.
757 * @param {!Event} event Contains the ID of the submitted print job. 775 * @param {!Event} event Contains the ID of the submitted print job.
758 * @private 776 * @private
759 */ 777 */
760 onCloudPrintSubmitDone_: function(event) { 778 onCloudPrintSubmitDone_: function(event) {
761 assert( 779 assert(
762 this.uiState_ == PrintPreviewUiState_.PRINTING, 780 this.uiState_ == PrintPreviewUiState_.PRINTING,
763 'Submited job to Google Cloud Print but not in printing state ' + 781 'Submited job to Google Cloud Print but not in printing state ' +
764 this.uiState_); 782 this.uiState_);
765 this.close_(); 783 this.close_(false);
766 }, 784 },
767 785
768 /** 786 /**
769 * Called when there was an error communicating with Google Cloud print. 787 * Called when there was an error communicating with Google Cloud print.
770 * Displays an error message in the print header. 788 * Displays an error message in the print header.
771 * @param {!Event} event Contains the error message. 789 * @param {!Event} event Contains the error message.
772 * @private 790 * @private
773 */ 791 */
774 onCloudPrintError_: function(event) { 792 onCloudPrintError_: function(event) {
775 if (event.status == 0) { 793 if (event.status == 0) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 'Trying to print when not in ready state: ' + this.uiState_); 868 'Trying to print when not in ready state: ' + this.uiState_);
851 this.printDocumentOrOpenPdfPreview_(false /*isPdfPreview*/); 869 this.printDocumentOrOpenPdfPreview_(false /*isPdfPreview*/);
852 }, 870 },
853 871
854 /** 872 /**
855 * Called when the print header's cancel button is clicked. Closes the 873 * Called when the print header's cancel button is clicked. Closes the
856 * print dialog. 874 * print dialog.
857 * @private 875 * @private
858 */ 876 */
859 onCancelButtonClick_: function() { 877 onCancelButtonClick_: function() {
860 this.close_(); 878 this.close_(true);
861 }, 879 },
862 880
863 /** 881 /**
864 * Called when the register promo for Cloud Print is clicked. 882 * Called when the register promo for Cloud Print is clicked.
865 * @private 883 * @private
866 */ 884 */
867 onCloudPrintRegisterPromoClick_: function(e) { 885 onCloudPrintRegisterPromoClick_: function(e) {
868 var devicesUrl = 'chrome://devices/register?id=' + e.destination.id; 886 var devicesUrl = 'chrome://devices/register?id=' + e.destination.id;
869 this.nativeLayer_.startForceOpenNewTab(devicesUrl); 887 this.nativeLayer_.startForceOpenNewTab(devicesUrl);
870 this.destinationStore_.waitForRegister(e.destination.id); 888 this.destinationStore_.waitForRegister(e.destination.id);
871 }, 889 },
872 890
873 /** 891 /**
874 * Consume escape key presses and ctrl + shift + p. Delegate everything else 892 * Consume escape key presses and ctrl + shift + p. Delegate everything else
875 * to the preview area. 893 * to the preview area.
876 * @param {KeyboardEvent} e The keyboard event. 894 * @param {KeyboardEvent} e The keyboard event.
877 * @private 895 * @private
878 * @suppress {uselessCode} 896 * @suppress {uselessCode}
879 * Current compiler preprocessor leaves all the code inside all the <if>s, 897 * Current compiler preprocessor leaves all the code inside all the <if>s,
880 * so the compiler claims that code after first return is unreachable. 898 * so the compiler claims that code after first return is unreachable.
881 */ 899 */
882 onKeyDown_: function(e) { 900 onKeyDown_: function(e) {
883 // Escape key closes the dialog. 901 // Escape key closes the dialog.
884 if (e.keyCode == 27 && !hasKeyModifiers(e)) { 902 if (e.keyCode == 27 && !hasKeyModifiers(e)) {
885 // On non-mac with toolkit-views, ESC key is handled by C++-side instead 903 // On non-mac with toolkit-views, ESC key is handled by C++-side instead
886 // of JS-side. 904 // of JS-side.
887 if (cr.isMac) { 905 if (cr.isMac) {
888 this.close_(); 906 this.close_(true);
889 e.preventDefault(); 907 e.preventDefault();
890 } 908 }
891 return; 909 return;
892 } 910 }
893 911
894 // On Mac, Cmd-. should close the print dialog. 912 // On Mac, Cmd-. should close the print dialog.
895 if (cr.isMac && e.keyCode == 190 && e.metaKey) { 913 if (cr.isMac && e.keyCode == 190 && e.metaKey) {
896 this.close_(); 914 this.close_(true);
897 e.preventDefault(); 915 e.preventDefault();
898 return; 916 return;
899 } 917 }
900 918
901 // Ctrl + Shift + p / Mac equivalent. 919 // Ctrl + Shift + p / Mac equivalent.
902 if (e.keyCode == 80) { 920 if (e.keyCode == 80) {
903 if ((cr.isMac && e.metaKey && e.altKey && !e.shiftKey && !e.ctrlKey) || 921 if ((cr.isMac && e.metaKey && e.altKey && !e.shiftKey && !e.ctrlKey) ||
904 (!cr.isMac && e.shiftKey && e.ctrlKey && !e.altKey && !e.metaKey)) { 922 (!cr.isMac && e.shiftKey && e.ctrlKey && !e.altKey && !e.metaKey)) {
905 this.openSystemPrintDialog_(); 923 this.openSystemPrintDialog_();
906 e.preventDefault(); 924 e.preventDefault();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 * @private 1038 * @private
1021 */ 1039 */
1022 onPageCountReady_: function(event) { 1040 onPageCountReady_: function(event) {
1023 if (event.fitToPageScaling >= 0) { 1041 if (event.fitToPageScaling >= 0) {
1024 this.scalingSettings_.updateFitToPageScaling(event.fitToPageScaling); 1042 this.scalingSettings_.updateFitToPageScaling(event.fitToPageScaling);
1025 } 1043 }
1026 }, 1044 },
1027 1045
1028 /** 1046 /**
1029 * Called when printing to a privet or extension printer fails. 1047 * Called when printing to a privet or extension printer fails.
1030 * @param {number | string} httpError The HTTP error code, or -1 if not an 1048 * @param {*} httpError The HTTP error code, or -1 or a string describing
1031 * HTTP error. 1049 * the error, if not an HTTP error.
1032 * @private 1050 * @private
1033 */ 1051 */
1034 onPrintFailed_: function(httpError) { 1052 onPrintFailed_: function(httpError) {
1035 console.error('Privet printing failed with error code ' + httpError); 1053 console.error('Privet printing failed with error code ' + httpError);
1036 this.printHeader_.setErrorMessage( 1054 this.printHeader_.setErrorMessage(
1037 loadTimeData.getString('couldNotPrint')); 1055 loadTimeData.getString('couldNotPrint'));
1038 }, 1056 },
1039 1057
1040 /** 1058 /**
1041 * Called when the print preview settings need to be changed for testing. 1059 * Called when the print preview settings need to be changed for testing.
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1249 * Google Cloud Print management page and closes the print preview. 1267 * Google Cloud Print management page and closes the print preview.
1250 * @private 1268 * @private
1251 */ 1269 */
1252 onNoDestinationsPromoClick_: function() { 1270 onNoDestinationsPromoClick_: function() {
1253 new print_preview.GcpPromoMetricsContext().record( 1271 new print_preview.GcpPromoMetricsContext().record(
1254 print_preview.Metrics.GcpPromoBucket.PROMO_CLICKED); 1272 print_preview.Metrics.GcpPromoBucket.PROMO_CLICKED);
1255 this.appState_.persistIsGcpPromoDismissed(true); 1273 this.appState_.persistIsGcpPromoDismissed(true);
1256 window.open( 1274 window.open(
1257 this.cloudPrintInterface_.baseUrl + 1275 this.cloudPrintInterface_.baseUrl +
1258 '?user=' + this.userInfo_.activeUser + '#printers'); 1276 '?user=' + this.userInfo_.activeUser + '#printers');
1259 this.close_(); 1277 this.close_(false);
1260 } 1278 }
1261 }; 1279 };
1262 1280
1263 // Export 1281 // Export
1264 return {PrintPreview: PrintPreview}; 1282 return {PrintPreview: PrintPreview};
1265 }); 1283 });
1266 1284
1267 // Pull in all other scripts in a single shot. 1285 // Pull in all other scripts in a single shot.
1268 // <include src="common/overlay.js"> 1286 // <include src="common/overlay.js">
1269 // <include src="common/search_box.js"> 1287 // <include src="common/search_box.js">
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1345 // <include src="search/destination_search.js"> 1363 // <include src="search/destination_search.js">
1346 // <include src="search/provisional_destination_resolver.js"> 1364 // <include src="search/provisional_destination_resolver.js">
1347 1365
1348 window.addEventListener('DOMContentLoaded', function() { 1366 window.addEventListener('DOMContentLoaded', function() {
1349 var previewWindow = /** @type {{isTest: boolean}} */ (window); 1367 var previewWindow = /** @type {{isTest: boolean}} */ (window);
1350 if (!previewWindow.isTest) { 1368 if (!previewWindow.isTest) {
1351 var printPreview = new print_preview.PrintPreview(); 1369 var printPreview = new print_preview.PrintPreview();
1352 printPreview.initialize(); 1370 printPreview.initialize();
1353 } 1371 }
1354 }); 1372 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698