Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / native_layer.js
blob7fc53facab09edae98570e83b7330901f663539e
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
3 // found in the LICENSE file.
5 cr.exportPath('print_preview');
7 /**
8 * @typedef {{selectSaveAsPdfDestination: boolean,
9 * layoutSettings.portrait: boolean,
10 * pageRange: string,
11 * headersAndFooters: boolean,
12 * backgroundColorsAndImages: boolean,
13 * margins: number}}
14 * @see chrome/browser/printing/print_preview_pdf_generated_browsertest.cc
16 print_preview.PreviewSettings;
18 cr.define('print_preview', function() {
19 'use strict';
21 /**
22 * An interface to the native Chromium printing system layer.
23 * @constructor
24 * @extends {cr.EventTarget}
26 function NativeLayer() {
27 cr.EventTarget.call(this);
29 // Bind global handlers
30 global.setInitialSettings = this.onSetInitialSettings_.bind(this);
31 global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this);
32 global.setPrinters = this.onSetPrinters_.bind(this);
33 global.updateWithPrinterCapabilities =
34 this.onUpdateWithPrinterCapabilities_.bind(this);
35 global.failedToGetPrinterCapabilities =
36 this.onFailedToGetPrinterCapabilities_.bind(this);
37 global.failedToGetPrivetPrinterCapabilities =
38 this.onFailedToGetPrivetPrinterCapabilities_.bind(this);
39 global.reloadPrintersList = this.onReloadPrintersList_.bind(this);
40 global.printToCloud = this.onPrintToCloud_.bind(this);
41 global.fileSelectionCancelled =
42 this.onFileSelectionCancelled_.bind(this);
43 global.fileSelectionCompleted =
44 this.onFileSelectionCompleted_.bind(this);
45 global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this);
46 global.invalidPrinterSettings =
47 this.onInvalidPrinterSettings_.bind(this);
48 global.onDidGetDefaultPageLayout =
49 this.onDidGetDefaultPageLayout_.bind(this);
50 global.onDidGetPreviewPageCount =
51 this.onDidGetPreviewPageCount_.bind(this);
52 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this);
53 global.updatePrintPreview = this.onUpdatePrintPreview_.bind(this);
54 global.printScalingDisabledForSourcePDF =
55 this.onPrintScalingDisabledForSourcePDF_.bind(this);
56 global.onDidGetAccessToken = this.onDidGetAccessToken_.bind(this);
57 global.autoCancelForTesting = this.autoCancelForTesting_.bind(this);
58 global.onPrivetPrinterChanged = this.onPrivetPrinterChanged_.bind(this);
59 global.onPrivetCapabilitiesSet =
60 this.onPrivetCapabilitiesSet_.bind(this);
61 global.onPrivetPrintFailed = this.onPrivetPrintFailed_.bind(this);
62 global.onEnableManipulateSettingsForTest =
63 this.onEnableManipulateSettingsForTest_.bind(this);
66 /**
67 * Event types dispatched from the Chromium native layer.
68 * @enum {string}
69 * @const
71 NativeLayer.EventType = {
72 ACCESS_TOKEN_READY: 'print_preview.NativeLayer.ACCESS_TOKEN_READY',
73 CAPABILITIES_SET: 'print_preview.NativeLayer.CAPABILITIES_SET',
74 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
75 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
76 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING',
77 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
78 FILE_SELECTION_COMPLETE:
79 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
80 GET_CAPABILITIES_FAIL: 'print_preview.NativeLayer.GET_CAPABILITIES_FAIL',
81 INITIAL_SETTINGS_SET: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET',
82 LOCAL_DESTINATIONS_SET: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET',
83 MANIPULATE_SETTINGS_FOR_TEST:
84 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
85 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY',
86 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
87 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
88 PREVIEW_GENERATION_DONE:
89 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
90 PREVIEW_GENERATION_FAIL:
91 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
92 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
93 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID',
94 PRIVET_PRINTER_CHANGED: 'print_preview.NativeLayer.PRIVET_PRINTER_CHANGED',
95 PRIVET_CAPABILITIES_SET:
96 'print_preview.NativeLayer.PRIVET_CAPABILITIES_SET',
97 PRIVET_PRINT_FAILED: 'print_preview.NativeLayer.PRIVET_PRINT_FAILED',
101 * Constant values matching printing::DuplexMode enum.
102 * @enum {number}
104 NativeLayer.DuplexMode = {
105 SIMPLEX: 0,
106 LONG_EDGE: 1,
107 UNKNOWN_DUPLEX_MODE: -1
111 * Enumeration of color modes used by Chromium.
112 * @enum {number}
113 * @private
115 NativeLayer.ColorMode_ = {
116 GRAY: 1,
117 COLOR: 2
121 * Version of the serialized state of the print preview.
122 * @type {number}
123 * @const
124 * @private
126 NativeLayer.SERIALIZED_STATE_VERSION_ = 1;
128 NativeLayer.prototype = {
129 __proto__: cr.EventTarget.prototype,
132 * Requests access token for cloud print requests.
133 * @param {string} authType type of access token.
135 startGetAccessToken: function(authType) {
136 chrome.send('getAccessToken', [authType]);
139 /** Gets the initial settings to initialize the print preview with. */
140 startGetInitialSettings: function() {
141 chrome.send('getInitialSettings');
145 * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
146 * event will be dispatched in response.
148 startGetLocalDestinations: function() {
149 chrome.send('getPrinters');
153 * Requests the network's privet print destinations. A number of
154 * PRIVET_PRINTER_CHANGED events will be fired in response, followed by a
155 * PRIVET_SEARCH_ENDED.
157 startGetPrivetDestinations: function() {
158 chrome.send('getPrivetPrinters');
162 * Requests that the privet print stack stop searching for privet print
163 * destinations.
165 stopGetPrivetDestinations: function() {
166 chrome.send('stopGetPrivetPrinters');
170 * Requests the privet destination's printing capabilities. A
171 * PRIVET_CAPABILITIES_SET event will be dispatched in response.
172 * @param {string} destinationId ID of the destination.
174 startGetPrivetDestinationCapabilities: function(destinationId) {
175 chrome.send('getPrivetPrinterCapabilities', [destinationId]);
179 * Requests the destination's printing capabilities. A CAPABILITIES_SET
180 * event will be dispatched in response.
181 * @param {string} destinationId ID of the destination.
183 startGetLocalDestinationCapabilities: function(destinationId) {
184 chrome.send('getPrinterCapabilities', [destinationId]);
188 * @param {!print_preview.Destination} destination Destination to print to.
189 * @param {!print_preview.ticket_items.Color} color Color ticket item.
190 * @return {number} Native layer color model.
191 * @private
193 getNativeColorModel_: function(destination, color) {
194 // For non-local printers native color model is ignored anyway.
195 var option = destination.isLocal ? color.getSelectedOption() : null;
196 var nativeColorModel = parseInt(option ? option.vendor_id : null, 10);
197 if (isNaN(nativeColorModel)) {
198 return color.getValue() ?
199 NativeLayer.ColorMode_.COLOR : NativeLayer.ColorMode_.GRAY;
201 return nativeColorModel;
205 * Requests that a preview be generated. The following events may be
206 * dispatched in response:
207 * - PAGE_COUNT_READY
208 * - PAGE_LAYOUT_READY
209 * - PAGE_PREVIEW_READY
210 * - PREVIEW_GENERATION_DONE
211 * - PREVIEW_GENERATION_FAIL
212 * @param {print_preview.Destination} destination Destination to print to.
213 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
214 * state of the print ticket.
215 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
216 * @param {number} requestId ID of the preview request.
218 startGetPreview: function(
219 destination, printTicketStore, documentInfo, requestId) {
220 assert(printTicketStore.isTicketValidForPreview(),
221 'Trying to generate preview when ticket is not valid');
223 var ticket = {
224 'pageRange': printTicketStore.pageRange.getDocumentPageRanges(),
225 'mediaSize': printTicketStore.mediaSize.getValue(),
226 'landscape': printTicketStore.landscape.getValue(),
227 'color': this.getNativeColorModel_(destination, printTicketStore.color),
228 'headerFooterEnabled': printTicketStore.headerFooter.getValue(),
229 'marginsType': printTicketStore.marginsType.getValue(),
230 'isFirstRequest': requestId == 0,
231 'requestID': requestId,
232 'previewModifiable': documentInfo.isModifiable,
233 'printToPDF':
234 destination != null &&
235 destination.id ==
236 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
237 'printWithCloudPrint': destination != null && !destination.isLocal,
238 'printWithPrivet': destination != null && destination.isPrivet,
239 'deviceName': destination == null ? 'foo' : destination.id,
240 'generateDraftData': documentInfo.isModifiable,
241 'fitToPageEnabled': printTicketStore.fitToPage.getValue(),
243 // NOTE: Even though the following fields don't directly relate to the
244 // preview, they still need to be included.
245 'duplex': printTicketStore.duplex.getValue() ?
246 NativeLayer.DuplexMode.LONG_EDGE : NativeLayer.DuplexMode.SIMPLEX,
247 'copies': printTicketStore.copies.getValueAsNumber(),
248 'collate': printTicketStore.collate.getValue(),
249 'shouldPrintBackgrounds': printTicketStore.cssBackground.getValue(),
250 'shouldPrintSelectionOnly': printTicketStore.selectionOnly.getValue()
253 // Set 'cloudPrintID' only if the destination is not local.
254 if (destination && !destination.isLocal) {
255 ticket['cloudPrintID'] = destination.id;
258 if (printTicketStore.marginsType.isCapabilityAvailable() &&
259 printTicketStore.marginsType.getValue() ==
260 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
261 var customMargins = printTicketStore.customMargins.getValue();
262 var orientationEnum =
263 print_preview.ticket_items.CustomMargins.Orientation;
264 ticket['marginsCustom'] = {
265 'marginTop': customMargins.get(orientationEnum.TOP),
266 'marginRight': customMargins.get(orientationEnum.RIGHT),
267 'marginBottom': customMargins.get(orientationEnum.BOTTOM),
268 'marginLeft': customMargins.get(orientationEnum.LEFT)
272 chrome.send(
273 'getPreview',
274 [JSON.stringify(ticket),
275 requestId > 0 ? documentInfo.pageCount : -1,
276 documentInfo.isModifiable]);
280 * Requests that the document be printed.
281 * @param {!print_preview.Destination} destination Destination to print to.
282 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
283 * state of the print ticket.
284 * @param {cloudprint.CloudPrintInterface} cloudPrintInterface Interface
285 * to Google Cloud Print.
286 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
287 * @param {boolean=} opt_isOpenPdfInPreview Whether to open the PDF in the
288 * system's preview application.
289 * @param {boolean=} opt_showSystemDialog Whether to open system dialog for
290 * advanced settings.
292 startPrint: function(destination, printTicketStore, cloudPrintInterface,
293 documentInfo, opt_isOpenPdfInPreview,
294 opt_showSystemDialog) {
295 assert(printTicketStore.isTicketValid(),
296 'Trying to print when ticket is not valid');
298 assert(!opt_showSystemDialog || (cr.isWindows && destination.isLocal),
299 'Implemented for Windows only');
301 var ticket = {
302 'pageRange': printTicketStore.pageRange.getDocumentPageRanges(),
303 'mediaSize': printTicketStore.mediaSize.getValue(),
304 'pageCount': printTicketStore.pageRange.getPageNumberSet().size,
305 'landscape': printTicketStore.landscape.getValue(),
306 'color': this.getNativeColorModel_(destination, printTicketStore.color),
307 'headerFooterEnabled': printTicketStore.headerFooter.getValue(),
308 'marginsType': printTicketStore.marginsType.getValue(),
309 'generateDraftData': true, // TODO(rltoscano): What should this be?
310 'duplex': printTicketStore.duplex.getValue() ?
311 NativeLayer.DuplexMode.LONG_EDGE : NativeLayer.DuplexMode.SIMPLEX,
312 'copies': printTicketStore.copies.getValueAsNumber(),
313 'collate': printTicketStore.collate.getValue(),
314 'shouldPrintBackgrounds': printTicketStore.cssBackground.getValue(),
315 'shouldPrintSelectionOnly': printTicketStore.selectionOnly.getValue(),
316 'previewModifiable': documentInfo.isModifiable,
317 'printToPDF': destination.id ==
318 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
319 'printWithCloudPrint': !destination.isLocal,
320 'printWithPrivet': destination.isPrivet,
321 'deviceName': destination.id,
322 'isFirstRequest': false,
323 'requestID': -1,
324 'fitToPageEnabled': printTicketStore.fitToPage.getValue(),
325 'pageWidth': documentInfo.pageSize.width,
326 'pageHeight': documentInfo.pageSize.height,
327 'showSystemDialog': opt_showSystemDialog
330 if (!destination.isLocal) {
331 // We can't set cloudPrintID if the destination is "Print with Cloud
332 // Print" because the native system will try to print to Google Cloud
333 // Print with this ID instead of opening a Google Cloud Print dialog.
334 ticket['cloudPrintID'] = destination.id;
337 if (printTicketStore.marginsType.isCapabilityAvailable() &&
338 printTicketStore.marginsType.isValueEqual(
339 print_preview.ticket_items.MarginsType.Value.CUSTOM)) {
340 var customMargins = printTicketStore.customMargins.getValue();
341 var orientationEnum =
342 print_preview.ticket_items.CustomMargins.Orientation;
343 ticket['marginsCustom'] = {
344 'marginTop': customMargins.get(orientationEnum.TOP),
345 'marginRight': customMargins.get(orientationEnum.RIGHT),
346 'marginBottom': customMargins.get(orientationEnum.BOTTOM),
347 'marginLeft': customMargins.get(orientationEnum.LEFT)
351 if (destination.isPrivet) {
352 ticket['ticket'] = printTicketStore.createPrintTicket(destination);
353 ticket['capabilities'] = JSON.stringify(destination.capabilities);
356 if (opt_isOpenPdfInPreview) {
357 ticket['OpenPDFInPreview'] = true;
360 chrome.send('print', [JSON.stringify(ticket)]);
363 /** Requests that the current pending print request be cancelled. */
364 startCancelPendingPrint: function() {
365 chrome.send('cancelPendingPrintRequest');
368 /** Shows the system's native printing dialog. */
369 startShowSystemDialog: function() {
370 assert(!cr.isWindows);
371 chrome.send('showSystemDialog');
374 /** Shows Google Cloud Print's web-based print dialog.
375 * @param {number} pageCount Number of pages to print.
377 startShowCloudPrintDialog: function(pageCount) {
378 chrome.send('printWithCloudPrintDialog', [pageCount]);
381 /** Closes the print preview dialog. */
382 startCloseDialog: function() {
383 chrome.send('closePrintPreviewDialog');
384 chrome.send('dialogClose');
387 /** Hide the print preview dialog and allow the native layer to close it. */
388 startHideDialog: function() {
389 chrome.send('hidePreview');
393 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event
394 * will be dispatched in response.
395 * @param {boolean} addAccount Whether to open an 'add a new account' or
396 * default sign in page.
398 startCloudPrintSignIn: function(addAccount) {
399 chrome.send('signIn', [addAccount]);
402 /** Navigates the user to the system printer settings interface. */
403 startManageLocalDestinations: function() {
404 chrome.send('manageLocalPrinters');
407 /** Navigates the user to the Google Cloud Print management page. */
408 startManageCloudDestinations: function() {
409 chrome.send('manageCloudPrinters');
412 /** Forces browser to open a new tab with the given URL address. */
413 startForceOpenNewTab: function(url) {
414 chrome.send('forceOpenNewTab', [url]);
418 * @param {!Object} initialSettings Object containing all initial settings.
420 onSetInitialSettings_: function(initialSettings) {
421 var numberFormatSymbols =
422 print_preview.MeasurementSystem.parseNumberFormat(
423 initialSettings['numberFormat']);
424 var unitType = print_preview.MeasurementSystem.UnitType.IMPERIAL;
425 if (initialSettings['measurementSystem'] != null) {
426 unitType = initialSettings['measurementSystem'];
429 var nativeInitialSettings = new print_preview.NativeInitialSettings(
430 initialSettings['printAutomaticallyInKioskMode'] || false,
431 initialSettings['appKioskMode'] || false,
432 initialSettings['hidePrintWithSystemDialogLink'] || false,
433 numberFormatSymbols[0] || ',',
434 numberFormatSymbols[1] || '.',
435 unitType,
436 initialSettings['previewModifiable'] || false,
437 initialSettings['initiatorTitle'] || '',
438 initialSettings['documentHasSelection'] || false,
439 initialSettings['shouldPrintSelectionOnly'] || false,
440 initialSettings['printerName'] || null,
441 initialSettings['appState'] || null);
443 var initialSettingsSetEvent = new Event(
444 NativeLayer.EventType.INITIAL_SETTINGS_SET);
445 initialSettingsSetEvent.initialSettings = nativeInitialSettings;
446 this.dispatchEvent(initialSettingsSetEvent);
450 * Turn on the integration of Cloud Print.
451 * @param {{cloudPrintURL: string, appKioskMode: string}} settings
452 * cloudPrintUrl: The URL to use for cloud print servers.
453 * @private
455 onSetUseCloudPrint_: function(settings) {
456 var cloudPrintEnableEvent = new Event(
457 NativeLayer.EventType.CLOUD_PRINT_ENABLE);
458 cloudPrintEnableEvent.baseCloudPrintUrl = settings['cloudPrintUrl'] || '';
459 cloudPrintEnableEvent.appKioskMode = settings['appKioskMode'] || false;
460 this.dispatchEvent(cloudPrintEnableEvent);
464 * Updates the print preview with local printers.
465 * Called from PrintPreviewHandler::SetupPrinterList().
466 * @param {Array} printers Array of printer info objects.
467 * @private
469 onSetPrinters_: function(printers) {
470 var localDestsSetEvent = new Event(
471 NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
472 localDestsSetEvent.destinationInfos = printers;
473 this.dispatchEvent(localDestsSetEvent);
477 * Called when native layer gets settings information for a requested local
478 * destination.
479 * @param {Object} settingsInfo printer setting information.
480 * @private
482 onUpdateWithPrinterCapabilities_: function(settingsInfo) {
483 var capsSetEvent = new Event(NativeLayer.EventType.CAPABILITIES_SET);
484 capsSetEvent.settingsInfo = settingsInfo;
485 this.dispatchEvent(capsSetEvent);
489 * Called when native layer gets settings information for a requested local
490 * destination.
491 * @param {string} destinationId Printer affected by error.
492 * @private
494 onFailedToGetPrinterCapabilities_: function(destinationId) {
495 var getCapsFailEvent = new Event(
496 NativeLayer.EventType.GET_CAPABILITIES_FAIL);
497 getCapsFailEvent.destinationId = destinationId;
498 getCapsFailEvent.destinationOrigin =
499 print_preview.Destination.Origin.LOCAL;
500 this.dispatchEvent(getCapsFailEvent);
504 * Called when native layer gets settings information for a requested privet
505 * destination.
506 * @param {string} destinationId Printer affected by error.
507 * @private
509 onFailedToGetPrivetPrinterCapabilities_: function(destinationId) {
510 var getCapsFailEvent = new Event(
511 NativeLayer.EventType.GET_CAPABILITIES_FAIL);
512 getCapsFailEvent.destinationId = destinationId;
513 getCapsFailEvent.destinationOrigin =
514 print_preview.Destination.Origin.PRIVET;
515 this.dispatchEvent(getCapsFailEvent);
518 /** Reloads the printer list. */
519 onReloadPrintersList_: function() {
520 cr.dispatchSimpleEvent(this, NativeLayer.EventType.DESTINATIONS_RELOAD);
524 * Called from the C++ layer.
525 * Take the PDF data handed to us and submit it to the cloud, closing the
526 * print preview dialog once the upload is successful.
527 * @param {string} data Data to send as the print job.
528 * @private
530 onPrintToCloud_: function(data) {
531 var printToCloudEvent = new Event(
532 NativeLayer.EventType.PRINT_TO_CLOUD);
533 printToCloudEvent.data = data;
534 this.dispatchEvent(printToCloudEvent);
538 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
539 * preview dialog regarding the file selection cancel event.
540 * @private
542 onFileSelectionCancelled_: function() {
543 cr.dispatchSimpleEvent(this, NativeLayer.EventType.FILE_SELECTION_CANCEL);
547 * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print
548 * preview dialog regarding the file selection completed event.
549 * @private
551 onFileSelectionCompleted_: function() {
552 // If the file selection is completed and the dialog is not already closed
553 // it means that a pending print to pdf request exists.
554 cr.dispatchSimpleEvent(
555 this, NativeLayer.EventType.FILE_SELECTION_COMPLETE);
559 * Display an error message when print preview fails.
560 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed().
561 * @private
563 onPrintPreviewFailed_: function() {
564 cr.dispatchSimpleEvent(
565 this, NativeLayer.EventType.PREVIEW_GENERATION_FAIL);
569 * Display an error message when encountered invalid printer settings.
570 * Called from PrintPreviewMessageHandler::OnInvalidPrinterSettings().
571 * @private
573 onInvalidPrinterSettings_: function() {
574 cr.dispatchSimpleEvent(this, NativeLayer.EventType.SETTINGS_INVALID);
578 * @param {{contentWidth: number, contentHeight: number, marginLeft: number,
579 * marginRight: number, marginTop: number, marginBottom: number,
580 * printableAreaX: number, printableAreaY: number,
581 * printableAreaWidth: number, printableAreaHeight: number}}
582 * pageLayout Specifies default page layout details in points.
583 * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed
584 * document has a custom page size style.
585 * @private
587 onDidGetDefaultPageLayout_: function(pageLayout, hasCustomPageSizeStyle) {
588 var pageLayoutChangeEvent = new Event(
589 NativeLayer.EventType.PAGE_LAYOUT_READY);
590 pageLayoutChangeEvent.pageLayout = pageLayout;
591 pageLayoutChangeEvent.hasCustomPageSizeStyle = hasCustomPageSizeStyle;
592 this.dispatchEvent(pageLayoutChangeEvent);
596 * Update the page count and check the page range.
597 * Called from PrintPreviewUI::OnDidGetPreviewPageCount().
598 * @param {number} pageCount The number of pages.
599 * @param {number} previewResponseId The preview request id that resulted in
600 * this response.
601 * @private
603 onDidGetPreviewPageCount_: function(pageCount, previewResponseId) {
604 var pageCountChangeEvent = new Event(
605 NativeLayer.EventType.PAGE_COUNT_READY);
606 pageCountChangeEvent.pageCount = pageCount;
607 pageCountChangeEvent.previewResponseId = previewResponseId;
608 this.dispatchEvent(pageCountChangeEvent);
612 * Notification that a print preview page has been rendered.
613 * Check if the settings have changed and request a regeneration if needed.
614 * Called from PrintPreviewUI::OnDidPreviewPage().
615 * @param {number} pageNumber The page number, 0-based.
616 * @param {number} previewUid Preview unique identifier.
617 * @param {number} previewResponseId The preview request id that resulted in
618 * this response.
619 * @private
621 onDidPreviewPage_: function(pageNumber, previewUid, previewResponseId) {
622 var pagePreviewGenEvent = new Event(
623 NativeLayer.EventType.PAGE_PREVIEW_READY);
624 pagePreviewGenEvent.pageIndex = pageNumber;
625 pagePreviewGenEvent.previewUid = previewUid;
626 pagePreviewGenEvent.previewResponseId = previewResponseId;
627 this.dispatchEvent(pagePreviewGenEvent);
631 * Notification that access token is ready.
632 * @param {string} authType Type of access token.
633 * @param {string} accessToken Access token.
634 * @private
636 onDidGetAccessToken_: function(authType, accessToken) {
637 var getAccessTokenEvent = new Event(
638 NativeLayer.EventType.ACCESS_TOKEN_READY);
639 getAccessTokenEvent.authType = authType;
640 getAccessTokenEvent.accessToken = accessToken;
641 this.dispatchEvent(getAccessTokenEvent);
645 * Update the print preview when new preview data is available.
646 * Create the PDF plugin as needed.
647 * Called from PrintPreviewUI::PreviewDataIsAvailable().
648 * @param {number} previewUid Preview unique identifier.
649 * @param {number} previewResponseId The preview request id that resulted in
650 * this response.
651 * @private
653 onUpdatePrintPreview_: function(previewUid, previewResponseId) {
654 var previewGenDoneEvent = new Event(
655 NativeLayer.EventType.PREVIEW_GENERATION_DONE);
656 previewGenDoneEvent.previewUid = previewUid;
657 previewGenDoneEvent.previewResponseId = previewResponseId;
658 this.dispatchEvent(previewGenDoneEvent);
662 * Updates the fit to page option state based on the print scaling option of
663 * source pdf. PDF's have an option to enable/disable print scaling. When we
664 * find out that the print scaling option is disabled for the source pdf, we
665 * uncheck the fitToPage_ to page checkbox. This function is called from C++
666 * code.
667 * @private
669 onPrintScalingDisabledForSourcePDF_: function() {
670 cr.dispatchSimpleEvent(this, NativeLayer.EventType.DISABLE_SCALING);
674 * Simulates a user click on the print preview dialog cancel button. Used
675 * only for testing.
676 * @private
678 autoCancelForTesting_: function() {
679 var properties = {view: window, bubbles: true, cancelable: true};
680 var click = new MouseEvent('click', properties);
681 document.querySelector('#print-header .cancel').dispatchEvent(click);
685 * @param {{serviceName: string, name: string}} printer Specifies
686 * information about the printer that was added.
687 * @private
689 onPrivetPrinterChanged_: function(printer) {
690 var privetPrinterChangedEvent =
691 new Event(NativeLayer.EventType.PRIVET_PRINTER_CHANGED);
692 privetPrinterChangedEvent.printer = printer;
693 this.dispatchEvent(privetPrinterChangedEvent);
697 * @param {Object} printer Specifies information about the printer that was
698 * added.
699 * @private
701 onPrivetCapabilitiesSet_: function(printer, capabilities) {
702 var privetCapabilitiesSetEvent =
703 new Event(NativeLayer.EventType.PRIVET_CAPABILITIES_SET);
704 privetCapabilitiesSetEvent.printer = printer;
705 privetCapabilitiesSetEvent.capabilities = capabilities;
706 this.dispatchEvent(privetCapabilitiesSetEvent);
710 * @param {string} http_error The HTTP response code or -1 if not an HTTP
711 * error.
712 * @private
714 onPrivetPrintFailed_: function(http_error) {
715 var privetPrintFailedEvent =
716 new Event(NativeLayer.EventType.PRIVET_PRINT_FAILED);
717 privetPrintFailedEvent.httpError = http_error;
718 this.dispatchEvent(privetPrintFailedEvent);
722 * Allows for onManipulateSettings to be called
723 * from the native layer.
724 * @private
726 onEnableManipulateSettingsForTest_: function() {
727 global.onManipulateSettingsForTest =
728 this.onManipulateSettingsForTest_.bind(this);
732 * Dispatches an event to print_preview.js to change
733 * a particular setting for print preview.
734 * @param {!print_preview.PreviewSettings} settings Object containing the
735 * value to be changed and that value should be set to.
736 * @private
738 onManipulateSettingsForTest_: function(settings) {
739 var manipulateSettingsEvent =
740 new Event(NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST);
741 manipulateSettingsEvent.settings = settings;
742 this.dispatchEvent(manipulateSettingsEvent);
746 * Sends a message to the test, letting it know that an
747 * option has been set to a particular value and that the change has
748 * finished modifying the preview area.
750 previewReadyForTest: function() {
751 if (global.onManipulateSettingsForTest)
752 chrome.send('UILoadedForTest');
756 * Notifies the test that the option it tried to change
757 * had not been changed successfully.
759 previewFailedForTest: function() {
760 if (global.onManipulateSettingsForTest)
761 chrome.send('UIFailedLoadingForTest');
766 * Initial settings retrieved from the native layer.
767 * @param {boolean} isInKioskAutoPrintMode Whether the print preview should be
768 * in auto-print mode.
769 * @param {boolean} isInAppKioskMode Whether the print preview is in App Kiosk
770 * mode.
771 * @param {string} thousandsDelimeter Character delimeter of thousands digits.
772 * @param {string} decimalDelimeter Character delimeter of the decimal point.
773 * @param {!print_preview.MeasurementSystem.UnitType} unitType Unit type of
774 * local machine's measurement system.
775 * @param {boolean} isDocumentModifiable Whether the document to print is
776 * modifiable.
777 * @param {string} documentTitle Title of the document.
778 * @param {boolean} documentHasSelection Whether the document has selected
779 * content.
780 * @param {boolean} selectionOnly Whether only selected content should be
781 * printed.
782 * @param {?string} systemDefaultDestinationId ID of the system default
783 * destination.
784 * @param {?string} serializedAppStateStr Serialized app state.
785 * @constructor
787 function NativeInitialSettings(
788 isInKioskAutoPrintMode,
789 isInAppKioskMode,
790 hidePrintWithSystemDialogLink,
791 thousandsDelimeter,
792 decimalDelimeter,
793 unitType,
794 isDocumentModifiable,
795 documentTitle,
796 documentHasSelection,
797 selectionOnly,
798 systemDefaultDestinationId,
799 serializedAppStateStr) {
802 * Whether the print preview should be in auto-print mode.
803 * @type {boolean}
804 * @private
806 this.isInKioskAutoPrintMode_ = isInKioskAutoPrintMode;
809 * Whether the print preview should switch to App Kiosk mode.
810 * @type {boolean}
811 * @private
813 this.isInAppKioskMode_ = isInAppKioskMode;
816 * Whether we should hide the link which shows the system print dialog.
817 * @type {boolean}
818 * @private
820 this.hidePrintWithSystemDialogLink_ = hidePrintWithSystemDialogLink;
823 * Character delimeter of thousands digits.
824 * @type {string}
825 * @private
827 this.thousandsDelimeter_ = thousandsDelimeter;
830 * Character delimeter of the decimal point.
831 * @type {string}
832 * @private
834 this.decimalDelimeter_ = decimalDelimeter;
837 * Unit type of local machine's measurement system.
838 * @type {string}
839 * @private
841 this.unitType_ = unitType;
844 * Whether the document to print is modifiable.
845 * @type {boolean}
846 * @private
848 this.isDocumentModifiable_ = isDocumentModifiable;
851 * Title of the document.
852 * @type {string}
853 * @private
855 this.documentTitle_ = documentTitle;
858 * Whether the document has selection.
859 * @type {string}
860 * @private
862 this.documentHasSelection_ = documentHasSelection;
865 * Whether selection only should be printed.
866 * @type {string}
867 * @private
869 this.selectionOnly_ = selectionOnly;
872 * ID of the system default destination.
873 * @type {?string}
874 * @private
876 this.systemDefaultDestinationId_ = systemDefaultDestinationId;
879 * Serialized app state.
880 * @type {?string}
881 * @private
883 this.serializedAppStateStr_ = serializedAppStateStr;
886 NativeInitialSettings.prototype = {
888 * @return {boolean} Whether the print preview should be in auto-print mode.
890 get isInKioskAutoPrintMode() {
891 return this.isInKioskAutoPrintMode_;
895 * @return {boolean} Whether the print preview should switch to App Kiosk
896 * mode.
898 get isInAppKioskMode() {
899 return this.isInAppKioskMode_;
903 * @return {boolean} Whether we should hide the link which shows the
904 system print dialog.
906 get hidePrintWithSystemDialogLink() {
907 return this.hidePrintWithSystemDialogLink_;
910 /** @return {string} Character delimeter of thousands digits. */
911 get thousandsDelimeter() {
912 return this.thousandsDelimeter_;
915 /** @return {string} Character delimeter of the decimal point. */
916 get decimalDelimeter() {
917 return this.decimalDelimeter_;
921 * @return {!print_preview.MeasurementSystem.UnitType} Unit type of local
922 * machine's measurement system.
924 get unitType() {
925 return this.unitType_;
928 /** @return {boolean} Whether the document to print is modifiable. */
929 get isDocumentModifiable() {
930 return this.isDocumentModifiable_;
933 /** @return {string} Document title. */
934 get documentTitle() {
935 return this.documentTitle_;
938 /** @return {boolean} Whether the document has selection. */
939 get documentHasSelection() {
940 return this.documentHasSelection_;
943 /** @return {boolean} Whether selection only should be printed. */
944 get selectionOnly() {
945 return this.selectionOnly_;
948 /** @return {?string} ID of the system default destination. */
949 get systemDefaultDestinationId() {
950 return this.systemDefaultDestinationId_;
953 /** @return {?string} Serialized app state. */
954 get serializedAppStateStr() {
955 return this.serializedAppStateStr_;
959 // Export
960 return {
961 NativeInitialSettings: NativeInitialSettings,
962 NativeLayer: NativeLayer