OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 cr.define('print_preview', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * An interface to the native Chromium printing system layer. |
| 10 * |
| 11 * @constructor |
| 12 * @extends {cr.EventTarget} |
| 13 */ |
| 14 function NativeLayer() { |
| 15 cr.EventTarget.call(this); |
| 16 }; |
| 17 |
| 18 /** |
| 19 * Events dispatched from the Chromium native layer. |
| 20 * @enum {string} |
| 21 * @const |
| 22 */ |
| 23 NativeLayer.Event = { |
| 24 CAPABILITIES_SET: 'print_preview.NativeLayer.CAPABILITIES_SET', |
| 25 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE', |
| 26 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD', |
| 27 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL', |
| 28 FILE_SELECTION_COMPLETE: |
| 29 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE', |
| 30 INITIAL_SETTINGS_SET: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET', |
| 31 LOCAL_DESTINATIONS_SET: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET', |
| 32 PAGE_COUNT_CHANGE: 'print_preview.NativeLayer.PAGE_COUNT_CHANGE', |
| 33 PAGE_LAYOUT_CHANGE: 'print_preview.NativeLayer.PAGE_LAYOUT_CHANGE', |
| 34 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY', |
| 35 PREVIEW_GENERATION_DONE: |
| 36 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE', |
| 37 PREVIEW_GENERATION_FAIL: |
| 38 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL', |
| 39 PREVIEW_RELOAD: 'print_preview.NativeLayer.PREVIEW_RELOAD', |
| 40 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD', |
| 41 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID' |
| 42 }; |
| 43 |
| 44 NativeLayer.prototype = { |
| 45 __proto__: cr.EventTarget.prototype, |
| 46 |
| 47 // TODO Remove me |
| 48 dispatchEvent: function(evt) { |
| 49 log(evt.type); |
| 50 cr.EventTarget.prototype.dispatchEvent.call(this, evt); |
| 51 }, |
| 52 |
| 53 /** Gets the initial settings to initialize the print preview with. */ |
| 54 startGetInitialSettings: function() { |
| 55 chrome.send('getInitialSettings'); |
| 56 }, |
| 57 |
| 58 /** |
| 59 * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET |
| 60 * event will be dispatched in response. |
| 61 */ |
| 62 startGetLocalDestinations: function() { |
| 63 chrome.send('getPrinters'); |
| 64 }, |
| 65 |
| 66 /** |
| 67 * Requests the destination's printing capabilities. A CAPABILITIES_SET |
| 68 * event will be dispatched in response. |
| 69 * @param {string} destinationId ID of the destination. |
| 70 */ |
| 71 startGetLocalDestinationCapabilities: function(destinationId) { |
| 72 chrome.send('getPrinterCapabilities', [destinationId]); |
| 73 }, |
| 74 |
| 75 /** |
| 76 * Requests that a preview be generated. The following events may be |
| 77 * dispatched in response: |
| 78 * - PAGE_COUNT_CHANGE |
| 79 * - PAGE_LAYOUT_CHANGE |
| 80 * - PAGE_PREVIEW_READY |
| 81 * - PREVIEW_GENERATION_DONE |
| 82 * - PREVIEW_GENERATION_FAIL |
| 83 * - PREVIEW_RELOAD |
| 84 * @param {string} serializedPrintTicket Seriliazed form of a print ticket. |
| 85 * @param {number} pageCount Page count of the document to preview. |
| 86 * @param {boolean} isDocumentModifiable Whether the document is modifiable. |
| 87 */ |
| 88 startGetPreview: function( |
| 89 serializedPrintTicket, pageCount, isDocumentModifiable) { |
| 90 chrome.send( |
| 91 'getPreview', |
| 92 [serializedPrintTicket, pageCount, isDocumentModifiable]); |
| 93 }, |
| 94 |
| 95 /** |
| 96 * Persists the selected destination and print ticket for the next print |
| 97 * session. |
| 98 * @param {string} destinatId ID of the destination. |
| 99 * @param {string} serializedTicket Serialized form of the print ticket. |
| 100 */ |
| 101 startSaveDestinationAndTicket: function(destinationId, serializedTicket) { |
| 102 chrome.send('saveLastPrinter', [destinationId, serializedTicket]); |
| 103 }, |
| 104 |
| 105 /** |
| 106 * Requests that the document be printed. |
| 107 * @param {string} localTicket Serialized ticket for printing to a local |
| 108 * destination. |
| 109 * @param {string} cloudTicket Serialized ticket for printing to a cloud |
| 110 * destination. |
| 111 */ |
| 112 startPrint: function(localTicket, cloudTicket) { |
| 113 chrome.send('print', [localTicket, cloudTicket]); |
| 114 }, |
| 115 |
| 116 /** Requests that the current pending print request be cancelled. */ |
| 117 startCancelPendingPrint: function() { |
| 118 chrome.send('cancelPendingPrintRequest'); |
| 119 }, |
| 120 |
| 121 /** Shows the system's native printing dialog. */ |
| 122 startShowSystemDialog: function() { |
| 123 chrome.send('showSystemDialog'); |
| 124 }, |
| 125 |
| 126 /** Closes the print preview dialog. */ |
| 127 startCloseDialog: function() { |
| 128 chrome.send('closePrintPreviewTab'); |
| 129 chrome.send('DialogClose'); |
| 130 }, |
| 131 |
| 132 /** |
| 133 * Opens the Google Cloud Print sign-in dialog. The DESTINATIONS_RELOAD |
| 134 * event will be dispatched in response. |
| 135 */ |
| 136 startCloudPrintSignIn: function() { |
| 137 chrome.send('signIn'); |
| 138 } |
| 139 }; |
| 140 |
| 141 return { |
| 142 NativeLayer: NativeLayer |
| 143 }; |
| 144 }); |
| 145 |
| 146 var nativeLayer = new print_preview.NativeLayer(); |
| 147 |
| 148 /** @param {object} initialSettings Object containing all initial settings. */ |
| 149 function setInitialSettings(initialSettings) { |
| 150 var initialSettingsSetEvt = new cr.Event( |
| 151 print_preview.NativeLayer.Event.INITIAL_SETTINGS_SET); |
| 152 initialSettingsSetEvt.initialSettings = initialSettings; |
| 153 nativeLayer.dispatchEvent(initialSettingsSetEvt); |
| 154 } |
| 155 |
| 156 /** |
| 157 * Turn on the integration of Cloud Print. |
| 158 * @param {string} cloudPrintURL The URL to use for cloud print servers. |
| 159 */ |
| 160 function setUseCloudPrint(cloudPrintURL) { |
| 161 var cloudPrintEnableEvt = new cr.Event( |
| 162 print_preview.NativeLayer.Event.CLOUD_PRINT_ENABLE); |
| 163 cloudPrintEnableEvt.baseCloudPrintUrl = cloudPrintURL; |
| 164 nativeLayer.dispatchEvent(cloudPrintEnableEvt); |
| 165 } |
| 166 |
| 167 /** |
| 168 * Updates the print preview with local printers. |
| 169 * Called from PrintPreviewHandler::SetupPrinterList(). |
| 170 * @param {Array} printers Array of printer info objects. |
| 171 */ |
| 172 function setPrinters(printers) { |
| 173 var localDestsSetEvt = new cr.Event( |
| 174 print_preview.NativeLayer.Event.LOCAL_DESTINATIONS_SET); |
| 175 localDestsSetEvt.destinationInfos = printers; |
| 176 nativeLayer.dispatchEvent(localDestsSetEvt); |
| 177 } |
| 178 |
| 179 /** |
| 180 * Called when native layer gets settings information for a requested local |
| 181 * destination. |
| 182 * @param {Object} settingsInfo printer setting information. |
| 183 */ |
| 184 function updateWithPrinterCapabilities(settingsInfo) { |
| 185 var capsSetEvt = new cr.Event( |
| 186 print_preview.NativeLayer.Event.CAPABILITIES_SET); |
| 187 capsSetEvt.settingsInfo = settingsInfo; |
| 188 nativeLayer.dispatchEvent(capsSetEvt); |
| 189 } |
| 190 |
| 191 /** Reloads the printer list. */ |
| 192 function reloadPrintersList() { |
| 193 cr.dispatchSimpleEvent( |
| 194 nativeLayer, print_preview.NativeLayer.Event.DESTINATIONS_RELOAD); |
| 195 } |
| 196 |
| 197 /** |
| 198 * Called from the C++ layer. |
| 199 * Take the PDF data handed to us and submit it to the cloud, closing the print |
| 200 * preview tab once the upload is successful. |
| 201 * @param {string} data Data to send as the print job. |
| 202 */ |
| 203 function printToCloud(data) { |
| 204 var printToCloudEvt = new cr.Event( |
| 205 print_preview.NativeLayer.Event.PRINT_TO_CLOUD); |
| 206 printToCloudEvt.data = data; |
| 207 nativeLayer.dispatchEvent(printToCloudEvt); |
| 208 } |
| 209 |
| 210 /** |
| 211 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print |
| 212 * preview tab regarding the file selection cancel event. |
| 213 */ |
| 214 function fileSelectionCancelled() { |
| 215 cr.dispatchSimpleEvent( |
| 216 nativeLayer, print_preview.NativeLayer.Event.FILE_SELECTION_CANCEL); |
| 217 } |
| 218 |
| 219 /** |
| 220 * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print |
| 221 * preview tab regarding the file selection completed event. |
| 222 */ |
| 223 function fileSelectionCompleted() { |
| 224 // If the file selection is completed and the tab is not already closed it |
| 225 // means that a pending print to pdf request exists. |
| 226 cr.dispatchSimpleEvent( |
| 227 nativeLayer, print_preview.NativeLayer.Event.FILE_SELECTION_COMPLETE); |
| 228 } |
| 229 |
| 230 /** |
| 231 * Display an error message when print preview fails. |
| 232 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed(). |
| 233 */ |
| 234 function printPreviewFailed() { |
| 235 cr.dispatchSimpleEvent( |
| 236 nativeLayer, print_preview.NativeLayer.Event.PREVIEW_GENERATION_FAIL); |
| 237 } |
| 238 |
| 239 /** |
| 240 * Display an error message when encountered invalid printer settings. |
| 241 * Called from PrintPreviewMessageHandler::OnInvalidPrinterSettings(). |
| 242 */ |
| 243 function invalidPrinterSettings() { |
| 244 cr.dispatchSimpleEvent( |
| 245 nativeLayer, print_preview.NativeLayer.Event.SETTINGS_INVALID); |
| 246 } |
| 247 |
| 248 /** |
| 249 * @param {{contentWidth: number, contentHeight: number, marginLeft: number, |
| 250 * marginRight: number, marginTop: number, marginBottom: number, |
| 251 * printableAreaX: number, printableAreaY: number, |
| 252 * printableAreaWidth: number, printableAreaHeight: number}} pageLayout |
| 253 * Specifies default page layout details in points. |
| 254 * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed |
| 255 * document has a custom page size style. |
| 256 */ |
| 257 function onDidGetDefaultPageLayout(pageLayout, hasCustomPageSizeStyle) { |
| 258 var pageLayoutChangeEvt = new cr.Event( |
| 259 print_preview.NativeLayer.Event.PAGE_LAYOUT_CHANGE); |
| 260 pageLayoutChangeEvt.pageLayout = pageLayout; |
| 261 pageLayoutChangeEvt.hasCustomPageSizeStyle = hasCustomPageSizeStyle; |
| 262 nativeLayer.dispatchEvent(pageLayoutChangeEvt); |
| 263 } |
| 264 |
| 265 /** |
| 266 * Update the page count and check the page range. |
| 267 * Called from PrintPreviewUI::OnDidGetPreviewPageCount(). |
| 268 * @param {number} pageCount The number of pages. |
| 269 * @param {number} previewResponseId The preview request id that resulted in |
| 270 * this response. |
| 271 */ |
| 272 function onDidGetPreviewPageCount(pageCount, previewResponseId) { |
| 273 var pageCountChangeEvt = new cr.Event( |
| 274 print_preview.NativeLayer.Event.PAGE_COUNT_CHANGE); |
| 275 pageCountChangeEvt.pageCount = pageCount; |
| 276 pageCountChangeEvt.previewResponseId = previewResponseId; |
| 277 nativeLayer.dispatchEvent(pageCountChangeEvt); |
| 278 } |
| 279 |
| 280 /** |
| 281 * Called when no pipelining previewed pages. |
| 282 * @param {string} previewUid Preview unique identifier. |
| 283 * @param {number} previewResponseId The preview request id that resulted in |
| 284 * this response. |
| 285 */ |
| 286 function reloadPreviewPages(previewUid, previewResponseId) { |
| 287 var previewReloadEvt = new cr.Event( |
| 288 print_preview.NativeLayer.Event.PREVIEW_RELOAD); |
| 289 previewReloadEvt.previewUid = previewUid; |
| 290 previewReloadEvt.previewResponseId = previewResponseId; |
| 291 nativeLayer.dispatchEvent(previewReloadEvt); |
| 292 } |
| 293 |
| 294 /** |
| 295 * Notification that a print preview page has been rendered. |
| 296 * Check if the settings have changed and request a regeneration if needed. |
| 297 * Called from PrintPreviewUI::OnDidPreviewPage(). |
| 298 * @param {number} pageNumber The page number, 0-based. |
| 299 * @param {string} previewUid Preview unique identifier. |
| 300 * @param {number} previewResponseId The preview request id that resulted in |
| 301 * this response. |
| 302 */ |
| 303 function onDidPreviewPage(pageNumber, previewUid, previewResponseId) { |
| 304 var pagePreviewGenEvt = new cr.Event( |
| 305 print_preview.NativeLayer.Event.PAGE_PREVIEW_READY); |
| 306 pagePreviewGenEvt.pageIndex = pageNumber; |
| 307 pagePreviewGenEvt.previewUid = previewUid; |
| 308 pagePreviewGenEvt.previewResponseId = previewResponseId; |
| 309 nativeLayer.dispatchEvent(pagePreviewGenEvt); |
| 310 } |
| 311 |
| 312 /** |
| 313 * Update the print preview when new preview data is available. |
| 314 * Create the PDF plugin as needed. |
| 315 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
| 316 * @param {string} previewUid Preview unique identifier. |
| 317 * @param {number} previewResponseId The preview request id that resulted in |
| 318 * this response. |
| 319 */ |
| 320 function updatePrintPreview(previewUid, previewResponseId) { |
| 321 var previewGenDoneEvt = new cr.Event( |
| 322 print_preview.NativeLayer.Event.PREVIEW_GENERATION_DONE); |
| 323 previewGenDoneEvt.previewUid = previewUid; |
| 324 previewGenDoneEvt.previewResponseId = previewResponseId; |
| 325 nativeLayer.dispatchEvent(previewGenDoneEvt); |
| 326 } |
OLD | NEW |