Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / native_layer.js
blob5da08fce2ab3f8d746651e04580e86e543e623ca
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.define('print_preview', function() {
6 'use strict';
8 /**
9 * An interface to the native Chromium printing system layer.
10 * @constructor
11 * @extends {cr.EventTarget}
13 function NativeLayer() {
14 cr.EventTarget.call(this);
16 // Bind global handlers
17 global.setInitialSettings = this.onSetInitialSettings_.bind(this);
18 global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this);
19 global.setPrinters = this.onSetPrinters_.bind(this);
20 global.updateWithPrinterCapabilities =
21 this.onUpdateWithPrinterCapabilities_.bind(this);
22 global.failedToGetPrinterCapabilities =
23 this.onFailedToGetPrinterCapabilities_.bind(this);
24 global.failedToGetPrivetPrinterCapabilities =
25 this.onFailedToGetPrivetPrinterCapabilities_.bind(this);
26 global.reloadPrintersList = this.onReloadPrintersList_.bind(this);
27 global.printToCloud = this.onPrintToCloud_.bind(this);
28 global.fileSelectionCancelled =
29 this.onFileSelectionCancelled_.bind(this);
30 global.fileSelectionCompleted =
31 this.onFileSelectionCompleted_.bind(this);
32 global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this);
33 global.invalidPrinterSettings =
34 this.onInvalidPrinterSettings_.bind(this);
35 global.onDidGetDefaultPageLayout =
36 this.onDidGetDefaultPageLayout_.bind(this);
37 global.onDidGetPreviewPageCount =
38 this.onDidGetPreviewPageCount_.bind(this);
39 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this);
40 global.updatePrintPreview = this.onUpdatePrintPreview_.bind(this);
41 global.printScalingDisabledForSourcePDF =
42 this.onPrintScalingDisabledForSourcePDF_.bind(this);
43 global.onDidGetAccessToken = this.onDidGetAccessToken_.bind(this);
44 global.autoCancelForTesting = this.autoCancelForTesting_.bind(this);
45 global.onPrivetPrinterChanged = this.onPrivetPrinterChanged_.bind(this);
46 global.onPrivetCapabilitiesSet =
47 this.onPrivetCapabilitiesSet_.bind(this);
48 global.onPrivetPrintFailed = this.onPrivetPrintFailed_.bind(this);
49 global.onEnableManipulateSettingsForTest =
50 this.onEnableManipulateSettingsForTest_.bind(this);
53 /**
54 * Event types dispatched from the Chromium native layer.
55 * @enum {string}
56 * @const
58 NativeLayer.EventType = {
59 ACCESS_TOKEN_READY: 'print_preview.NativeLayer.ACCESS_TOKEN_READY',
60 CAPABILITIES_SET: 'print_preview.NativeLayer.CAPABILITIES_SET',
61 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
62 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
63 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING',
64 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
65 FILE_SELECTION_COMPLETE:
66 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
67 GET_CAPABILITIES_FAIL: 'print_preview.NativeLayer.GET_CAPABILITIES_FAIL',
68 INITIAL_SETTINGS_SET: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET',
69 LOCAL_DESTINATIONS_SET: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET',
70 MANIPULATE_SETTINGS_FOR_TEST:
71 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
72 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY',
73 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
74 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
75 PREVIEW_GENERATION_DONE:
76 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
77 PREVIEW_GENERATION_FAIL:
78 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
79 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
80 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID',
81 PRIVET_PRINTER_CHANGED: 'print_preview.NativeLayer.PRIVET_PRINTER_CHANGED',
82 PRIVET_CAPABILITIES_SET:
83 'print_preview.NativeLayer.PRIVET_CAPABILITIES_SET',
84 PRIVET_PRINT_FAILED: 'print_preview.NativeLayer.PRIVET_PRINT_FAILED',
87 /**
88 * Constant values matching printing::DuplexMode enum.
89 * @enum {number}
91 NativeLayer.DuplexMode = {
92 SIMPLEX: 0,
93 LONG_EDGE: 1,
94 UNKNOWN_DUPLEX_MODE: -1
97 /**
98 * Enumeration of color modes used by Chromium.
99 * @enum {number}
100 * @private
102 NativeLayer.ColorMode_ = {
103 GRAY: 1,
104 COLOR: 2
108 * Version of the serialized state of the print preview.
109 * @type {number}
110 * @const
111 * @private
113 NativeLayer.SERIALIZED_STATE_VERSION_ = 1;
115 NativeLayer.prototype = {
116 __proto__: cr.EventTarget.prototype,
119 * Requests access token for cloud print requests.
120 * @param {string} authType type of access token.
122 startGetAccessToken: function(authType) {
123 chrome.send('getAccessToken', [authType]);
126 /** Gets the initial settings to initialize the print preview with. */
127 startGetInitialSettings: function() {
128 chrome.send('getInitialSettings');
132 * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
133 * event will be dispatched in response.
135 startGetLocalDestinations: function() {
136 chrome.send('getPrinters');
140 * Requests the network's privet print destinations. A number of
141 * PRIVET_PRINTER_CHANGED events will be fired in response, followed by a
142 * PRIVET_SEARCH_ENDED.
144 startGetPrivetDestinations: function() {
145 chrome.send('getPrivetPrinters');
149 * Requests that the privet print stack stop searching for privet print
150 * destinations.
152 stopGetPrivetDestinations: function() {
153 chrome.send('stopGetPrivetPrinters');
157 * Requests the privet destination's printing capabilities. A
158 * PRIVET_CAPABILITIES_SET event will be dispatched in response.
159 * @param {string} destinationId ID of the destination.
161 startGetPrivetDestinationCapabilities: function(destinationId) {
162 chrome.send('getPrivetPrinterCapabilities', [destinationId]);
166 * Requests the destination's printing capabilities. A CAPABILITIES_SET
167 * event will be dispatched in response.
168 * @param {string} destinationId ID of the destination.
170 startGetLocalDestinationCapabilities: function(destinationId) {
171 chrome.send('getPrinterCapabilities', [destinationId]);
175 * @param {!print_preview.Destination} destination Destination to print to.
176 * @param {!print_preview.ticket_items.Color} color Color ticket item.
177 * @return {number} Native layer color model.
178 * @private
180 getNativeColorModel_: function(destination, color) {
181 // For non-local printers native color model is ignored anyway.
182 var option = destination.isLocal ? color.getSelectedOption() : null;
183 var nativeColorModel = parseInt(option ? option.vendor_id : null);
184 if (isNaN(nativeColorModel)) {
185 return color.getValue() ?
186 NativeLayer.ColorMode_.COLOR : NativeLayer.ColorMode_.GRAY;
188 return nativeColorModel;
192 * Requests that a preview be generated. The following events may be
193 * dispatched in response:
194 * - PAGE_COUNT_READY
195 * - PAGE_LAYOUT_READY
196 * - PAGE_PREVIEW_READY
197 * - PREVIEW_GENERATION_DONE
198 * - PREVIEW_GENERATION_FAIL
199 * @param {print_preview.Destination} destination Destination to print to.
200 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
201 * state of the print ticket.
202 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
203 * @param {number} ID of the preview request.
205 startGetPreview: function(
206 destination, printTicketStore, documentInfo, requestId) {
207 assert(printTicketStore.isTicketValidForPreview(),
208 'Trying to generate preview when ticket is not valid');
210 var ticket = {
211 'pageRange': printTicketStore.pageRange.getDocumentPageRanges(),
212 'mediaSize': printTicketStore.mediaSize.getValue(),
213 'landscape': printTicketStore.landscape.getValue(),
214 'color': this.getNativeColorModel_(destination, printTicketStore.color),
215 'headerFooterEnabled': printTicketStore.headerFooter.getValue(),
216 'marginsType': printTicketStore.marginsType.getValue(),
217 'isFirstRequest': requestId == 0,
218 'requestID': requestId,
219 'previewModifiable': documentInfo.isModifiable,
220 'printToPDF':
221 destination != null &&
222 destination.id ==
223 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
224 'printWithCloudPrint': destination != null && !destination.isLocal,
225 'printWithPrivet': destination != null && destination.isPrivet,
226 'deviceName': destination == null ? 'foo' : destination.id,
227 'generateDraftData': documentInfo.isModifiable,
228 'fitToPageEnabled': printTicketStore.fitToPage.getValue(),
230 // NOTE: Even though the following fields don't directly relate to the
231 // preview, they still need to be included.
232 'duplex': printTicketStore.duplex.getValue() ?
233 NativeLayer.DuplexMode.LONG_EDGE : NativeLayer.DuplexMode.SIMPLEX,
234 'copies': printTicketStore.copies.getValueAsNumber(),
235 'collate': printTicketStore.collate.getValue(),
236 'shouldPrintBackgrounds': printTicketStore.cssBackground.getValue(),
237 'shouldPrintSelectionOnly': printTicketStore.selectionOnly.getValue()
240 // Set 'cloudPrintID' only if the destination is not local.
241 if (destination && !destination.isLocal) {
242 ticket['cloudPrintID'] = destination.id;
245 if (printTicketStore.marginsType.isCapabilityAvailable() &&
246 printTicketStore.marginsType.getValue() ==
247 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
248 var customMargins = printTicketStore.customMargins.getValue();
249 var orientationEnum =
250 print_preview.ticket_items.CustomMargins.Orientation;
251 ticket['marginsCustom'] = {
252 'marginTop': customMargins.get(orientationEnum.TOP),
253 'marginRight': customMargins.get(orientationEnum.RIGHT),
254 'marginBottom': customMargins.get(orientationEnum.BOTTOM),
255 'marginLeft': customMargins.get(orientationEnum.LEFT)
259 chrome.send(
260 'getPreview',
261 [JSON.stringify(ticket),
262 requestId > 0 ? documentInfo.pageCount : -1,
263 documentInfo.isModifiable]);
267 * Requests that the document be printed.
268 * @param {!print_preview.Destination} destination Destination to print to.
269 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
270 * state of the print ticket.
271 * @param {print_preview.CloudPrintInterface} cloudPrintInterface Interface
272 * to Google Cloud Print.
273 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
274 * @param {boolean=} opt_isOpenPdfInPreview Whether to open the PDF in the
275 * system's preview application.
276 * @param {boolean=} opt_showSystemDialog Whether to open system dialog for
277 * advanced settings.
279 startPrint: function(destination, printTicketStore, cloudPrintInterface,
280 documentInfo, opt_isOpenPdfInPreview,
281 opt_showSystemDialog) {
282 assert(printTicketStore.isTicketValid(),
283 'Trying to print when ticket is not valid');
285 assert(!opt_showSystemDialog || (cr.isWindows && destination.isLocal),
286 'Implemented for Windows only');
288 var ticket = {
289 'pageRange': printTicketStore.pageRange.getDocumentPageRanges(),
290 'mediaSize': printTicketStore.mediaSize.getValue(),
291 'pageCount': printTicketStore.pageRange.getPageNumberSet().size,
292 'landscape': printTicketStore.landscape.getValue(),
293 'color': this.getNativeColorModel_(destination, printTicketStore.color),
294 'headerFooterEnabled': printTicketStore.headerFooter.getValue(),
295 'marginsType': printTicketStore.marginsType.getValue(),
296 'generateDraftData': true, // TODO(rltoscano): What should this be?
297 'duplex': printTicketStore.duplex.getValue() ?
298 NativeLayer.DuplexMode.LONG_EDGE : NativeLayer.DuplexMode.SIMPLEX,
299 'copies': printTicketStore.copies.getValueAsNumber(),
300 'collate': printTicketStore.collate.getValue(),
301 'shouldPrintBackgrounds': printTicketStore.cssBackground.getValue(),
302 'shouldPrintSelectionOnly': printTicketStore.selectionOnly.getValue(),
303 'previewModifiable': documentInfo.isModifiable,
304 'printToPDF': destination.id ==
305 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
306 'printWithCloudPrint': !destination.isLocal,
307 'printWithPrivet': destination.isPrivet,
308 'deviceName': destination.id,
309 'isFirstRequest': false,
310 'requestID': -1,
311 'fitToPageEnabled': printTicketStore.fitToPage.getValue(),
312 'pageWidth': documentInfo.pageSize.width,
313 'pageHeight': documentInfo.pageSize.height,
314 'showSystemDialog': opt_showSystemDialog
317 if (!destination.isLocal) {
318 // We can't set cloudPrintID if the destination is "Print with Cloud
319 // Print" because the native system will try to print to Google Cloud
320 // Print with this ID instead of opening a Google Cloud Print dialog.
321 ticket['cloudPrintID'] = destination.id;
324 if (printTicketStore.marginsType.isCapabilityAvailable() &&
325 printTicketStore.marginsType.isValueEqual(
326 print_preview.ticket_items.MarginsType.Value.CUSTOM)) {
327 var customMargins = printTicketStore.customMargins.getValue();
328 var orientationEnum =
329 print_preview.ticket_items.CustomMargins.Orientation;
330 ticket['marginsCustom'] = {
331 'marginTop': customMargins.get(orientationEnum.TOP),
332 'marginRight': customMargins.get(orientationEnum.RIGHT),
333 'marginBottom': customMargins.get(orientationEnum.BOTTOM),
334 'marginLeft': customMargins.get(orientationEnum.LEFT)
338 if (destination.isPrivet) {
339 ticket['ticket'] = printTicketStore.createPrintTicket(destination);
340 ticket['capabilities'] = JSON.stringify(destination.capabilities);
343 if (opt_isOpenPdfInPreview) {
344 ticket['OpenPDFInPreview'] = true;
347 chrome.send('print', [JSON.stringify(ticket)]);
350 /** Requests that the current pending print request be cancelled. */
351 startCancelPendingPrint: function() {
352 chrome.send('cancelPendingPrintRequest');
355 /** Shows the system's native printing dialog. */
356 startShowSystemDialog: function() {
357 assert(!cr.isWindows);
358 chrome.send('showSystemDialog');
361 /** Shows Google Cloud Print's web-based print dialog.
362 * @param {number} pageCount Number of pages to print.
364 startShowCloudPrintDialog: function(pageCount) {
365 chrome.send('printWithCloudPrintDialog', [pageCount]);
368 /** Closes the print preview dialog. */
369 startCloseDialog: function() {
370 chrome.send('closePrintPreviewDialog');
371 chrome.send('dialogClose');
374 /** Hide the print preview dialog and allow the native layer to close it. */
375 startHideDialog: function() {
376 chrome.send('hidePreview');
380 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event
381 * will be dispatched in response.
382 * @param {boolean} addAccount Whether to open an 'add a new account' or
383 * default sign in page.
385 startCloudPrintSignIn: function(addAccount) {
386 chrome.send('signIn', [addAccount]);
389 /** Navigates the user to the system printer settings interface. */
390 startManageLocalDestinations: function() {
391 chrome.send('manageLocalPrinters');
394 /** Navigates the user to the Google Cloud Print management page. */
395 startManageCloudDestinations: function() {
396 chrome.send('manageCloudPrinters');
399 /** Forces browser to open a new tab with the given URL address. */
400 startForceOpenNewTab: function(url) {
401 chrome.send('forceOpenNewTab', [url]);
405 * @param {!Object} initialSettings Object containing all initial settings.
407 onSetInitialSettings_: function(initialSettings) {
408 var numberFormatSymbols =
409 print_preview.MeasurementSystem.parseNumberFormat(
410 initialSettings['numberFormat']);
411 var unitType = print_preview.MeasurementSystem.UnitType.IMPERIAL;
412 if (initialSettings['measurementSystem'] != null) {
413 unitType = initialSettings['measurementSystem'];
416 var nativeInitialSettings = new print_preview.NativeInitialSettings(
417 initialSettings['printAutomaticallyInKioskMode'] || false,
418 initialSettings['appKioskMode'] || false,
419 initialSettings['hidePrintWithSystemDialogLink'] || false,
420 numberFormatSymbols[0] || ',',
421 numberFormatSymbols[1] || '.',
422 unitType,
423 initialSettings['previewModifiable'] || false,
424 initialSettings['initiatorTitle'] || '',
425 initialSettings['documentHasSelection'] || false,
426 initialSettings['shouldPrintSelectionOnly'] || false,
427 initialSettings['printerName'] || null,
428 initialSettings['appState'] || null);
430 var initialSettingsSetEvent = new Event(
431 NativeLayer.EventType.INITIAL_SETTINGS_SET);
432 initialSettingsSetEvent.initialSettings = nativeInitialSettings;
433 this.dispatchEvent(initialSettingsSetEvent);
437 * Turn on the integration of Cloud Print.
438 * @param {string} cloudPrintURL The URL to use for cloud print servers.
439 * @private
441 onSetUseCloudPrint_: function(settings) {
442 var cloudPrintEnableEvent = new Event(
443 NativeLayer.EventType.CLOUD_PRINT_ENABLE);
444 cloudPrintEnableEvent.baseCloudPrintUrl = settings['cloudPrintUrl'] || '';
445 cloudPrintEnableEvent.appKioskMode = settings['appKioskMode'] || false;
446 this.dispatchEvent(cloudPrintEnableEvent);
450 * Updates the print preview with local printers.
451 * Called from PrintPreviewHandler::SetupPrinterList().
452 * @param {Array} printers Array of printer info objects.
453 * @private
455 onSetPrinters_: function(printers) {
456 var localDestsSetEvent = new Event(
457 NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
458 localDestsSetEvent.destinationInfos = printers;
459 this.dispatchEvent(localDestsSetEvent);
463 * Called when native layer gets settings information for a requested local
464 * destination.
465 * @param {Object} settingsInfo printer setting information.
466 * @private
468 onUpdateWithPrinterCapabilities_: function(settingsInfo) {
469 var capsSetEvent = new Event(NativeLayer.EventType.CAPABILITIES_SET);
470 capsSetEvent.settingsInfo = settingsInfo;
471 this.dispatchEvent(capsSetEvent);
475 * Called when native layer gets settings information for a requested local
476 * destination.
477 * @param {string} printerId printer affected by error.
478 * @private
480 onFailedToGetPrinterCapabilities_: function(destinationId) {
481 var getCapsFailEvent = new Event(
482 NativeLayer.EventType.GET_CAPABILITIES_FAIL);
483 getCapsFailEvent.destinationId = destinationId;
484 getCapsFailEvent.destinationOrigin =
485 print_preview.Destination.Origin.LOCAL;
486 this.dispatchEvent(getCapsFailEvent);
490 * Called when native layer gets settings information for a requested privet
491 * destination.
492 * @param {string} printerId printer affected by error.
493 * @private
495 onFailedToGetPrivetPrinterCapabilities_: function(destinationId) {
496 var getCapsFailEvent = new Event(
497 NativeLayer.EventType.GET_CAPABILITIES_FAIL);
498 getCapsFailEvent.destinationId = destinationId;
499 getCapsFailEvent.destinationOrigin =
500 print_preview.Destination.Origin.PRIVET;
501 this.dispatchEvent(getCapsFailEvent);
504 /** Reloads the printer list. */
505 onReloadPrintersList_: function() {
506 cr.dispatchSimpleEvent(this, NativeLayer.EventType.DESTINATIONS_RELOAD);
510 * Called from the C++ layer.
511 * Take the PDF data handed to us and submit it to the cloud, closing the
512 * print preview dialog once the upload is successful.
513 * @param {string} data Data to send as the print job.
514 * @private
516 onPrintToCloud_: function(data) {
517 var printToCloudEvent = new Event(
518 NativeLayer.EventType.PRINT_TO_CLOUD);
519 printToCloudEvent.data = data;
520 this.dispatchEvent(printToCloudEvent);
524 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
525 * preview dialog regarding the file selection cancel event.
526 * @private
528 onFileSelectionCancelled_: function() {
529 cr.dispatchSimpleEvent(this, NativeLayer.EventType.FILE_SELECTION_CANCEL);
533 * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print
534 * preview dialog regarding the file selection completed event.
535 * @private
537 onFileSelectionCompleted_: function() {
538 // If the file selection is completed and the dialog is not already closed
539 // it means that a pending print to pdf request exists.
540 cr.dispatchSimpleEvent(
541 this, NativeLayer.EventType.FILE_SELECTION_COMPLETE);
545 * Display an error message when print preview fails.
546 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed().
547 * @private
549 onPrintPreviewFailed_: function() {
550 cr.dispatchSimpleEvent(
551 this, NativeLayer.EventType.PREVIEW_GENERATION_FAIL);
555 * Display an error message when encountered invalid printer settings.
556 * Called from PrintPreviewMessageHandler::OnInvalidPrinterSettings().
557 * @private
559 onInvalidPrinterSettings_: function() {
560 cr.dispatchSimpleEvent(this, NativeLayer.EventType.SETTINGS_INVALID);
564 * @param {{contentWidth: number, contentHeight: number, marginLeft: number,
565 * marginRight: number, marginTop: number, marginBottom: number,
566 * printableAreaX: number, printableAreaY: number,
567 * printableAreaWidth: number, printableAreaHeight: number}}
568 * pageLayout Specifies default page layout details in points.
569 * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed
570 * document has a custom page size style.
571 * @private
573 onDidGetDefaultPageLayout_: function(pageLayout, hasCustomPageSizeStyle) {
574 var pageLayoutChangeEvent = new Event(
575 NativeLayer.EventType.PAGE_LAYOUT_READY);
576 pageLayoutChangeEvent.pageLayout = pageLayout;
577 pageLayoutChangeEvent.hasCustomPageSizeStyle = hasCustomPageSizeStyle;
578 this.dispatchEvent(pageLayoutChangeEvent);
582 * Update the page count and check the page range.
583 * Called from PrintPreviewUI::OnDidGetPreviewPageCount().
584 * @param {number} pageCount The number of pages.
585 * @param {number} previewResponseId The preview request id that resulted in
586 * this response.
587 * @private
589 onDidGetPreviewPageCount_: function(pageCount, previewResponseId) {
590 var pageCountChangeEvent = new Event(
591 NativeLayer.EventType.PAGE_COUNT_READY);
592 pageCountChangeEvent.pageCount = pageCount;
593 pageCountChangeEvent.previewResponseId = previewResponseId;
594 this.dispatchEvent(pageCountChangeEvent);
598 * Notification that a print preview page has been rendered.
599 * Check if the settings have changed and request a regeneration if needed.
600 * Called from PrintPreviewUI::OnDidPreviewPage().
601 * @param {number} pageNumber The page number, 0-based.
602 * @param {number} previewUid Preview unique identifier.
603 * @param {number} previewResponseId The preview request id that resulted in
604 * this response.
605 * @private
607 onDidPreviewPage_: function(pageNumber, previewUid, previewResponseId) {
608 var pagePreviewGenEvent = new Event(
609 NativeLayer.EventType.PAGE_PREVIEW_READY);
610 pagePreviewGenEvent.pageIndex = pageNumber;
611 pagePreviewGenEvent.previewUid = previewUid;
612 pagePreviewGenEvent.previewResponseId = previewResponseId;
613 this.dispatchEvent(pagePreviewGenEvent);
617 * Notification that access token is ready.
618 * @param {string} authType Type of access token.
619 * @param {string} accessToken Access token.
620 * @private
622 onDidGetAccessToken_: function(authType, accessToken) {
623 var getAccessTokenEvent = new Event(
624 NativeLayer.EventType.ACCESS_TOKEN_READY);
625 getAccessTokenEvent.authType = authType;
626 getAccessTokenEvent.accessToken = accessToken;
627 this.dispatchEvent(getAccessTokenEvent);
631 * Update the print preview when new preview data is available.
632 * Create the PDF plugin as needed.
633 * Called from PrintPreviewUI::PreviewDataIsAvailable().
634 * @param {number} previewUid Preview unique identifier.
635 * @param {number} previewResponseId The preview request id that resulted in
636 * this response.
637 * @private
639 onUpdatePrintPreview_: function(previewUid, previewResponseId) {
640 var previewGenDoneEvent = new Event(
641 NativeLayer.EventType.PREVIEW_GENERATION_DONE);
642 previewGenDoneEvent.previewUid = previewUid;
643 previewGenDoneEvent.previewResponseId = previewResponseId;
644 this.dispatchEvent(previewGenDoneEvent);
648 * Updates the fit to page option state based on the print scaling option of
649 * source pdf. PDF's have an option to enable/disable print scaling. When we
650 * find out that the print scaling option is disabled for the source pdf, we
651 * uncheck the fitToPage_ to page checkbox. This function is called from C++
652 * code.
653 * @private
655 onPrintScalingDisabledForSourcePDF_: function() {
656 cr.dispatchSimpleEvent(this, NativeLayer.EventType.DISABLE_SCALING);
660 * Simulates a user click on the print preview dialog cancel button. Used
661 * only for testing.
662 * @private
664 autoCancelForTesting_: function() {
665 var properties = {view: window, bubbles: true, cancelable: true};
666 var click = new MouseEvent('click', properties);
667 document.querySelector('#print-header .cancel').dispatchEvent(click);
671 * @param {{serviceName: string, name: string}} printer Specifies
672 * information about the printer that was added.
673 * @private
675 onPrivetPrinterChanged_: function(printer) {
676 var privetPrinterChangedEvent =
677 new Event(NativeLayer.EventType.PRIVET_PRINTER_CHANGED);
678 privetPrinterChangedEvent.printer = printer;
679 this.dispatchEvent(privetPrinterChangedEvent);
683 * @param {Object} printer Specifies information about the printer that was
684 * added.
685 * @private
687 onPrivetCapabilitiesSet_: function(printer, capabilities) {
688 var privetCapabilitiesSetEvent =
689 new Event(NativeLayer.EventType.PRIVET_CAPABILITIES_SET);
690 privetCapabilitiesSetEvent.printer = printer;
691 privetCapabilitiesSetEvent.capabilities = capabilities;
692 this.dispatchEvent(privetCapabilitiesSetEvent);
696 * @param {string} http_error The HTTP response code or -1 if not an HTTP
697 * error.
698 * @private
700 onPrivetPrintFailed_: function(http_error) {
701 var privetPrintFailedEvent =
702 new Event(NativeLayer.EventType.PRIVET_PRINT_FAILED);
703 privetPrintFailedEvent.httpError = http_error;
704 this.dispatchEvent(privetPrintFailedEvent);
708 * Allows for onManipulateSettings to be called
709 * from the native layer.
710 * @private
712 onEnableManipulateSettingsForTest_: function() {
713 global.onManipulateSettingsForTest =
714 this.onManipulateSettingsForTest_.bind(this);
718 * Dispatches an event to print_preview.js to change
719 * a particular setting for print preview.
720 * @param {!Object} settings Object containing the value to be
721 * changed and that value should be set to.
722 * @private
724 onManipulateSettingsForTest_: function(settings) {
725 var manipulateSettingsEvent =
726 new Event(NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST);
727 manipulateSettingsEvent.settings = settings;
728 this.dispatchEvent(manipulateSettingsEvent);
732 * Sends a message to the test, letting it know that an
733 * option has been set to a particular value and that the change has
734 * finished modifying the preview area.
736 previewReadyForTest: function() {
737 if (global.onManipulateSettingsForTest)
738 chrome.send('UILoadedForTest');
742 * Notifies the test that the option it tried to change
743 * had not been changed successfully.
745 previewFailedForTest: function() {
746 if (global.onManipulateSettingsForTest)
747 chrome.send('UIFailedLoadingForTest');
752 * Initial settings retrieved from the native layer.
753 * @param {boolean} isInKioskAutoPrintMode Whether the print preview should be
754 * in auto-print mode.
755 * @param {boolean} isInAppKioskMode Whether the print preview is in App Kiosk
756 * mode.
757 * @param {string} thousandsDelimeter Character delimeter of thousands digits.
758 * @param {string} decimalDelimeter Character delimeter of the decimal point.
759 * @param {!print_preview.MeasurementSystem.UnitType} unitType Unit type of
760 * local machine's measurement system.
761 * @param {boolean} isDocumentModifiable Whether the document to print is
762 * modifiable.
763 * @param {string} documentTitle Title of the document.
764 * @param {boolean} documentHasSelection Whether the document has selected
765 * content.
766 * @param {boolean} selectionOnly Whether only selected content should be
767 * printed.
768 * @param {?string} systemDefaultDestinationId ID of the system default
769 * destination.
770 * @param {?string} serializedAppStateStr Serialized app state.
771 * @constructor
773 function NativeInitialSettings(
774 isInKioskAutoPrintMode,
775 isInAppKioskMode,
776 hidePrintWithSystemDialogLink,
777 thousandsDelimeter,
778 decimalDelimeter,
779 unitType,
780 isDocumentModifiable,
781 documentTitle,
782 documentHasSelection,
783 selectionOnly,
784 systemDefaultDestinationId,
785 serializedAppStateStr) {
788 * Whether the print preview should be in auto-print mode.
789 * @type {boolean}
790 * @private
792 this.isInKioskAutoPrintMode_ = isInKioskAutoPrintMode;
795 * Whether the print preview should switch to App Kiosk mode.
796 * @type {boolean}
797 * @private
799 this.isInAppKioskMode_ = isInAppKioskMode;
802 * Whether we should hide the link which shows the system print dialog.
803 * @type {boolean}
804 * @private
806 this.hidePrintWithSystemDialogLink_ = hidePrintWithSystemDialogLink;
809 * Character delimeter of thousands digits.
810 * @type {string}
811 * @private
813 this.thousandsDelimeter_ = thousandsDelimeter;
816 * Character delimeter of the decimal point.
817 * @type {string}
818 * @private
820 this.decimalDelimeter_ = decimalDelimeter;
823 * Unit type of local machine's measurement system.
824 * @type {string}
825 * @private
827 this.unitType_ = unitType;
830 * Whether the document to print is modifiable.
831 * @type {boolean}
832 * @private
834 this.isDocumentModifiable_ = isDocumentModifiable;
837 * Title of the document.
838 * @type {string}
839 * @private
841 this.documentTitle_ = documentTitle;
844 * Whether the document has selection.
845 * @type {string}
846 * @private
848 this.documentHasSelection_ = documentHasSelection;
851 * Whether selection only should be printed.
852 * @type {string}
853 * @private
855 this.selectionOnly_ = selectionOnly;
858 * ID of the system default destination.
859 * @type {?string}
860 * @private
862 this.systemDefaultDestinationId_ = systemDefaultDestinationId;
865 * Serialized app state.
866 * @type {?string}
867 * @private
869 this.serializedAppStateStr_ = serializedAppStateStr;
872 NativeInitialSettings.prototype = {
874 * @return {boolean} Whether the print preview should be in auto-print mode.
876 get isInKioskAutoPrintMode() {
877 return this.isInKioskAutoPrintMode_;
881 * @return {boolean} Whether the print preview should switch to App Kiosk
882 * mode.
884 get isInAppKioskMode() {
885 return this.isInAppKioskMode_;
889 * @return {boolean} Whether we should hide the link which shows the
890 system print dialog.
892 get hidePrintWithSystemDialogLink() {
893 return this.hidePrintWithSystemDialogLink_;
896 /** @return {string} Character delimeter of thousands digits. */
897 get thousandsDelimeter() {
898 return this.thousandsDelimeter_;
901 /** @return {string} Character delimeter of the decimal point. */
902 get decimalDelimeter() {
903 return this.decimalDelimeter_;
907 * @return {!print_preview.MeasurementSystem.UnitType} Unit type of local
908 * machine's measurement system.
910 get unitType() {
911 return this.unitType_;
914 /** @return {boolean} Whether the document to print is modifiable. */
915 get isDocumentModifiable() {
916 return this.isDocumentModifiable_;
919 /** @return {string} Document title. */
920 get documentTitle() {
921 return this.documentTitle_;
924 /** @return {bool} Whether the document has selection. */
925 get documentHasSelection() {
926 return this.documentHasSelection_;
929 /** @return {bool} Whether selection only should be printed. */
930 get selectionOnly() {
931 return this.selectionOnly_;
934 /** @return {?string} ID of the system default destination. */
935 get systemDefaultDestinationId() {
936 return this.systemDefaultDestinationId_;
939 /** @return {?string} Serialized app state. */
940 get serializedAppStateStr() {
941 return this.serializedAppStateStr_;
945 // Export
946 return {
947 NativeInitialSettings: NativeInitialSettings,
948 NativeLayer: NativeLayer