Commit 2c3bb35d authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview componentization: incorporate native layer

Make test and existing code changes as needed to incorporate native
layer into the new UI. No behavior changes.

Bug: 773928
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I660a4707a2a54d75e5ca7dde2b9ec10496c5055b
Reviewed-on: https://chromium-review.googlesource.com/807585Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521874}
parent f74659ec
......@@ -35,5 +35,14 @@
],
'includes': ['../../../../third_party/closure_compiler/compile_js2.gypi'],
},
{
'target_name': 'native_layer',
'dependencies': [
'data/compiled_resources2.gyp:destination',
'data/compiled_resources2.gyp:measurement_system',
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:cr',
],
'includes': ['../../../../third_party/closure_compiler/compile_js2.gypi'],
},
],
}
......@@ -31,6 +31,13 @@
],
'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'],
},
{
'target_name': 'measurement_system',
'dependencies': [
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:cr',
],
'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'],
},
{
'target_name': 'printable_area',
'dependencies': [
......
<link rel="import" href="chrome://resources/html/cr.html">
<script src="native_layer.js"></script>
......@@ -126,6 +126,7 @@
'target_name': 'model',
'dependencies': [
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:cr',
'../compiled_resources2.gyp:native_layer',
'../data/compiled_resources2.gyp:destination',
'../data/compiled_resources2.gyp:document_info',
],
......
<link rel="import" href="chrome://resources/html/polymer.html">
<link rel="import" href="../native_layer.html">
<link rel="import" href="../data/destination.html">
<link rel="import" href="../data/document_info.html">
......
......@@ -273,6 +273,23 @@ Polymer({
*/
MONOCHROME_TYPES_: ['STANDARD_MONOCHROME', 'CUSTOM_MONOCHROME'],
/** @private {!print_preview.NativeLayer} */
nativeLayer_: print_preview.NativeLayer.getInstance(),
/** @override */
attached: function() {
this.nativeLayer_.getInitialSettings().then(
this.onInitialSettingsSet_.bind(this));
},
/**
* @param {!print_preview.NativeInitialSettings} settings
* @private
*/
onInitialSettingsSet_: function(settings) {
// Do nothing here for now.
},
/**
* Updates the availability of the settings sections.
* @private
......
......@@ -189,12 +189,86 @@ cr.define('print_preview', function() {
this.generateDraft_ = this.documentInfo_.isModifiable;
return {
id: this.inFlightRequestId_,
request: this.nativeLayer_.getPreview(
this.destinationStore_.selectedDestination, this.printTicketStore_,
this.documentInfo_, this.generateDraft_, this.inFlightRequestId_),
request: this.getPreview_(),
};
}
/**
* @return {!Promise} Promise that resolves when the preview has been
* generated.
* @private
*/
getPreview_() {
const printTicketStore = this.printTicketStore_;
const destination = assert(this.destinationStore_.selectedDestination);
assert(
printTicketStore.isTicketValidForPreview(),
'Trying to generate preview when ticket is not valid');
const ticket = {
pageRange: printTicketStore.pageRange.getDocumentPageRanges(),
mediaSize: printTicketStore.mediaSize.getValue(),
landscape: printTicketStore.landscape.getValue(),
color: PreviewGenerator.getNativeColorModel(
destination, printTicketStore.color),
headerFooterEnabled: printTicketStore.headerFooter.getValue(),
marginsType: printTicketStore.marginsType.getValue(),
isFirstRequest: this.inFlightRequestId_ == 0,
requestID: this.inFlightRequestId_,
previewModifiable: this.documentInfo_.isModifiable,
generateDraftData: this.generateDraft_,
fitToPageEnabled: printTicketStore.fitToPage.getValue(),
scaleFactor: printTicketStore.scaling.getValueAsNumber(),
// NOTE: Even though the following fields dont directly relate to the
// preview, they still need to be included.
// e.g. printing::PrintSettingsFromJobSettings() still checks for them.
collate: true,
copies: 1,
deviceName: destination.id,
dpiHorizontal: 'horizontal_dpi' in printTicketStore.dpi.getValue() ?
printTicketStore.dpi.getValue().horizontal_dpi :
0,
dpiVertical: 'vertical_dpi' in printTicketStore.dpi.getValue() ?
printTicketStore.dpi.getValue().vertical_dpi :
0,
duplex: printTicketStore.duplex.getValue() ?
PreviewGenerator.DuplexMode.LONG_EDGE :
PreviewGenerator.DuplexMode.SIMPLEX,
printToPDF: destination.id ==
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
printWithCloudPrint: !destination.isLocal,
printWithPrivet: destination.isPrivet,
printWithExtension: destination.isExtension,
rasterizePDF: false,
shouldPrintBackgrounds: printTicketStore.cssBackground.getValue(),
shouldPrintSelectionOnly: printTicketStore.selectionOnly.getValue()
};
// Set 'cloudPrintID' only if the destination is not local.
if (destination && !destination.isLocal) {
ticket.cloudPrintID = destination.id;
}
if (printTicketStore.marginsType.isCapabilityAvailable() &&
printTicketStore.marginsType.getValue() ==
print_preview.ticket_items.MarginsTypeValue.CUSTOM) {
const customMargins = printTicketStore.customMargins.getValue();
const orientationEnum =
print_preview.ticket_items.CustomMarginsOrientation;
ticket.marginsCustom = {
marginTop: customMargins.get(orientationEnum.TOP),
marginRight: customMargins.get(orientationEnum.RIGHT),
marginBottom: customMargins.get(orientationEnum.BOTTOM),
marginLeft: customMargins.get(orientationEnum.LEFT)
};
}
const pageCount =
this.inFlightRequestId_ > 0 ? this.documentInfo_.pageCount : -1;
return this.nativeLayer_.getPreview(JSON.stringify(ticket), pageCount);
}
/**
* Dispatches a PAGE_READY event to signal that a page preview is ready.
* @param {number} previewIndex Index of the page with respect to the pages
......@@ -409,6 +483,35 @@ cr.define('print_preview', function() {
FAIL: 'print_preview.PreviewGenerator.FAIL'
};
/**
* Enumeration of color modes used by Chromium.
* @enum {number}
*/
PreviewGenerator.ColorMode = {GRAY: 1, COLOR: 2};
/**
* @param {!print_preview.Destination} destination Destination to print to.
* @param {!print_preview.ticket_items.Color} color Color ticket item.
* @return {number} Native color model.
*/
PreviewGenerator.getNativeColorModel = function(destination, color) {
// For non-local printers native color model is ignored anyway.
const option = destination.isLocal ? color.getSelectedOption() : null;
const nativeColorModel = parseInt(option ? option.vendor_id : null, 10);
if (isNaN(nativeColorModel)) {
return color.getValue() ? PreviewGenerator.ColorMode.COLOR :
PreviewGenerator.ColorMode.GRAY;
}
return nativeColorModel;
};
/**
* Constant values matching printing::DuplexMode enum.
* @enum {number}
*/
PreviewGenerator
.DuplexMode = {SIMPLEX: 0, LONG_EDGE: 1, UNKNOWN_DUPLEX_MODE: -1};
// Export
return {PreviewGenerator: PreviewGenerator};
});
......@@ -559,10 +559,7 @@ cr.define('print_preview', function() {
.PRINT_WITH_SETTINGS_COLLAPSED);
}
const destination = assert(this.destinationStore_.selectedDestination);
const whenPrintDone = this.nativeLayer_.print(
destination, this.printTicketStore_, this.documentInfo_,
this.uiState_ == PrintPreviewUiState_.OPENING_PDF_PREVIEW,
this.showSystemDialogBeforeNextPrint_);
const whenPrintDone = this.sendPrintRequest_(destination);
if (this.uiState_ == PrintPreviewUiState_.OPENING_PDF_PREVIEW ||
(destination.isLocal && !destination.isPrivet &&
!destination.isExtension &&
......@@ -600,6 +597,96 @@ cr.define('print_preview', function() {
return print_preview.PrintAttemptResult_.PRINTED;
},
/**
* @param {!print_preview.Destination} destination Destination to print to.
* @return {!Promise} Promise that resolves when print request is resolved
* or rejected.
* @private
*/
sendPrintRequest_: function(destination) {
const printTicketStore = this.printTicketStore_;
const documentInfo = this.documentInfo_;
assert(
printTicketStore.isTicketValid(),
'Trying to print when ticket is not valid');
assert(
!this.showSystemDialogBeforeNextPrint_ ||
(cr.isWindows && destination.isLocal),
'Implemented for Windows only');
// Note: update
// chrome/browser/ui/webui/print_preview/print_preview_handler_unittest.cc
// with any changes to ticket creation.
const ticket = {
mediaSize: printTicketStore.mediaSize.getValue(),
pageCount: printTicketStore.pageRange.getPageNumberSet().size,
landscape: printTicketStore.landscape.getValue(),
color: print_preview.PreviewGenerator.getNativeColorModel(
destination, printTicketStore.color),
headerFooterEnabled: false, // Only used in print preview
marginsType: printTicketStore.marginsType.getValue(),
duplex: printTicketStore.duplex.getValue() ?
print_preview.PreviewGenerator.DuplexMode.LONG_EDGE :
print_preview.PreviewGenerator.DuplexMode.SIMPLEX,
copies: printTicketStore.copies.getValueAsNumber(),
collate: printTicketStore.collate.getValue(),
shouldPrintBackgrounds: printTicketStore.cssBackground.getValue(),
shouldPrintSelectionOnly: false, // Only used in print preview
previewModifiable: documentInfo.isModifiable,
printToPDF: destination.id ==
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
printWithCloudPrint: !destination.isLocal,
printWithPrivet: destination.isPrivet,
printWithExtension: destination.isExtension,
rasterizePDF: printTicketStore.rasterize.getValue(),
scaleFactor: printTicketStore.scaling.getValueAsNumber(),
dpiHorizontal: 'horizontal_dpi' in printTicketStore.dpi.getValue() ?
printTicketStore.dpi.getValue().horizontal_dpi :
0,
dpiVertical: 'vertical_dpi' in printTicketStore.dpi.getValue() ?
printTicketStore.dpi.getValue().vertical_dpi :
0,
deviceName: destination.id,
fitToPageEnabled: printTicketStore.fitToPage.getValue(),
pageWidth: documentInfo.pageSize.width,
pageHeight: documentInfo.pageSize.height,
showSystemDialog: this.showSystemDialogBeforeNextPrint_
};
if (!destination.isLocal) {
// 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.marginsType.isCapabilityAvailable() &&
printTicketStore.marginsType.isValueEqual(
print_preview.ticket_items.MarginsTypeValue.CUSTOM)) {
const customMargins = printTicketStore.customMargins.getValue();
const orientationEnum =
print_preview.ticket_items.CustomMarginsOrientation;
ticket.marginsCustom = {
marginTop: customMargins.get(orientationEnum.TOP),
marginRight: customMargins.get(orientationEnum.RIGHT),
marginBottom: customMargins.get(orientationEnum.BOTTOM),
marginLeft: customMargins.get(orientationEnum.LEFT)
};
}
if (destination.isPrivet || destination.isExtension) {
ticket.ticket = printTicketStore.createPrintTicket(destination);
ticket.capabilities = JSON.stringify(destination.capabilities);
}
if (this.uiState_ == PrintPreviewUiState_.OPENING_PDF_PREVIEW) {
ticket.OpenPDFInPreview = true;
}
return this.nativeLayer_.print(JSON.stringify(ticket));
},
/**
* Closes the print preview.
* @param {boolean} isCancel Whether this was called due to the user
......
......@@ -29,6 +29,12 @@
<structure name="IDR_PRINT_PREVIEW_NEW_MODEL_JS"
file="new/model.js"
type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_NATIVE_LAYER_HTML"
file="native_layer.html"
type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_NATIVE_LAYER_JS"
file="native_layer.js"
type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_DATA_DESTINATION_HTML"
file="data/destination.html"
type="chrome_html" />
......
......@@ -73,22 +73,20 @@ cr.define('print_preview', function() {
}
/** @override */
getPreview(
destination, printTicketStore, documentInfo, generateDraft, requestId) {
getPreview(printTicket, pageCount) {
this.methodCalled('getPreview', {
destination: destination,
printTicketStore: printTicketStore,
documentInfo: documentInfo,
generateDraft: generateDraft,
requestId: requestId,
printTicket: printTicket,
pageCount: pageCount
});
if (destination.id == this.badPrinterId_) {
const printTicketParsed = JSON.parse(printTicket);
if (printTicketParsed.deviceName == this.badPrinterId_) {
let rejectString = print_preview.PreviewArea.EventType.SETTINGS_INVALID;
rejectString = rejectString.substring(
rejectString.lastIndexOf('.') + 1, rejectString.length);
return Promise.reject(rejectString);
}
const pageRanges = printTicketStore.pageRange.getDocumentPageRanges();
const pageRanges = printTicketParsed.pageRange;
const requestId = printTicketParsed.requestID;
if (pageRanges.length == 0) { // assume full length document, 1 page.
cr.webUIListenerCallback('page-count-ready', 1, requestId, 100);
cr.webUIListenerCallback('page-preview-ready', 0, 0, requestId);
......@@ -122,20 +120,8 @@ cr.define('print_preview', function() {
}
/** @override */
print(
destination,
printTicketStore,
documentInfo,
opt_isOpenPdfInPreview,
opt_showSystemDialog
) {
this.methodCalled('print', {
destination: destination,
printTicketStore: printTicketStore,
documentInfo: documentInfo,
openPdfInPreview: opt_isOpenPdfInPreview || false,
showSystemDialog: opt_showSystemDialog || false,
});
print(printTicket) {
this.methodCalled('print', printTicket);
return Promise.resolve();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment