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');
8 * @typedef {{selectSaveAsPdfDestination: boolean,
9 * layoutSettings.portrait: boolean,
11 * headersAndFooters: boolean,
12 * backgroundColorsAndImages: boolean,
14 * @see chrome/browser/printing/print_preview_pdf_generated_browsertest.cc
16 print_preview
.PreviewSettings
;
18 cr
.define('print_preview', function() {
22 * An interface to the native Chromium printing system layer.
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);
70 global
.detectDistillablePage
=
71 this.detectDistillablePage_
.bind(this);
72 global
.onProvisionalPrinterResolved
=
73 this.onProvisionalDestinationResolved_
.bind(this);
74 global
.failedToResolveProvisionalPrinter
=
75 this.failedToResolveProvisionalDestination_
.bind(this);
79 * Event types dispatched from the Chromium native layer.
83 NativeLayer
.EventType
= {
84 ACCESS_TOKEN_READY
: 'print_preview.NativeLayer.ACCESS_TOKEN_READY',
85 ALLOW_DISTILL_PAGE
: 'print_preview.NativeLayer.ALLOW_DISTILL_PAGE',
86 CAPABILITIES_SET
: 'print_preview.NativeLayer.CAPABILITIES_SET',
87 CLOUD_PRINT_ENABLE
: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
88 DESTINATIONS_RELOAD
: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
89 DISABLE_SCALING
: 'print_preview.NativeLayer.DISABLE_SCALING',
90 FILE_SELECTION_CANCEL
: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
91 FILE_SELECTION_COMPLETE
:
92 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
93 GET_CAPABILITIES_FAIL
: 'print_preview.NativeLayer.GET_CAPABILITIES_FAIL',
94 INITIAL_SETTINGS_SET
: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET',
95 LOCAL_DESTINATIONS_SET
: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET',
96 MANIPULATE_SETTINGS_FOR_TEST
:
97 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
98 PAGE_COUNT_READY
: 'print_preview.NativeLayer.PAGE_COUNT_READY',
99 PAGE_LAYOUT_READY
: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
100 PAGE_PREVIEW_READY
: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
101 PREVIEW_GENERATION_DONE
:
102 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
103 PREVIEW_GENERATION_FAIL
:
104 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
105 PRINT_TO_CLOUD
: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
106 SETTINGS_INVALID
: 'print_preview.NativeLayer.SETTINGS_INVALID',
107 PRIVET_PRINTER_CHANGED
: 'print_preview.NativeLayer.PRIVET_PRINTER_CHANGED',
108 PRIVET_CAPABILITIES_SET
:
109 'print_preview.NativeLayer.PRIVET_CAPABILITIES_SET',
110 PRIVET_PRINT_FAILED
: 'print_preview.NativeLayer.PRIVET_PRINT_FAILED',
111 EXTENSION_PRINTERS_ADDED
:
112 'print_preview.NativeLayer.EXTENSION_PRINTERS_ADDED',
113 EXTENSION_CAPABILITIES_SET
:
114 'print_preview.NativeLayer.EXTENSION_CAPABILITIES_SET',
115 PRINT_PRESET_OPTIONS
: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS',
116 PROVISIONAL_DESTINATION_RESOLVED
:
117 'print_preview.NativeLayer.PROVISIONAL_DESTINATION_RESOLVED'
121 * Constant values matching printing::DuplexMode enum.
124 NativeLayer
.DuplexMode
= {
127 UNKNOWN_DUPLEX_MODE
: -1
131 * Enumeration of color modes used by Chromium.
135 NativeLayer
.ColorMode_
= {
141 * Version of the serialized state of the print preview.
146 NativeLayer
.SERIALIZED_STATE_VERSION_
= 1;
148 NativeLayer
.prototype = {
149 __proto__
: cr
.EventTarget
.prototype,
152 * Requests access token for cloud print requests.
153 * @param {string} authType type of access token.
155 startGetAccessToken: function(authType
) {
156 chrome
.send('getAccessToken', [authType
]);
159 /** Gets the initial settings to initialize the print preview with. */
160 startGetInitialSettings: function() {
161 chrome
.send('getInitialSettings');
165 * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
166 * event will be dispatched in response.
168 startGetLocalDestinations: function() {
169 chrome
.send('getPrinters');
173 * Requests the network's privet print destinations. A number of
174 * PRIVET_PRINTER_CHANGED events will be fired in response, followed by a
175 * PRIVET_SEARCH_ENDED.
177 startGetPrivetDestinations: function() {
178 chrome
.send('getPrivetPrinters');
182 * Requests that the privet print stack stop searching for privet print
185 stopGetPrivetDestinations: function() {
186 chrome
.send('stopGetPrivetPrinters');
190 * Requests the privet destination's printing capabilities. A
191 * PRIVET_CAPABILITIES_SET event will be dispatched in response.
192 * @param {string} destinationId ID of the destination.
194 startGetPrivetDestinationCapabilities: function(destinationId
) {
195 chrome
.send('getPrivetPrinterCapabilities', [destinationId
]);
199 * Requests that extension system dispatches an event requesting the list of
200 * extension managed printers.
202 startGetExtensionDestinations: function() {
203 chrome
.send('getExtensionPrinters');
207 * Requests an extension destination's printing capabilities. A
208 * EXTENSION_CAPABILITIES_SET event will be dispatched in response.
209 * @param {string} destinationId The ID of the destination whose
210 * capabilities are requested.
212 startGetExtensionDestinationCapabilities: function(destinationId
) {
213 chrome
.send('getExtensionPrinterCapabilities', [destinationId
]);
217 * Requests the destination's printing capabilities. A CAPABILITIES_SET
218 * event will be dispatched in response.
219 * @param {string} destinationId ID of the destination.
221 startGetLocalDestinationCapabilities: function(destinationId
) {
222 chrome
.send('getPrinterCapabilities', [destinationId
]);
226 * Requests Chrome to resolve provisional extension destination by granting
227 * the provider extension access to the printer. Chrome will respond with
228 * the resolved destination properties by calling
229 * {@code onProvisionalPrinterResolved}, or in case of an error
230 * {@code failedToResolveProvisionalPrinter}
231 * @param {string} provisionalDestinationId
233 grantExtensionPrinterAccess: function(provisionalDestinationId
) {
234 chrome
.send('grantExtensionPrinterAccess', [provisionalDestinationId
]);
238 * @param {!print_preview.Destination} destination Destination to print to.
239 * @param {!print_preview.ticket_items.Color} color Color ticket item.
240 * @return {number} Native layer color model.
243 getNativeColorModel_: function(destination
, color
) {
244 // For non-local printers native color model is ignored anyway.
245 var option
= destination
.isLocal
? color
.getSelectedOption() : null;
246 var nativeColorModel
= parseInt(option
? option
.vendor_id
: null, 10);
247 if (isNaN(nativeColorModel
)) {
248 return color
.getValue() ?
249 NativeLayer
.ColorMode_
.COLOR
: NativeLayer
.ColorMode_
.GRAY
;
251 return nativeColorModel
;
255 * Requests that a preview be generated. The following events may be
256 * dispatched in response:
258 * - PAGE_LAYOUT_READY
259 * - PAGE_PREVIEW_READY
260 * - PREVIEW_GENERATION_DONE
261 * - PREVIEW_GENERATION_FAIL
262 * @param {print_preview.Destination} destination Destination to print to.
263 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
264 * state of the print ticket.
265 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
266 * @param {number} requestId ID of the preview request.
268 startGetPreview: function(
269 destination
, printTicketStore
, documentInfo
, requestId
) {
270 assert(printTicketStore
.isTicketValidForPreview(),
271 'Trying to generate preview when ticket is not valid');
274 'pageRange': printTicketStore
.pageRange
.getDocumentPageRanges(),
275 'mediaSize': printTicketStore
.mediaSize
.getValue(),
276 'landscape': printTicketStore
.landscape
.getValue(),
277 'color': this.getNativeColorModel_(destination
, printTicketStore
.color
),
278 'headerFooterEnabled': printTicketStore
.headerFooter
.getValue(),
279 'distillPage': printTicketStore
.distillPage
.getValue(),
280 'marginsType': printTicketStore
.marginsType
.getValue(),
281 'isFirstRequest': requestId
== 0,
282 'requestID': requestId
,
283 'previewModifiable': documentInfo
.isModifiable
,
285 destination
!= null &&
287 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
,
288 'printWithCloudPrint': destination
!= null && !destination
.isLocal
,
289 'printWithPrivet': destination
!= null && destination
.isPrivet
,
290 'printWithExtension': destination
!= null && destination
.isExtension
,
291 'deviceName': destination
== null ? 'foo' : destination
.id
,
292 'generateDraftData': documentInfo
.isModifiable
,
293 'fitToPageEnabled': printTicketStore
.fitToPage
.getValue(),
295 // NOTE: Even though the following fields don't directly relate to the
296 // preview, they still need to be included.
297 'duplex': printTicketStore
.duplex
.getValue() ?
298 NativeLayer
.DuplexMode
.LONG_EDGE
: NativeLayer
.DuplexMode
.SIMPLEX
,
301 'shouldPrintBackgrounds': printTicketStore
.cssBackground
.getValue(),
302 'shouldPrintSelectionOnly': printTicketStore
.selectionOnly
.getValue()
305 // Set 'cloudPrintID' only if the destination is not local.
306 if (destination
&& !destination
.isLocal
) {
307 ticket
['cloudPrintID'] = destination
.id
;
310 if (printTicketStore
.marginsType
.isCapabilityAvailable() &&
311 printTicketStore
.marginsType
.getValue() ==
312 print_preview
.ticket_items
.MarginsType
.Value
.CUSTOM
) {
313 var customMargins
= printTicketStore
.customMargins
.getValue();
314 var orientationEnum
=
315 print_preview
.ticket_items
.CustomMargins
.Orientation
;
316 ticket
['marginsCustom'] = {
317 'marginTop': customMargins
.get(orientationEnum
.TOP
),
318 'marginRight': customMargins
.get(orientationEnum
.RIGHT
),
319 'marginBottom': customMargins
.get(orientationEnum
.BOTTOM
),
320 'marginLeft': customMargins
.get(orientationEnum
.LEFT
)
326 [JSON
.stringify(ticket
),
327 requestId
> 0 ? documentInfo
.pageCount
: -1,
328 documentInfo
.isModifiable
]);
332 * Requests that the document be printed.
333 * @param {!print_preview.Destination} destination Destination to print to.
334 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
335 * state of the print ticket.
336 * @param {cloudprint.CloudPrintInterface} cloudPrintInterface Interface
337 * to Google Cloud Print.
338 * @param {!print_preview.DocumentInfo} documentInfo Document data model.
339 * @param {boolean=} opt_isOpenPdfInPreview Whether to open the PDF in the
340 * system's preview application.
341 * @param {boolean=} opt_showSystemDialog Whether to open system dialog for
344 startPrint: function(destination
, printTicketStore
, cloudPrintInterface
,
345 documentInfo
, opt_isOpenPdfInPreview
,
346 opt_showSystemDialog
) {
347 assert(printTicketStore
.isTicketValid(),
348 'Trying to print when ticket is not valid');
350 assert(!opt_showSystemDialog
|| (cr
.isWindows
&& destination
.isLocal
),
351 'Implemented for Windows only');
354 'pageRange': printTicketStore
.pageRange
.getDocumentPageRanges(),
355 'mediaSize': printTicketStore
.mediaSize
.getValue(),
356 'pageCount': printTicketStore
.pageRange
.getPageNumberSet().size
,
357 'landscape': printTicketStore
.landscape
.getValue(),
358 'color': this.getNativeColorModel_(destination
, printTicketStore
.color
),
359 'headerFooterEnabled': printTicketStore
.headerFooter
.getValue(),
360 'marginsType': printTicketStore
.marginsType
.getValue(),
361 'generateDraftData': true, // TODO(rltoscano): What should this be?
362 'duplex': printTicketStore
.duplex
.getValue() ?
363 NativeLayer
.DuplexMode
.LONG_EDGE
: NativeLayer
.DuplexMode
.SIMPLEX
,
364 'copies': printTicketStore
.copies
.getValueAsNumber(),
365 'collate': printTicketStore
.collate
.getValue(),
366 'shouldPrintBackgrounds': printTicketStore
.cssBackground
.getValue(),
367 'shouldPrintSelectionOnly': printTicketStore
.selectionOnly
.getValue(),
368 'previewModifiable': documentInfo
.isModifiable
,
369 'printToPDF': destination
.id
==
370 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
,
371 'printWithCloudPrint': !destination
.isLocal
,
372 'printWithPrivet': destination
.isPrivet
,
373 'printWithExtension': destination
.isExtension
,
374 'deviceName': destination
.id
,
375 'isFirstRequest': false,
377 'fitToPageEnabled': printTicketStore
.fitToPage
.getValue(),
378 'pageWidth': documentInfo
.pageSize
.width
,
379 'pageHeight': documentInfo
.pageSize
.height
,
380 'showSystemDialog': opt_showSystemDialog
383 if (!destination
.isLocal
) {
384 // We can't set cloudPrintID if the destination is "Print with Cloud
385 // Print" because the native system will try to print to Google Cloud
386 // Print with this ID instead of opening a Google Cloud Print dialog.
387 ticket
['cloudPrintID'] = destination
.id
;
390 if (printTicketStore
.marginsType
.isCapabilityAvailable() &&
391 printTicketStore
.marginsType
.isValueEqual(
392 print_preview
.ticket_items
.MarginsType
.Value
.CUSTOM
)) {
393 var customMargins
= printTicketStore
.customMargins
.getValue();
394 var orientationEnum
=
395 print_preview
.ticket_items
.CustomMargins
.Orientation
;
396 ticket
['marginsCustom'] = {
397 'marginTop': customMargins
.get(orientationEnum
.TOP
),
398 'marginRight': customMargins
.get(orientationEnum
.RIGHT
),
399 'marginBottom': customMargins
.get(orientationEnum
.BOTTOM
),
400 'marginLeft': customMargins
.get(orientationEnum
.LEFT
)
404 if (destination
.isPrivet
|| destination
.isExtension
) {
405 ticket
['ticket'] = printTicketStore
.createPrintTicket(destination
);
406 ticket
['capabilities'] = JSON
.stringify(destination
.capabilities
);
409 if (opt_isOpenPdfInPreview
) {
410 ticket
['OpenPDFInPreview'] = true;
413 chrome
.send('print', [JSON
.stringify(ticket
)]);
416 /** Requests that the current pending print request be cancelled. */
417 startCancelPendingPrint: function() {
418 chrome
.send('cancelPendingPrintRequest');
421 /** Shows the system's native printing dialog. */
422 startShowSystemDialog: function() {
423 assert(!cr
.isWindows
);
424 chrome
.send('showSystemDialog');
427 /** Closes the print preview dialog. */
428 startCloseDialog: function() {
429 chrome
.send('closePrintPreviewDialog');
430 chrome
.send('dialogClose');
433 /** Hide the print preview dialog and allow the native layer to close it. */
434 startHideDialog: function() {
435 chrome
.send('hidePreview');
439 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event
440 * will be dispatched in response.
441 * @param {boolean} addAccount Whether to open an 'add a new account' or
442 * default sign in page.
444 startCloudPrintSignIn: function(addAccount
) {
445 chrome
.send('signIn', [addAccount
]);
448 /** Navigates the user to the system printer settings interface. */
449 startManageLocalDestinations: function() {
450 chrome
.send('manageLocalPrinters');
454 * Navigates the user to the Google Cloud Print management page.
455 * @param {?string} user Email address of the user to open the management
456 * page for (user must be currently logged in, indeed) or {@code null}
457 * to open this page for the primary user.
459 startManageCloudDestinations: function(user
) {
460 chrome
.send('manageCloudPrinters', [user
|| '']);
463 /** Forces browser to open a new tab with the given URL address. */
464 startForceOpenNewTab: function(url
) {
465 chrome
.send('forceOpenNewTab', [url
]);
469 * @param {!Object} initialSettings Object containing all initial settings.
471 onSetInitialSettings_: function(initialSettings
) {
472 var numberFormatSymbols
=
473 print_preview
.MeasurementSystem
.parseNumberFormat(
474 initialSettings
['numberFormat']);
475 var unitType
= print_preview
.MeasurementSystem
.UnitType
.IMPERIAL
;
476 if (initialSettings
['measurementSystem'] != null) {
477 unitType
= initialSettings
['measurementSystem'];
480 var nativeInitialSettings
= new print_preview
.NativeInitialSettings(
481 initialSettings
['printAutomaticallyInKioskMode'] || false,
482 initialSettings
['appKioskMode'] || false,
483 initialSettings
['hidePrintWithSystemDialogLink'] || false,
484 numberFormatSymbols
[0] || ',',
485 numberFormatSymbols
[1] || '.',
487 initialSettings
['previewModifiable'] || false,
488 initialSettings
['initiatorTitle'] || '',
489 initialSettings
['documentHasSelection'] || false,
490 initialSettings
['shouldPrintSelectionOnly'] || false,
491 initialSettings
['printerName'] || null,
492 initialSettings
['appState'] || null);
494 var initialSettingsSetEvent
= new Event(
495 NativeLayer
.EventType
.INITIAL_SETTINGS_SET
);
496 initialSettingsSetEvent
.initialSettings
= nativeInitialSettings
;
497 this.dispatchEvent(initialSettingsSetEvent
);
501 * Turn on the integration of Cloud Print.
502 * @param {{cloudPrintURL: string, appKioskMode: string}} settings
503 * cloudPrintUrl: The URL to use for cloud print servers.
506 onSetUseCloudPrint_: function(settings
) {
507 var cloudPrintEnableEvent
= new Event(
508 NativeLayer
.EventType
.CLOUD_PRINT_ENABLE
);
509 cloudPrintEnableEvent
.baseCloudPrintUrl
= settings
['cloudPrintUrl'] || '';
510 cloudPrintEnableEvent
.appKioskMode
= settings
['appKioskMode'] || false;
511 this.dispatchEvent(cloudPrintEnableEvent
);
515 * Updates the print preview with local printers.
516 * Called from PrintPreviewHandler::SetupPrinterList().
517 * @param {Array} printers Array of printer info objects.
520 onSetPrinters_: function(printers
) {
521 var localDestsSetEvent
= new Event(
522 NativeLayer
.EventType
.LOCAL_DESTINATIONS_SET
);
523 localDestsSetEvent
.destinationInfos
= printers
;
524 this.dispatchEvent(localDestsSetEvent
);
528 * Called when native layer gets settings information for a requested local
530 * @param {Object} settingsInfo printer setting information.
533 onUpdateWithPrinterCapabilities_: function(settingsInfo
) {
534 var capsSetEvent
= new Event(NativeLayer
.EventType
.CAPABILITIES_SET
);
535 capsSetEvent
.settingsInfo
= settingsInfo
;
536 this.dispatchEvent(capsSetEvent
);
540 * Called when native layer gets settings information for a requested local
542 * @param {string} destinationId Printer affected by error.
545 onFailedToGetPrinterCapabilities_: function(destinationId
) {
546 var getCapsFailEvent
= new Event(
547 NativeLayer
.EventType
.GET_CAPABILITIES_FAIL
);
548 getCapsFailEvent
.destinationId
= destinationId
;
549 getCapsFailEvent
.destinationOrigin
=
550 print_preview
.Destination
.Origin
.LOCAL
;
551 this.dispatchEvent(getCapsFailEvent
);
555 * Called when native layer gets settings information for a requested privet
557 * @param {string} destinationId Printer affected by error.
560 onFailedToGetPrivetPrinterCapabilities_: function(destinationId
) {
561 var getCapsFailEvent
= new Event(
562 NativeLayer
.EventType
.GET_CAPABILITIES_FAIL
);
563 getCapsFailEvent
.destinationId
= destinationId
;
564 getCapsFailEvent
.destinationOrigin
=
565 print_preview
.Destination
.Origin
.PRIVET
;
566 this.dispatchEvent(getCapsFailEvent
);
570 * Called when native layer fails to get settings information for a
571 * requested extension destination.
572 * @param {string} destinationId Printer affected by error.
575 onFailedToGetExtensionPrinterCapabilities_: function(destinationId
) {
576 var getCapsFailEvent
= new Event(
577 NativeLayer
.EventType
.GET_CAPABILITIES_FAIL
);
578 getCapsFailEvent
.destinationId
= destinationId
;
579 getCapsFailEvent
.destinationOrigin
=
580 print_preview
.Destination
.Origin
.EXTENSION
;
581 this.dispatchEvent(getCapsFailEvent
);
584 /** Reloads the printer list. */
585 onReloadPrintersList_: function() {
586 cr
.dispatchSimpleEvent(this, NativeLayer
.EventType
.DESTINATIONS_RELOAD
);
590 * Called from the C++ layer.
591 * Take the PDF data handed to us and submit it to the cloud, closing the
592 * print preview dialog once the upload is successful.
593 * @param {string} data Data to send as the print job.
596 onPrintToCloud_: function(data
) {
597 var printToCloudEvent
= new Event(
598 NativeLayer
.EventType
.PRINT_TO_CLOUD
);
599 printToCloudEvent
.data
= data
;
600 this.dispatchEvent(printToCloudEvent
);
604 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
605 * preview dialog regarding the file selection cancel event.
608 onFileSelectionCancelled_: function() {
609 cr
.dispatchSimpleEvent(this, NativeLayer
.EventType
.FILE_SELECTION_CANCEL
);
613 * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print
614 * preview dialog regarding the file selection completed event.
617 onFileSelectionCompleted_: function() {
618 // If the file selection is completed and the dialog is not already closed
619 // it means that a pending print to pdf request exists.
620 cr
.dispatchSimpleEvent(
621 this, NativeLayer
.EventType
.FILE_SELECTION_COMPLETE
);
625 * Display an error message when print preview fails.
626 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed().
629 onPrintPreviewFailed_: function() {
630 cr
.dispatchSimpleEvent(
631 this, NativeLayer
.EventType
.PREVIEW_GENERATION_FAIL
);
635 * Display an error message when encountered invalid printer settings.
636 * Called from PrintPreviewMessageHandler::OnInvalidPrinterSettings().
639 onInvalidPrinterSettings_: function() {
640 cr
.dispatchSimpleEvent(this, NativeLayer
.EventType
.SETTINGS_INVALID
);
644 * @param {{contentWidth: number, contentHeight: number, marginLeft: number,
645 * marginRight: number, marginTop: number, marginBottom: number,
646 * printableAreaX: number, printableAreaY: number,
647 * printableAreaWidth: number, printableAreaHeight: number}}
648 * pageLayout Specifies default page layout details in points.
649 * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed
650 * document has a custom page size style.
653 onDidGetDefaultPageLayout_: function(pageLayout
, hasCustomPageSizeStyle
) {
654 var pageLayoutChangeEvent
= new Event(
655 NativeLayer
.EventType
.PAGE_LAYOUT_READY
);
656 pageLayoutChangeEvent
.pageLayout
= pageLayout
;
657 pageLayoutChangeEvent
.hasCustomPageSizeStyle
= hasCustomPageSizeStyle
;
658 this.dispatchEvent(pageLayoutChangeEvent
);
662 * Update the page count and check the page range.
663 * Called from PrintPreviewUI::OnDidGetPreviewPageCount().
664 * @param {number} pageCount The number of pages.
665 * @param {number} previewResponseId The preview request id that resulted in
669 onDidGetPreviewPageCount_: function(pageCount
, previewResponseId
) {
670 var pageCountChangeEvent
= new Event(
671 NativeLayer
.EventType
.PAGE_COUNT_READY
);
672 pageCountChangeEvent
.pageCount
= pageCount
;
673 pageCountChangeEvent
.previewResponseId
= previewResponseId
;
674 this.dispatchEvent(pageCountChangeEvent
);
678 * Notification that a print preview page has been rendered.
679 * Check if the settings have changed and request a regeneration if needed.
680 * Called from PrintPreviewUI::OnDidPreviewPage().
681 * @param {number} pageNumber The page number, 0-based.
682 * @param {number} previewUid Preview unique identifier.
683 * @param {number} previewResponseId The preview request id that resulted in
687 onDidPreviewPage_: function(pageNumber
, previewUid
, previewResponseId
) {
688 var pagePreviewGenEvent
= new Event(
689 NativeLayer
.EventType
.PAGE_PREVIEW_READY
);
690 pagePreviewGenEvent
.pageIndex
= pageNumber
;
691 pagePreviewGenEvent
.previewUid
= previewUid
;
692 pagePreviewGenEvent
.previewResponseId
= previewResponseId
;
693 this.dispatchEvent(pagePreviewGenEvent
);
697 * Notification that access token is ready.
698 * @param {string} authType Type of access token.
699 * @param {string} accessToken Access token.
702 onDidGetAccessToken_: function(authType
, accessToken
) {
703 var getAccessTokenEvent
= new Event(
704 NativeLayer
.EventType
.ACCESS_TOKEN_READY
);
705 getAccessTokenEvent
.authType
= authType
;
706 getAccessTokenEvent
.accessToken
= accessToken
;
707 this.dispatchEvent(getAccessTokenEvent
);
711 * Update the print preview when new preview data is available.
712 * Create the PDF plugin as needed.
713 * Called from PrintPreviewUI::PreviewDataIsAvailable().
714 * @param {number} previewUid Preview unique identifier.
715 * @param {number} previewResponseId The preview request id that resulted in
719 onUpdatePrintPreview_: function(previewUid
, previewResponseId
) {
720 var previewGenDoneEvent
= new Event(
721 NativeLayer
.EventType
.PREVIEW_GENERATION_DONE
);
722 previewGenDoneEvent
.previewUid
= previewUid
;
723 previewGenDoneEvent
.previewResponseId
= previewResponseId
;
724 this.dispatchEvent(previewGenDoneEvent
);
728 * Updates print preset options from source PDF document.
729 * Called from PrintPreviewUI::OnSetOptionsFromDocument().
730 * @param {{disableScaling: boolean, copies: number,
731 * duplex: number}} options Specifies
732 * printing options according to source document presets.
735 onPrintPresetOptionsFromDocument_: function(options
) {
736 var printPresetOptionsEvent
= new Event(
737 NativeLayer
.EventType
.PRINT_PRESET_OPTIONS
);
738 printPresetOptionsEvent
.optionsFromDocument
= options
;
739 this.dispatchEvent(printPresetOptionsEvent
);
743 * Updates the interface to show the "Distill Page" option
744 * when PrintPreviewHandler::HandleIsPageDistillableResult
745 * determines that this page can be distilled with the
749 detectDistillablePage_: function() {
750 var allowDistillPageEvent
= new Event(
751 NativeLayer
.EventType
.ALLOW_DISTILL_PAGE
);
752 this.dispatchEvent(allowDistillPageEvent
);
756 * Simulates a user click on the print preview dialog cancel button. Used
760 autoCancelForTesting_: function() {
761 var properties
= {view
: window
, bubbles
: true, cancelable
: true};
762 var click
= new MouseEvent('click', properties
);
763 document
.querySelector('#print-header .cancel').dispatchEvent(click
);
767 * @param {{serviceName: string, name: string}} printer Specifies
768 * information about the printer that was added.
771 onPrivetPrinterChanged_: function(printer
) {
772 var privetPrinterChangedEvent
=
773 new Event(NativeLayer
.EventType
.PRIVET_PRINTER_CHANGED
);
774 privetPrinterChangedEvent
.printer
= printer
;
775 this.dispatchEvent(privetPrinterChangedEvent
);
779 * @param {Object} printer Specifies information about the printer that was
783 onPrivetCapabilitiesSet_: function(printer
, capabilities
) {
784 var privetCapabilitiesSetEvent
=
785 new Event(NativeLayer
.EventType
.PRIVET_CAPABILITIES_SET
);
786 privetCapabilitiesSetEvent
.printer
= printer
;
787 privetCapabilitiesSetEvent
.capabilities
= capabilities
;
788 this.dispatchEvent(privetCapabilitiesSetEvent
);
792 * @param {string} http_error The HTTP response code or -1 if not an HTTP
796 onPrivetPrintFailed_: function(http_error
) {
797 var privetPrintFailedEvent
=
798 new Event(NativeLayer
.EventType
.PRIVET_PRINT_FAILED
);
799 privetPrintFailedEvent
.httpError
= http_error
;
800 this.dispatchEvent(privetPrintFailedEvent
);
804 * @param {Array<!{extensionId: string,
805 * extensionName: string,
808 * description: (string|undefined),
809 * provisional: (boolean|undefined)}>} printers The list
810 * containing information about printers added by an extension.
811 * @param {boolean} done Whether this is the final list of extension
814 onExtensionPrintersAdded_: function(printers
, done
) {
815 var event
= new Event(NativeLayer
.EventType
.EXTENSION_PRINTERS_ADDED
);
816 event
.printers
= printers
;
818 this.dispatchEvent(event
);
822 * Called when an extension responds to a request for an extension printer
824 * @param {string} printerId The printer's ID.
825 * @param {!Object} capabilities The reported printer capabilities.
827 onExtensionCapabilitiesSet_: function(printerId
,
829 var event
= new Event(NativeLayer
.EventType
.EXTENSION_CAPABILITIES_SET
);
830 event
.printerId
= printerId
;
831 event
.capabilities
= capabilities
;
832 this.dispatchEvent(event
);
836 * Called when Chrome reports that attempt to resolve a provisional
837 * destination failed.
838 * @param {string} destinationId The provisional destination ID.
841 failedToResolveProvisionalDestination_: function(destinationId
) {
843 NativeLayer
.EventType
.PROVISIONAL_DESTINATION_RESOLVED
);
844 evt
.provisionalId
= destinationId
;
845 evt
.destination
= null;
846 this.dispatchEvent(evt
);
850 * Called when Chrome reports that a provisional destination has been
851 * successfully resolved.
852 * Currently used only for extension provided destinations.
853 * @param {string} provisionalDestinationId The provisional destination id.
854 * @param {!{extensionId: string,
855 * extensionName: string,
858 * description: (string|undefined)}} destinationInfo The resolved
862 onProvisionalDestinationResolved_: function(provisionalDestinationId
,
865 NativeLayer
.EventType
.PROVISIONAL_DESTINATION_RESOLVED
);
866 evt
.provisionalId
= provisionalDestinationId
;
867 evt
.destination
= destinationInfo
;
868 this.dispatchEvent(evt
);
872 * Allows for onManipulateSettings to be called
873 * from the native layer.
876 onEnableManipulateSettingsForTest_: function() {
877 global
.onManipulateSettingsForTest
=
878 this.onManipulateSettingsForTest_
.bind(this);
882 * Dispatches an event to print_preview.js to change
883 * a particular setting for print preview.
884 * @param {!print_preview.PreviewSettings} settings Object containing the
885 * value to be changed and that value should be set to.
888 onManipulateSettingsForTest_: function(settings
) {
889 var manipulateSettingsEvent
=
890 new Event(NativeLayer
.EventType
.MANIPULATE_SETTINGS_FOR_TEST
);
891 manipulateSettingsEvent
.settings
= settings
;
892 this.dispatchEvent(manipulateSettingsEvent
);
896 * Sends a message to the test, letting it know that an
897 * option has been set to a particular value and that the change has
898 * finished modifying the preview area.
900 previewReadyForTest: function() {
901 if (global
.onManipulateSettingsForTest
)
902 chrome
.send('UILoadedForTest');
906 * Notifies the test that the option it tried to change
907 * had not been changed successfully.
909 previewFailedForTest: function() {
910 if (global
.onManipulateSettingsForTest
)
911 chrome
.send('UIFailedLoadingForTest');
916 * Initial settings retrieved from the native layer.
917 * @param {boolean} isInKioskAutoPrintMode Whether the print preview should be
918 * in auto-print mode.
919 * @param {boolean} isInAppKioskMode Whether the print preview is in App Kiosk
921 * @param {string} thousandsDelimeter Character delimeter of thousands digits.
922 * @param {string} decimalDelimeter Character delimeter of the decimal point.
923 * @param {!print_preview.MeasurementSystem.UnitType} unitType Unit type of
924 * local machine's measurement system.
925 * @param {boolean} isDocumentModifiable Whether the document to print is
927 * @param {string} documentTitle Title of the document.
928 * @param {boolean} documentHasSelection Whether the document has selected
930 * @param {boolean} selectionOnly Whether only selected content should be
932 * @param {?string} systemDefaultDestinationId ID of the system default
934 * @param {?string} serializedAppStateStr Serialized app state.
937 function NativeInitialSettings(
938 isInKioskAutoPrintMode
,
940 hidePrintWithSystemDialogLink
,
944 isDocumentModifiable
,
946 documentHasSelection
,
948 systemDefaultDestinationId
,
949 serializedAppStateStr
) {
952 * Whether the print preview should be in auto-print mode.
956 this.isInKioskAutoPrintMode_
= isInKioskAutoPrintMode
;
959 * Whether the print preview should switch to App Kiosk mode.
963 this.isInAppKioskMode_
= isInAppKioskMode
;
966 * Whether we should hide the link which shows the system print dialog.
970 this.hidePrintWithSystemDialogLink_
= hidePrintWithSystemDialogLink
;
973 * Character delimeter of thousands digits.
977 this.thousandsDelimeter_
= thousandsDelimeter
;
980 * Character delimeter of the decimal point.
984 this.decimalDelimeter_
= decimalDelimeter
;
987 * Unit type of local machine's measurement system.
991 this.unitType_
= unitType
;
994 * Whether the document to print is modifiable.
998 this.isDocumentModifiable_
= isDocumentModifiable
;
1001 * Title of the document.
1005 this.documentTitle_
= documentTitle
;
1008 * Whether the document has selection.
1012 this.documentHasSelection_
= documentHasSelection
;
1015 * Whether selection only should be printed.
1019 this.selectionOnly_
= selectionOnly
;
1022 * ID of the system default destination.
1026 this.systemDefaultDestinationId_
= systemDefaultDestinationId
;
1029 * Serialized app state.
1033 this.serializedAppStateStr_
= serializedAppStateStr
;
1036 NativeInitialSettings
.prototype = {
1038 * @return {boolean} Whether the print preview should be in auto-print mode.
1040 get isInKioskAutoPrintMode() {
1041 return this.isInKioskAutoPrintMode_
;
1045 * @return {boolean} Whether the print preview should switch to App Kiosk
1048 get isInAppKioskMode() {
1049 return this.isInAppKioskMode_
;
1053 * @return {boolean} Whether we should hide the link which shows the
1054 system print dialog.
1056 get hidePrintWithSystemDialogLink() {
1057 return this.hidePrintWithSystemDialogLink_
;
1060 /** @return {string} Character delimeter of thousands digits. */
1061 get thousandsDelimeter() {
1062 return this.thousandsDelimeter_
;
1065 /** @return {string} Character delimeter of the decimal point. */
1066 get decimalDelimeter() {
1067 return this.decimalDelimeter_
;
1071 * @return {!print_preview.MeasurementSystem.UnitType} Unit type of local
1072 * machine's measurement system.
1075 return this.unitType_
;
1078 /** @return {boolean} Whether the document to print is modifiable. */
1079 get isDocumentModifiable() {
1080 return this.isDocumentModifiable_
;
1083 /** @return {string} Document title. */
1084 get documentTitle() {
1085 return this.documentTitle_
;
1088 /** @return {boolean} Whether the document has selection. */
1089 get documentHasSelection() {
1090 return this.documentHasSelection_
;
1093 /** @return {boolean} Whether selection only should be printed. */
1094 get selectionOnly() {
1095 return this.selectionOnly_
;
1098 /** @return {?string} ID of the system default destination. */
1099 get systemDefaultDestinationId() {
1100 return this.systemDefaultDestinationId_
;
1103 /** @return {?string} Serialized app state. */
1104 get serializedAppStateStr() {
1105 return this.serializedAppStateStr_
;
1111 NativeInitialSettings
: NativeInitialSettings
,
1112 NativeLayer
: NativeLayer