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

Side by Side Diff: chrome/browser/resources/print_preview/native_layer.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 cr.exportPath('print_preview'); 5 cr.exportPath('print_preview');
6 6
7 /** 7 /**
8 * @typedef {{selectSaveAsPdfDestination: boolean, 8 * @typedef {{selectSaveAsPdfDestination: boolean,
9 * layoutSettings.portrait: boolean, 9 * layoutSettings.portrait: boolean,
10 * pageRange: string, 10 * pageRange: string,
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 'use strict'; 70 'use strict';
71 71
72 /** 72 /**
73 * An interface to the native Chromium printing system layer. 73 * An interface to the native Chromium printing system layer.
74 * @constructor 74 * @constructor
75 */ 75 */
76 function NativeLayer() { 76 function NativeLayer() {
77 // Bind global handlers 77 // Bind global handlers
78 global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this); 78 global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this);
79 global.reloadPrintersList = this.onReloadPrintersList_.bind(this); 79 global.reloadPrintersList = this.onReloadPrintersList_.bind(this);
80 global.printToCloud = this.onPrintToCloud_.bind(this);
81 global.fileSelectionCancelled = this.onFileSelectionCancelled_.bind(this);
82 global.fileSelectionCompleted = this.onFileSelectionCompleted_.bind(this);
83 global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this); 80 global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this);
84 global.invalidPrinterSettings = this.onInvalidPrinterSettings_.bind(this); 81 global.invalidPrinterSettings = this.onInvalidPrinterSettings_.bind(this);
85 global.onDidGetDefaultPageLayout = 82 global.onDidGetDefaultPageLayout =
86 this.onDidGetDefaultPageLayout_.bind(this); 83 this.onDidGetDefaultPageLayout_.bind(this);
87 global.onDidGetPreviewPageCount = this.onDidGetPreviewPageCount_.bind(this); 84 global.onDidGetPreviewPageCount = this.onDidGetPreviewPageCount_.bind(this);
88 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this); 85 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this);
89 global.updatePrintPreview = this.onUpdatePrintPreview_.bind(this); 86 global.updatePrintPreview = this.onUpdatePrintPreview_.bind(this);
90 global.onDidGetAccessToken = this.onDidGetAccessToken_.bind(this); 87 global.onDidGetAccessToken = this.onDidGetAccessToken_.bind(this);
91 global.onEnableManipulateSettingsForTest = 88 global.onEnableManipulateSettingsForTest =
92 this.onEnableManipulateSettingsForTest_.bind(this); 89 this.onEnableManipulateSettingsForTest_.bind(this);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 /** 122 /**
126 * Event types dispatched from the Chromium native layer. 123 * Event types dispatched from the Chromium native layer.
127 * @enum {string} 124 * @enum {string}
128 * @const 125 * @const
129 */ 126 */
130 NativeLayer.EventType = { 127 NativeLayer.EventType = {
131 ACCESS_TOKEN_READY: 'print_preview.NativeLayer.ACCESS_TOKEN_READY', 128 ACCESS_TOKEN_READY: 'print_preview.NativeLayer.ACCESS_TOKEN_READY',
132 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE', 129 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
133 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD', 130 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
134 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING', 131 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING',
135 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
136 FILE_SELECTION_COMPLETE:
137 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
138 MANIPULATE_SETTINGS_FOR_TEST: 132 MANIPULATE_SETTINGS_FOR_TEST:
139 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST', 133 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
140 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY', 134 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY',
141 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY', 135 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
142 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY', 136 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
143 PREVIEW_GENERATION_DONE: 137 PREVIEW_GENERATION_DONE:
144 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE', 138 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
145 PREVIEW_GENERATION_FAIL: 139 PREVIEW_GENERATION_FAIL:
146 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL', 140 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
147 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
148 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID', 141 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID',
149 PRINT_PRESET_OPTIONS: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS', 142 PRINT_PRESET_OPTIONS: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS',
150 PROVISIONAL_DESTINATION_RESOLVED: 143 PROVISIONAL_DESTINATION_RESOLVED:
151 'print_preview.NativeLayer.PROVISIONAL_DESTINATION_RESOLVED' 144 'print_preview.NativeLayer.PROVISIONAL_DESTINATION_RESOLVED'
152 }; 145 };
153 146
154 /** 147 /**
155 * Constant values matching printing::DuplexMode enum. 148 * Constant values matching printing::DuplexMode enum.
156 * @enum {number} 149 * @enum {number}
157 */ 150 */
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 * @param {!print_preview.Destination} destination Destination to print to. 403 * @param {!print_preview.Destination} destination Destination to print to.
411 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the 404 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
412 * state of the print ticket. 405 * state of the print ticket.
413 * @param {cloudprint.CloudPrintInterface} cloudPrintInterface Interface 406 * @param {cloudprint.CloudPrintInterface} cloudPrintInterface Interface
414 * to Google Cloud Print. 407 * to Google Cloud Print.
415 * @param {!print_preview.DocumentInfo} documentInfo Document data model. 408 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
416 * @param {boolean=} opt_isOpenPdfInPreview Whether to open the PDF in the 409 * @param {boolean=} opt_isOpenPdfInPreview Whether to open the PDF in the
417 * system's preview application. 410 * system's preview application.
418 * @param {boolean=} opt_showSystemDialog Whether to open system dialog for 411 * @param {boolean=} opt_showSystemDialog Whether to open system dialog for
419 * advanced settings. 412 * advanced settings.
413 * @return {!Promise} Promise that will resolve when the print request is
414 * finished or rejected.
420 */ 415 */
421 startPrint: function( 416 print: function(
422 destination, printTicketStore, cloudPrintInterface, documentInfo, 417 destination, printTicketStore, cloudPrintInterface, documentInfo,
423 opt_isOpenPdfInPreview, opt_showSystemDialog) { 418 opt_isOpenPdfInPreview, opt_showSystemDialog) {
424 assert( 419 assert(
425 printTicketStore.isTicketValid(), 420 printTicketStore.isTicketValid(),
426 'Trying to print when ticket is not valid'); 421 'Trying to print when ticket is not valid');
427 422
428 assert( 423 assert(
429 !opt_showSystemDialog || (cr.isWindows && destination.isLocal), 424 !opt_showSystemDialog || (cr.isWindows && destination.isLocal),
430 'Implemented for Windows only'); 425 'Implemented for Windows only');
431 426
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 482
488 if (destination.isPrivet || destination.isExtension) { 483 if (destination.isPrivet || destination.isExtension) {
489 ticket['ticket'] = printTicketStore.createPrintTicket(destination); 484 ticket['ticket'] = printTicketStore.createPrintTicket(destination);
490 ticket['capabilities'] = JSON.stringify(destination.capabilities); 485 ticket['capabilities'] = JSON.stringify(destination.capabilities);
491 } 486 }
492 487
493 if (opt_isOpenPdfInPreview) { 488 if (opt_isOpenPdfInPreview) {
494 ticket['OpenPDFInPreview'] = true; 489 ticket['OpenPDFInPreview'] = true;
495 } 490 }
496 491
497 chrome.send('print', [JSON.stringify(ticket)]); 492 return cr.sendWithPromise('print', JSON.stringify(ticket));
498 }, 493 },
499 494
500 /** Requests that the current pending print request be cancelled. */ 495 /** Requests that the current pending print request be cancelled. */
501 startCancelPendingPrint: function() { 496 startCancelPendingPrint: function() {
502 chrome.send('cancelPendingPrintRequest'); 497 chrome.send('cancelPendingPrintRequest');
503 }, 498 },
504 499
505 /** Shows the system's native printing dialog. */ 500 /** Shows the system's native printing dialog. */
506 startShowSystemDialog: function() { 501 startShowSystemDialog: function() {
507 assert(!cr.isWindows); 502 assert(!cr.isWindows);
508 chrome.send('showSystemDialog'); 503 chrome.send('showSystemDialog');
509 }, 504 },
510 505
511 /** Closes the print preview dialog. */ 506 /**
512 startCloseDialog: function() { 507 * Closes the print preview dialog.
513 chrome.send('closePrintPreviewDialog'); 508 * If |isCancel| is true, also sends a message to Print Preview Handler in
509 * order to update UMA statistics.
510 * @param {boolean} isCancel whether this was called due to the user
511 * closing the dialog without printing.
512 */
513 startCloseDialog: function(isCancel) {
514 if (isCancel)
515 chrome.send('closePrintPreviewDialog');
dpapad 2017/06/23 23:17:26 Nit (optional): chrome.send(isCancel ? 'closePrin
rbpotter 2017/06/23 23:34:35 We want to send dialogClose either way though, so
dpapad 2017/06/23 23:45:01 Ah right, I misread the original code. Ignore my s
514 chrome.send('dialogClose'); 516 chrome.send('dialogClose');
515 }, 517 },
516 518
517 /** Hide the print preview dialog and allow the native layer to close it. */ 519 /** Hide the print preview dialog and allow the native layer to close it. */
518 startHideDialog: function() { 520 startHideDialog: function() {
519 chrome.send('hidePreview'); 521 chrome.send('hidePreview');
520 }, 522 },
521 523
522 /** 524 /**
523 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event 525 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 this.eventTarget_.dispatchEvent(cloudPrintEnableEvent); 565 this.eventTarget_.dispatchEvent(cloudPrintEnableEvent);
564 }, 566 },
565 567
566 /** Reloads the printer list. */ 568 /** Reloads the printer list. */
567 onReloadPrintersList_: function() { 569 onReloadPrintersList_: function() {
568 cr.dispatchSimpleEvent( 570 cr.dispatchSimpleEvent(
569 this.eventTarget_, NativeLayer.EventType.DESTINATIONS_RELOAD); 571 this.eventTarget_, NativeLayer.EventType.DESTINATIONS_RELOAD);
570 }, 572 },
571 573
572 /** 574 /**
573 * Called from the C++ layer.
574 * Take the PDF data handed to us and submit it to the cloud, closing the
575 * print preview dialog once the upload is successful.
576 * @param {string} data Data to send as the print job.
577 * @private
578 */
579 onPrintToCloud_: function(data) {
580 var printToCloudEvent = new Event(NativeLayer.EventType.PRINT_TO_CLOUD);
581 printToCloudEvent.data = data;
582 this.eventTarget_.dispatchEvent(printToCloudEvent);
583 },
584
585 /**
586 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
587 * preview dialog regarding the file selection cancel event.
588 * @private
589 */
590 onFileSelectionCancelled_: function() {
591 cr.dispatchSimpleEvent(
592 this.eventTarget_, NativeLayer.EventType.FILE_SELECTION_CANCEL);
593 },
594
595 /**
596 * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print
597 * preview dialog regarding the file selection completed event.
598 * @private
599 */
600 onFileSelectionCompleted_: function() {
601 // If the file selection is completed and the dialog is not already closed
602 // it means that a pending print to pdf request exists.
603 cr.dispatchSimpleEvent(
604 this.eventTarget_, NativeLayer.EventType.FILE_SELECTION_COMPLETE);
605 },
606
607 /**
608 * Display an error message when print preview fails. 575 * Display an error message when print preview fails.
609 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed(). 576 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed().
610 * @private 577 * @private
611 */ 578 */
612 onPrintPreviewFailed_: function() { 579 onPrintPreviewFailed_: function() {
613 cr.dispatchSimpleEvent( 580 cr.dispatchSimpleEvent(
614 this.eventTarget_, NativeLayer.EventType.PREVIEW_GENERATION_FAIL); 581 this.eventTarget_, NativeLayer.EventType.PREVIEW_GENERATION_FAIL);
615 }, 582 },
616 583
617 /** 584 /**
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
979 return this.serializedDefaultDestinationSelectionRulesStr_; 946 return this.serializedDefaultDestinationSelectionRulesStr_;
980 } 947 }
981 }; 948 };
982 949
983 // Export 950 // Export
984 return { 951 return {
985 NativeInitialSettings: NativeInitialSettings, 952 NativeInitialSettings: NativeInitialSettings,
986 NativeLayer: NativeLayer 953 NativeLayer: NativeLayer
987 }; 954 };
988 }); 955 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698