| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.exportPath('print_preview'); | 5 cr.exportPath('print_preview'); |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * @typedef {{selectSaveAsPdfDestination: boolean, | 8 * @typedef {{selectSaveAsPdfDestination: boolean, |
| 9 * layoutSettings.portrait: boolean, | 9 * layoutSettings.portrait: boolean, |
| 10 * pageRange: string, | 10 * pageRange: string, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 print_preview.ProvisionalDestinationInfo; | 78 print_preview.ProvisionalDestinationInfo; |
| 79 | 79 |
| 80 cr.define('print_preview', function() { | 80 cr.define('print_preview', function() { |
| 81 'use strict'; | 81 'use strict'; |
| 82 | 82 |
| 83 /** | 83 /** |
| 84 * An interface to the native Chromium printing system layer. | 84 * An interface to the native Chromium printing system layer. |
| 85 * @constructor | 85 * @constructor |
| 86 */ | 86 */ |
| 87 function NativeLayer() { | 87 function NativeLayer() { |
| 88 // Bind global handlers | |
| 89 global.reloadPrintersList = this.onReloadPrintersList_.bind(this); | |
| 90 global.onDidGetDefaultPageLayout = | |
| 91 this.onDidGetDefaultPageLayout_.bind(this); | |
| 92 global.onDidGetPreviewPageCount = this.onDidGetPreviewPageCount_.bind(this); | |
| 93 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this); | |
| 94 global.onEnableManipulateSettingsForTest = | |
| 95 this.onEnableManipulateSettingsForTest_.bind(this); | |
| 96 global.printPresetOptionsFromDocument = | |
| 97 this.onPrintPresetOptionsFromDocument_.bind(this); | |
| 98 | |
| 99 /** @private {!cr.EventTarget} */ | |
| 100 this.eventTarget_ = new cr.EventTarget(); | |
| 101 } | 88 } |
| 102 | 89 |
| 103 /** @private {?print_preview.NativeLayer} */ | 90 /** @private {?print_preview.NativeLayer} */ |
| 104 var currentInstance = null; | 91 var currentInstance = null; |
| 105 | 92 |
| 106 /** | 93 /** |
| 107 * @return {!print_preview.NativeLayer} The singleton instance. | 94 * @return {!print_preview.NativeLayer} The singleton instance. |
| 108 * Creates a new NativeLayer if the current instance is not set. | 95 * Creates a new NativeLayer if the current instance is not set. |
| 109 */ | 96 */ |
| 110 NativeLayer.getInstance = function() { | 97 NativeLayer.getInstance = function() { |
| 111 if (currentInstance == null) | 98 if (currentInstance == null) |
| 112 currentInstance = new NativeLayer(); | 99 currentInstance = new NativeLayer(); |
| 113 return assert(currentInstance); | 100 return assert(currentInstance); |
| 114 }; | 101 }; |
| 115 | 102 |
| 116 /** | 103 /** |
| 117 * @param {!print_preview.NativeLayer} instance The NativeLayer instance | 104 * @param {!print_preview.NativeLayer} instance The NativeLayer instance |
| 118 * to set for print preview construction. | 105 * to set for print preview construction. |
| 119 */ | 106 */ |
| 120 NativeLayer.setInstance = function(instance) { | 107 NativeLayer.setInstance = function(instance) { |
| 121 currentInstance = instance; | 108 currentInstance = instance; |
| 122 }; | 109 }; |
| 123 | 110 |
| 124 /** | 111 /** |
| 125 * Event types dispatched from the Chromium native layer. | |
| 126 * @enum {string} | |
| 127 * @const | |
| 128 */ | |
| 129 NativeLayer.EventType = { | |
| 130 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD', | |
| 131 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING', | |
| 132 MANIPULATE_SETTINGS_FOR_TEST: | |
| 133 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST', | |
| 134 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY', | |
| 135 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY', | |
| 136 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY', | |
| 137 PREVIEW_GENERATION_DONE: | |
| 138 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE', | |
| 139 PREVIEW_GENERATION_FAIL: | |
| 140 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL', | |
| 141 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID', | |
| 142 PRINT_PRESET_OPTIONS: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS', | |
| 143 }; | |
| 144 | |
| 145 /** | |
| 146 * Constant values matching printing::DuplexMode enum. | 112 * Constant values matching printing::DuplexMode enum. |
| 147 * @enum {number} | 113 * @enum {number} |
| 148 */ | 114 */ |
| 149 NativeLayer.DuplexMode = {SIMPLEX: 0, LONG_EDGE: 1, UNKNOWN_DUPLEX_MODE: -1}; | 115 NativeLayer.DuplexMode = {SIMPLEX: 0, LONG_EDGE: 1, UNKNOWN_DUPLEX_MODE: -1}; |
| 150 | 116 |
| 151 /** | 117 /** |
| 152 * Enumeration of color modes used by Chromium. | 118 * Enumeration of color modes used by Chromium. |
| 153 * @enum {number} | 119 * @enum {number} |
| 154 * @private | 120 * @private |
| 155 */ | 121 */ |
| 156 NativeLayer.ColorMode_ = {GRAY: 1, COLOR: 2}; | 122 NativeLayer.ColorMode_ = {GRAY: 1, COLOR: 2}; |
| 157 | 123 |
| 158 /** | 124 /** |
| 159 * Version of the serialized state of the print preview. | 125 * Version of the serialized state of the print preview. |
| 160 * @type {number} | 126 * @type {number} |
| 161 * @const | 127 * @const |
| 162 * @private | 128 * @private |
| 163 */ | 129 */ |
| 164 NativeLayer.SERIALIZED_STATE_VERSION_ = 1; | 130 NativeLayer.SERIALIZED_STATE_VERSION_ = 1; |
| 165 | 131 |
| 166 NativeLayer.prototype = { | 132 NativeLayer.prototype = { |
| 167 /** @return {!cr.EventTarget} The event target for the native layer.*/ | |
| 168 getEventTarget: function() { | |
| 169 return this.eventTarget_; | |
| 170 }, | |
| 171 | |
| 172 /** | 133 /** |
| 173 * Requests access token for cloud print requests. | 134 * Requests access token for cloud print requests. |
| 174 * @param {string} authType type of access token. | 135 * @param {string} authType type of access token. |
| 175 * @return {!Promise<string>} | 136 * @return {!Promise<string>} |
| 176 */ | 137 */ |
| 177 getAccessToken: function(authType) { | 138 getAccessToken: function(authType) { |
| 178 return cr.sendWithPromise('getAccessToken', authType); | 139 return cr.sendWithPromise('getAccessToken', authType); |
| 179 }, | 140 }, |
| 180 | 141 |
| 181 /** | 142 /** |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 var option = destination.isLocal ? color.getSelectedOption() : null; | 268 var option = destination.isLocal ? color.getSelectedOption() : null; |
| 308 var nativeColorModel = parseInt(option ? option.vendor_id : null, 10); | 269 var nativeColorModel = parseInt(option ? option.vendor_id : null, 10); |
| 309 if (isNaN(nativeColorModel)) { | 270 if (isNaN(nativeColorModel)) { |
| 310 return color.getValue() ? NativeLayer.ColorMode_.COLOR : | 271 return color.getValue() ? NativeLayer.ColorMode_.COLOR : |
| 311 NativeLayer.ColorMode_.GRAY; | 272 NativeLayer.ColorMode_.GRAY; |
| 312 } | 273 } |
| 313 return nativeColorModel; | 274 return nativeColorModel; |
| 314 }, | 275 }, |
| 315 | 276 |
| 316 /** | 277 /** |
| 317 * Requests that a preview be generated. The following events may be | 278 * Requests that a preview be generated. The following Web UI events may |
| 318 * dispatched in response: | 279 * be triggered in response: |
| 319 * - PAGE_COUNT_READY | 280 * 'print-preset-options', |
| 320 * - PAGE_LAYOUT_READY | 281 * 'page-count-ready', |
| 321 * - PAGE_PREVIEW_READY | 282 * 'page-layout-ready', |
| 322 * - PREVIEW_GENERATION_DONE | 283 * 'page-preview-ready' |
| 323 * - PREVIEW_GENERATION_FAIL | |
| 324 * @param {!print_preview.Destination} destination Destination to print to. | 284 * @param {!print_preview.Destination} destination Destination to print to. |
| 325 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the | 285 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get the |
| 326 * state of the print ticket. | 286 * state of the print ticket. |
| 327 * @param {!print_preview.DocumentInfo} documentInfo Document data model. | 287 * @param {!print_preview.DocumentInfo} documentInfo Document data model. |
| 328 * @param {boolean} generateDraft Tell the renderer to re-render. | 288 * @param {boolean} generateDraft Tell the renderer to re-render. |
| 329 * @param {number} requestId ID of the preview request. | 289 * @param {number} requestId ID of the preview request. |
| 330 * @return {!Promise<number>} Promise that resolves with the unique ID of | 290 * @return {!Promise<number>} Promise that resolves with the unique ID of |
| 331 * the preview UI when the preview has been generated. | 291 * the preview UI when the preview has been generated. |
| 332 */ | 292 */ |
| 333 getPreview: function( | 293 getPreview: function( |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 /** Hide the print preview dialog and allow the native layer to close it. */ | 479 /** Hide the print preview dialog and allow the native layer to close it. */ |
| 520 startHideDialog: function() { | 480 startHideDialog: function() { |
| 521 chrome.send('hidePreview'); | 481 chrome.send('hidePreview'); |
| 522 }, | 482 }, |
| 523 | 483 |
| 524 /** | 484 /** |
| 525 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event | 485 * Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event |
| 526 * will be dispatched in response. | 486 * will be dispatched in response. |
| 527 * @param {boolean} addAccount Whether to open an 'add a new account' or | 487 * @param {boolean} addAccount Whether to open an 'add a new account' or |
| 528 * default sign in page. | 488 * default sign in page. |
| 489 * @return {!Promise} Promise that resolves when the sign in tab has been |
| 490 * closed and the destinations should be reloaded. |
| 529 */ | 491 */ |
| 530 startCloudPrintSignIn: function(addAccount) { | 492 signIn: function(addAccount) { |
| 531 chrome.send('signIn', [addAccount]); | 493 return cr.sendWithPromise('signIn', addAccount); |
| 532 }, | 494 }, |
| 533 | 495 |
| 534 /** Navigates the user to the system printer settings interface. */ | 496 /** Navigates the user to the system printer settings interface. */ |
| 535 startManageLocalDestinations: function() { | 497 startManageLocalDestinations: function() { |
| 536 chrome.send('manageLocalPrinters'); | 498 chrome.send('manageLocalPrinters'); |
| 537 }, | 499 }, |
| 538 | 500 |
| 539 /** | 501 /** |
| 540 * Navigates the user to the Google Cloud Print management page. | 502 * Navigates the user to the Google Cloud Print management page. |
| 541 * @param {?string} user Email address of the user to open the management | 503 * @param {?string} user Email address of the user to open the management |
| 542 * page for (user must be currently logged in, indeed) or {@code null} | 504 * page for (user must be currently logged in, indeed) or {@code null} |
| 543 * to open this page for the primary user. | 505 * to open this page for the primary user. |
| 544 */ | 506 */ |
| 545 startManageCloudDestinations: function(user) { | 507 startManageCloudDestinations: function(user) { |
| 546 chrome.send('manageCloudPrinters', [user || '']); | 508 chrome.send('manageCloudPrinters', [user || '']); |
| 547 }, | 509 }, |
| 548 | 510 |
| 549 /** Forces browser to open a new tab with the given URL address. */ | 511 /** Forces browser to open a new tab with the given URL address. */ |
| 550 startForceOpenNewTab: function(url) { | 512 startForceOpenNewTab: function(url) { |
| 551 chrome.send('forceOpenNewTab', [url]); | 513 chrome.send('forceOpenNewTab', [url]); |
| 552 }, | 514 }, |
| 553 | 515 |
| 554 /** Reloads the printer list. */ | |
| 555 onReloadPrintersList_: function() { | |
| 556 cr.dispatchSimpleEvent( | |
| 557 this.eventTarget_, NativeLayer.EventType.DESTINATIONS_RELOAD); | |
| 558 }, | |
| 559 | |
| 560 /** | |
| 561 * @param {{contentWidth: number, contentHeight: number, marginLeft: number, | |
| 562 * marginRight: number, marginTop: number, marginBottom: number, | |
| 563 * printableAreaX: number, printableAreaY: number, | |
| 564 * printableAreaWidth: number, printableAreaHeight: number}} | |
| 565 * pageLayout Specifies default page layout details in points. | |
| 566 * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed | |
| 567 * document has a custom page size style. | |
| 568 * @private | |
| 569 */ | |
| 570 onDidGetDefaultPageLayout_: function(pageLayout, hasCustomPageSizeStyle) { | |
| 571 var pageLayoutChangeEvent = | |
| 572 new Event(NativeLayer.EventType.PAGE_LAYOUT_READY); | |
| 573 pageLayoutChangeEvent.pageLayout = pageLayout; | |
| 574 pageLayoutChangeEvent.hasCustomPageSizeStyle = hasCustomPageSizeStyle; | |
| 575 this.eventTarget_.dispatchEvent(pageLayoutChangeEvent); | |
| 576 }, | |
| 577 | |
| 578 /** | |
| 579 * Update the page count and check the page range. | |
| 580 * Called from PrintPreviewUI::OnDidGetPreviewPageCount(). | |
| 581 * @param {number} pageCount The number of pages. | |
| 582 * @param {number} previewResponseId The preview request id that resulted in | |
| 583 * this response. | |
| 584 * @param {number} fitToPageScaling The scaling percentage required to fit | |
| 585 * the document to page, rounded to the nearest integer. | |
| 586 * @private | |
| 587 */ | |
| 588 onDidGetPreviewPageCount_: function( | |
| 589 pageCount, previewResponseId, fitToPageScaling) { | |
| 590 var pageCountChangeEvent = | |
| 591 new Event(NativeLayer.EventType.PAGE_COUNT_READY); | |
| 592 pageCountChangeEvent.pageCount = pageCount; | |
| 593 pageCountChangeEvent.previewResponseId = previewResponseId; | |
| 594 pageCountChangeEvent.fitToPageScaling = fitToPageScaling; | |
| 595 this.eventTarget_.dispatchEvent(pageCountChangeEvent); | |
| 596 }, | |
| 597 | |
| 598 /** | |
| 599 * Notification that a print preview page has been rendered. | |
| 600 * Check if the settings have changed and request a regeneration if needed. | |
| 601 * Called from PrintPreviewUI::OnDidPreviewPage(). | |
| 602 * @param {number} pageNumber The page number, 0-based. | |
| 603 * @param {number} previewUid Preview unique identifier. | |
| 604 * @param {number} previewResponseId The preview request id that resulted in | |
| 605 * this response. | |
| 606 * @private | |
| 607 */ | |
| 608 onDidPreviewPage_: function(pageNumber, previewUid, previewResponseId) { | |
| 609 var pagePreviewGenEvent = | |
| 610 new Event(NativeLayer.EventType.PAGE_PREVIEW_READY); | |
| 611 pagePreviewGenEvent.pageIndex = pageNumber; | |
| 612 pagePreviewGenEvent.previewUid = previewUid; | |
| 613 pagePreviewGenEvent.previewResponseId = previewResponseId; | |
| 614 this.eventTarget_.dispatchEvent(pagePreviewGenEvent); | |
| 615 }, | |
| 616 | |
| 617 /** | |
| 618 * Updates print preset options from source PDF document. | |
| 619 * Called from PrintPreviewUI::OnSetOptionsFromDocument(). | |
| 620 * @param {{disableScaling: boolean, copies: number, | |
| 621 * duplex: number}} options Specifies | |
| 622 * printing options according to source document presets. | |
| 623 * @private | |
| 624 */ | |
| 625 onPrintPresetOptionsFromDocument_: function(options) { | |
| 626 var printPresetOptionsEvent = | |
| 627 new Event(NativeLayer.EventType.PRINT_PRESET_OPTIONS); | |
| 628 printPresetOptionsEvent.optionsFromDocument = options; | |
| 629 this.eventTarget_.dispatchEvent(printPresetOptionsEvent); | |
| 630 }, | |
| 631 | |
| 632 /** | |
| 633 * Allows for onManipulateSettings to be called | |
| 634 * from the native layer. | |
| 635 * @private | |
| 636 */ | |
| 637 onEnableManipulateSettingsForTest_: function() { | |
| 638 global.onManipulateSettingsForTest = | |
| 639 this.onManipulateSettingsForTest_.bind(this); | |
| 640 }, | |
| 641 | |
| 642 /** | |
| 643 * Dispatches an event to print_preview.js to change | |
| 644 * a particular setting for print preview. | |
| 645 * @param {!print_preview.PreviewSettings} settings Object containing the | |
| 646 * value to be changed and that value should be set to. | |
| 647 * @private | |
| 648 */ | |
| 649 onManipulateSettingsForTest_: function(settings) { | |
| 650 var manipulateSettingsEvent = | |
| 651 new Event(NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST); | |
| 652 manipulateSettingsEvent.settings = settings; | |
| 653 this.eventTarget_.dispatchEvent(manipulateSettingsEvent); | |
| 654 }, | |
| 655 | |
| 656 /** | 516 /** |
| 657 * Sends a message to the test, letting it know that an | 517 * Sends a message to the test, letting it know that an |
| 658 * option has been set to a particular value and that the change has | 518 * option has been set to a particular value and that the change has |
| 659 * finished modifying the preview area. | 519 * finished modifying the preview area. |
| 660 */ | 520 */ |
| 661 previewReadyForTest: function() { | 521 previewReadyForTest: function() { |
| 662 if (global.onManipulateSettingsForTest) | 522 chrome.send('UILoadedForTest'); |
| 663 chrome.send('UILoadedForTest'); | |
| 664 }, | 523 }, |
| 665 | 524 |
| 666 /** | 525 /** |
| 667 * Notifies the test that the option it tried to change | 526 * Notifies the test that the option it tried to change |
| 668 * had not been changed successfully. | 527 * had not been changed successfully. |
| 669 */ | 528 */ |
| 670 previewFailedForTest: function() { | 529 previewFailedForTest: function() { |
| 671 if (global.onManipulateSettingsForTest) | 530 chrome.send('UIFailedLoadingForTest'); |
| 672 chrome.send('UIFailedLoadingForTest'); | |
| 673 } | 531 } |
| 674 }; | 532 }; |
| 675 | 533 |
| 676 /** | 534 /** |
| 677 * Initial settings retrieved from the native layer. | 535 * Initial settings retrieved from the native layer. |
| 678 * @param {boolean} isInKioskAutoPrintMode Whether the print preview should be | 536 * @param {boolean} isInKioskAutoPrintMode Whether the print preview should be |
| 679 * in auto-print mode. | 537 * in auto-print mode. |
| 680 * @param {boolean} isInAppKioskMode Whether the print preview is in App Kiosk | 538 * @param {boolean} isInAppKioskMode Whether the print preview is in App Kiosk |
| 681 * mode. | 539 * mode. |
| 682 * @param {string} thousandsDelimeter Character delimeter of thousands digits. | 540 * @param {string} thousandsDelimeter Character delimeter of thousands digits. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 return this.serializedDefaultDestinationSelectionRulesStr_; | 703 return this.serializedDefaultDestinationSelectionRulesStr_; |
| 846 } | 704 } |
| 847 }; | 705 }; |
| 848 | 706 |
| 849 // Export | 707 // Export |
| 850 return { | 708 return { |
| 851 NativeInitialSettings: NativeInitialSettings, | 709 NativeInitialSettings: NativeInitialSettings, |
| 852 NativeLayer: NativeLayer | 710 NativeLayer: NativeLayer |
| 853 }; | 711 }; |
| 854 }); | 712 }); |
| OLD | NEW |