Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / native_layer.js
blob8c72a538284ac3e98bdaeda005022f06bf5b9754
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.failedToGetExtensionPrinterCapabilities =
40 this.onFailedToGetExtensionPrinterCapabilities_.bind(this);
41 global.reloadPrintersList = this.onReloadPrintersList_.bind(this);
42 global.printToCloud = this.onPrintToCloud_.bind(this);
43 global.fileSelectionCancelled =
44 this.onFileSelectionCancelled_.bind(this);
45 global.fileSelectionCompleted =
46 this.onFileSelectionCompleted_.bind(this);
47 global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this);
48 global.invalidPrinterSettings =
49 this.onInvalidPrinterSettings_.bind(this);
50 global.onDidGetDefaultPageLayout =
51 this.onDidGetDefaultPageLayout_.bind(this);
52 global.onDidGetPreviewPageCount =
53 this.onDidGetPreviewPageCount_.bind(this);
54 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this);
55 global.updatePrintPreview = this.onUpdatePrintPreview_.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.onExtensionPrintersAdded =
63 this.onExtensionPrintersAdded_.bind(this);
64 global.onExtensionCapabilitiesSet =
65 this.onExtensionCapabilitiesSet_.bind(this);
66 global.onEnableManipulateSettingsForTest =
67 this.onEnableManipulateSettingsForTest_.bind(this);
68 global.printPresetOptionsFromDocument =
69 this.onPrintPresetOptionsFromDocument_.bind(this);
72 /**
73 * Event types dispatched from the Chromium native layer.
74 * @enum {string}
75 * @const
77 NativeLayer.EventType = {
78 ACCESS_TOKEN_READY: 'print_preview.NativeLayer.ACCESS_TOKEN_READY',
79 CAPABILITIES_SET: 'print_preview.NativeLayer.CAPABILITIES_SET',
80 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
81 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
82 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING',
83 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
84 FILE_SELECTION_COMPLETE:
85 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
86 GET_CAPABILITIES_FAIL: 'print_preview.NativeLayer.GET_CAPABILITIES_FAIL',
87 INITIAL_SETTINGS_SET: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET',
88 LOCAL_DESTINATIONS_SET: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET',
89 MANIPULATE_SETTINGS_FOR_TEST:
90 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
91 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY',
92 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
93 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
94 PREVIEW_GENERATION_DONE:
95 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
96 PREVIEW_GENERATION_FAIL:
97 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
98 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
99 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID',
100 PRIVET_PRINTER_CHANGED: 'print_preview.NativeLayer.PRIVET_PRINTER_CHANGED',
101 PRIVET_CAPABILITIES_SET:
102 'print_preview.NativeLayer.PRIVET_CAPABILITIES_SET',
103 PRIVET_PRINT_FAILED: 'print_preview.NativeLayer.PRIVET_PRINT_FAILED',
104 EXTENSION_PRINTERS_ADDED:
105 'print_preview.NativeLayer.EXTENSION_PRINTERS_ADDED',
106 EXTENSION_CAPABILITIES_SET:
107 'print_preview.NativeLayer.EXTENSION_CAPABILITIES_SET',
108 PRINT_PRESET_OPTIONS: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS',
112 * Constant values matching printing::DuplexMode enum.
113 * @enum {number}
115 NativeLayer.DuplexMode = {
116 SIMPLEX: 0,
117 LONG_EDGE: 1,
118 UNKNOWN_DUPLEX_MODE: -1
122 * Enumeration of color modes used by Chromium.
123 * @enum {number}
124 * @private
126 NativeLayer.ColorMode_ = {
127 GRAY: 1,
128 COLOR: 2
132 * Version of the serialized state of the print preview.
133 * @type {number}
134 * @const
135 * @private
137 NativeLayer.SERIALIZED_STATE_VERSION_ = 1;
139 NativeLayer.prototype = {
140 __proto__: cr.EventTarget.prototype,
143 * Requests access token for cloud print requests.
144 * @param {string} authType type of access token.
146 startGetAccessToken: function(authType) {
147 chrome.send('getAccessToken', [authType]);
150 /** Gets the initial settings to initialize the print preview with. */
151 startGetInitialSettings: function() {
152 chrome.send('getInitialSettings');
156 * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
157 * event will be dispatched in response.
159 startGetLocalDestinations: function() {
160 chrome.send('getPrinters');
164 * Requests the network's privet print destinations. A number of
165 * PRIVET_PRINTER_CHANGED events will be fired in response, followed by a
166 * PRIVET_SEARCH_ENDED.
168 startGetPrivetDestinations: function() {
169 chrome.send('getPrivetPrinters');
173 * Requests that the privet print stack stop searching for privet print
174 * destinations.
176 stopGetPrivetDestinations: function() {
177 chrome.send('stopGetPrivetPrinters');
181 * Requests the privet destination's printing capabilities. A
182 * PRIVET_CAPABILITIES_SET event will be dispatched in response.
183 * @param {string} destinationId ID of the destination.
185 startGetPrivetDestinationCapabilities: function(destinationId) {
186 chrome.send('getPrivetPrinterCapabilities', [destinationId]);
190 * Requests that extension system dispatches an event requesting the list of
191 * extension managed printers.
193 startGetExtensionDestinations: function() {
194 chrome.send('getExtensionPrinters');
198 * Requests an extension destination's printing capabilities. A
199 * EXTENSION_CAPABILITIES_SET event will be dispatched in response.
200 * @param {string} destinationId The ID of the destination whose
201 * capabilities are requested.
203 startGetExtensionDestinationCapabilities: function(destinationId) {
204 chrome.send('getExtensionPrinterCapabilities', [destinationId]);
208 * Requests the destination's printing capabilities. A CAPABILITIES_SET
209 * event will be dispatched in response.
210 * @param {string} destinationId ID of the destination.
212 startGetLocalDestinationCapabilities: function(destinationId) {
213 chrome.send('getPrinterCapabilities', [destinationId]);
217 * @param {!print_preview.Destination} destination Destination to print to.
218 * @param {!print_preview.ticket_items.Color} color Color ticket item.
219 * @return {number} Native layer color model.
220 * @private
222 getNativeColorModel_: function(destination, color) {
223 // For non-local printers native color model is ignored anyway.
224 var option = destination.isLocal ? color.getSelectedOption() : null;
225 var nativeColorModel = parseInt(option ? option.vendor_id : null, 10);
226 if (isNaN(nativeColorModel)) {
227 return color.getValue() ?
228 NativeLayer.ColorMode_.COLOR : NativeLayer.ColorMode_.GRAY;
230 return nativeColorModel;
234 * Requests that a preview be generated. The following events may be
235 * dispatched in response:
236 * - PAGE_COUNT_READY
237 * - PAGE_LAYOUT_READY
238 * - PAGE_PREVIEW_READY
239 * - PREVIEW_GENERATION_DONE
240 * - PREVIEW_GENERATION_FAIL
241 * @param {print_preview.Destination} destination Destination to print to.
242 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
243 * state of the print ticket.
244 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
245 * @param {number} requestId ID of the preview request.
247 startGetPreview: function(
248 destination, printTicketStore, documentInfo, requestId) {
249 assert(printTicketStore.isTicketValidForPreview(),
250 'Trying to generate preview when ticket is not valid');
252 var ticket = {
253 'pageRange': printTicketStore.pageRange.getDocumentPageRanges(),
254 'mediaSize': printTicketStore.mediaSize.getValue(),
255 'landscape': printTicketStore.landscape.getValue(),
256 'color': this.getNativeColorModel_(destination, printTicketStore.color),
257 'headerFooterEnabled': printTicketStore.headerFooter.getValue(),
258 'marginsType': printTicketStore.marginsType.getValue(),
259 'isFirstRequest': requestId == 0,
260 'requestID': requestId,
261 'previewModifiable': documentInfo.isModifiable,
262 'printToPDF':
263 destination != null &&
264 destination.id ==
265 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
266 'printWithCloudPrint': destination != null && !destination.isLocal,
267 'printWithPrivet': destination != null && destination.isPrivet,
268 'printWithExtension': destination != null && destination.isExtension,
269 'deviceName': destination == null ? 'foo' : destination.id,
270 'generateDraftData': documentInfo.isModifiable,
271 'fitToPageEnabled': printTicketStore.fitToPage.getValue(),
273 // NOTE: Even though the following fields don't directly relate to the
274 // preview, they still need to be included.
275 'duplex': printTicketStore.duplex.getValue() ?
276 NativeLayer.DuplexMode.LONG_EDGE : NativeLayer.DuplexMode.SIMPLEX,
277 'copies': 1,
278 'collate': true,
279 'shouldPrintBackgrounds': printTicketStore.cssBackground.getValue(),
280 'shouldPrintSelectionOnly': printTicketStore.selectionOnly.getValue()
283 // Set 'cloudPrintID' only if the destination is not local.
284 if (destination && !destination.isLocal) {
285 ticket['cloudPrintID'] = destination.id;
288 if (printTicketStore.marginsType.isCapabilityAvailable() &&
289 printTicketStore.marginsType.getValue() ==
290 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
291 var customMargins = printTicketStore.customMargins.getValue();
292 var orientationEnum =
293 print_preview.ticket_items.CustomMargins.Orientation;
294 ticket['marginsCustom'] = {
295 'marginTop': customMargins.get(orientationEnum.TOP),
296 'marginRight': customMargins.get(orientationEnum.RIGHT),
297 'marginBottom': customMargins.get(orientationEnum.BOTTOM),
298 'marginLeft': customMargins.get(orientationEnum.LEFT)
302 chrome.send(
303 'getPreview',
304 [JSON.stringify(ticket),
305 requestId > 0 ? documentInfo.pageCount : -1,
306 documentInfo.isModifiable]);
310 * Requests that the document be printed.
311 * @param {!print_preview.Destination} destination Destination to print to.
312 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
313 * state of the print ticket.
314 * @param {cloudprint.CloudPrintInterface} cloudPrintInterface Interface
315 * to Google Cloud Print.
316 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
317 * @param {boolean=} opt_isOpenPdfInPreview Whether to open the PDF in the
318 * system's preview application.
319 * @param {boolean=} opt_showSystemDialog Whether to open system dialog for
320 * advanced settings.
322 startPrint: function(destination, printTicketStore, cloudPrintInterface,
323 documentInfo, opt_isOpenPdfInPreview,
324 opt_showSystemDialog) {
325 assert(printTicketStore.isTicketValid(),
326 'Trying to print when ticket is not valid');
328 assert(!opt_showSystemDialog || (cr.isWindows && destination.isLocal),
329 'Implemented for Windows only');
331 var ticket = {
332 'pageRange': printTicketStore.pageRange.getDocumentPageRanges(),
333 'mediaSize': printTicketStore.mediaSize.getValue(),
334 'pageCount': printTicketStore.pageRange.getPageNumberSet().size,
335 'landscape': printTicketStore.landscape.getValue(),
336 'color': this.getNativeColorModel_(destination, printTicketStore.color),
337 'headerFooterEnabled': printTicketStore.headerFooter.getValue(),
338 'marginsType': printTicketStore.marginsType.getValue(),
339 'generateDraftData': true, // TODO(rltoscano): What should this be?
340 'duplex': printTicketStore.duplex.getValue() ?
341 NativeLayer.DuplexMode.LONG_EDGE : NativeLayer.DuplexMode.SIMPLEX,
342 'copies': printTicketStore.copies.getValueAsNumber(),
343 'collate': printTicketStore.collate.getValue(),
344 'shouldPrintBackgrounds': printTicketStore.cssBackground.getValue(),
345 'shouldPrintSelectionOnly': printTicketStore.selectionOnly.getValue(),
346 'previewModifiable': documentInfo.isModifiable,
347 'printToPDF': destination.id ==
348 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
349 'printWithCloudPrint': !destination.isLocal,
350 'printWithPrivet': destination.isPrivet,
351 'printWithExtension': destination.isExtension,
352 'deviceName': destination.id,
353 'isFirstRequest': false,
354 'requestID': -1,
355 'fitToPageEnabled': printTicketStore.fitToPage.getValue(),
356 'pageWidth': documentInfo.pageSize.width,
357 'pageHeight': documentInfo.pageSize.height,
358 'showSystemDialog': opt_showSystemDialog
361 if (!destination.isLocal) {
362 // We can't set cloudPrintID if the destination is "Print with Cloud
363 // Print" because the native system will try to print to Google Cloud
364 // Print with this ID instead of opening a Google Cloud Print dialog.
365 ticket['cloudPrintID'] = destination.id;
368 if (printTicketStore.marginsType.isCapabilityAvailable() &&
369 printTicketStore.marginsType.isValueEqual(
370 print_preview.ticket_items.MarginsType.Value.CUSTOM)) {
371 var customMargins = printTicketStore.customMargins.getValue();
372 var orientationEnum =
373 print_preview.ticket_items.CustomMargins.Orientation;
374 ticket['marginsCustom'] = {
375 'marginTop': customMargins.get(orientationEnum.TOP),
376 'marginRight': customMargins.get(orientationEnum.RIGHT),
377 'marginBottom': customMargins.get(orientationEnum.BOTTOM),
378 'marginLeft': customMargins.get(orientationEnum.LEFT)
382 if (destination.isPrivet || destination.isExtension) {
383 ticket['ticket'] = printTicketStore.createPrintTicket(destination);
384 ticket['capabilities'] = JSON.stringify(destination.capabilities);
387 if (opt_isOpenPdfInPreview) {
388 ticket['OpenPDFInPreview'] = true;
391 chrome.send('print', [JSON.stringify(ticket)]);
394 /** Requests that the current pending print request be cancelled. */
395 startCancelPendingPrint: function() {
396 chrome.send('cancelPendingPrintRequest');
399 /** Shows the system's native printing dialog. */
400 startShowSystemDialog: function() {
401 assert(!cr.isWindows);
402 chrome.send('showSystemDialog');
405 /** Closes the print preview dialog. */
406 startCloseDialog: function() {
407 chrome.send('closePrintPreviewDialog');
408 chrome.send('dialogClose');
411 /** Hide the print preview dialog and allow the native layer to close it. */
412 startHideDialog: function() {
413 chrome.send('hidePreview');
417 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event
418 * will be dispatched in response.
419 * @param {boolean} addAccount Whether to open an 'add a new account' or
420 * default sign in page.
422 startCloudPrintSignIn: function(addAccount) {
423 chrome.send('signIn', [addAccount]);
426 /** Navigates the user to the system printer settings interface. */
427 startManageLocalDestinations: function() {
428 chrome.send('manageLocalPrinters');
432 * Navigates the user to the Google Cloud Print management page.
433 * @param {?string} user Email address of the user to open the management
434 * page for (user must be currently logged in, indeed) or {@code null}
435 * to open this page for the primary user.
437 startManageCloudDestinations: function(user) {
438 chrome.send('manageCloudPrinters', [user || '']);
441 /** Forces browser to open a new tab with the given URL address. */
442 startForceOpenNewTab: function(url) {
443 chrome.send('forceOpenNewTab', [url]);
447 * @param {!Object} initialSettings Object containing all initial settings.
449 onSetInitialSettings_: function(initialSettings) {
450 var numberFormatSymbols =
451 print_preview.MeasurementSystem.parseNumberFormat(
452 initialSettings['numberFormat']);
453 var unitType = print_preview.MeasurementSystem.UnitType.IMPERIAL;
454 if (initialSettings['measurementSystem'] != null) {
455 unitType = initialSettings['measurementSystem'];
458 var nativeInitialSettings = new print_preview.NativeInitialSettings(
459 initialSettings['printAutomaticallyInKioskMode'] || false,
460 initialSettings['appKioskMode'] || false,
461 initialSettings['hidePrintWithSystemDialogLink'] || false,
462 numberFormatSymbols[0] || ',',
463 numberFormatSymbols[1] || '.',
464 unitType,
465 initialSettings['previewModifiable'] || false,
466 initialSettings['initiatorTitle'] || '',
467 initialSettings['documentHasSelection'] || false,
468 initialSettings['shouldPrintSelectionOnly'] || false,
469 initialSettings['printerName'] || null,
470 initialSettings['appState'] || null);
472 var initialSettingsSetEvent = new Event(
473 NativeLayer.EventType.INITIAL_SETTINGS_SET);
474 initialSettingsSetEvent.initialSettings = nativeInitialSettings;
475 this.dispatchEvent(initialSettingsSetEvent);
479 * Turn on the integration of Cloud Print.
480 * @param {{cloudPrintURL: string, appKioskMode: string}} settings
481 * cloudPrintUrl: The URL to use for cloud print servers.
482 * @private
484 onSetUseCloudPrint_: function(settings) {
485 var cloudPrintEnableEvent = new Event(
486 NativeLayer.EventType.CLOUD_PRINT_ENABLE);
487 cloudPrintEnableEvent.baseCloudPrintUrl = settings['cloudPrintUrl'] || '';
488 cloudPrintEnableEvent.appKioskMode = settings['appKioskMode'] || false;
489 this.dispatchEvent(cloudPrintEnableEvent);
493 * Updates the print preview with local printers.
494 * Called from PrintPreviewHandler::SetupPrinterList().
495 * @param {Array} printers Array of printer info objects.
496 * @private
498 onSetPrinters_: function(printers) {
499 var localDestsSetEvent = new Event(
500 NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
501 localDestsSetEvent.destinationInfos = printers;
502 this.dispatchEvent(localDestsSetEvent);
506 * Called when native layer gets settings information for a requested local
507 * destination.
508 * @param {Object} settingsInfo printer setting information.
509 * @private
511 onUpdateWithPrinterCapabilities_: function(settingsInfo) {
512 var capsSetEvent = new Event(NativeLayer.EventType.CAPABILITIES_SET);
513 capsSetEvent.settingsInfo = settingsInfo;
514 this.dispatchEvent(capsSetEvent);
518 * Called when native layer gets settings information for a requested local
519 * destination.
520 * @param {string} destinationId Printer affected by error.
521 * @private
523 onFailedToGetPrinterCapabilities_: function(destinationId) {
524 var getCapsFailEvent = new Event(
525 NativeLayer.EventType.GET_CAPABILITIES_FAIL);
526 getCapsFailEvent.destinationId = destinationId;
527 getCapsFailEvent.destinationOrigin =
528 print_preview.Destination.Origin.LOCAL;
529 this.dispatchEvent(getCapsFailEvent);
533 * Called when native layer gets settings information for a requested privet
534 * destination.
535 * @param {string} destinationId Printer affected by error.
536 * @private
538 onFailedToGetPrivetPrinterCapabilities_: function(destinationId) {
539 var getCapsFailEvent = new Event(
540 NativeLayer.EventType.GET_CAPABILITIES_FAIL);
541 getCapsFailEvent.destinationId = destinationId;
542 getCapsFailEvent.destinationOrigin =
543 print_preview.Destination.Origin.PRIVET;
544 this.dispatchEvent(getCapsFailEvent);
548 * Called when native layer fails to get settings information for a
549 * requested extension destination.
550 * @param {string} destinationId Printer affected by error.
551 * @private
553 onFailedToGetExtensionPrinterCapabilities_: function(destinationId) {
554 var getCapsFailEvent = new Event(
555 NativeLayer.EventType.GET_CAPABILITIES_FAIL);
556 getCapsFailEvent.destinationId = destinationId;
557 getCapsFailEvent.destinationOrigin =
558 print_preview.Destination.Origin.EXTENSION;
559 this.dispatchEvent(getCapsFailEvent);
562 /** Reloads the printer list. */
563 onReloadPrintersList_: function() {
564 cr.dispatchSimpleEvent(this, NativeLayer.EventType.DESTINATIONS_RELOAD);
568 * Called from the C++ layer.
569 * Take the PDF data handed to us and submit it to the cloud, closing the
570 * print preview dialog once the upload is successful.
571 * @param {string} data Data to send as the print job.
572 * @private
574 onPrintToCloud_: function(data) {
575 var printToCloudEvent = new Event(
576 NativeLayer.EventType.PRINT_TO_CLOUD);
577 printToCloudEvent.data = data;
578 this.dispatchEvent(printToCloudEvent);
582 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
583 * preview dialog regarding the file selection cancel event.
584 * @private
586 onFileSelectionCancelled_: function() {
587 cr.dispatchSimpleEvent(this, NativeLayer.EventType.FILE_SELECTION_CANCEL);
591 * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print
592 * preview dialog regarding the file selection completed event.
593 * @private
595 onFileSelectionCompleted_: function() {
596 // If the file selection is completed and the dialog is not already closed
597 // it means that a pending print to pdf request exists.
598 cr.dispatchSimpleEvent(
599 this, NativeLayer.EventType.FILE_SELECTION_COMPLETE);
603 * Display an error message when print preview fails.
604 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed().
605 * @private
607 onPrintPreviewFailed_: function() {
608 cr.dispatchSimpleEvent(
609 this, NativeLayer.EventType.PREVIEW_GENERATION_FAIL);
613 * Display an error message when encountered invalid printer settings.
614 * Called from PrintPreviewMessageHandler::OnInvalidPrinterSettings().
615 * @private
617 onInvalidPrinterSettings_: function() {
618 cr.dispatchSimpleEvent(this, NativeLayer.EventType.SETTINGS_INVALID);
622 * @param {{contentWidth: number, contentHeight: number, marginLeft: number,
623 * marginRight: number, marginTop: number, marginBottom: number,
624 * printableAreaX: number, printableAreaY: number,
625 * printableAreaWidth: number, printableAreaHeight: number}}
626 * pageLayout Specifies default page layout details in points.
627 * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed
628 * document has a custom page size style.
629 * @private
631 onDidGetDefaultPageLayout_: function(pageLayout, hasCustomPageSizeStyle) {
632 var pageLayoutChangeEvent = new Event(
633 NativeLayer.EventType.PAGE_LAYOUT_READY);
634 pageLayoutChangeEvent.pageLayout = pageLayout;
635 pageLayoutChangeEvent.hasCustomPageSizeStyle = hasCustomPageSizeStyle;
636 this.dispatchEvent(pageLayoutChangeEvent);
640 * Update the page count and check the page range.
641 * Called from PrintPreviewUI::OnDidGetPreviewPageCount().
642 * @param {number} pageCount The number of pages.
643 * @param {number} previewResponseId The preview request id that resulted in
644 * this response.
645 * @private
647 onDidGetPreviewPageCount_: function(pageCount, previewResponseId) {
648 var pageCountChangeEvent = new Event(
649 NativeLayer.EventType.PAGE_COUNT_READY);
650 pageCountChangeEvent.pageCount = pageCount;
651 pageCountChangeEvent.previewResponseId = previewResponseId;
652 this.dispatchEvent(pageCountChangeEvent);
656 * Notification that a print preview page has been rendered.
657 * Check if the settings have changed and request a regeneration if needed.
658 * Called from PrintPreviewUI::OnDidPreviewPage().
659 * @param {number} pageNumber The page number, 0-based.
660 * @param {number} previewUid Preview unique identifier.
661 * @param {number} previewResponseId The preview request id that resulted in
662 * this response.
663 * @private
665 onDidPreviewPage_: function(pageNumber, previewUid, previewResponseId) {
666 var pagePreviewGenEvent = new Event(
667 NativeLayer.EventType.PAGE_PREVIEW_READY);
668 pagePreviewGenEvent.pageIndex = pageNumber;
669 pagePreviewGenEvent.previewUid = previewUid;
670 pagePreviewGenEvent.previewResponseId = previewResponseId;
671 this.dispatchEvent(pagePreviewGenEvent);
675 * Notification that access token is ready.
676 * @param {string} authType Type of access token.
677 * @param {string} accessToken Access token.
678 * @private
680 onDidGetAccessToken_: function(authType, accessToken) {
681 var getAccessTokenEvent = new Event(
682 NativeLayer.EventType.ACCESS_TOKEN_READY);
683 getAccessTokenEvent.authType = authType;
684 getAccessTokenEvent.accessToken = accessToken;
685 this.dispatchEvent(getAccessTokenEvent);
689 * Update the print preview when new preview data is available.
690 * Create the PDF plugin as needed.
691 * Called from PrintPreviewUI::PreviewDataIsAvailable().
692 * @param {number} previewUid Preview unique identifier.
693 * @param {number} previewResponseId The preview request id that resulted in
694 * this response.
695 * @private
697 onUpdatePrintPreview_: function(previewUid, previewResponseId) {
698 var previewGenDoneEvent = new Event(
699 NativeLayer.EventType.PREVIEW_GENERATION_DONE);
700 previewGenDoneEvent.previewUid = previewUid;
701 previewGenDoneEvent.previewResponseId = previewResponseId;
702 this.dispatchEvent(previewGenDoneEvent);
706 * Updates print preset options from source PDF document.
707 * Called from PrintPreviewUI::OnSetOptionsFromDocument().
708 * @param {{disableScaling: boolean, copies: number,
709 * duplex: number}} options Specifies
710 * printing options according to source document presets.
711 * @private
713 onPrintPresetOptionsFromDocument_: function(options) {
714 var printPresetOptionsEvent = new Event(
715 NativeLayer.EventType.PRINT_PRESET_OPTIONS);
716 printPresetOptionsEvent.optionsFromDocument = options;
717 this.dispatchEvent(printPresetOptionsEvent);
721 * Simulates a user click on the print preview dialog cancel button. Used
722 * only for testing.
723 * @private
725 autoCancelForTesting_: function() {
726 var properties = {view: window, bubbles: true, cancelable: true};
727 var click = new MouseEvent('click', properties);
728 document.querySelector('#print-header .cancel').dispatchEvent(click);
732 * @param {{serviceName: string, name: string}} printer Specifies
733 * information about the printer that was added.
734 * @private
736 onPrivetPrinterChanged_: function(printer) {
737 var privetPrinterChangedEvent =
738 new Event(NativeLayer.EventType.PRIVET_PRINTER_CHANGED);
739 privetPrinterChangedEvent.printer = printer;
740 this.dispatchEvent(privetPrinterChangedEvent);
744 * @param {Object} printer Specifies information about the printer that was
745 * added.
746 * @private
748 onPrivetCapabilitiesSet_: function(printer, capabilities) {
749 var privetCapabilitiesSetEvent =
750 new Event(NativeLayer.EventType.PRIVET_CAPABILITIES_SET);
751 privetCapabilitiesSetEvent.printer = printer;
752 privetCapabilitiesSetEvent.capabilities = capabilities;
753 this.dispatchEvent(privetCapabilitiesSetEvent);
757 * @param {string} http_error The HTTP response code or -1 if not an HTTP
758 * error.
759 * @private
761 onPrivetPrintFailed_: function(http_error) {
762 var privetPrintFailedEvent =
763 new Event(NativeLayer.EventType.PRIVET_PRINT_FAILED);
764 privetPrintFailedEvent.httpError = http_error;
765 this.dispatchEvent(privetPrintFailedEvent);
769 * @param {Array<!{extensionId: string,
770 * extensionName: string,
771 * id: string,
772 * name: string,
773 * description: (string|undefined)}>} printers The list
774 * containing information about printers added by an extension.
775 * @param {boolean} done Whether this is the final list of extension
776 * managed printers.
778 onExtensionPrintersAdded_: function(printers, done) {
779 var event = new Event(NativeLayer.EventType.EXTENSION_PRINTERS_ADDED);
780 event.printers = printers;
781 event.done = done;
782 this.dispatchEvent(event);
786 * Called when an extension responds to a request for an extension printer
787 * capabilities.
788 * @param {string} printerId The printer's ID.
789 * @param {!Object} capabilities The reported printer capabilities.
791 onExtensionCapabilitiesSet_: function(printerId,
792 capabilities) {
793 var event = new Event(NativeLayer.EventType.EXTENSION_CAPABILITIES_SET);
794 event.printerId = printerId;
795 event.capabilities = capabilities;
796 this.dispatchEvent(event);
800 * Allows for onManipulateSettings to be called
801 * from the native layer.
802 * @private
804 onEnableManipulateSettingsForTest_: function() {
805 global.onManipulateSettingsForTest =
806 this.onManipulateSettingsForTest_.bind(this);
810 * Dispatches an event to print_preview.js to change
811 * a particular setting for print preview.
812 * @param {!print_preview.PreviewSettings} settings Object containing the
813 * value to be changed and that value should be set to.
814 * @private
816 onManipulateSettingsForTest_: function(settings) {
817 var manipulateSettingsEvent =
818 new Event(NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST);
819 manipulateSettingsEvent.settings = settings;
820 this.dispatchEvent(manipulateSettingsEvent);
824 * Sends a message to the test, letting it know that an
825 * option has been set to a particular value and that the change has
826 * finished modifying the preview area.
828 previewReadyForTest: function() {
829 if (global.onManipulateSettingsForTest)
830 chrome.send('UILoadedForTest');
834 * Notifies the test that the option it tried to change
835 * had not been changed successfully.
837 previewFailedForTest: function() {
838 if (global.onManipulateSettingsForTest)
839 chrome.send('UIFailedLoadingForTest');
844 * Initial settings retrieved from the native layer.
845 * @param {boolean} isInKioskAutoPrintMode Whether the print preview should be
846 * in auto-print mode.
847 * @param {boolean} isInAppKioskMode Whether the print preview is in App Kiosk
848 * mode.
849 * @param {string} thousandsDelimeter Character delimeter of thousands digits.
850 * @param {string} decimalDelimeter Character delimeter of the decimal point.
851 * @param {!print_preview.MeasurementSystem.UnitType} unitType Unit type of
852 * local machine's measurement system.
853 * @param {boolean} isDocumentModifiable Whether the document to print is
854 * modifiable.
855 * @param {string} documentTitle Title of the document.
856 * @param {boolean} documentHasSelection Whether the document has selected
857 * content.
858 * @param {boolean} selectionOnly Whether only selected content should be
859 * printed.
860 * @param {?string} systemDefaultDestinationId ID of the system default
861 * destination.
862 * @param {?string} serializedAppStateStr Serialized app state.
863 * @constructor
865 function NativeInitialSettings(
866 isInKioskAutoPrintMode,
867 isInAppKioskMode,
868 hidePrintWithSystemDialogLink,
869 thousandsDelimeter,
870 decimalDelimeter,
871 unitType,
872 isDocumentModifiable,
873 documentTitle,
874 documentHasSelection,
875 selectionOnly,
876 systemDefaultDestinationId,
877 serializedAppStateStr) {
880 * Whether the print preview should be in auto-print mode.
881 * @type {boolean}
882 * @private
884 this.isInKioskAutoPrintMode_ = isInKioskAutoPrintMode;
887 * Whether the print preview should switch to App Kiosk mode.
888 * @type {boolean}
889 * @private
891 this.isInAppKioskMode_ = isInAppKioskMode;
894 * Whether we should hide the link which shows the system print dialog.
895 * @type {boolean}
896 * @private
898 this.hidePrintWithSystemDialogLink_ = hidePrintWithSystemDialogLink;
901 * Character delimeter of thousands digits.
902 * @type {string}
903 * @private
905 this.thousandsDelimeter_ = thousandsDelimeter;
908 * Character delimeter of the decimal point.
909 * @type {string}
910 * @private
912 this.decimalDelimeter_ = decimalDelimeter;
915 * Unit type of local machine's measurement system.
916 * @type {string}
917 * @private
919 this.unitType_ = unitType;
922 * Whether the document to print is modifiable.
923 * @type {boolean}
924 * @private
926 this.isDocumentModifiable_ = isDocumentModifiable;
929 * Title of the document.
930 * @type {string}
931 * @private
933 this.documentTitle_ = documentTitle;
936 * Whether the document has selection.
937 * @type {string}
938 * @private
940 this.documentHasSelection_ = documentHasSelection;
943 * Whether selection only should be printed.
944 * @type {string}
945 * @private
947 this.selectionOnly_ = selectionOnly;
950 * ID of the system default destination.
951 * @type {?string}
952 * @private
954 this.systemDefaultDestinationId_ = systemDefaultDestinationId;
957 * Serialized app state.
958 * @type {?string}
959 * @private
961 this.serializedAppStateStr_ = serializedAppStateStr;
964 NativeInitialSettings.prototype = {
966 * @return {boolean} Whether the print preview should be in auto-print mode.
968 get isInKioskAutoPrintMode() {
969 return this.isInKioskAutoPrintMode_;
973 * @return {boolean} Whether the print preview should switch to App Kiosk
974 * mode.
976 get isInAppKioskMode() {
977 return this.isInAppKioskMode_;
981 * @return {boolean} Whether we should hide the link which shows the
982 system print dialog.
984 get hidePrintWithSystemDialogLink() {
985 return this.hidePrintWithSystemDialogLink_;
988 /** @return {string} Character delimeter of thousands digits. */
989 get thousandsDelimeter() {
990 return this.thousandsDelimeter_;
993 /** @return {string} Character delimeter of the decimal point. */
994 get decimalDelimeter() {
995 return this.decimalDelimeter_;
999 * @return {!print_preview.MeasurementSystem.UnitType} Unit type of local
1000 * machine's measurement system.
1002 get unitType() {
1003 return this.unitType_;
1006 /** @return {boolean} Whether the document to print is modifiable. */
1007 get isDocumentModifiable() {
1008 return this.isDocumentModifiable_;
1011 /** @return {string} Document title. */
1012 get documentTitle() {
1013 return this.documentTitle_;
1016 /** @return {boolean} Whether the document has selection. */
1017 get documentHasSelection() {
1018 return this.documentHasSelection_;
1021 /** @return {boolean} Whether selection only should be printed. */
1022 get selectionOnly() {
1023 return this.selectionOnly_;
1026 /** @return {?string} ID of the system default destination. */
1027 get systemDefaultDestinationId() {
1028 return this.systemDefaultDestinationId_;
1031 /** @return {?string} Serialized app state. */
1032 get serializedAppStateStr() {
1033 return this.serializedAppStateStr_;
1037 // Export
1038 return {
1039 NativeInitialSettings: NativeInitialSettings,
1040 NativeLayer: NativeLayer