Commit 41e97392 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Update CloudPrintInterface and InvitationStore events

- Remove PROCESS_INVITE_FAILED event that was handled identically to
PROCESS_INVITE event.
- Use CustomEvent instead of Event for all events

Bug: None
Change-Id: I9de2723779267dc0e8b8d195337b89d9aeb5ca8f
Reviewed-on: https://chromium-review.googlesource.com/c/1395178
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#619886}
parent 2a7d9319
......@@ -14,7 +14,6 @@ cloudprint.CloudPrintInterfaceEventType = {
PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE',
PRINTER_FAILED: 'cloudprint.CloudPrintInterface.PRINTER_FAILED',
PROCESS_INVITE_DONE: 'cloudprint.CloudPrintInterface.PROCESS_INVITE_DONE',
PROCESS_INVITE_FAILED: 'cloudprint.CloudPrintInterface.PROCESS_INVITE_FAILED',
SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE',
SEARCH_FAILED: 'cloudprint.CloudPrintInterface.SEARCH_FAILED',
SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE',
......@@ -25,25 +24,19 @@ cloudprint.CloudPrintInterfaceEventType = {
* @typedef {{
* user: string,
* origin: !print_preview.DestinationOrigin,
* printers: (!Array<!print_preview.Destination>|undefined)
* printers: (!Array<!print_preview.Destination>|undefined),
* searchDone: boolean,
* }}
*/
cloudprint.CloudPrintInterfaceSearchDoneEvent;
/**
* @typedef {{
* printer: !print_preview.Destination,
* }}
*/
cloudprint.CloudPrintInterfacePrinterDoneEvent;
cloudprint.CloudPrintInterfaceSearchDoneDetail;
/**
* @typedef {{
* destinationId: string,
* destinationOrigin: !print_preview.DestinationOrigin,
* origin: !print_preview.DestinationOrigin,
* }}
*/
cloudprint.CloudPrintInterfacePrinterFailedEvent;
cloudprint.CloudPrintInterfacePrinterFailedDetail;
/**
* @typedef {{
......@@ -51,21 +44,17 @@ cloudprint.CloudPrintInterfacePrinterFailedEvent;
* user: string,
* }}
*/
cloudprint.CloudPrintInterfaceInvitesDoneEvent;
/**
* @typedef {{
* user: string,
* }}
*/
cloudprint.CloudPrintInterfaceInvitesFailedEvent;
cloudprint.CloudPrintInterfaceInvitesDoneDetail;
/**
* @typedef {{
* invitation: !print_preview.Invitation,
* printer: ?print_preview.Destination,
* accept: boolean,
* user: string,
* }}
*/
cloudprint.CloudPrintInterfaceProcessInviteEvent;
cloudprint.CloudPrintInterfaceProcessInviteDetail;
cr.define('cloudprint', function() {
/** @interface */
......
......@@ -287,26 +287,25 @@ cr.define('cloudprint', function() {
}
/**
* Creates a Google Cloud Print interface error that is ready to dispatch.
* @param {!cloudprint.CloudPrintInterfaceEventType} type Type of the
* error.
* Creates an object containing information about the error based on the
* request.
* @param {!cloudprint.CloudPrintRequest} request Request that has been
* completed.
* @return {!Event} Google Cloud Print interface error event.
* @return {!{ status: number,
* errorCode: number,
* message: string,
* origin: !print_preview.DestinationOrigin }} Information
* about the error.
* @private
*/
createErrorEvent_(type, request) {
const errorEvent = new Event(type);
errorEvent.status = request.xhr.status;
if (request.xhr.status == 200) {
errorEvent.errorCode = request.result['errorCode'];
errorEvent.message = request.result['message'];
} else {
errorEvent.errorCode = 0;
errorEvent.message = '';
}
errorEvent.origin = request.origin;
return errorEvent;
createErrorEventDetail_(request) {
const status200 = request.xhr.status === 200;
return {
status: request.xhr.status,
errorCode: status200 ? request.result['errorCode'] : 0,
message: status200 ? request.result['message'] : '',
origin: request.origin,
};
}
/**
......@@ -407,7 +406,6 @@ cr.define('cloudprint', function() {
activeUser = request.result && request.result['request'] &&
request.result['request']['user'];
}
let event = null;
if (request.xhr.status == 200 && request.result['success']) {
// Extract printers.
const printerListJson = request.result['printers'] || [];
......@@ -423,17 +421,24 @@ cr.define('cloudprint', function() {
// Extract and store users.
this.setUsers_(request);
// Dispatch SEARCH_DONE event.
event = new Event(CloudPrintInterfaceEventType.SEARCH_DONE);
event.origin = request.origin;
event.printers = printerList;
event.isRecent = isRecent;
this.eventTarget_.dispatchEvent(
new CustomEvent(CloudPrintInterfaceEventType.SEARCH_DONE, {
detail: {
origin: request.origin,
printers: printerList,
isRecent: isRecent,
user: activeUser,
searchDone: lastRequestForThisOrigin,
}
}));
} else {
event = this.createErrorEvent_(
CloudPrintInterfaceEventType.SEARCH_FAILED, request);
const errorEventDetail = this.createErrorEventDetail_(request);
errorEventDetail.user = activeUser;
errorEventDetail.searchDone = lastRequestForThisOrigin;
this.eventTarget_.dispatchEvent(new CustomEvent(
CloudPrintInterfaceEventType.SEARCH_FAILED,
{detail: errorEventDetail}));
}
event.user = activeUser;
event.searchDone = lastRequestForThisOrigin;
this.eventTarget_.dispatchEvent(event);
}
/**
......@@ -443,7 +448,6 @@ cr.define('cloudprint', function() {
* @private
*/
onInvitesDone_(request) {
let event = null;
const activeUser = (request.result && request.result['request'] &&
request.result['request']['user']) ||
'';
......@@ -460,14 +464,17 @@ cr.define('cloudprint', function() {
}
});
// Dispatch INVITES_DONE event.
event = new Event(CloudPrintInterfaceEventType.INVITES_DONE);
event.invitations = invitationList;
this.eventTarget_.dispatchEvent(
new CustomEvent(CloudPrintInterfaceEventType.INVITES_DONE, {
detail: {
invitations: invitationList,
user: activeUser,
}
}));
} else {
event = this.createErrorEvent_(
CloudPrintInterfaceEventType.INVITES_FAILED, request);
this.eventTarget_.dispatchEvent(new CustomEvent(
CloudPrintInterfaceEventType.INVITES_FAILED, {detail: activeUser}));
}
event.user = activeUser;
this.eventTarget_.dispatchEvent(event);
}
/**
......@@ -479,28 +486,27 @@ cr.define('cloudprint', function() {
* @private
*/
onProcessInviteDone_(invitation, accept, request) {
let event = null;
const activeUser = (request.result && request.result['request'] &&
request.result['request']['user']) ||
'';
if (request.xhr.status == 200 && request.result['success']) {
event = new Event(CloudPrintInterfaceEventType.PROCESS_INVITE_DONE);
if (accept) {
let printer = null;
if (request.xhr.status == 200 && request.result['success'] && accept) {
try {
event.printer = cloudprint.parseCloudDestination(
printer = cloudprint.parseCloudDestination(
request.result['printer'], request.origin, activeUser);
} catch (e) {
console.error('Failed to parse cloud print destination: ' + e);
}
}
} else {
event = this.createErrorEvent_(
CloudPrintInterfaceEventType.PROCESS_INVITE_FAILED, request);
this.eventTarget_.dispatchEvent(
new CustomEvent(CloudPrintInterfaceEventType.PROCESS_INVITE_DONE, {
detail: {
printer: printer,
invitation: invitation,
accept: accept,
user: activeUser,
}
event.invitation = invitation;
event.accept = accept;
event.user = activeUser;
this.eventTarget_.dispatchEvent(event);
}));
}
/**
......@@ -511,14 +517,14 @@ cr.define('cloudprint', function() {
*/
onSubmitDone_(request) {
if (request.xhr.status == 200 && request.result['success']) {
const submitDoneEvent =
new Event(CloudPrintInterfaceEventType.SUBMIT_DONE);
submitDoneEvent.jobId = request.result['job']['id'];
this.eventTarget_.dispatchEvent(submitDoneEvent);
this.eventTarget_.dispatchEvent(new CustomEvent(
CloudPrintInterfaceEventType.SUBMIT_DONE,
{detail: request.result['job']['id']}));
} else {
const errorEvent = this.createErrorEvent_(
CloudPrintInterfaceEventType.SUBMIT_FAILED, request);
this.eventTarget_.dispatchEvent(errorEvent);
const errorEventDetail = this.createErrorEventDetail_(request);
this.eventTarget_.dispatchEvent(new CustomEvent(
CloudPrintInterfaceEventType.SUBMIT_FAILED,
{detail: errorEventDetail}));
}
}
......@@ -567,16 +573,14 @@ cr.define('cloudprint', function() {
JSON.stringify(printerJson));
return;
}
const printerDoneEvent =
new Event(CloudPrintInterfaceEventType.PRINTER_DONE);
printerDoneEvent.printer = printer;
this.eventTarget_.dispatchEvent(printerDoneEvent);
this.eventTarget_.dispatchEvent(new CustomEvent(
CloudPrintInterfaceEventType.PRINTER_DONE, {detail: printer}));
} else {
const errorEvent = this.createErrorEvent_(
CloudPrintInterfaceEventType.PRINTER_FAILED, request);
errorEvent.destinationId = destinationId;
errorEvent.destinationOrigin = request.origin;
this.eventTarget_.dispatchEvent(errorEvent);
const errorEventDetail = this.createErrorEventDetail_(request);
errorEventDetail.destinationId = destinationId;
this.eventTarget_.dispatchEvent(new CustomEvent(
CloudPrintInterfaceEventType.PRINTER_FAILED,
{detail: errorEventDetail}));
}
}
}
......
......@@ -1220,22 +1220,25 @@ cr.define('print_preview', function() {
/**
* Called when the /search call completes, either successfully or not.
* In case of success, stores fetched destinations.
* @param {!cloudprint.CloudPrintInterfaceSearchDoneEvent} event Contains
* the request result.
* @param {!CustomEvent} event Contains the request result.
* @private
*/
onCloudPrintSearchDone_(event) {
if (event.printers && event.printers.length > 0) {
this.insertDestinations_(event.printers);
const payload =
/** @type {!cloudprint.CloudPrintInterfaceSearchDoneDetail} */ (
event.detail);
if (payload.printers && payload.printers.length > 0) {
this.insertDestinations_(payload.printers);
if (this.selectFirstDestination_) {
this.selectDestination(this.destinations_[0]);
this.selectFirstDestination_ = false;
}
}
if (event.searchDone) {
const origins = this.loadedCloudOrigins_[event.user] || [];
if (origins.indexOf(event.origin) < 0) {
this.loadedCloudOrigins_[event.user] = origins.concat([event.origin]);
if (payload.searchDone) {
const origins = this.loadedCloudOrigins_[payload.user] || [];
if (origins.indexOf(payload.origin) < 0) {
this.loadedCloudOrigins_[payload.user] =
origins.concat([payload.origin]);
}
}
this.dispatchEvent(
......@@ -1262,43 +1265,46 @@ cr.define('print_preview', function() {
/**
* Called when /printer call completes. Updates the specified destination's
* print capabilities.
* @param {!cloudprint.CloudPrintInterfacePrinterDoneEvent} event Contains
* detailed information about the destination.
* @param {!CustomEvent} event Contains detailed information about the
* destination.
* @private
*/
onCloudPrintPrinterDone_(event) {
this.updateDestination_(event.printer);
this.updateDestination_(
/** @type {!print_preview.Destination} */ (event.detail));
}
/**
* Called when the Google Cloud Print interface fails to lookup a
* destination. Selects another destination if the failed destination was
* the initial destination.
* @param {!cloudprint.CloudPrintInterfacePrinterFailedEvent} event
* Contains the ID of the destination that was failed to be looked up.
* @param {!CustomEvent} event Contains the ID of the destination that was
* failed to be looked up.
* @private
*/
onCloudPrintPrinterFailed_(event) {
const eventDetail =
/** @type {!cloudprint.CloudPrintInterfacePrinterFailedDetail } */ (
event.detail);
if (this.autoSelectMatchingDestination_ &&
this.autoSelectMatchingDestination_.matchIdAndOrigin(
event.destinationId, event.destinationOrigin)) {
eventDetail.destinationId, eventDetail.origin)) {
console.error(
'Failed to fetch last used printer caps: ' + event.destinationId);
'Failed to fetch last used printer caps: ' +
eventDetail.destinationId);
this.selectDefaultDestination_();
}
}
/**
* Called when printer sharing invitation was processed successfully.
* @param {Event} event Contains detailed information about the invite and
* newly accepted destination (if known).
* @param {!CustomEvent} event Contains detailed information about the
* invite and newly accepted destination (if known).
* @private
*/
onCloudPrintProcessInviteDone_(event) {
if (event.accept && event.printer) {
// Hint the destination list to promote this new destination.
event.printer.isRecent = true;
this.insertDestination_(event.printer);
if (event.detail.accept && event.detail.printer) {
this.insertDestination_(event.detail.printer);
}
}
......
......@@ -96,15 +96,11 @@ cr.define('print_preview', function() {
this.tracker_.add(
this.cloudPrintInterface_.getEventTarget(),
cloudprint.CloudPrintInterfaceEventType.INVITES_FAILED,
this.onCloudPrintInvitesDone_.bind(this));
this.onCloudPrintInvitesFailed_.bind(this));
this.tracker_.add(
this.cloudPrintInterface_.getEventTarget(),
cloudprint.CloudPrintInterfaceEventType.PROCESS_INVITE_DONE,
this.onCloudPrintProcessInviteDone_.bind(this));
this.tracker_.add(
this.cloudPrintInterface_.getEventTarget(),
cloudprint.CloudPrintInterfaceEventType.PROCESS_INVITE_FAILED,
this.onCloudPrintProcessInviteFailed_.bind(this));
}
/** Initiates loading of cloud printer sharing invitations. */
......@@ -118,8 +114,8 @@ cr.define('print_preview', function() {
if (this.loadStatus_.hasOwnProperty(this.userInfo_.activeUser)) {
if (this.loadStatus_[this.userInfo_.activeUser] ==
print_preview.InvitationStoreLoadStatus.DONE) {
cr.dispatchSimpleEvent(
this, InvitationStore.EventType.INVITATION_SEARCH_DONE);
this.dispatchEvent(new CustomEvent(
InvitationStore.EventType.INVITATION_SEARCH_DONE));
}
return;
}
......@@ -161,54 +157,44 @@ cr.define('print_preview', function() {
/**
* Called when printer sharing invitations are fetched.
* @param {!cloudprint.CloudPrintInterfaceInvitesDoneEvent} event Contains
* the list of invitations.
* @param {!CustomEvent} event Contains the list of invitations.
* @private
*/
onCloudPrintInvitesDone_(event) {
this.loadStatus_[event.user] =
const invitesDoneDetail =
/** @type {!cloudprint.CloudPrintInterfaceInvitesDoneDetail} */ (
event.detail);
this.loadStatus_[invitesDoneDetail.user] =
print_preview.InvitationStoreLoadStatus.DONE;
this.invitations_[event.user] = event.invitations;
this.invitations_[invitesDoneDetail.user] = invitesDoneDetail.invitations;
cr.dispatchSimpleEvent(
this, InvitationStore.EventType.INVITATION_SEARCH_DONE);
this.dispatchEvent(
new CustomEvent(InvitationStore.EventType.INVITATION_SEARCH_DONE));
}
/**
* Called when printer sharing invitations fetch has failed.
* @param {!cloudprint.CloudPrintInterfaceInvitesFailedEvent} event
* @param {!CustomEvent} event
* @private
*/
onCloudPrintInvitesFailed_(event) {
this.loadStatus_[event.user] =
this.loadStatus_[/** @type {string} */ (event.detail)] =
print_preview.InvitationStoreLoadStatus.FAILED;
}
/**
* Called when printer sharing invitation was processed successfully.
* @param {!cloudprint.CloudPrintInterfaceProcessInviteEvent} event
* Contains detailed information about the invite and newly accepted
* destination.
* @param {!CustomEvent} event Contains detailed information about the
* invite.
* @private
*/
onCloudPrintProcessInviteDone_(event) {
this.invitationProcessed_(event.invitation);
cr.dispatchSimpleEvent(
this, InvitationStore.EventType.INVITATION_PROCESSED);
}
/**
* Called when /printer call completes. Updates the specified destination's
* print capabilities.
* @param {!cloudprint.CloudPrintInterfaceProcessInviteEvent} event
* Contains detailed information about the invite and destination.
* @private
*/
onCloudPrintProcessInviteFailed_(event) {
this.invitationProcessed_(event.invitation);
// TODO: Display an error.
cr.dispatchSimpleEvent(
this, InvitationStore.EventType.INVITATION_PROCESSED);
this.invitationProcessed_(
/** @type {!cloudprint.CloudPrintInterfaceProcessInviteDetail} */ (
event.detail)
.invitation);
this.dispatchEvent(
new CustomEvent(InvitationStore.EventType.INVITATION_PROCESSED));
}
}
......
......@@ -604,26 +604,29 @@ Polymer({
/**
* Called when there was an error communicating with Google Cloud print.
* Displays an error message in the print header.
* @param {!Event} event Contains the error message.
* @param {!CustomEvent} event Contains the error message.
* @private
*/
onCloudPrintError_: function(event) {
if (event.status == 0) {
if (event.detail.status == 0) {
return; // Ignore, the system does not have internet connectivity.
}
if (event.status == 403) {
if (event.detail.status == 403) {
if (!this.isInAppKioskMode_) {
this.$.destinationSettings.showCloudPrintPromo();
}
} else {
this.errorMessage_ = event.message;
this.errorMessage_ = event.detail.message;
this.$.state.transitTo(print_preview_new.State.FATAL_ERROR);
}
if (event.status == 200) {
if (event.detail.status == 200) {
console.error(
`Google Cloud Print Error: (${event.errorCode}) ${event.message}`);
'Google Cloud Print Error: ' +
`(${event.detail.errorCode}) ${event.detail.message}`);
} else {
console.error(`Google Cloud Print Error: HTTP status ${event.status}`);
console.error(
'Google Cloud Print Error: ' +
`HTTP status ${event.detail.status}`);
}
},
......
......@@ -45,16 +45,19 @@ cr.define('print_preview', function() {
*/
search() {
this.searchInProgress_ = true;
const printers = [];
this.cloudPrintersMap_.forEach((value) => printers.push(value));
const searchDoneEvent =
new Event(cloudprint.CloudPrintInterfaceEventType.SEARCH_DONE);
searchDoneEvent.origin = print_preview.DestinationOrigin.COOKIES;
searchDoneEvent.printers = [];
this.cloudPrintersMap_.forEach((value) => {
searchDoneEvent.printers.push(value);
new CustomEvent(cloudprint.CloudPrintInterfaceEventType.SEARCH_DONE, {
detail: {
origin: print_preview.DestinationOrigin.COOKIES,
printers: printers,
isRecent: true,
user: 'foo@chromium.org',
searchDone: true,
}
});
searchDoneEvent.isRecent = true;
searchDoneEvent.user = 'foo@chromium.org';
searchDoneEvent.searchDone = true;
this.searchInProgress_ = false;
this.eventTarget_.dispatchEvent(searchDoneEvent);
}
......@@ -70,12 +73,11 @@ cr.define('print_preview', function() {
printer(printerId, origin, account) {
const printer = this.cloudPrintersMap_.get(printerId);
if (!!printer) {
const printerDoneEvent =
new Event(cloudprint.CloudPrintInterfaceEventType.PRINTER_DONE);
printerDoneEvent.printer = printer;
printerDoneEvent.printer.capabilities =
printer.capabilities =
print_preview_test_utils.getCddTemplate(printerId);
this.eventTarget_.dispatchEvent(printerDoneEvent);
this.eventTarget_.dispatchEvent(new CustomEvent(
cloudprint.CloudPrintInterfaceEventType.PRINTER_DONE,
{detail: printer}));
}
}
}
......
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