Commit c1665b3a authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Finish removing global Javascript functions.

Remove remaining global javascript functions from the print preview
native layer and convert to sendWithPromise and web UI events.

BUG=717296
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2969383003
Cr-Commit-Position: refs/heads/master@{#485430}
parent 09ac01b3
......@@ -177,7 +177,6 @@ cr.define('print_preview', function() {
this.useSystemDefaultAsDefault_ =
loadTimeData.getBoolean('useSystemDefaultPrinter');
this.addEventListeners_();
this.reset_();
}
......@@ -545,6 +544,22 @@ cr.define('print_preview', function() {
this.cloudPrintInterface_.isCloudDestinationSearchInProgress;
},
/**
* Starts listening for relevant WebUI events and adds the listeners to
* |listenerTracker|. |listenerTracker| is responsible for removing the
* listeners when necessary.
* @param {!WebUIListenerTracker} listenerTracker
*/
addWebUIEventListeners: function(listenerTracker) {
listenerTracker.add(
'privet-printer-added', this.onPrivetPrinterAdded_.bind(this));
listenerTracker.add(
'extension-printers-added',
this.onExtensionPrintersAdded_.bind(this));
listenerTracker.add(
'reload-printer-list', this.onDestinationsReload.bind(this));
},
/**
* Initializes the destination store. Sets the initially selected
* destination. If any inserted destinations match this ID, that destination
......@@ -1179,8 +1194,8 @@ cr.define('print_preview', function() {
* @param {?print_preview.Destination} destination Information about the
* destination if it was resolved successfully.
*/
dispatchProvisionalDestinationResolvedEvent_: function(provisionalId,
destination) {
dispatchProvisionalDestinationResolvedEvent_: function(
provisionalId, destination) {
var event = new Event(
DestinationStore.EventType.PROVISIONAL_DESTINATION_RESOLVED);
event.provisionalId = provisionalId;
......@@ -1332,18 +1347,6 @@ cr.define('print_preview', function() {
}
},
/**
* Binds handlers to events.
* @private
*/
addEventListeners_: function() {
var nativeLayerEventTarget = this.nativeLayer_.getEventTarget();
this.tracker_.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD,
this.onDestinationsReload_.bind(this));
},
/**
* Creates a local PDF print destination.
* @private
......@@ -1549,7 +1552,7 @@ cr.define('print_preview', function() {
if (printer.serviceName == this.waitForRegisterDestination_ &&
!printer.isUnregistered) {
this.waitForRegisterDestination_ = null;
this.onDestinationsReload_();
this.onDestinationsReload();
} else {
this.insertDestinations_(
print_preview.PrivetDestinationParser.parse(printer));
......@@ -1617,11 +1620,10 @@ cr.define('print_preview', function() {
},
/**
* Called from native layer after the user was requested to sign in, and did
* so successfully.
* @private
* Called from print preview after the user was requested to sign in, and
* did so successfully.
*/
onDestinationsReload_: function() {
onDestinationsReload: function() {
this.reset_();
this.autoSelectMatchingDestination_ =
this.convertPreselectedToDestinationMatch_();
......
......@@ -85,19 +85,6 @@ cr.define('print_preview', function() {
* @constructor
*/
function NativeLayer() {
// Bind global handlers
global.reloadPrintersList = this.onReloadPrintersList_.bind(this);
global.onDidGetDefaultPageLayout =
this.onDidGetDefaultPageLayout_.bind(this);
global.onDidGetPreviewPageCount = this.onDidGetPreviewPageCount_.bind(this);
global.onDidPreviewPage = this.onDidPreviewPage_.bind(this);
global.onEnableManipulateSettingsForTest =
this.onEnableManipulateSettingsForTest_.bind(this);
global.printPresetOptionsFromDocument =
this.onPrintPresetOptionsFromDocument_.bind(this);
/** @private {!cr.EventTarget} */
this.eventTarget_ = new cr.EventTarget();
}
/** @private {?print_preview.NativeLayer} */
......@@ -121,27 +108,6 @@ cr.define('print_preview', function() {
currentInstance = instance;
};
/**
* Event types dispatched from the Chromium native layer.
* @enum {string}
* @const
*/
NativeLayer.EventType = {
DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
DISABLE_SCALING: 'print_preview.NativeLayer.DISABLE_SCALING',
MANIPULATE_SETTINGS_FOR_TEST:
'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY',
PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
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',
SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID',
PRINT_PRESET_OPTIONS: 'print_preview.NativeLayer.PRINT_PRESET_OPTIONS',
};
/**
* Constant values matching printing::DuplexMode enum.
* @enum {number}
......@@ -164,11 +130,6 @@ cr.define('print_preview', function() {
NativeLayer.SERIALIZED_STATE_VERSION_ = 1;
NativeLayer.prototype = {
/** @return {!cr.EventTarget} The event target for the native layer.*/
getEventTarget: function() {
return this.eventTarget_;
},
/**
* Requests access token for cloud print requests.
* @param {string} authType type of access token.
......@@ -314,13 +275,12 @@ cr.define('print_preview', function() {
},
/**
* Requests that a preview be generated. The following events may be
* dispatched in response:
* - PAGE_COUNT_READY
* - PAGE_LAYOUT_READY
* - PAGE_PREVIEW_READY
* - PREVIEW_GENERATION_DONE
* - PREVIEW_GENERATION_FAIL
* Requests that a preview be generated. The following Web UI events may
* be triggered in response:
* 'print-preset-options',
* 'page-count-ready',
* 'page-layout-ready',
* 'page-preview-ready'
* @param {!print_preview.Destination} destination Destination to print to.
* @param {!print_preview.PrintTicketStore} printTicketStore Used to get the
* state of the print ticket.
......@@ -526,9 +486,11 @@ cr.define('print_preview', function() {
* will be dispatched in response.
* @param {boolean} addAccount Whether to open an 'add a new account' or
* default sign in page.
* @return {!Promise} Promise that resolves when the sign in tab has been
* closed and the destinations should be reloaded.
*/
startCloudPrintSignIn: function(addAccount) {
chrome.send('signIn', [addAccount]);
signIn: function(addAccount) {
return cr.sendWithPromise('signIn', addAccount);
},
/** Navigates the user to the system printer settings interface. */
......@@ -551,116 +513,13 @@ cr.define('print_preview', function() {
chrome.send('forceOpenNewTab', [url]);
},
/** Reloads the printer list. */
onReloadPrintersList_: function() {
cr.dispatchSimpleEvent(
this.eventTarget_, NativeLayer.EventType.DESTINATIONS_RELOAD);
},
/**
* @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.
* @private
*/
onDidGetDefaultPageLayout_: function(pageLayout, hasCustomPageSizeStyle) {
var pageLayoutChangeEvent =
new Event(NativeLayer.EventType.PAGE_LAYOUT_READY);
pageLayoutChangeEvent.pageLayout = pageLayout;
pageLayoutChangeEvent.hasCustomPageSizeStyle = hasCustomPageSizeStyle;
this.eventTarget_.dispatchEvent(pageLayoutChangeEvent);
},
/**
* 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.
* @param {number} fitToPageScaling The scaling percentage required to fit
* the document to page, rounded to the nearest integer.
* @private
*/
onDidGetPreviewPageCount_: function(
pageCount, previewResponseId, fitToPageScaling) {
var pageCountChangeEvent =
new Event(NativeLayer.EventType.PAGE_COUNT_READY);
pageCountChangeEvent.pageCount = pageCount;
pageCountChangeEvent.previewResponseId = previewResponseId;
pageCountChangeEvent.fitToPageScaling = fitToPageScaling;
this.eventTarget_.dispatchEvent(pageCountChangeEvent);
},
/**
* 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 {number} previewUid Preview unique identifier.
* @param {number} previewResponseId The preview request id that resulted in
* this response.
* @private
*/
onDidPreviewPage_: function(pageNumber, previewUid, previewResponseId) {
var pagePreviewGenEvent =
new Event(NativeLayer.EventType.PAGE_PREVIEW_READY);
pagePreviewGenEvent.pageIndex = pageNumber;
pagePreviewGenEvent.previewUid = previewUid;
pagePreviewGenEvent.previewResponseId = previewResponseId;
this.eventTarget_.dispatchEvent(pagePreviewGenEvent);
},
/**
* Updates print preset options from source PDF document.
* Called from PrintPreviewUI::OnSetOptionsFromDocument().
* @param {{disableScaling: boolean, copies: number,
* duplex: number}} options Specifies
* printing options according to source document presets.
* @private
*/
onPrintPresetOptionsFromDocument_: function(options) {
var printPresetOptionsEvent =
new Event(NativeLayer.EventType.PRINT_PRESET_OPTIONS);
printPresetOptionsEvent.optionsFromDocument = options;
this.eventTarget_.dispatchEvent(printPresetOptionsEvent);
},
/**
* Allows for onManipulateSettings to be called
* from the native layer.
* @private
*/
onEnableManipulateSettingsForTest_: function() {
global.onManipulateSettingsForTest =
this.onManipulateSettingsForTest_.bind(this);
},
/**
* Dispatches an event to print_preview.js to change
* a particular setting for print preview.
* @param {!print_preview.PreviewSettings} settings Object containing the
* value to be changed and that value should be set to.
* @private
*/
onManipulateSettingsForTest_: function(settings) {
var manipulateSettingsEvent =
new Event(NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST);
manipulateSettingsEvent.settings = settings;
this.eventTarget_.dispatchEvent(manipulateSettingsEvent);
},
/**
* Sends a message to the test, letting it know that an
* option has been set to a particular value and that the change has
* finished modifying the preview area.
*/
previewReadyForTest: function() {
if (global.onManipulateSettingsForTest)
chrome.send('UILoadedForTest');
chrome.send('UILoadedForTest');
},
/**
......@@ -668,8 +527,7 @@ cr.define('print_preview', function() {
* had not been changed successfully.
*/
previewFailedForTest: function() {
if (global.onManipulateSettingsForTest)
chrome.send('UIFailedLoadingForTest');
chrome.send('UIFailedLoadingForTest');
}
};
......
......@@ -135,15 +135,6 @@ cr.define('print_preview', function() {
* @private
*/
this.selectedDestination_ = null;
/**
* Event tracker used to keep track of native layer events.
* @type {!EventTracker}
* @private
*/
this.tracker_ = new EventTracker();
this.addEventListeners_();
}
/**
......@@ -170,6 +161,21 @@ cr.define('print_preview', function() {
PreviewGenerator.prototype = {
__proto__: cr.EventTarget.prototype,
/**
* Starts listening for relevant WebUI events and adds the listeners to
* |listenerTracker|. |listenerTracker| is responsible for removing the
* listeners when necessary.
* @param {!WebUIListenerTracker} listenerTracker
*/
addWebUIEventListeners: function(listenerTracker) {
listenerTracker.add(
'page-count-ready', this.onPageCountReady_.bind(this));
listenerTracker.add(
'page-layout-ready', this.onPageLayoutReady_.bind(this));
listenerTracker.add(
'page-preview-ready', this.onPagePreviewReady_.bind(this));
},
/**
* Request that new preview be generated. A preview request will not be
* generated if the print ticket has not changed sufficiently.
......@@ -216,31 +222,6 @@ cr.define('print_preview', function() {
};
},
/** Removes all event listeners that the preview generator has attached. */
removeEventListeners: function() {
this.tracker_.removeAll();
},
/**
* Adds event listeners to the relevant native layer events.
* @private
*/
addEventListeners_: function() {
var nativeLayerEventTarget = this.nativeLayer_.getEventTarget();
this.tracker_.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.PAGE_LAYOUT_READY,
this.onPageLayoutReady_.bind(this));
this.tracker_.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.PAGE_COUNT_READY,
this.onPageCountReady_.bind(this));
this.tracker_.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.PAGE_PREVIEW_READY,
this.onPagePreviewReady_.bind(this));
},
/**
* 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
......@@ -322,70 +303,86 @@ cr.define('print_preview', function() {
/**
* Called when the page layout of the document is ready. Always occurs
* as a result of a preview request.
* @param {Event} event Contains layout info about the document.
* @param {{marginTop: number,
* marginLeft: number,
* marginBottom: number,
* marginRight: number,
* contentWidth: number,
* contentHeight: number,
* printableAreaX: number,
* printableAreaY: number,
* printableAreaWidth: number,
* printableAreaHeight: number,
* }} pageLayout Layout information about the document.
* @param {boolean} hasCustomPageSizeStyle Whether this document has a
* custom page size or style to use.
* @private
*/
onPageLayoutReady_: function(event) {
onPageLayoutReady_: function(pageLayout, hasCustomPageSizeStyle) {
// NOTE: A request ID is not specified, so assuming its for the current
// in-flight request.
var origin = new print_preview.Coordinate2d(
event.pageLayout.printableAreaX, event.pageLayout.printableAreaY);
pageLayout.printableAreaX, pageLayout.printableAreaY);
var size = new print_preview.Size(
event.pageLayout.printableAreaWidth,
event.pageLayout.printableAreaHeight);
pageLayout.printableAreaWidth, pageLayout.printableAreaHeight);
var margins = new print_preview.Margins(
Math.round(event.pageLayout.marginTop),
Math.round(event.pageLayout.marginRight),
Math.round(event.pageLayout.marginBottom),
Math.round(event.pageLayout.marginLeft));
Math.round(pageLayout.marginTop), Math.round(pageLayout.marginRight),
Math.round(pageLayout.marginBottom),
Math.round(pageLayout.marginLeft));
var o = print_preview.ticket_items.CustomMarginsOrientation;
var pageSize = new print_preview.Size(
event.pageLayout.contentWidth + margins.get(o.LEFT) +
margins.get(o.RIGHT),
event.pageLayout.contentHeight + margins.get(o.TOP) +
pageLayout.contentWidth + margins.get(o.LEFT) + margins.get(o.RIGHT),
pageLayout.contentHeight + margins.get(o.TOP) +
margins.get(o.BOTTOM));
this.documentInfo_.updatePageInfo(
new print_preview.PrintableArea(origin, size), pageSize,
event.hasCustomPageSizeStyle, margins);
hasCustomPageSizeStyle, margins);
},
/**
* Called when the document page count is received from the native layer.
* Always occurs as a result of a preview request.
* @param {Event} event Contains the document's page count.
* @param {number} pageCount The document's page count.
* @param {number} previewResponseId The request ID that corresponds to this
* page count.
* @param {number} fitToPageScaling The scaling required to fit the document
* to page (unused).
* @private
*/
onPageCountReady_: function(event) {
if (this.inFlightRequestId_ != event.previewResponseId) {
onPageCountReady_: function(
pageCount, previewResponseId, fitToPageScaling) {
if (this.inFlightRequestId_ != previewResponseId) {
return; // Ignore old response.
}
this.documentInfo_.updatePageCount(event.pageCount);
this.documentInfo_.updatePageCount(pageCount);
this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges();
},
/**
* Called when a page's preview has been generated. Dispatches a
* PAGE_READY event.
* @param {Event} event Contains the page index and preview UID.
* @param {number} pageIndex The index of the page whose preview is ready.
* @param {number} previewUid The unique ID of the print preview UI.
* @param {number} previewResponseId The preview request ID that this page
* preview is a response to.
* @private
*/
onPagePreviewReady_: function(event) {
if (this.inFlightRequestId_ != event.previewResponseId) {
onPagePreviewReady_: function(pageIndex, previewUid, previewResponseId) {
if (this.inFlightRequestId_ != previewResponseId) {
return; // Ignore old response.
}
var pageNumber = event.pageIndex + 1;
var pageNumber = pageIndex + 1;
var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet();
if (pageNumberSet.hasPageNumber(pageNumber)) {
var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber);
if (previewIndex == 0) {
this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex);
this.dispatchPreviewStartEvent_(previewUid, pageIndex);
}
this.dispatchPageReadyEvent_(
previewIndex, pageNumber, event.previewUid);
this.dispatchPageReadyEvent_(previewIndex, pageNumber, previewUid);
}
},
......
......@@ -357,6 +357,7 @@ cr.define('print_preview', function() {
this.previewGenerator_,
print_preview.PreviewGenerator.EventType.DOCUMENT_READY,
this.onDocumentReady_.bind(this));
this.previewGenerator_.addWebUIEventListeners(this.listenerTracker);
} else {
this.showCustomMessage(loadTimeData.getString('noPlugin'));
}
......@@ -365,9 +366,6 @@ cr.define('print_preview', function() {
/** @override */
exitDocument: function() {
print_preview.Component.prototype.exitDocument.call(this);
if (this.previewGenerator_) {
this.previewGenerator_.removeEventListeners();
}
this.overlayEl_ = null;
this.openSystemDialogButton_ = null;
},
......
......@@ -310,6 +310,13 @@ cr.define('print_preview', function() {
* @private
*/
this.showSystemDialogBeforeNextPrint_ = false;
/**
* Whether the preview is listening for the manipulate-settings-for-test
* UI event.
* @private {boolean}
*/
this.isListeningForManipulateSettings_ = false;
}
PrintPreview.prototype = {
......@@ -332,35 +339,21 @@ cr.define('print_preview', function() {
print_preview.PrintPreviewFocusManager.getInstance().initialize();
cr.ui.FocusOutlineManager.forDocument(document);
this.listenerTracker.add('print-failed', this.onPrintFailed_.bind(this));
this.destinationStore_.addWebUIEventListeners(this.listenerTracker);
this.listenerTracker.add(
'privet-printer-added',
this.destinationStore_.onPrivetPrinterAdded_.bind(
this.destinationStore_));
'use-cloud-print', this.onCloudPrintEnable_.bind(this));
this.listenerTracker.add(
'extension-printers-added',
this.destinationStore_.onExtensionPrintersAdded_.bind(
this.destinationStore_));
'print-preset-options',
this.onPrintPresetOptionsFromDocument_.bind(this));
this.listenerTracker.add(
'use-cloud-print', this.onCloudPrintEnable_.bind(this));
'preview-page-count', this.onPageCountReady_.bind(this));
this.listenerTracker.add(
'enable-manipulate-settings-for-test',
this.onEnableManipulateSettingsForTest_.bind(this));
},
/** @override */
enterDocument: function() {
// Native layer events.
var nativeLayerEventTarget = this.nativeLayer_.getEventTarget();
this.tracker.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS,
this.onPrintPresetOptionsFromDocument_.bind(this));
this.tracker.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.PAGE_COUNT_READY,
this.onPageCountReady_.bind(this));
this.tracker.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST,
this.onManipulateSettingsForTest_.bind(this));
if ($('system-dialog-link')) {
this.tracker.add(
getRequiredElement('system-dialog-link'), 'click',
......@@ -824,7 +817,8 @@ cr.define('print_preview', function() {
onPreviewGenerationDone_: function() {
this.isPreviewGenerationInProgress_ = false;
this.printHeader_.isPrintButtonEnabled = true;
this.nativeLayer_.previewReadyForTest();
if (this.isListeningForManipulateSettings_)
this.nativeLayer_.previewReadyForTest();
this.printIfReady_();
},
......@@ -1002,41 +996,46 @@ cr.define('print_preview', function() {
* @private
*/
onCloudPrintSignInActivated_: function(addAccount) {
this.nativeLayer_.startCloudPrintSignIn(addAccount);
this.nativeLayer_.signIn(addAccount)
.then(this.destinationStore_.onDestinationsReload.bind(
this.destinationStore_));
},
/**
* Updates printing options according to source document presets.
* @param {Event} event Contains options from source document.
* @param {boolean} disableScaling Whether the document disables scaling.
* @param {number} copies The default number of copies from the document.
* @param {number} duplex The default duplex setting from the document.
* @private
*/
onPrintPresetOptionsFromDocument_: function(event) {
if (event.optionsFromDocument.disableScaling)
onPrintPresetOptionsFromDocument_: function(
disableScaling, copies, duplex) {
if (disableScaling)
this.documentInfo_.updateIsScalingDisabled(true);
if (event.optionsFromDocument.copies > 0 &&
this.printTicketStore_.copies.isCapabilityAvailable()) {
this.printTicketStore_.copies.updateValue(
event.optionsFromDocument.copies);
if (copies > 0 && this.printTicketStore_.copies.isCapabilityAvailable()) {
this.printTicketStore_.copies.updateValue(copies);
}
if (event.optionsFromDocument.duplex >= 0 &&
this.printTicketStore_.duplex.isCapabilityAvailable()) {
this.printTicketStore_.duplex.updateValue(
event.optionsFromDocument.duplex);
if (duplex >= 0 & this.printTicketStore_.duplex.isCapabilityAvailable()) {
this.printTicketStore_.duplex.updateValue(duplex);
}
},
/**
* Called when the Page Count Ready message is received to update the fit to
* page scaling value in the scaling settings.
* @param {Event} event Event object representing the page count ready
* message
* @param {number} pageCount The document's page count (unused).
* @param {number} previewResponseId The request ID that corresponds to this
* page count (unused).
* @param {number} fitToPageScaling The scaling required to fit the document
* to page.
* @private
*/
onPageCountReady_: function(event) {
if (event.fitToPageScaling >= 0) {
this.scalingSettings_.updateFitToPageScaling(event.fitToPageScaling);
onPageCountReady_: function(
pageCount, previewResponseId, fitToPageScaling) {
if (fitToPageScaling >= 0) {
this.scalingSettings_.updateFitToPageScaling(fitToPageScaling);
}
},
......@@ -1052,15 +1051,25 @@ cr.define('print_preview', function() {
loadTimeData.getString('couldNotPrint'));
},
/**
* Called to start listening for the manipulate-settings-for-test WebUI
* event so that settings can be modified by this event.
* @private
*/
onEnableManipulateSettingsForTest_: function() {
this.listenerTracker.add(
'manipulate-settings-for-test',
this.onManipulateSettingsForTest_.bind(this));
this.isListeningForManipulateSettings_ = true;
},
/**
* Called when the print preview settings need to be changed for testing.
* @param {Event} event Event object that contains the option that is to
* be changed and what to set that option.
* @param {!print_preview.PreviewSettings} settings Contains print preview
* settings to change and the values to change them to.
* @private
*/
onManipulateSettingsForTest_: function(event) {
var settings =
/** @type {print_preview.PreviewSettings} */ (event.settings);
onManipulateSettingsForTest_: function(settings) {
if ('selectSaveAsPdfDestination' in settings) {
this.saveAsPdfForTest_(); // No parameters.
} else if ('layoutSettings' in settings) {
......
......@@ -1173,24 +1173,24 @@ void PrintPreviewHandler::HandlePrinterSetup(const base::ListValue* args) {
weak_factory_.GetWeakPtr(), callback_id, printer_name));
}
void PrintPreviewHandler::OnSigninComplete() {
if (print_preview_ui())
print_preview_ui()->OnReloadPrintersList();
void PrintPreviewHandler::OnSigninComplete(const std::string& callback_id) {
ResolveJavascriptCallback(base::Value(callback_id), base::Value());
}
void PrintPreviewHandler::HandleSignin(const base::ListValue* args) {
std::string callback_id;
bool add_account = false;
bool success = args->GetBoolean(0, &add_account);
DCHECK(success);
CHECK(args->GetString(0, &callback_id));
CHECK(!callback_id.empty());
CHECK(args->GetBoolean(1, &add_account));
Profile* profile = Profile::FromBrowserContext(
preview_web_contents()->GetBrowserContext());
chrome::ScopedTabbedBrowserDisplayer displayer(profile);
print_dialog_cloud::CreateCloudPrintSigninTab(
displayer.browser(),
add_account,
displayer.browser(), add_account,
base::Bind(&PrintPreviewHandler::OnSigninComplete,
weak_factory_.GetWeakPtr()));
weak_factory_.GetWeakPtr(), callback_id));
}
void PrintPreviewHandler::HandleGetAccessToken(const base::ListValue* args) {
......@@ -1460,7 +1460,7 @@ WebContents* PrintPreviewHandler::GetInitiator() const {
void PrintPreviewHandler::OnAddAccountToCookieCompleted(
const std::string& account_id,
const GoogleServiceAuthError& error) {
OnSigninComplete();
FireWebUIListener("reload-printer-list");
}
void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename,
......@@ -1557,6 +1557,34 @@ void PrintPreviewHandler::OnInvalidPrinterSettings() {
preview_callbacks_.pop();
}
void PrintPreviewHandler::SendPrintPresetOptions(bool disable_scaling,
int copies,
int duplex) {
FireWebUIListener("print-preset-options", base::Value(disable_scaling),
base::Value(copies), base::Value(duplex));
}
void PrintPreviewHandler::SendPageCountReady(int page_count,
int request_id,
int fit_to_page_scaling) {
FireWebUIListener("page-count-ready", base::Value(page_count),
base::Value(request_id), base::Value(fit_to_page_scaling));
}
void PrintPreviewHandler::SendPageLayoutReady(
const base::DictionaryValue& layout,
bool has_custom_page_size_style) {
FireWebUIListener("page-layout-ready", layout,
base::Value(has_custom_page_size_style));
}
void PrintPreviewHandler::SendPagePreviewReady(int page_index,
int preview_uid,
int preview_response_id) {
FireWebUIListener("page-preview-ready", base::Value(page_index),
base::Value(preview_uid), base::Value(preview_response_id));
}
void PrintPreviewHandler::OnPrintPreviewCancelled() {
CHECK(!preview_callbacks_.empty());
RejectJavascriptCallback(base::Value(preview_callbacks_.front()),
......
......@@ -86,6 +86,23 @@ class PrintPreviewHandler
// Called when print preview is ready.
void OnPrintPreviewReady(int preview_uid, int request_id);
// Send the print preset options from the document.
void SendPrintPresetOptions(bool disable_scaling, int copies, int duplex);
// Send the print preview page count and fit to page scaling
void SendPageCountReady(int page_count,
int request_id,
int fit_to_page_scaling);
// Send the default page layout
void SendPageLayoutReady(const base::DictionaryValue& layout,
bool has_custom_page_size_style);
// Notify the WebUI that the page preview is ready.
void SendPagePreviewReady(int page_index,
int preview_uid,
int preview_response_id);
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
// Called when the user press ctrl+shift+p to display the native system
// dialog.
......@@ -187,7 +204,7 @@ class PrintPreviewHandler
#endif
// Callback for the signin dialog to call once signin is complete.
void OnSigninComplete();
void OnSigninComplete(const std::string& callback_id);
// Brings up a dialog to allow the user to sign into cloud print.
// |args| is unused.
......
......@@ -559,11 +559,8 @@ void PrintPreviewUI::OnDidGetPreviewPageCount(
DCHECK_GT(params.page_count, 0);
if (g_testing_delegate)
g_testing_delegate->DidGetPreviewPageCount(params.page_count);
base::Value count(params.page_count);
base::Value request_id(params.preview_request_id);
base::Value fit_to_page_scaling(params.fit_to_page_scaling);
web_ui()->CallJavascriptFunctionUnsafe("onDidGetPreviewPageCount", count,
request_id, fit_to_page_scaling);
handler_->SendPageCountReady(params.page_count, params.preview_request_id,
params.fit_to_page_scaling);
}
void PrintPreviewUI::OnDidGetDefaultPageLayout(
......@@ -590,22 +587,15 @@ void PrintPreviewUI::OnDidGetDefaultPageLayout(
printable_area.width());
layout.SetInteger(printing::kSettingPrintableAreaHeight,
printable_area.height());
base::Value has_page_size_style(has_custom_page_size_style);
web_ui()->CallJavascriptFunctionUnsafe("onDidGetDefaultPageLayout", layout,
has_page_size_style);
handler_->SendPageLayoutReady(layout, has_custom_page_size_style);
}
void PrintPreviewUI::OnDidPreviewPage(int page_number,
int preview_request_id) {
DCHECK_GE(page_number, 0);
base::Value number(page_number);
base::Value ui_identifier(id_);
base::Value request_id(preview_request_id);
if (g_testing_delegate)
g_testing_delegate->DidRenderPreviewPage(web_ui()->GetWebContents());
web_ui()->CallJavascriptFunctionUnsafe("onDidPreviewPage", number,
ui_identifier, request_id);
handler_->SendPagePreviewReady(page_number, id_, preview_request_id);
}
void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count,
......@@ -667,19 +657,10 @@ void PrintPreviewUI::OnClosePrintPreviewDialog() {
delegate->OnDialogCloseFromWebUI();
}
void PrintPreviewUI::OnReloadPrintersList() {
web_ui()->CallJavascriptFunctionUnsafe("reloadPrintersList");
}
void PrintPreviewUI::OnSetOptionsFromDocument(
const PrintHostMsg_SetOptionsFromDocument_Params& params) {
base::DictionaryValue options;
options.SetBoolean(printing::kSettingDisableScaling,
params.is_scaling_disabled);
options.SetInteger(printing::kSettingCopies, params.copies);
options.SetInteger(printing::kSettingDuplexMode, params.duplex);
web_ui()->CallJavascriptFunctionUnsafe("printPresetOptionsFromDocument",
options);
handler_->SendPrintPresetOptions(params.is_scaling_disabled, params.copies,
params.duplex);
}
// static
......
......@@ -135,9 +135,6 @@ class PrintPreviewUI : public ConstrainedWebDialogUI {
// Closes the print preview dialog.
void OnClosePrintPreviewDialog();
// Reload the printers list.
void OnReloadPrintersList();
// Notifies the WebUI to set print preset options from source PDF.
void OnSetOptionsFromDocument(
const PrintHostMsg_SetOptionsFromDocument_Params& params);
......
......@@ -20,12 +20,6 @@ cr.define('print_preview', function() {
'setupPrinter'
]);
/**
* @private {!cr.EventTarget} The event target used for dispatching and
* receiving events.
*/
this.eventTarget_ = new cr.EventTarget();
/**
* @private {!print_preview.NativeInitialSettings} The initial settings
* to be used for the response to a |getInitialSettings| call.
......@@ -95,12 +89,31 @@ cr.define('print_preview', function() {
generateDraft: generateDraft,
requestId: requestId,
});
var rejectString = print_preview.PreviewArea.EventType.SETTINGS_INVALID;
rejectString = rejectString.substring(
rejectString.lastIndexOf(".") + 1, rejectString.length);
return destination.id == this.badPrinterId_ ?
Promise.reject(rejectString) :
Promise.resolve(requestId);
if (destination.id == this.badPrinterId_) {
var rejectString = print_preview.PreviewArea.EventType.SETTINGS_INVALID;
rejectString = rejectString.substring(
rejectString.lastIndexOf('.') + 1, rejectString.length);
return Promise.reject(rejectString);
}
var pageRanges = printTicketStore.pageRange.getDocumentPageRanges();
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);
} else {
var pages = pageRanges.reduce(function(soFar, range) {
for (var page = range.from; page <= range.to; page++) {
soFar.push(page);
}
return soFar;
}, []);
cr.webUIListenerCallback(
'page-count-ready', pages.length, requestId, 100);
pages.forEach(function(page) {
cr.webUIListenerCallback(
'page-preview-ready', page - 1, 0, requestId);
});
}
return Promise.resolve(requestId);
},
/** @override */
......@@ -136,17 +149,8 @@ cr.define('print_preview', function() {
},
/** Stubs for |print_preview.NativeLayer| methods that call C++ handlers. */
previewReadyForTest: function() {},
startHideDialog: function () {},
/** @return {!cr.EventTarget} The native layer event target. */
getEventTarget: function() { return this.eventTarget_; },
/** @param {!cr.EventTarget} eventTarget The event target to use. */
setEventTarget: function(eventTarget) {
this.eventTarget_ = eventTarget;
},
/**
* @param {!print_preview.NativeInitialSettings} settings The settings
* to return as a response to |getInitialSettings|.
......
......@@ -17,15 +17,6 @@ cr.define('print_preview', function() {
*/
this.loadCallback_ = null;
/** @private {!EventTracker} The plugin stub's event tracker. */
this.tracker_ = new EventTracker();
// Call documentLoadComplete as soon as the preview area starts the
// preview.
this.tracker_.add(
area,
print_preview.PreviewArea.EventType.PREVIEW_GENERATION_IN_PROGRESS,
this.documentLoadComplete.bind(this));
}
/**
......@@ -36,11 +27,6 @@ cr.define('print_preview', function() {
this.loadCallback_ = callback;
}
documentLoadComplete() {
if (this.loadCallback_)
this.loadCallback_();
}
/**
* Stubbed out since some tests result in a call.
* @param {string} url The url to initialize the plugin to.
......@@ -49,6 +35,17 @@ cr.define('print_preview', function() {
* @param {boolean} modifiable Whether the source document is modifiable.
*/
resetPrintPreviewMode(url, color, pages, modifiable) {}
/**
* Called when the preview area wants the plugin to load a preview page.
* Immediately calls loadCallback_().
* @param {string} url The preview URL
* @param {number} index The index of the page number to load.
*/
loadPreviewPage(url, index) {
if (this.loadCallback_)
this.loadCallback_();
}
}
return {PDFPluginStub: PDFPluginStub};
......
......@@ -124,13 +124,7 @@ TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() {
};
setup(function() {
Mock4JS.clearMocksToVerify();
nativeLayer_ = new print_preview.NativeLayerStub();
var nativeLayerEventTarget = mock(cr.EventTarget);
nativeLayer_.setEventTarget(nativeLayerEventTarget.proxy());
nativeLayerEventTarget.expects(atLeastOnce())
.addEventListener(ANYTHING, ANYTHING, ANYTHING);
invitationStore_ = new print_preview.InvitationStore();
destinationStore_ = new print_preview.DestinationStore(
nativeLayer_, new print_preview.UserInfo(),
......@@ -142,10 +136,6 @@ TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() {
destinationSearch_.decorate($('destination-search'));
});
teardown(function() {
Mock4JS.verifyAllMocks();
});
test('ResolutionFails', function() {
var destId = "001122DEADBEEF";
if (cr.isChromeOS) {
......
......@@ -693,21 +693,12 @@ cr.define('print_preview_test', function() {
return setupSettingsAndDestinationsWithCapabilities().then(function() {
// Indicate that the number of copies print preset is set for source
// PDF.
var printPresetOptions = {
disableScaling: true,
copies: 2
};
var printPresetOptionsEvent = new Event(
print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS);
printPresetOptionsEvent.optionsFromDocument = printPresetOptions;
nativeLayer.getEventTarget().
dispatchEvent(printPresetOptionsEvent);
var copies = 2;
cr.webUIListenerCallback('print-preset-options', true, copies);
checkSectionVisible($('copies-settings'), true);
expectEquals(
printPresetOptions.copies,
parseInt($('copies-settings').
querySelector('.user-value').value));
copies,
parseInt($('copies-settings').querySelector('.user-value').value));
return whenAnimationDone('other-options-collapsible');
});
......@@ -720,15 +711,7 @@ cr.define('print_preview_test', function() {
return setupSettingsAndDestinationsWithCapabilities().then(function() {
// Indicate that the duplex print preset is set to 'long edge' for
// source PDF.
var printPresetOptions = {
duplex: 1
};
var printPresetOptionsEvent = new Event(
print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS);
printPresetOptionsEvent.optionsFromDocument = printPresetOptions;
nativeLayer.getEventTarget().
dispatchEvent(printPresetOptionsEvent);
cr.webUIListenerCallback('print-preset-options', false, 1, 1);
var otherOptions = $('other-options-settings');
checkSectionVisible(otherOptions, true);
var duplexContainer =
......
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