Commit 9e40d051 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Prefetch recent destinations

Prefetch capabilities for recent destinations. This will speed up
selection of these destinations and set up for adding a dropdown.

Bug: 920056
Change-Id: I2e53a8d353b91905dc1bec49f61815a4c416f052
Reviewed-on: https://chromium-review.googlesource.com/c/1404489
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#622172}
parent 4aa1c73f
......@@ -396,13 +396,15 @@ cr.define('print_preview', function() {
}
this.recentDestinations_ = recentDestinations;
let origin = null;
let id = '';
let account = '';
let name = '';
let capabilities = null;
let extensionId = '';
let extensionName = '';
const destinationParams = {
origin: this.platformOrigin_,
id: '',
account: '',
name: '',
capabilities: null,
extensionId: '',
extensionName: '',
};
let foundDestination = false;
// Run through the destinations forward. As soon as we find a
// destination, don't select any future destinations, just mark
......@@ -410,25 +412,31 @@ cr.define('print_preview', function() {
// destinations/updating the print ticket and this selecting a new
// destination that causes random print preview errors.
for (const destination of recentDestinations) {
origin = destination.origin;
id = destination.id;
account = destination.account || '';
name = destination.displayName || '';
capabilities = destination.capabilities;
extensionId = destination.extensionId || '';
extensionName = destination.extensionName || '';
const candidate =
this.destinationMap_[this.getDestinationKey_(origin, id, account)];
destinationParams.origin = assert(destination.origin);
destinationParams.id = destination.id;
destinationParams.account = destination.account || '';
destinationParams.name = destination.displayName || '';
destinationParams.capabilities = destination.capabilities;
destinationParams.extensionId = destination.extensionId || '';
destinationParams.extensionName = destination.extensionName || '';
const candidate = this.destinationMap_[this.getDestinationKey_(
destinationParams.origin, destinationParams.id,
destinationParams.account)];
if (candidate != null) {
candidate.isRecent = true;
if (!foundDestination && !this.useSystemDefaultAsDefault_) {
this.selectDestination(candidate);
}
foundDestination = true;
} else if (!foundDestination && !this.useSystemDefaultAsDefault_) {
foundDestination = this.fetchPreselectedDestination_(
origin, id, account, name, capabilities, extensionId,
extensionName);
} else {
// Only automatically select the destination if we are not using the
// system default and we have not already found a destination to
// select.
const autoSelect =
!foundDestination && !this.useSystemDefaultAsDefault_;
const foundNewDestination =
this.fetchPreselectedDestination_(destinationParams, autoSelect);
foundDestination = foundDestination || foundNewDestination;
}
}
......@@ -437,21 +445,23 @@ cr.define('print_preview', function() {
}
// Try the system default
id = this.systemDefaultDestinationId_;
origin = id == print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ?
destinationParams.id = this.systemDefaultDestinationId_;
destinationParams.origin = destinationParams.id ==
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ?
print_preview.DestinationOrigin.LOCAL :
this.platformOrigin_;
account = '';
destinationParams.account = '';
const systemDefaultCandidate =
this.destinationMap_[this.getDestinationKey_(origin, id, account)];
this.destinationMap_[this.getDestinationKey_(
destinationParams.origin, destinationParams.id,
destinationParams.account)];
if (systemDefaultCandidate != null) {
this.selectDestination(systemDefaultCandidate);
return;
}
if (this.fetchPreselectedDestination_(
origin, id, account, name, capabilities, extensionId,
extensionName)) {
destinationParams, true /* autoSelect */)) {
return;
}
......@@ -459,25 +469,29 @@ cr.define('print_preview', function() {
}
/**
* Attempts to fetch capabilities of the destination identified by the
* provided origin, id and account.
* @param {print_preview.DestinationOrigin} origin Destination
* origin.
* @param {string} id Destination id.
* @param {string} account User account destination is registered for.
* @param {string} name Destination display name.
* @param {?print_preview.Cdd} capabilities Destination capabilities.
* @param {string} extensionId Extension ID associated with this
* destination.
* @param {string} extensionName Extension name associated with this
* destination.
* Attempts to fetch capabilities of the destination identified by
* |destinationParams|.
* @param {!{
* origin: !print_preview.DestinationOrigin,
* id: string,
* account: string,
* name: string,
* capabilities: ?print_preview.Cdd,
* extensionId: string,
* extensionName: string
* }} destinationParams Parameters identifying the destination.
* @param {boolean} autoSelect Whether to automatically select the
* destination if it is fetched successfully.
* @return {boolean} Whether capabilities fetch was successfully started.
* @private
*/
fetchPreselectedDestination_(
origin, id, account, name, capabilities, extensionId, extensionName) {
this.autoSelectMatchingDestination_ =
this.createExactDestinationMatch_(origin, id);
fetchPreselectedDestination_(destinationParams, autoSelect) {
const id = destinationParams.id;
const origin = destinationParams.origin;
if (autoSelect) {
this.autoSelectMatchingDestination_ =
this.createExactDestinationMatch_(origin, id);
}
const type = print_preview.originToType(origin);
if (type == print_preview.PrinterType.LOCAL_PRINTER) {
......@@ -490,7 +504,8 @@ cr.define('print_preview', function() {
if (this.cloudPrintInterface_ &&
(origin == print_preview.DestinationOrigin.COOKIES ||
origin == print_preview.DestinationOrigin.DEVICE)) {
this.cloudPrintInterface_.printer(id, origin, account);
this.cloudPrintInterface_.printer(
id, origin, destinationParams.account);
return true;
}
......@@ -500,23 +515,28 @@ cr.define('print_preview', function() {
// privet or extension printers in this case.
this.startLoadDestinations(type);
if (!autoSelect) {
return true;
}
// Create a fake selectedDestination_ that is not actually in the
// destination store. When the real destination is created, this
// destination will be overwritten.
const params =
(origin === print_preview.DestinationOrigin.PRIVET) ? {} : {
description: '',
extensionId: extensionId,
extensionName: extensionName,
extensionId: destinationParams.extensionId,
extensionName: destinationParams.extensionName,
provisionalType: print_preview.DestinationProvisionalType.NONE
};
this.selectedDestination_ = new print_preview.Destination(
id, print_preview.DestinationType.LOCAL, origin, name,
false /*isRecent*/,
id, print_preview.DestinationType.LOCAL, origin,
destinationParams.name, false /*isRecent*/,
print_preview.DestinationConnectionStatus.ONLINE, params);
if (capabilities) {
this.selectedDestination_.capabilities = capabilities;
if (destinationParams.capabilities) {
this.selectedDestination_.capabilities =
destinationParams.capabilities;
this.dispatchEvent(
new CustomEvent(DestinationStore.EventType
.SELECTED_DESTINATION_CAPABILITIES_READY));
......@@ -823,9 +843,16 @@ cr.define('print_preview', function() {
!this.autoSelectMatchingDestination_.matchIdAndOrigin(
this.systemDefaultDestinationId_, this.platformOrigin_)) {
if (this.fetchPreselectedDestination_(
this.platformOrigin_, this.systemDefaultDestinationId_,
'' /*account*/, '' /*name*/, null /*capabilities*/,
'' /*extensionId*/, '' /*extensionName*/)) {
{
origin: this.platformOrigin_,
id: this.systemDefaultDestinationId_,
account: '',
name: '',
capabilities: null,
extensionId: '',
extensionName: ''
},
true /* autoSelect */)) {
return;
}
}
......
......@@ -32,6 +32,9 @@ cr.define('destination_select_test', function() {
/** @type {!Array<!print_preview.Destination>} */
let destinations = [];
/** @type {number} */
let numPrintersSelected = 0;
/** @override */
setup(function() {
initialSettings = print_preview_test_utils.getDefaultInitialSettings();
......@@ -57,11 +60,16 @@ cr.define('destination_select_test', function() {
page = document.createElement('print-preview-app');
document.body.appendChild(page);
const promises = [nativeLayer.whenCalled('getInitialSettings')];
if (!opt_expectPrinterFailure) {
promises.push(nativeLayer.whenCalled('getPrinterCapabilities'));
}
return Promise.all(promises);
return nativeLayer.whenCalled('getInitialSettings').then(() => {
page.destinationStore_.addEventListener(
print_preview.DestinationStore.EventType.DESTINATION_SELECT,
function() {
numPrintersSelected++;
});
return opt_expectPrinterFailure ?
Promise.resolve() :
nativeLayer.whenCalled('getPrinterCapabilities');
});
}
/**
......@@ -93,9 +101,9 @@ cr.define('destination_select_test', function() {
recentDestinations: [recentDestination],
});
return setInitialSettings().then(function(argsArray) {
assertEquals('ID1', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
return setInitialSettings().then(function(args) {
assertEquals('ID1', args.destinationId);
assertEquals(print_preview.PrinterType.LOCAL, args.type);
assertEquals('ID1', page.destination_.id);
assertPrinterDisplay('One');
});
......@@ -116,11 +124,11 @@ cr.define('destination_select_test', function() {
});
return setInitialSettings()
.then(function(argsArray) {
.then(function(args) {
// Should have loaded ID1 as the selected printer, since it was most
// recent.
assertEquals('ID1', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('ID1', args.destinationId);
assertEquals(print_preview.PrinterType.LOCAL, args.type);
assertEquals('ID1', page.destination_.id);
assertPrinterDisplay('One');
......@@ -158,11 +166,11 @@ cr.define('destination_select_test', function() {
});
return setInitialSettings()
.then(function(argsArray) {
.then(function(args) {
// Should have loaded ID1 as the selected printer, since it was most
// recent.
assertEquals('ID1', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('ID1', args.destinationId);
assertEquals(print_preview.PrinterType.LOCAL, args.type);
assertEquals('ID1', page.destination_.id);
return nativeLayer.whenCalled('getPreview');
......@@ -172,18 +180,16 @@ cr.define('destination_select_test', function() {
assertEquals(0, ticket.requestID);
assertEquals('ID1', ticket.deviceName);
// None of the other printers should have been loaded. Should only
// have ID1 and Save as PDF. They will be loaded when the dialog is
// opened and startLoadDestinations() is called.
// The other recent destinations should be prefetched, but only one
// should have been selected so there was only one preview request.
const reportedPrinters = page.destinationStore_.destinations();
assertEquals(2, reportedPrinters.length);
assertEquals(4, reportedPrinters.length);
destinations.forEach((destination, index) => {
if (destination.id == 'ID1') {
return;
}
assertFalse(reportedPrinters.some(p => p.id == destination.id));
assertEquals(
index < 3,
reportedPrinters.some(p => p.id == destination.id));
});
assertEquals(1, numPrintersSelected);
});
});
......@@ -195,11 +201,11 @@ cr.define('destination_select_test', function() {
initialSettings.serializedDefaultDestinationSelectionRulesStr =
JSON.stringify({namePattern: '.*Four.*'});
initialSettings.serializedAppStateStr = '';
return setInitialSettings().then(function(argsArray) {
return setInitialSettings().then(function(args) {
// Should have loaded ID4 as the selected printer, since it matches
// the rules.
assertEquals('ID4', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('ID4', args.destinationId);
assertEquals(print_preview.PrinterType.LOCAL, args.type);
assertEquals('ID4', page.destination_.id);
assertPrinterDisplay('Four');
});
......@@ -223,14 +229,20 @@ cr.define('destination_select_test', function() {
recentDestinations: recentDestinations,
});
return setInitialSettings().then(function(argsArray) {
// Need to load FooDevice as the printer, since it is the system
// default.
assertEquals('FooDevice', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('FooDevice', page.destination_.id);
assertPrinterDisplay('FooName');
});
return Promise
.all([
setInitialSettings(),
test_util.eventToPromise(
print_preview.DestinationStore.EventType
.SELECTED_DESTINATION_CAPABILITIES_READY,
page.destinationStore_),
])
.then(function(argsArray) {
// Need to load FooDevice as the printer, since it is the system
// default.
assertEquals('FooDevice', page.destination_.id);
assertPrinterDisplay('FooName');
});
});
/**
......@@ -245,10 +257,10 @@ cr.define('destination_select_test', function() {
initialSettings.isInAppKioskMode = true;
initialSettings.printerName = '';
return setInitialSettings().then(function(argsArray) {
return setInitialSettings().then(function(args) {
// Should have loaded the first destination as the selected printer.
assertEquals(destinations[0].id, argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals(destinations[0].id, args.destinationId);
assertEquals(print_preview.PrinterType.LOCAL, args.type);
assertEquals(destinations[0].id, page.destination_.id);
assertPrinterDisplay(destinations[0].displayName);
});
......
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