| 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..f7ffd3f792d741a8199c34148af7e9790a1b02ce
|
| --- /dev/null
|
| +++ b/chrome/browser/resources/print_preview/preview_generator.js
|
| @@ -0,0 +1,332 @@
|
| +// 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';
|
| +
|
| + // TODO Need to handle termination case--detach event listeners from native
|
| + // layer.
|
| +
|
| + /**
|
| + * Interface to the Chromium print preview generator.
|
| + *
|
| + * @param {print_preview.DestinationStore!} destinationStore Used to get the
|
| + * currently selected destination.
|
| + * @param {print_preview.PrintTicketStore!} printTicketStore Used to read the
|
| + * state of the ticket and write document information.
|
| + * @param {print_preview.NativeLayer!} nativeLayer Used to communicate to
|
| + * Chromium's preview rendering system.
|
| + * @constructor
|
| + * @extends {cr.EventTarget}
|
| + */
|
| + function PreviewGenerator(destinationStore, printTicketStore, nativeLayer) {
|
| + cr.EventTarget.call(this);
|
| +
|
| + /**
|
| + * Used to get the currently selected destination.
|
| + * @type {print_preview.DestinationStore!}
|
| + * @private
|
| + */
|
| + this.destinationStore_ = destinationStore;
|
| +
|
| + /**
|
| + * Used to read the state of the ticket and write document information.
|
| + * @type {print_preview.PrintTicketStore!}
|
| + * @private
|
| + */
|
| + this.printTicketStore_ = printTicketStore;
|
| +
|
| + /**
|
| + * Interface to the Chromium native layer.
|
| + * @type {print_preview.NativeLayer!}
|
| + * @private
|
| + */
|
| + this.nativeLayer_ = nativeLayer;
|
| +
|
| + /**
|
| + * 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;
|
| +
|
| + /**
|
| + * Margins type used to generate the last preview.
|
| + * @type {print_preview.Margins.Type}
|
| + * @private
|
| + */
|
| + this.marginsType_ = print_preview.Margins.Type.DEFAULT;
|
| +
|
| + /**
|
| + * Custom margins used to generate the last preview.
|
| + * @type {print_preview.Margins}
|
| + * @private
|
| + */
|
| + this.customMargins_ = 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();
|
| + this.marginsType_ = this.printTicketStore_.getMarginsType();
|
| + this.customMargins_ = this.printTicketStore_.getCustomMargins();
|
| +
|
| + this.inFlightRequestId_++;
|
| + this.nativeLayer_.startGetPreview(
|
| + this.destinationStore_.selectedDestination,
|
| + this.printTicketStore_,
|
| + this.inFlightRequestId_);
|
| + }
|
| + },
|
| +
|
| + /**
|
| + * Adds event listeners to the relevant native layer events.
|
| + * @private
|
| + */
|
| + addEventListeners_: function() {
|
| + this.tracker_.add(
|
| + this.nativeLayer_,
|
| + print_preview.NativeLayer.Event.PAGE_LAYOUT_CHANGE,
|
| + this.onPageLayoutChange_.bind(this));
|
| + this.tracker_.add(
|
| + this.nativeLayer_,
|
| + print_preview.NativeLayer.Event.PAGE_COUNT_CHANGE,
|
| + this.onPageCountChange_.bind(this));
|
| + this.tracker_.add(
|
| + this.nativeLayer_,
|
| + print_preview.NativeLayer.Event.PREVIEW_RELOAD,
|
| + this.onPreviewReload_.bind(this));
|
| + this.tracker_.add(
|
| + this.nativeLayer_,
|
| + print_preview.NativeLayer.Event.PAGE_PREVIEW_READY,
|
| + this.onPagePreviewReady_.bind(this));
|
| + this.tracker_.add(
|
| + this.nativeLayer_,
|
| + print_preview.NativeLayer.Event.PREVIEW_GENERATION_DONE,
|
| + this.onPreviewGenerationDone_.bind(this));
|
| + this.tracker_.add(
|
| + this.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. Always occurs
|
| + * as a result of a preview request.
|
| + * @param {cr.Event} evt Contains layout info about the document.
|
| + * @private
|
| + */
|
| + onPageLayoutChange_: function(evt) {
|
| + // NOTE: A request ID is not specified, so assuming its for the current
|
| + // in-flight request.
|
| +
|
| + // TODO Do we need this?
|
| + //var hasCustomPageSizeStyle = evt.hasCustomPageSizeStyle;
|
| +
|
| + // Printable area info is unavailable on linux nor Google Chrome OS.
|
| + // TODO Maybe we can not have to have this if condition, and always set
|
| + // printable area?
|
| + log(JSON.stringify(evt.pageLayout));
|
| + if (!cr.isLinux && !cr.isChromeOS) {
|
| + var origin = new print_preview.Coordinate2d(
|
| + evt.pageLayout.printableAreaX,
|
| + evt.pageLayout.printableAreaY);
|
| + var size = new print_preview.Size(
|
| + evt.pageLayout.printableAreaWidth,
|
| + evt.pageLayout.printableAreaHeight);
|
| + this.printTicketStore_.updatePrintableArea(
|
| + new print_preview.PrintableArea(origin, size));
|
| + } // TODO else create a default printable area?
|
| +
|
| + var margins = new print_preview.Margins(
|
| + evt.pageLayout.marginTop,
|
| + evt.pageLayout.marginRight,
|
| + evt.pageLayout.marginBottom,
|
| + evt.pageLayout.marginLeft);
|
| + if (this.printTicketStore_.hasMarginsCapability()) {
|
| + this.printTicketStore_.updateCustomMargins(margins);
|
| + }
|
| +
|
| + var pageSize = new print_preview.Size(
|
| + evt.pageLayout.contentWidth + margins.left + margins.right,
|
| + evt.pageLayout.contentHeight + margins.top + margins.bottom);
|
| + this.printTicketStore_.updatePageSize(pageSize);
|
| + },
|
| +
|
| + /**
|
| + * Called when the document page count is received from the native layer.
|
| + * Always occurs as a result of a preview request.
|
| + * @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 pageNumberSet = this.printTicketStore_.getPageNumberSet();
|
| + for (var i = 0; i < pageNumberSet.size; i++) {
|
| + var pageNumber = pageNumberSet.getPageNumberAt(i);
|
| + 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_;
|
| + return this.inFlightRequestId_ == -1 ||
|
| + ticketStore.isLandscapeEnabled() != this.isLandscapeEnabled_ ||
|
| + ticketStore.isHeaderFooterEnabled() != this.isHeaderFooterEnabled_ ||
|
| + ticketStore.isColorEnabled() != this.isColorEnabled_ ||
|
| + !ticketStore.getPageNumberSet().equals(this.pageNumberSet_) ||
|
| + ticketStore.getMarginsType() != this.marginsType_ ||
|
| + (this.marginsType_ == print_preview.Margins.Type.CUSTOM &&
|
| + !ticketStore.getCustomMargins().equals(this.customMargins_));
|
| + }
|
| + };
|
| +
|
| + // Export
|
| + return {
|
| + PreviewGenerator: PreviewGenerator
|
| + };
|
| +});
|
|
|