Index: chrome/browser/resources/print_preview/preview_generator.js |
diff --git a/chrome/browser/resources/print_preview/preview_generator.js b/chrome/browser/resources/print_preview/preview_generator.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..be7c31cf14da169250a7db136d18f6cf3c17455d |
--- /dev/null |
+++ b/chrome/browser/resources/print_preview/preview_generator.js |
@@ -0,0 +1,282 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+cr.define('print_preview', function() { |
+ 'use strict'; |
+ |
+ /** |
+ * Interface to the Chromium print preview generator. |
+ * |
+ * @param {print_preview.PrintTicketStore!} printTicketStore Used to read the |
+ * state of the ticket and write document information. |
+ * @constructor |
+ * @extends {cr.EventTarget} |
+ */ |
+ function PreviewGenerator(printTicketStore) { |
+ cr.EventTarget.call(this); |
+ |
+ /** |
+ * Used to read the state of the ticket and write document information. |
+ * @type {print_preview.PrintTicketStore!} |
+ * @private |
+ */ |
+ this.printTicketStore_ = printTicketStore; |
+ |
+ /** |
+ * ID of current in-flight request. Requests that do not share this ID will |
+ * be ignored. |
+ * @type {number} |
+ * @private |
+ */ |
+ this.inFlightRequestId_ = -1; |
+ |
+ /** |
+ * Whether the previews are being generated in landscape mode. |
+ * @type {boolean} |
+ * @private |
+ */ |
+ this.isLandscapeEnabled_ = false; |
+ |
+ /** |
+ * Whether the previews are being generated with a header and footer. |
+ * @type {boolean} |
+ * @private |
+ */ |
+ this.isHeaderFooterEnabled_ = false; |
+ |
+ /** |
+ * Whether the previews are being generated in color. |
+ * @type {boolean} |
+ * @private |
+ */ |
+ this.isColorEnabled_ = false; |
+ |
+ /** |
+ * Page number set used to generate the last preview. |
+ * @type {print_preview.PageNumberSet} |
+ * @private |
+ */ |
+ this.pageNumberSet_ = null; |
+ |
+ /** |
+ * Event tracker used to keep track of native layer events. |
+ * @type {EventTracker} |
+ * @private |
+ */ |
+ this.tracker_ = new EventTracker(); |
+ |
+ this.addEventListeners_(); |
+ }; |
+ |
+ /** |
+ * Events dispatched by the preview generator. |
+ * @enum {string} |
+ */ |
+ PreviewGenerator.Event = { |
+ // Dispatched when the document can be printed. |
+ DOCUMENT_READY: 'print_preview.PreviewGenerator.DOCUMENT_READY', |
+ |
+ // Dispatched when a page preview is ready. The previewIndex field of the |
+ // event is the index of the page in the modified document, not the |
+ // original. So page 4 of the original document might be previewIndex = 0 of |
+ // the modified document. |
+ PAGE_READY: 'print_preview.PreviewGenerator.PAGE_READY', |
+ |
+ // Dispatched when the current print preview request fails. |
+ FAIL: 'print_preview.PreviewGenerator.FAIL' |
+ }; |
+ |
+ PreviewGenerator.prototype = { |
+ __proto__: cr.EventTarget.prototype, |
+ |
+ /** @override */ |
+ dispatchEvent: function(evt) { |
+ // TODO REMOVE ME |
+ log(evt.type); |
+ cr.EventTarget.prototype.dispatchEvent.call(this, evt); |
+ }, |
+ |
+ /** |
+ * Request that new preview be generated. A preview request will not be |
+ * generated if the print ticket has not changed sufficiently. |
+ */ |
+ requestPreview: function() { |
+ if (this.hasPreviewChanged_()) { |
+ log('print_preview.PreviewGenerator.requestPreview'); |
+ this.isLandscapeEnabled_ = this.printTicketStore_.isLandscapeEnabled(); |
+ this.isHeaderFooterEnabled_ = |
+ this.printTicketStore_.isHeaderFooterEnabled(); |
+ this.isColorEnabled_ = this.printTicketStore_.isColorEnabled(); |
+ this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet(); |
+ // TODO Update margins |
+ this.inFlightRequestId_++; |
+ // TODO Maybe move some of these ticket fields into nativeLayer API. |
+ var ticket = this.printTicketStore_.createTicketForPreviewGenerator(); |
+ ticket['requestID'] = this.inFlightRequestId_; |
+ ticket['isFirstRequest'] = this.inFlightRequestId_ == 0; |
+ nativeLayer.startGetPreview( |
+ JSON.stringify(ticket), |
+ this.inFlightRequestId_ == 0 ? -1 : this.printTicketStore_.pageCount, |
+ //this.printTicketStore_.getPageNumberSet().size, |
+ this.printTicketStore_.isDocumentModifiable); |
+ } |
+ }, |
+ |
+ /** |
+ * Adds event listeners to the relevant native layer events. |
+ * @private |
+ */ |
+ addEventListeners_: function() { |
+ this.tracker_.add( |
+ nativeLayer, |
+ print_preview.NativeLayer.Event.PAGE_LAYOUT_CHANGE, |
+ this.onPageLayoutChange_.bind(this)); |
+ this.tracker_.add( |
+ nativeLayer, |
+ print_preview.NativeLayer.Event.PAGE_COUNT_CHANGE, |
+ this.onPageCountChange_.bind(this)); |
+ this.tracker_.add( |
+ nativeLayer, |
+ print_preview.NativeLayer.Event.PREVIEW_RELOAD, |
+ this.onPreviewReload_.bind(this)); |
+ this.tracker_.add( |
+ nativeLayer, |
+ print_preview.NativeLayer.Event.PAGE_PREVIEW_READY, |
+ this.onPagePreviewReady_.bind(this)); |
+ this.tracker_.add( |
+ nativeLayer, |
+ print_preview.NativeLayer.Event.PREVIEW_GENERATION_DONE, |
+ this.onPreviewGenerationDone_.bind(this)); |
+ this.tracker_.add( |
+ nativeLayer, |
+ print_preview.NativeLayer.Event.PREVIEW_GENERATION_FAIL, |
+ this.onPreviewGenerationFail_.bind(this)); |
+ }, |
+ |
+ dispatchPageReadyEvent_: function(previewIndex, pageNumber, previewUid) { |
+ var pageGenEvt = new cr.Event(PreviewGenerator.Event.PAGE_READY); |
+ pageGenEvt.previewIndex = previewIndex; |
+ pageGenEvt.previewUrl = |
+ 'chrome://print/' + previewUid + '/' + (pageNumber - 1) + |
+ '/print.pdf'; |
+ this.dispatchEvent(pageGenEvt); |
+ }, |
+ |
+ /** |
+ * Called when the page layout of the document has changed. |
+ * @param {cr.Event} evt Contains information about the document. |
+ * @private |
+ */ |
+ onPageLayoutChange_: function(evt) { |
+ var pageLayout = evt.pageLayout; |
+ var hasCustomPageSizeStyle = evt.hasCustomPageSizeStyle; |
+ // NOTE: A request ID is not specified, so assuming its for the current |
+ // in-flight request. |
+ // TODO |
+ // hasPageSizeStyle = hasCustomPageSizeStyle; |
+ // marginSettings.currentDefaultPageLayout = new print_preview.PageLayout( |
+ // pageLayout.contentWidth, |
+ // pageLayout.contentHeight, |
+ // pageLayout.marginLeft, |
+ // pageLayout.marginTop, |
+ // pageLayout.marginRight, |
+ // pageLayout.marginBottom); |
+ // headerFooterSettings.checkAndHideHeaderFooterOption( |
+ // pageLayout, marginSettings.selectedMarginsValue); |
+ }, |
+ |
+ /** |
+ * Called when the document page count is received from the native layer. |
+ * @param {cr.Event} evt Contains the document's page count. |
+ * @private |
+ */ |
+ onPageCountChange_: function(evt) { |
+ if (this.inFlightRequestId_ != evt.previewResponseId) { |
+ return; // Ignore old response. |
+ } |
+ this.printTicketStore_.updatePageCount(evt.pageCount); |
+ }, |
+ |
+ /** |
+ * Called when the print preview should be reloaded. |
+ * @param {cr.Event} evt Contains the preview UID and request ID. |
+ * @private |
+ */ |
+ onPreviewReload_: function(evt) { |
+ if (this.inFlightRequestId_ != evt.previewResponseId) { |
+ return; // Ignore old response. |
+ } |
+ var pageNumberSetSize = this.printTicketStore_.getPageNumberSet().size; |
+ for (var i = 0; i < pageNumberSetSize; i++) { |
+ var pageNumber = |
+ this.printTicketStore_.getPageNumberSet().getPageNumberAt(i); |
+ log('PageReady -- idx:' + i + ' num:' + pageNumber); |
+ this.dispatchPageReadyEvent_(i, pageNumber, evt.previewUid); |
+ } |
+ }, |
+ |
+ /** |
+ * Called when a page's preview has been generated. Dispatches a |
+ * PAGE_READY event. |
+ * @param {cr.Event} evt Contains the page index and preview UID. |
+ * @private |
+ */ |
+ onPagePreviewReady_: function(evt) { |
+ if (this.inFlightRequestId_ != evt.previewResponseId) { |
+ return; // Ignore old response. |
+ } |
+ var pageNumber = evt.pageIndex + 1; |
+ if (this.printTicketStore_.getPageNumberSet().hasPageNumber(pageNumber)) { |
+ var previewIndex = this.printTicketStore_.getPageNumberSet() |
+ .getPageNumberIndex(pageNumber); |
+ this.dispatchPageReadyEvent_(previewIndex, pageNumber, evt.previewUid); |
+ } |
+ }, |
+ |
+ /** |
+ * Called when the preview generation is complete. Dispatches a |
+ * DOCUMENT_READY event. |
+ * @param {cr.Event} evt Contains the preview UID and response ID. |
+ * @private |
+ */ |
+ onPreviewGenerationDone_: function(evt) { |
+ if (this.inFlightRequestId_ != evt.previewResponseId) { |
+ return; // Ignore old response. |
+ } |
+ }, |
+ |
+ /** |
+ * Called when the preview generation fails. |
+ * @private |
+ */ |
+ onPreviewGenerationFail_: function() { |
+ // NOTE: No request ID is returned from Chromium so its assumed its the |
+ // current one. |
+ cr.dispatchSimpleEvent(this, PreviewGenerator.Event.FAIL); |
+ }, |
+ |
+ /** |
+ * @return {boolean} Whether the print ticket has changed sufficiently to |
+ * determine whether a new preview request should be issued. |
+ * @private |
+ */ |
+ hasPreviewChanged_: function() { |
+ var ticketStore = this.printTicketStore_; |
+ var result = this.inFlightRequestId_ == -1 || |
+ ticketStore.isLandscapeEnabled() != this.isLandscapeEnabled_ || |
+ ticketStore.isHeaderFooterEnabled() != this.isHeaderFooterEnabled_ || |
+ ticketStore.isColorEnabled() != this.isColorEnabled_ || |
+ !ticketStore.getPageNumberSet().equals(this.pageNumberSet_); |
+ // TODO Check margins. |
+ log('print_preview.PreviewGenerator.hasPreviewChanged_: ' + |
+ ticketStore.getPageNumberSet().equals(this.pageNumberSet_)); |
+ return result; |
+ }, |
+ }; |
+ |
+ // Export |
+ return { |
+ PreviewGenerator: PreviewGenerator |
+ }; |
+}); |