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 * Interface to the Chromium print preview generator. |
| 10 * |
| 11 * @param {print_preview.PrintTicketStore!} printTicketStore Used to read the |
| 12 * state of the ticket and write document information. |
| 13 * @constructor |
| 14 * @extends {cr.EventTarget} |
| 15 */ |
| 16 function PreviewGenerator(printTicketStore) { |
| 17 cr.EventTarget.call(this); |
| 18 |
| 19 /** |
| 20 * Used to read the state of the ticket and write document information. |
| 21 * @type {print_preview.PrintTicketStore!} |
| 22 * @private |
| 23 */ |
| 24 this.printTicketStore_ = printTicketStore; |
| 25 |
| 26 /** |
| 27 * ID of current in-flight request. Requests that do not share this ID will |
| 28 * be ignored. |
| 29 * @type {number} |
| 30 * @private |
| 31 */ |
| 32 this.inFlightRequestId_ = -1; |
| 33 |
| 34 /** |
| 35 * Whether the previews are being generated in landscape mode. |
| 36 * @type {boolean} |
| 37 * @private |
| 38 */ |
| 39 this.isLandscapeEnabled_ = false; |
| 40 |
| 41 /** |
| 42 * Whether the previews are being generated with a header and footer. |
| 43 * @type {boolean} |
| 44 * @private |
| 45 */ |
| 46 this.isHeaderFooterEnabled_ = false; |
| 47 |
| 48 /** |
| 49 * Whether the previews are being generated in color. |
| 50 * @type {boolean} |
| 51 * @private |
| 52 */ |
| 53 this.isColorEnabled_ = false; |
| 54 |
| 55 /** |
| 56 * Page number set used to generate the last preview. |
| 57 * @type {print_preview.PageNumberSet} |
| 58 * @private |
| 59 */ |
| 60 this.pageNumberSet_ = null; |
| 61 |
| 62 /** |
| 63 * Event tracker used to keep track of native layer events. |
| 64 * @type {EventTracker} |
| 65 * @private |
| 66 */ |
| 67 this.tracker_ = new EventTracker(); |
| 68 |
| 69 this.addEventListeners_(); |
| 70 }; |
| 71 |
| 72 /** |
| 73 * Events dispatched by the preview generator. |
| 74 * @enum {string} |
| 75 */ |
| 76 PreviewGenerator.Event = { |
| 77 // Dispatched when the document can be printed. |
| 78 DOCUMENT_READY: 'print_preview.PreviewGenerator.DOCUMENT_READY', |
| 79 |
| 80 // Dispatched when a page preview is ready. The previewIndex field of the |
| 81 // event is the index of the page in the modified document, not the |
| 82 // original. So page 4 of the original document might be previewIndex = 0 of |
| 83 // the modified document. |
| 84 PAGE_READY: 'print_preview.PreviewGenerator.PAGE_READY', |
| 85 |
| 86 // Dispatched when the current print preview request fails. |
| 87 FAIL: 'print_preview.PreviewGenerator.FAIL' |
| 88 }; |
| 89 |
| 90 PreviewGenerator.prototype = { |
| 91 __proto__: cr.EventTarget.prototype, |
| 92 |
| 93 /** @override */ |
| 94 dispatchEvent: function(evt) { |
| 95 // TODO REMOVE ME |
| 96 log(evt.type); |
| 97 cr.EventTarget.prototype.dispatchEvent.call(this, evt); |
| 98 }, |
| 99 |
| 100 /** |
| 101 * Request that new preview be generated. A preview request will not be |
| 102 * generated if the print ticket has not changed sufficiently. |
| 103 */ |
| 104 requestPreview: function() { |
| 105 if (this.hasPreviewChanged_()) { |
| 106 log('print_preview.PreviewGenerator.requestPreview'); |
| 107 this.isLandscapeEnabled_ = this.printTicketStore_.isLandscapeEnabled(); |
| 108 this.isHeaderFooterEnabled_ = |
| 109 this.printTicketStore_.isHeaderFooterEnabled(); |
| 110 this.isColorEnabled_ = this.printTicketStore_.isColorEnabled(); |
| 111 this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet(); |
| 112 // TODO Update margins |
| 113 this.inFlightRequestId_++; |
| 114 // TODO Maybe move some of these ticket fields into nativeLayer API. |
| 115 var ticket = this.printTicketStore_.createTicketForPreviewGenerator(); |
| 116 ticket['requestID'] = this.inFlightRequestId_; |
| 117 ticket['isFirstRequest'] = this.inFlightRequestId_ == 0; |
| 118 nativeLayer.startGetPreview( |
| 119 JSON.stringify(ticket), |
| 120 this.inFlightRequestId_ == 0 ? -1 : this.printTicketStore_.pageCount
, |
| 121 //this.printTicketStore_.getPageNumberSet().size, |
| 122 this.printTicketStore_.isDocumentModifiable); |
| 123 } |
| 124 }, |
| 125 |
| 126 /** |
| 127 * Adds event listeners to the relevant native layer events. |
| 128 * @private |
| 129 */ |
| 130 addEventListeners_: function() { |
| 131 this.tracker_.add( |
| 132 nativeLayer, |
| 133 print_preview.NativeLayer.Event.PAGE_LAYOUT_CHANGE, |
| 134 this.onPageLayoutChange_.bind(this)); |
| 135 this.tracker_.add( |
| 136 nativeLayer, |
| 137 print_preview.NativeLayer.Event.PAGE_COUNT_CHANGE, |
| 138 this.onPageCountChange_.bind(this)); |
| 139 this.tracker_.add( |
| 140 nativeLayer, |
| 141 print_preview.NativeLayer.Event.PREVIEW_RELOAD, |
| 142 this.onPreviewReload_.bind(this)); |
| 143 this.tracker_.add( |
| 144 nativeLayer, |
| 145 print_preview.NativeLayer.Event.PAGE_PREVIEW_READY, |
| 146 this.onPagePreviewReady_.bind(this)); |
| 147 this.tracker_.add( |
| 148 nativeLayer, |
| 149 print_preview.NativeLayer.Event.PREVIEW_GENERATION_DONE, |
| 150 this.onPreviewGenerationDone_.bind(this)); |
| 151 this.tracker_.add( |
| 152 nativeLayer, |
| 153 print_preview.NativeLayer.Event.PREVIEW_GENERATION_FAIL, |
| 154 this.onPreviewGenerationFail_.bind(this)); |
| 155 }, |
| 156 |
| 157 dispatchPageReadyEvent_: function(previewIndex, pageNumber, previewUid) { |
| 158 var pageGenEvt = new cr.Event(PreviewGenerator.Event.PAGE_READY); |
| 159 pageGenEvt.previewIndex = previewIndex; |
| 160 pageGenEvt.previewUrl = |
| 161 'chrome://print/' + previewUid + '/' + (pageNumber - 1) + |
| 162 '/print.pdf'; |
| 163 this.dispatchEvent(pageGenEvt); |
| 164 }, |
| 165 |
| 166 /** |
| 167 * Called when the page layout of the document has changed. |
| 168 * @param {cr.Event} evt Contains information about the document. |
| 169 * @private |
| 170 */ |
| 171 onPageLayoutChange_: function(evt) { |
| 172 var pageLayout = evt.pageLayout; |
| 173 var hasCustomPageSizeStyle = evt.hasCustomPageSizeStyle; |
| 174 // NOTE: A request ID is not specified, so assuming its for the current |
| 175 // in-flight request. |
| 176 // TODO |
| 177 // hasPageSizeStyle = hasCustomPageSizeStyle; |
| 178 // marginSettings.currentDefaultPageLayout = new print_preview.PageLayout
( |
| 179 // pageLayout.contentWidth, |
| 180 // pageLayout.contentHeight, |
| 181 // pageLayout.marginLeft, |
| 182 // pageLayout.marginTop, |
| 183 // pageLayout.marginRight, |
| 184 // pageLayout.marginBottom); |
| 185 // headerFooterSettings.checkAndHideHeaderFooterOption( |
| 186 // pageLayout, marginSettings.selectedMarginsValue); |
| 187 }, |
| 188 |
| 189 /** |
| 190 * Called when the document page count is received from the native layer. |
| 191 * @param {cr.Event} evt Contains the document's page count. |
| 192 * @private |
| 193 */ |
| 194 onPageCountChange_: function(evt) { |
| 195 if (this.inFlightRequestId_ != evt.previewResponseId) { |
| 196 return; // Ignore old response. |
| 197 } |
| 198 this.printTicketStore_.updatePageCount(evt.pageCount); |
| 199 }, |
| 200 |
| 201 /** |
| 202 * Called when the print preview should be reloaded. |
| 203 * @param {cr.Event} evt Contains the preview UID and request ID. |
| 204 * @private |
| 205 */ |
| 206 onPreviewReload_: function(evt) { |
| 207 if (this.inFlightRequestId_ != evt.previewResponseId) { |
| 208 return; // Ignore old response. |
| 209 } |
| 210 var pageNumberSetSize = this.printTicketStore_.getPageNumberSet().size; |
| 211 for (var i = 0; i < pageNumberSetSize; i++) { |
| 212 var pageNumber = |
| 213 this.printTicketStore_.getPageNumberSet().getPageNumberAt(i); |
| 214 log('PageReady -- idx:' + i + ' num:' + pageNumber); |
| 215 this.dispatchPageReadyEvent_(i, pageNumber, evt.previewUid); |
| 216 } |
| 217 }, |
| 218 |
| 219 /** |
| 220 * Called when a page's preview has been generated. Dispatches a |
| 221 * PAGE_READY event. |
| 222 * @param {cr.Event} evt Contains the page index and preview UID. |
| 223 * @private |
| 224 */ |
| 225 onPagePreviewReady_: function(evt) { |
| 226 if (this.inFlightRequestId_ != evt.previewResponseId) { |
| 227 return; // Ignore old response. |
| 228 } |
| 229 var pageNumber = evt.pageIndex + 1; |
| 230 if (this.printTicketStore_.getPageNumberSet().hasPageNumber(pageNumber)) { |
| 231 var previewIndex = this.printTicketStore_.getPageNumberSet() |
| 232 .getPageNumberIndex(pageNumber); |
| 233 this.dispatchPageReadyEvent_(previewIndex, pageNumber, evt.previewUid); |
| 234 } |
| 235 }, |
| 236 |
| 237 /** |
| 238 * Called when the preview generation is complete. Dispatches a |
| 239 * DOCUMENT_READY event. |
| 240 * @param {cr.Event} evt Contains the preview UID and response ID. |
| 241 * @private |
| 242 */ |
| 243 onPreviewGenerationDone_: function(evt) { |
| 244 if (this.inFlightRequestId_ != evt.previewResponseId) { |
| 245 return; // Ignore old response. |
| 246 } |
| 247 }, |
| 248 |
| 249 /** |
| 250 * Called when the preview generation fails. |
| 251 * @private |
| 252 */ |
| 253 onPreviewGenerationFail_: function() { |
| 254 // NOTE: No request ID is returned from Chromium so its assumed its the |
| 255 // current one. |
| 256 cr.dispatchSimpleEvent(this, PreviewGenerator.Event.FAIL); |
| 257 }, |
| 258 |
| 259 /** |
| 260 * @return {boolean} Whether the print ticket has changed sufficiently to |
| 261 * determine whether a new preview request should be issued. |
| 262 * @private |
| 263 */ |
| 264 hasPreviewChanged_: function() { |
| 265 var ticketStore = this.printTicketStore_; |
| 266 var result = this.inFlightRequestId_ == -1 || |
| 267 ticketStore.isLandscapeEnabled() != this.isLandscapeEnabled_ || |
| 268 ticketStore.isHeaderFooterEnabled() != this.isHeaderFooterEnabled_ || |
| 269 ticketStore.isColorEnabled() != this.isColorEnabled_ || |
| 270 !ticketStore.getPageNumberSet().equals(this.pageNumberSet_); |
| 271 // TODO Check margins. |
| 272 log('print_preview.PreviewGenerator.hasPreviewChanged_: ' + |
| 273 ticketStore.getPageNumberSet().equals(this.pageNumberSet_)); |
| 274 return result; |
| 275 }, |
| 276 }; |
| 277 |
| 278 // Export |
| 279 return { |
| 280 PreviewGenerator: PreviewGenerator |
| 281 }; |
| 282 }); |
OLD | NEW |