Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: chrome/browser/resources/print_preview/native_layer.js

Issue 2938073003: Change getAccessToken and getExtensionPrinterAccess to sendWithPromise (Closed)
Patch Set: Fix annotation Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 /** 60 /**
61 * @typedef {{ 61 * @typedef {{
62 * printerId: string, 62 * printerId: string,
63 * success: boolean, 63 * success: boolean,
64 * capabilities: Object, 64 * capabilities: Object,
65 * }} 65 * }}
66 */ 66 */
67 print_preview.PrinterSetupResponse; 67 print_preview.PrinterSetupResponse;
68 68
69 /**
70 * @typedef {{
71 * extensionId: string,
72 * extensionName: string,
73 * id: string,
74 * name: string,
75 * description: (string|undefined),
76 * }}
77 */
78 print_preview.ProvisionalDestinationInfo;
79
69 cr.define('print_preview', function() { 80 cr.define('print_preview', function() {
70 'use strict'; 81 'use strict';
71 82
72 /** 83 /**
73 * An interface to the native Chromium printing system layer. 84 * An interface to the native Chromium printing system layer.
74 * @constructor 85 * @constructor
75 */ 86 */
76 function NativeLayer() { 87 function NativeLayer() {
77 // Bind global handlers 88 // Bind global handlers
78 global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this); 89 global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this);
79 global.reloadPrintersList = this.onReloadPrintersList_.bind(this); 90 global.reloadPrintersList = this.onReloadPrintersList_.bind(this);
80 global.printToCloud = this.onPrintToCloud_.bind(this); 91 global.printToCloud = this.onPrintToCloud_.bind(this);
81 global.fileSelectionCancelled = this.onFileSelectionCancelled_.bind(this); 92 global.fileSelectionCancelled = this.onFileSelectionCancelled_.bind(this);
82 global.fileSelectionCompleted = this.onFileSelectionCompleted_.bind(this); 93 global.fileSelectionCompleted = this.onFileSelectionCompleted_.bind(this);
83 global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this); 94 global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this);
84 global.invalidPrinterSettings = this.onInvalidPrinterSettings_.bind(this); 95 global.invalidPrinterSettings = this.onInvalidPrinterSettings_.bind(this);
85 global.onDidGetDefaultPageLayout = 96 global.onDidGetDefaultPageLayout =
86 this.onDidGetDefaultPageLayout_.bind(this); 97 this.onDidGetDefaultPageLayout_.bind(this);
87 global.onDidGetPreviewPageCount = this.onDidGetPreviewPageCount_.bind(this); 98 global.onDidGetPreviewPageCount = this.onDidGetPreviewPageCount_.bind(this);
88 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this); 99 global.onDidPreviewPage = this.onDidPreviewPage_.bind(this);
89 global.updatePrintPreview = this.onUpdatePrintPreview_.bind(this); 100 global.updatePrintPreview = this.onUpdatePrintPreview_.bind(this);
90 global.onDidGetAccessToken = this.onDidGetAccessToken_.bind(this);
91 global.onEnableManipulateSettingsForTest = 101 global.onEnableManipulateSettingsForTest =
92 this.onEnableManipulateSettingsForTest_.bind(this); 102 this.onEnableManipulateSettingsForTest_.bind(this);
93 global.printPresetOptionsFromDocument = 103 global.printPresetOptionsFromDocument =
94 this.onPrintPresetOptionsFromDocument_.bind(this); 104 this.onPrintPresetOptionsFromDocument_.bind(this);
95 global.onProvisionalPrinterResolved =
96 this.onProvisionalDestinationResolved_.bind(this);
97 global.failedToResolveProvisionalPrinter =
98 this.failedToResolveProvisionalDestination_.bind(this);
99 105
100 /** @private {!cr.EventTarget} */ 106 /** @private {!cr.EventTarget} */
101 this.eventTarget_ = new cr.EventTarget(); 107 this.eventTarget_ = new cr.EventTarget();
102 } 108 }
103 109
104 /** @private {?print_preview.NativeLayer} */ 110 /** @private {?print_preview.NativeLayer} */
105 var currentInstance = null; 111 var currentInstance = null;
106 112
107 /** 113 /**
108 * @return {!print_preview.NativeLayer} The singleton instance. 114 * @return {!print_preview.NativeLayer} The singleton instance.
(...skipping 12 matching lines...) Expand all
121 NativeLayer.setInstance = function(instance) { 127 NativeLayer.setInstance = function(instance) {
122 currentInstance = instance; 128 currentInstance = instance;
123 }; 129 };
124 130
125 /** 131 /**
126 * Event types dispatched from the Chromium native layer. 132 * Event types dispatched from the Chromium native layer.
127 * @enum {string} 133 * @enum {string}
128 * @const 134 * @const
129 */ 135 */
130 NativeLayer.EventType = { 136 NativeLayer.EventType = {
131 ACCESS_TOKEN_READY: 'print_preview.NativeLayer.ACCESS_TOKEN_READY',
132 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE', 137 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
133 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD', 138 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
134 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING', 139 DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING',
135 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL', 140 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
136 FILE_SELECTION_COMPLETE: 141 FILE_SELECTION_COMPLETE:
137 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE', 142 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
138 MANIPULATE_SETTINGS_FOR_TEST: 143 MANIPULATE_SETTINGS_FOR_TEST:
139 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST', 144 'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
140 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY', 145 PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY',
141 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY', 146 PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
142 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY', 147 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
143 PREVIEW_GENERATION_DONE: 148 PREVIEW_GENERATION_DONE:
144 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE', 149 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
145 PREVIEW_GENERATION_FAIL: 150 PREVIEW_GENERATION_FAIL:
146 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL', 151 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
147 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD', 152 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
148 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID', 153 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID',
149 PRINT_PRESET_OPTIONS: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS', 154 PRINT_PRESET_OPTIONS: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS',
150 PROVISIONAL_DESTINATION_RESOLVED:
151 'print_preview.NativeLayer.PROVISIONAL_DESTINATION_RESOLVED'
152 }; 155 };
153 156
154 /** 157 /**
155 * Constant values matching printing::DuplexMode enum. 158 * Constant values matching printing::DuplexMode enum.
156 * @enum {number} 159 * @enum {number}
157 */ 160 */
158 NativeLayer.DuplexMode = {SIMPLEX: 0, LONG_EDGE: 1, UNKNOWN_DUPLEX_MODE: -1}; 161 NativeLayer.DuplexMode = {SIMPLEX: 0, LONG_EDGE: 1, UNKNOWN_DUPLEX_MODE: -1};
159 162
160 /** 163 /**
161 * Enumeration of color modes used by Chromium. 164 * Enumeration of color modes used by Chromium.
(...skipping 12 matching lines...) Expand all
174 177
175 NativeLayer.prototype = { 178 NativeLayer.prototype = {
176 /** @return {!cr.EventTarget} The event target for the native layer.*/ 179 /** @return {!cr.EventTarget} The event target for the native layer.*/
177 getEventTarget: function() { 180 getEventTarget: function() {
178 return this.eventTarget_; 181 return this.eventTarget_;
179 }, 182 },
180 183
181 /** 184 /**
182 * Requests access token for cloud print requests. 185 * Requests access token for cloud print requests.
183 * @param {string} authType type of access token. 186 * @param {string} authType type of access token.
187 * @return {!Promise<string>}
184 */ 188 */
185 startGetAccessToken: function(authType) { 189 getAccessToken: function(authType) {
186 chrome.send('getAccessToken', [authType]); 190 return cr.sendWithPromise('getAccessToken', authType);
187 }, 191 },
188 192
189 /** 193 /**
190 * Gets the initial settings to initialize the print preview with. 194 * Gets the initial settings to initialize the print preview with.
191 * @return {!Promise<!print_preview.NativeInitialSettings>} 195 * @return {!Promise<!print_preview.NativeInitialSettings>}
192 */ 196 */
193 getInitialSettings: function() { 197 getInitialSettings: function() {
194 return cr.sendWithPromise('getInitialSettings') 198 return cr.sendWithPromise('getInitialSettings')
195 .then( 199 .then(
196 /** 200 /**
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 * capabilities are requested. 283 * capabilities are requested.
280 * @return {!Promise<!print_preview.Cdd>} 284 * @return {!Promise<!print_preview.Cdd>}
281 */ 285 */
282 getExtensionPrinterCapabilities: function(destinationId) { 286 getExtensionPrinterCapabilities: function(destinationId) {
283 return cr.sendWithPromise( 287 return cr.sendWithPromise(
284 'getExtensionPrinterCapabilities', destinationId); 288 'getExtensionPrinterCapabilities', destinationId);
285 }, 289 },
286 290
287 /** 291 /**
288 * Requests Chrome to resolve provisional extension destination by granting 292 * Requests Chrome to resolve provisional extension destination by granting
289 * the provider extension access to the printer. Chrome will respond with 293 * the provider extension access to the printer.
290 * the resolved destination properties by calling
291 * {@code onProvisionalPrinterResolved}, or in case of an error
292 * {@code failedToResolveProvisionalPrinter}
293 * @param {string} provisionalDestinationId 294 * @param {string} provisionalDestinationId
295 * @return {!Promise<!print_preview.ProvisionalDestinationInfo>}
294 */ 296 */
295 grantExtensionPrinterAccess: function(provisionalDestinationId) { 297 grantExtensionPrinterAccess: function(provisionalDestinationId) {
296 chrome.send('grantExtensionPrinterAccess', [provisionalDestinationId]); 298 return cr.sendWithPromise('grantExtensionPrinterAccess',
299 provisionalDestinationId);
297 }, 300 },
298 301
299 /** 302 /**
300 * Requests that Chrome peform printer setup for the given printer. 303 * Requests that Chrome peform printer setup for the given printer.
301 * @param {string} printerId 304 * @param {string} printerId
302 * @return {!Promise<!print_preview.PrinterSetupResponse>} 305 * @return {!Promise<!print_preview.PrinterSetupResponse>}
303 */ 306 */
304 setupPrinter: function(printerId) { 307 setupPrinter: function(printerId) {
305 return cr.sendWithPromise('setupPrinter', printerId); 308 return cr.sendWithPromise('setupPrinter', printerId);
306 }, 309 },
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 onDidPreviewPage_: function(pageNumber, previewUid, previewResponseId) { 678 onDidPreviewPage_: function(pageNumber, previewUid, previewResponseId) {
676 var pagePreviewGenEvent = 679 var pagePreviewGenEvent =
677 new Event(NativeLayer.EventType.PAGE_PREVIEW_READY); 680 new Event(NativeLayer.EventType.PAGE_PREVIEW_READY);
678 pagePreviewGenEvent.pageIndex = pageNumber; 681 pagePreviewGenEvent.pageIndex = pageNumber;
679 pagePreviewGenEvent.previewUid = previewUid; 682 pagePreviewGenEvent.previewUid = previewUid;
680 pagePreviewGenEvent.previewResponseId = previewResponseId; 683 pagePreviewGenEvent.previewResponseId = previewResponseId;
681 this.eventTarget_.dispatchEvent(pagePreviewGenEvent); 684 this.eventTarget_.dispatchEvent(pagePreviewGenEvent);
682 }, 685 },
683 686
684 /** 687 /**
685 * Notification that access token is ready.
686 * @param {string} authType Type of access token.
687 * @param {string} accessToken Access token.
688 * @private
689 */
690 onDidGetAccessToken_: function(authType, accessToken) {
691 var getAccessTokenEvent =
692 new Event(NativeLayer.EventType.ACCESS_TOKEN_READY);
693 getAccessTokenEvent.authType = authType;
694 getAccessTokenEvent.accessToken = accessToken;
695 this.eventTarget_.dispatchEvent(getAccessTokenEvent);
696 },
697
698 /**
699 * Update the print preview when new preview data is available. 688 * Update the print preview when new preview data is available.
700 * Create the PDF plugin as needed. 689 * Create the PDF plugin as needed.
701 * Called from PrintPreviewUI::PreviewDataIsAvailable(). 690 * Called from PrintPreviewUI::PreviewDataIsAvailable().
702 * @param {number} previewUid Preview unique identifier. 691 * @param {number} previewUid Preview unique identifier.
703 * @param {number} previewResponseId The preview request id that resulted in 692 * @param {number} previewResponseId The preview request id that resulted in
704 * this response. 693 * this response.
705 * @private 694 * @private
706 */ 695 */
707 onUpdatePrintPreview_: function(previewUid, previewResponseId) { 696 onUpdatePrintPreview_: function(previewUid, previewResponseId) {
708 var previewGenDoneEvent = 697 var previewGenDoneEvent =
(...skipping 12 matching lines...) Expand all
721 * @private 710 * @private
722 */ 711 */
723 onPrintPresetOptionsFromDocument_: function(options) { 712 onPrintPresetOptionsFromDocument_: function(options) {
724 var printPresetOptionsEvent = 713 var printPresetOptionsEvent =
725 new Event(NativeLayer.EventType.PRINT_PRESET_OPTIONS); 714 new Event(NativeLayer.EventType.PRINT_PRESET_OPTIONS);
726 printPresetOptionsEvent.optionsFromDocument = options; 715 printPresetOptionsEvent.optionsFromDocument = options;
727 this.eventTarget_.dispatchEvent(printPresetOptionsEvent); 716 this.eventTarget_.dispatchEvent(printPresetOptionsEvent);
728 }, 717 },
729 718
730 /** 719 /**
731 * Called when Chrome reports that attempt to resolve a provisional
732 * destination failed.
733 * @param {string} destinationId The provisional destination ID.
734 * @private
735 */
736 failedToResolveProvisionalDestination_: function(destinationId) {
737 var evt =
738 new Event(NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED);
739 evt.provisionalId = destinationId;
740 evt.destination = null;
741 this.eventTarget_.dispatchEvent(evt);
742 },
743
744 /**
745 * Called when Chrome reports that a provisional destination has been
746 * successfully resolved.
747 * Currently used only for extension provided destinations.
748 * @param {string} provisionalDestinationId The provisional destination id.
749 * @param {!{extensionId: string,
750 * extensionName: string,
751 * id: string,
752 * name: string,
753 * description: (string|undefined)}} destinationInfo The resolved
754 * destination info.
755 * @private
756 */
757 onProvisionalDestinationResolved_: function(
758 provisionalDestinationId, destinationInfo) {
759 var evt =
760 new Event(NativeLayer.EventType.PROVISIONAL_DESTINATION_RESOLVED);
761 evt.provisionalId = provisionalDestinationId;
762 evt.destination = destinationInfo;
763 this.eventTarget_.dispatchEvent(evt);
764 },
765
766 /**
767 * Allows for onManipulateSettings to be called 720 * Allows for onManipulateSettings to be called
768 * from the native layer. 721 * from the native layer.
769 * @private 722 * @private
770 */ 723 */
771 onEnableManipulateSettingsForTest_: function() { 724 onEnableManipulateSettingsForTest_: function() {
772 global.onManipulateSettingsForTest = 725 global.onManipulateSettingsForTest =
773 this.onManipulateSettingsForTest_.bind(this); 726 this.onManipulateSettingsForTest_.bind(this);
774 }, 727 },
775 728
776 /** 729 /**
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
979 return this.serializedDefaultDestinationSelectionRulesStr_; 932 return this.serializedDefaultDestinationSelectionRulesStr_;
980 } 933 }
981 }; 934 };
982 935
983 // Export 936 // Export
984 return { 937 return {
985 NativeInitialSettings: NativeInitialSettings, 938 NativeInitialSettings: NativeInitialSettings,
986 NativeLayer: NativeLayer 939 NativeLayer: NativeLayer
987 }; 940 };
988 }); 941 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698