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

Unified Diff: chrome/browser/resources/print_preview/native_layer.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove extra files Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/print_preview/native_layer.js
diff --git a/chrome/browser/resources/print_preview/native_layer.js b/chrome/browser/resources/print_preview/native_layer.js
new file mode 100644
index 0000000000000000000000000000000000000000..be43a8ee5868ddadcf52d230f73456693189c3fc
--- /dev/null
+++ b/chrome/browser/resources/print_preview/native_layer.js
@@ -0,0 +1,433 @@
+// 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';
+
+ /**
+ * An interface to the native Chromium printing system layer.
+ *
+ * @constructor
+ * @extends {cr.EventTarget}
+ */
+ function NativeLayer() {
+ cr.EventTarget.call(this);
+ };
+
+ /**
+ * Events dispatched from the Chromium native layer.
+ * @enum {string}
+ * @const
+ */
+ NativeLayer.Event = {
+ CAPABILITIES_SET: 'print_preview.NativeLayer.CAPABILITIES_SET',
+ CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
+ DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
+ FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
+ FILE_SELECTION_COMPLETE:
+ 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
+ INITIAL_SETTINGS_SET: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET',
+ LOCAL_DESTINATIONS_SET: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET',
+ PAGE_COUNT_CHANGE: 'print_preview.NativeLayer.PAGE_COUNT_CHANGE',
+ PAGE_LAYOUT_CHANGE: 'print_preview.NativeLayer.PAGE_LAYOUT_CHANGE',
+ PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
+ PREVIEW_GENERATION_DONE:
+ 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
+ PREVIEW_GENERATION_FAIL:
+ 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
+ PREVIEW_RELOAD: 'print_preview.NativeLayer.PREVIEW_RELOAD',
+ PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
+ SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID'
+ };
+
+ NativeLayer.prototype = {
+ __proto__: cr.EventTarget.prototype,
+
+ // TODO Remove me
+ dispatchEvent: function(evt) {
+ log(evt.type);
+ cr.EventTarget.prototype.dispatchEvent.call(this, evt);
+ },
+
+ /** Gets the initial settings to initialize the print preview with. */
+ startGetInitialSettings: function() {
+ chrome.send('getInitialSettings');
+ },
+
+ /**
+ * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
+ * event will be dispatched in response.
+ */
+ startGetLocalDestinations: function() {
+ chrome.send('getPrinters');
+ },
+
+ /**
+ * Requests the destination's printing capabilities. A CAPABILITIES_SET
+ * event will be dispatched in response.
+ * @param {string} destinationId ID of the destination.
+ */
+ startGetLocalDestinationCapabilities: function(destinationId) {
+ chrome.send('getPrinterCapabilities', [destinationId]);
+ },
+
+ /**
+ * Requests that a preview be generated. The following events may be
+ * dispatched in response:
+ * - PAGE_COUNT_CHANGE
+ * - PAGE_LAYOUT_CHANGE
+ * - PAGE_PREVIEW_READY
+ * - PREVIEW_GENERATION_DONE
+ * - PREVIEW_GENERATION_FAIL
+ * - PREVIEW_RELOAD
+ * @param {print_preview.Destination?} destination Destination to print to.
+ * @param {print_preview.PrintTicketStore!} printTicketStore Used to get the
+ * state of the print ticket.
+ * @param {number} ID of the preview request.
+ */
+ startGetPreview: function(destination, printTicketStore, requestId) {
+ if (!printTicketStore.isTicketValid()) {
+ throw Error('Trying to generate preview when ticket is not valid');
+ }
+
+ var ticket = {
+ 'pageRange': [], // TODO this.ticket_.pageRangeSequence,
+ 'landscape': printTicketStore.isLandscapeEnabled(),
+ 'color': printTicketStore.getColorMode(),
+ 'headerFooterEnabled': printTicketStore.isHeaderFooterEnabled(),
+ 'marginsType': printTicketStore.getMarginsType(),
+ 'isFirstRequest': requestId == 0,
+ 'requestID': requestId,
+ 'previewModifiable': printTicketStore.isDocumentModifiable,
+ 'printToPDF': destination != null && destination.isPrintToPdf,
+ 'printWithCloudPrint': destination != null && !destination.isLocal,
+ 'deviceName': destination == null ? 'foo' : destination.id,
+ 'cloudPrintID': destination == null ? 'foo' : destination.id,
+
+ // This field seems to issue a reloadPreviewPages call when true.
+ 'generateDraftData': true, // TODO What should this value be?
+
+ // NOTE: Even though the following fields don't directly relate to the
+ // preview, they still need to be included.
+ 'duplex': printTicketStore.getDuplexMode(),
+ 'copies': printTicketStore.getCopies(),
+ 'collate': printTicketStore.isCollateEnabled()
+ };
+
+ if (printTicketStore.getMarginsType() ==
+ print_preview.Margins.Type.CUSTOM) {
+ var customMargins = printTicketStore.getCustomMargins();
+ ticket['marginsCustom'] = {
+ 'marginTop': customMargins.top,
+ 'marginRight': customMargins.right,
+ 'marginBottom': customMargins.bottom,
+ 'marginLeft': customMargins.left
+ };
+ }
+
+ var pageCount = requestId == 0 ? -1 : printTicketStore.pageCount;
+ // TODO this.printTicketStore_.getPageNumberSet().size,
+ chrome.send(
+ 'getPreview',
+ [JSON.stringify(ticket),
+ pageCount,
+ printTicketStore.isDocumentModifiable]);
+ },
+
+ /**
+ * Persists the selected destination and print ticket for the next print
+ * session.
+ * @param {string} destinatId ID of the destination.
+ * @param {string} serializedTicket Serialized form of the print ticket.
+ */
+ startSaveDestinationAndTicket: function(destinationId, serializedTicket) {
+ chrome.send('saveLastPrinter', [destinationId, serializedTicket]);
+ },
+
+ /**
+ * Requests that the document be printed.
+ * @param {print_preview.Destination!} destination Destination to print to.
+ * @param {print_preview.PrintTicketStore!} printTicketStore Used to get the
+ * state of the print ticket.
+ * @param {print_preview.CloudPrintInterface?} cloudPrintInterface Interface
+ * to Google Cloud Print.
+ */
+ startPrint: function(destination, printTicketStore, cloudPrintInterface) {
+ if (!printTicketStore.isTicketValid()) {
+ throw Error(
+ 'Requesting ticket for preview generator when ticket is not valid');
+ }
+
+ var ticket = {
+ 'pageRange': [], // TODO this.ticket_.pageRangeSequence,
+ 'landscape': printTicketStore.isLandscapeEnabled(),
+ 'color': printTicketStore.getColorMode(),
+ 'headerFooterEnabled': printTicketStore.isHeaderFooterEnabled(),
+ 'marginsType': printTicketStore.getMarginsType(),
+ 'generateDraftData': true, // TODO What should this value be?
+ 'duplex': printTicketStore.getDuplexMode(),
+ 'copies': printTicketStore.getCopies(),
+ 'collate': printTicketStore.isCollateEnabled(),
+ 'previewModifiable': printTicketStore.isDocumentModifiable,
+ 'printToPDF': destination.isPrintToPdf,
+ 'printWithCloudPrint': !destination.isLocal,
+ 'deviceName': destination.id,
+ 'isFirstRequest': false,
+ 'requestID': -1
+ };
+
+ if (!destination.isLocal && !destination.isPrintWithCloudPrint) {
+ // We can't set cloudPrintID if the destination is "Print with Cloud
+ // Print" because the native system will try to print to Google Cloud
+ // Print with this ID instead of opening a Google Cloud Print dialog.
+ ticket['cloudPrintID'] = destination.id;
+ }
+
+ if (printTicketStore.getMarginsType() ==
+ print_preview.Margins.Type.CUSTOM) {
+ var customMargins = printTicketStore.getCustomMargins();
+ ticket['marginsCustom'] = {
+ 'marginTop': customMargins.top,
+ 'marginRight': customMargins.right,
+ 'marginBottom': customMargins.bottom,
+ 'marginLeft': customMargins.left
+ };
+ }
+
+ var cloudTicket = null;
+ if (!destination.isLocal) {
+ if (!cloudPrintInterface) {
+ throw Error(
+ 'Trying to print to a cloud destination but Google Cloud Print ' +
+ 'integration is disabled');
+ }
+ cloudTicket = cloudPrintInterface.createPrintTicket(
+ destination, printTicketStore);
+ cloudTicket = JSON.stringify(cloudTicket);
+ }
+
+ chrome.send('print', [JSON.stringify(ticket), cloudTicket]);
+ },
+
+ /** Requests that the current pending print request be cancelled. */
+ startCancelPendingPrint: function() {
+ chrome.send('cancelPendingPrintRequest');
+ },
+
+ /** Shows the system's native printing dialog. */
+ startShowSystemDialog: function() {
+ chrome.send('showSystemDialog');
+ },
+
+ /** Closes the print preview dialog. */
+ startCloseDialog: function() {
+ chrome.send('closePrintPreviewTab');
+ chrome.send('DialogClose');
+ },
+
+ /**
+ * Opens the Google Cloud Print sign-in dialog. The DESTINATIONS_RELOAD
+ * event will be dispatched in response.
+ */
+ startCloudPrintSignIn: function() {
+ chrome.send('signIn');
+ },
+
+ /** Navigates the user to the system printer settings interface. */
+ startManageLocalPrinters: function() {
+ chrome.send('manageLocalPrinters');
+ },
+
+ /** Navigates the user to the Google Cloud Print management page. */
+ startManageCloudPrinters: function() {
+ chrome.send('manageCloudPrinters');
+ }
+ };
+
+ return {
+ NativeLayer: NativeLayer
+ };
+});
+
+var nativeLayer = new print_preview.NativeLayer();
+
+/** @param {object} initialSettings Object containing all initial settings. */
+function setInitialSettings(initialSettings) {
+ var initialSettingsSetEvt = new cr.Event(
+ print_preview.NativeLayer.Event.INITIAL_SETTINGS_SET);
+ initialSettingsSetEvt.initialSettings = initialSettings;
+ nativeLayer.dispatchEvent(initialSettingsSetEvt);
+}
+
+/**
+ * Turn on the integration of Cloud Print.
+ * @param {string} cloudPrintURL The URL to use for cloud print servers.
+ */
+function setUseCloudPrint(cloudPrintURL) {
+ var cloudPrintEnableEvt = new cr.Event(
+ print_preview.NativeLayer.Event.CLOUD_PRINT_ENABLE);
+ cloudPrintEnableEvt.baseCloudPrintUrl = cloudPrintURL;
+ nativeLayer.dispatchEvent(cloudPrintEnableEvt);
+}
+
+/**
+ * Updates the print preview with local printers.
+ * Called from PrintPreviewHandler::SetupPrinterList().
+ * @param {Array} printers Array of printer info objects.
+ */
+function setPrinters(printers) {
+ var localDestsSetEvt = new cr.Event(
+ print_preview.NativeLayer.Event.LOCAL_DESTINATIONS_SET);
+ localDestsSetEvt.destinationInfos = printers;
+ nativeLayer.dispatchEvent(localDestsSetEvt);
+}
+
+/**
+ * Called when native layer gets settings information for a requested local
+ * destination.
+ * @param {Object} settingsInfo printer setting information.
+ */
+function updateWithPrinterCapabilities(settingsInfo) {
+ var capsSetEvt = new cr.Event(
+ print_preview.NativeLayer.Event.CAPABILITIES_SET);
+ capsSetEvt.settingsInfo = settingsInfo;
+ nativeLayer.dispatchEvent(capsSetEvt);
+}
+
+/** Reloads the printer list. */
+function reloadPrintersList() {
+ cr.dispatchSimpleEvent(
+ nativeLayer, print_preview.NativeLayer.Event.DESTINATIONS_RELOAD);
+}
+
+/**
+ * Called from the C++ layer.
+ * Take the PDF data handed to us and submit it to the cloud, closing the print
+ * preview tab once the upload is successful.
+ * @param {string} data Data to send as the print job.
+ */
+function printToCloud(data) {
+ var printToCloudEvt = new cr.Event(
+ print_preview.NativeLayer.Event.PRINT_TO_CLOUD);
+ printToCloudEvt.data = data;
+ nativeLayer.dispatchEvent(printToCloudEvt);
+}
+
+/**
+ * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
+ * preview tab regarding the file selection cancel event.
+ */
+function fileSelectionCancelled() {
+ cr.dispatchSimpleEvent(
+ nativeLayer, print_preview.NativeLayer.Event.FILE_SELECTION_CANCEL);
+}
+
+/**
+ * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print
+ * preview tab regarding the file selection completed event.
+ */
+function fileSelectionCompleted() {
+ // If the file selection is completed and the tab is not already closed it
+ // means that a pending print to pdf request exists.
+ cr.dispatchSimpleEvent(
+ nativeLayer, print_preview.NativeLayer.Event.FILE_SELECTION_COMPLETE);
+}
+
+/**
+ * Display an error message when print preview fails.
+ * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed().
+ */
+function printPreviewFailed() {
+ cr.dispatchSimpleEvent(
+ nativeLayer, print_preview.NativeLayer.Event.PREVIEW_GENERATION_FAIL);
+}
+
+/**
+ * Display an error message when encountered invalid printer settings.
+ * Called from PrintPreviewMessageHandler::OnInvalidPrinterSettings().
+ */
+function invalidPrinterSettings() {
+ cr.dispatchSimpleEvent(
+ nativeLayer, print_preview.NativeLayer.Event.SETTINGS_INVALID);
+}
+
+/**
+ * @param {{contentWidth: number, contentHeight: number, marginLeft: number,
+ * marginRight: number, marginTop: number, marginBottom: number,
+ * printableAreaX: number, printableAreaY: number,
+ * printableAreaWidth: number, printableAreaHeight: number}} pageLayout
+ * Specifies default page layout details in points.
+ * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed
+ * document has a custom page size style.
+ */
+function onDidGetDefaultPageLayout(pageLayout, hasCustomPageSizeStyle) {
+ var pageLayoutChangeEvt = new cr.Event(
+ print_preview.NativeLayer.Event.PAGE_LAYOUT_CHANGE);
+ pageLayoutChangeEvt.pageLayout = pageLayout;
+ pageLayoutChangeEvt.hasCustomPageSizeStyle = hasCustomPageSizeStyle;
+ nativeLayer.dispatchEvent(pageLayoutChangeEvt);
+}
+
+/**
+ * Update the page count and check the page range.
+ * Called from PrintPreviewUI::OnDidGetPreviewPageCount().
+ * @param {number} pageCount The number of pages.
+ * @param {number} previewResponseId The preview request id that resulted in
+ * this response.
+ */
+function onDidGetPreviewPageCount(pageCount, previewResponseId) {
+ var pageCountChangeEvt = new cr.Event(
+ print_preview.NativeLayer.Event.PAGE_COUNT_CHANGE);
+ pageCountChangeEvt.pageCount = pageCount;
+ pageCountChangeEvt.previewResponseId = previewResponseId;
+ nativeLayer.dispatchEvent(pageCountChangeEvt);
+}
+
+/**
+ * Called when no pipelining previewed pages.
+ * @param {string} previewUid Preview unique identifier.
+ * @param {number} previewResponseId The preview request id that resulted in
+ * this response.
+ */
+function reloadPreviewPages(previewUid, previewResponseId) {
+ var previewReloadEvt = new cr.Event(
+ print_preview.NativeLayer.Event.PREVIEW_RELOAD);
+ previewReloadEvt.previewUid = previewUid;
+ previewReloadEvt.previewResponseId = previewResponseId;
+ nativeLayer.dispatchEvent(previewReloadEvt);
+}
+
+/**
+ * Notification that a print preview page has been rendered.
+ * Check if the settings have changed and request a regeneration if needed.
+ * Called from PrintPreviewUI::OnDidPreviewPage().
+ * @param {number} pageNumber The page number, 0-based.
+ * @param {string} previewUid Preview unique identifier.
+ * @param {number} previewResponseId The preview request id that resulted in
+ * this response.
+ */
+function onDidPreviewPage(pageNumber, previewUid, previewResponseId) {
+ var pagePreviewGenEvt = new cr.Event(
+ print_preview.NativeLayer.Event.PAGE_PREVIEW_READY);
+ pagePreviewGenEvt.pageIndex = pageNumber;
+ pagePreviewGenEvt.previewUid = previewUid;
+ pagePreviewGenEvt.previewResponseId = previewResponseId;
+ nativeLayer.dispatchEvent(pagePreviewGenEvt);
+}
+
+/**
+ * Update the print preview when new preview data is available.
+ * Create the PDF plugin as needed.
+ * Called from PrintPreviewUI::PreviewDataIsAvailable().
+ * @param {string} previewUid Preview unique identifier.
+ * @param {number} previewResponseId The preview request id that resulted in
+ * this response.
+ */
+function updatePrintPreview(previewUid, previewResponseId) {
+ var previewGenDoneEvt = new cr.Event(
+ print_preview.NativeLayer.Event.PREVIEW_GENERATION_DONE);
+ previewGenDoneEvt.previewUid = previewUid;
+ previewGenDoneEvt.previewResponseId = previewResponseId;
+ nativeLayer.dispatchEvent(previewGenDoneEvt);
+}

Powered by Google App Engine
This is Rietveld 408576698