Rename AuthType to Origin of destination.

Store AuthType in AppState.

Current Destination.Type is more suitable for selecting display styles. Origin represents better how we get this destination.

BUG=179229

Review URL: https://chromiumcodereview.appspot.com/14340003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195151 0039d316-1c4b-4281-b951-d872f2087c98
parent 264d8a3e
......@@ -350,7 +350,7 @@ cr.define('cloudprint', function() {
try {
printerList.push(
cloudprint.CloudDestinationParser.parse(
printerJson, print_preview.Destination.AuthType.COOKIES));
printerJson, print_preview.Destination.Origin.COOKIES));
} catch (err) {
console.error('Unable to parse cloud print destination: ' + err);
}
......@@ -400,7 +400,7 @@ cr.define('cloudprint', function() {
var printer;
try {
printer = cloudprint.CloudDestinationParser.parse(
printerJson, print_preview.Destination.AuthType.COOKIES);
printerJson, print_preview.Destination.Origin.COOKIES);
} catch (err) {
console.error('Failed to parse cloud print destination: ' +
JSON.stringify(printerJson));
......@@ -414,6 +414,7 @@ cr.define('cloudprint', function() {
var errorEvent = this.createErrorEvent_(
CloudPrintInterface.EventType.PRINTER_FAILED, status, result);
errorEvent.destinationId = destinationId;
errorEvent.destinationOrigin = print_preview.Destination.Origin.COOKIES;
this.dispatchEvent(errorEvent);
}
},
......
......@@ -18,11 +18,11 @@ cr.define('print_preview', function() {
this.selectedDestinationId_ = null;
/**
* Whether the selected destination is a local destination.
* @type {?boolean}
* Origin of the selected destination.
* @type {?string}
* @private
*/
this.isSelectedDestinationLocal_ = null;
this.selectedDestinationOrigin_ = null;
/**
* Whether the GCP promotion has been dismissed.
......@@ -105,7 +105,8 @@ cr.define('print_preview', function() {
AppState.Field_ = {
VERSION: 'version',
SELECTED_DESTINATION_ID: 'selectedDestinationId',
IS_SELECTED_DESTINATION_LOCAL: 'isSelectedDestinationLocal',
SELECTED_DESTINATION_ORIGIN: 'selectedDestinationOrigin',
IS_SELECTED_DESTINATION_LOCAL: 'isSelectedDestinationLocal', // Deprecated
IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
MARGINS_TYPE: 'marginsType',
CUSTOM_MARGINS: 'customMargins',
......@@ -131,9 +132,9 @@ cr.define('print_preview', function() {
return this.selectedDestinationId_;
},
/** @return {?boolean} Whether the selected destination is local. */
get isSelectedDestinationLocal() {
return this.isSelectedDestinationLocal_;
/** @return {?string} Origin of the selected destination. */
get selectedDestinationOrigin() {
return this.selectedDestinationOrigin_;
},
/** @return {boolean} Whether the GCP promotion has been dismissed. */
......@@ -195,14 +196,20 @@ cr.define('print_preview', function() {
}
var state = JSON.parse(serializedAppStateStr);
if (state[AppState.Field_.VERSION] == 2) {
if (state[AppState.Field_.VERSION] == AppState.VERSION_) {
this.selectedDestinationId_ =
state[AppState.Field_.SELECTED_DESTINATION_ID] || null;
if (state.hasOwnProperty(
AppState.Field_.IS_SELECTED_DESTINATION_LOCAL)) {
this.isSelectedDestinationLocal_ =
state[AppState.Field_.IS_SELECTED_DESTINATION_LOCAL];
this.selectedDestinationOrigin_ =
state[AppState.Field_.IS_SELECTED_DESTINATION_LOCAL] ?
print_preview.Destination.Origin.LOCAL :
print_preview.Destination.Origin.COOKIES;
} else {
this.selectedDestinationOrigin_ =
state[AppState.Field_.SELECTED_DESTINATION_ORIGIN] || null;
}
this.isGcpPromoDismissed_ =
state[AppState.Field_.IS_GCP_PROMO_DISMISSED] || false;
if (state.hasOwnProperty(AppState.Field_.MARGINS_TYPE)) {
......@@ -242,7 +249,7 @@ cr.define('print_preview', function() {
*/
persistSelectedDestination: function(dest) {
this.selectedDestinationId_ = dest.id;
this.isSelectedDestinationLocal_ = dest.isLocal;
this.selectedDestinationOrigin_ = dest.origin;
this.persist_();
},
......@@ -339,8 +346,8 @@ cr.define('print_preview', function() {
obj[AppState.Field_.VERSION] = AppState.VERSION_;
obj[AppState.Field_.SELECTED_DESTINATION_ID] =
this.selectedDestinationId_;
obj[AppState.Field_.IS_SELECTED_DESTINATION_LOCAL] =
this.isSelectedDestinationLocal_;
obj[AppState.Field_.SELECTED_DESTINATION_ORIGIN] =
this.selectedDestinationOrigin_;
obj[AppState.Field_.IS_GCP_PROMO_DISMISSED] = this.isGcpPromoDismissed_;
obj[AppState.Field_.MARGINS_TYPE] = this.marginsType_;
if (this.customMargins_) {
......
......@@ -56,11 +56,11 @@ cr.define('cloudprint', function() {
* response.
* @param {!Object} json Object that represents a Google Cloud Print search or
* printer response.
* @param {!print_preview.Destination.AuthType} authType The authentication
* type used to find printer.
* @param {!print_preview.Destination.Origin} origin The origin of the
* response.
* @return {!print_preview.Destination} Parsed destination.
*/
CloudDestinationParser.parse = function(json, authType) {
CloudDestinationParser.parse = function(json, origin) {
if (!json.hasOwnProperty(CloudDestinationParser.Field_.ID) ||
!json.hasOwnProperty(CloudDestinationParser.Field_.TYPE) ||
!json.hasOwnProperty(CloudDestinationParser.Field_.DISPLAY_NAME)) {
......@@ -83,7 +83,7 @@ cr.define('cloudprint', function() {
id,
CloudDestinationParser.parseType_(
json[CloudDestinationParser.Field_.TYPE]),
authType,
origin,
json[CloudDestinationParser.Field_.DISPLAY_NAME],
arrayContains(tags, CloudDestinationParser.RECENT_TAG_) /*isRecent*/,
connectionStatus,
......
......@@ -10,8 +10,8 @@ cr.define('print_preview', function() {
* destinations.
* @param {string} id ID of the destination.
* @param {!print_preview.Destination.Type} type Type of the destination.
* @param {!print_preview.Destination.AuthType} authType Type of the
* authentication used to access the destination.
* @param {!print_preview.Destination.Origin} origin Origin of the
* destination.
* @param {string} displayName Display name of the destination.
* @param {boolean} isRecent Whether the destination has been used recently.
* @param {!print_preview.Destination.ConnectionStatus} connectionStatus
......@@ -23,7 +23,7 @@ cr.define('print_preview', function() {
* destination.
* @constructor
*/
function Destination(id, type, authType, displayName, isRecent,
function Destination(id, type, origin, displayName, isRecent,
connectionStatus, opt_params) {
/**
* ID of the destination.
......@@ -40,11 +40,11 @@ cr.define('print_preview', function() {
this.type_ = type;
/**
* Type of authentication for the destination.
* @type {!print_preview.Destination.AuthType}
* Origin of the destination.
* @type {!print_preview.Destination.Origin}
* @private
*/
this.authType_ = authType;
this.origin_ = origin;
/**
* Display name of the destination.
......@@ -142,10 +142,10 @@ cr.define('print_preview', function() {
};
/**
* Enumeration of the authentication types for cloud destinations.
* Enumeration of the origin types for cloud destinations.
* @enum {string}
*/
Destination.AuthType = {
Destination.Origin = {
LOCAL: 'local',
COOKIES: 'cookies',
PROFILE: 'profile',
......@@ -192,11 +192,10 @@ cr.define('print_preview', function() {
},
/**
* @return {!print_preview.Destination.AuthType} Type of authentication for
* the destination.
* @return {!print_preview.Destination.Origin} Origin of the destination.
*/
get authType() {
return this.authType_;
get origin() {
return this.origin_;
},
/** @return {string} Display name of the destination. */
......@@ -226,7 +225,7 @@ cr.define('print_preview', function() {
/** @return {boolean} Whether the destination is local or cloud-based. */
get isLocal() {
return this.type_ == Destination.Type.LOCAL;
return this.origin_ == Destination.Origin.LOCAL;
},
/**
......
......@@ -39,7 +39,7 @@ cr.define('print_preview', function() {
this.destinations_ = [];
/**
* Cache used for constant lookup of destinations by ID.
* Cache used for constant lookup of destinations by origin and id.
* @type {object.<string, !print_preview.Destination>}
* @private
*/
......@@ -62,11 +62,11 @@ cr.define('print_preview', function() {
this.initialDestinationId_ = null;
/**
* Whether the initial destination is a local one or not.
* @type {boolean}
* Initial origin used to auto-select destination.
* @type {print_preview.Destination.Origin}
* @private
*/
this.isInitialDestinationLocal_ = true;
this.initialDestinationOrigin_ = print_preview.Destination.Origin.LOCAL;
/**
* Whether the destination store will auto select the destination that
......@@ -154,7 +154,7 @@ cr.define('print_preview', function() {
var dest = new print_preview.Destination(
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
print_preview.Destination.Type.LOCAL,
print_preview.Destination.AuthType.LOCAL,
print_preview.Destination.Origin.LOCAL,
localStrings.getString('printToPDF'),
false /*isRecent*/,
print_preview.Destination.ConnectionStatus.ONLINE);
......@@ -218,25 +218,31 @@ cr.define('print_preview', function() {
* @private
*/
init: function(systemDefaultDestinationId) {
if (this.appState_.selectedDestinationId) {
if (this.appState_.selectedDestinationId &&
this.appState_.selectedDestinationOrigin) {
this.initialDestinationId_ = this.appState_.selectedDestinationId;
this.isInitialDestinationLocal_ =
this.appState_.isSelectedDestinationLocal;
this.initialDestinationOrigin_ =
this.appState_.selectedDestinationOrigin_;
} else {
this.initialDestinationId_ = systemDefaultDestinationId;
this.isInitialDestinationLocal_ = true;
this.initialDestinationOrigin_ =
print_preview.Destination.Origin.LOCAL;
}
this.isInAutoSelectMode_ = true;
if (this.initialDestinationId_ == null) {
if (this.initialDestinationId_ == null ||
this.initialDestinationOrigin_ == null) {
assert(this.destinations_.length > 0,
'No destinations available to select');
this.selectDestination(this.destinations_[0]);
} else {
var candidate = this.destinationMap_[this.initialDestinationId_];
var key = this.getDestinationKey_(this.initialDestinationOrigin_,
this.initialDestinationId_);
var candidate = this.destinationMap_[key];
if (candidate != null) {
this.selectDestination(candidate);
} else if (!cr.isChromeOS && this.isInitialDestinationLocal_) {
} else if (!cr.isChromeOS &&
this.initialDestinationOrigin_ ==
print_preview.Destination.Origin.LOCAL) {
this.nativeLayer_.startGetLocalDestinationCapabilities(
this.initialDestinationId_);
}
......@@ -267,8 +273,10 @@ cr.define('print_preview', function() {
cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED,
this.onCloudPrintPrinterFailed_.bind(this));
// Fetch initial destination if its a cloud destination.
if (this.isInAutoSelectMode_ && !this.isInitialDestinationLocal_) {
this.cloudPrintInterface_.printer(this.initialDestinationId_);
var origin = this.initialDestinationOrigin_;
if (this.isInAutoSelectMode_ &&
origin != print_preview.Destination.Origin.LOCAL) {
this.cloudPrintInterface_.printer(this.initialDestinationId_, origin);
}
},
......@@ -313,7 +321,8 @@ cr.define('print_preview', function() {
assert(this.cloudPrintInterface_ != null,
'Selected destination is a cloud destination, but Google ' +
'Cloud Print is not enabled');
this.cloudPrintInterface_.printer(destination.id);
this.cloudPrintInterface_.printer(destination.id,
destination.origin);
}
} else {
cr.dispatchSimpleEvent(
......@@ -334,8 +343,7 @@ cr.define('print_preview', function() {
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATIONS_INSERTED);
if (this.isInAutoSelectMode_ &&
(this.initialDestinationId_ == null ||
destination.id == this.initialDestinationId_)) {
this.matchInitialDestination_(destination.id, destination.origin)) {
this.selectDestination(destination);
}
}
......@@ -356,8 +364,7 @@ cr.define('print_preview', function() {
insertedDestination = true;
if (this.isInAutoSelectMode_ &&
destinationToAutoSelect == null &&
(this.initialDestinationId_ == null ||
dest.id == this.initialDestinationId_)) {
this.matchInitialDestination_(dest.id, dest.origin)) {
destinationToAutoSelect = dest;
}
}
......@@ -376,15 +383,17 @@ cr.define('print_preview', function() {
* the destination doesn't already exist, it will be added.
* @param {!print_preview.Destination} destination Destination to update.
* @return {!print_preview.Destination} The existing destination that was
* updated.
* updated or {@code null} if it was the new destination.
*/
updateDestination: function(destination) {
var existingDestination = this.destinationMap_[destination.id];
var key = this.getDestinationKey_(destination.origin, destination.id);
var existingDestination = this.destinationMap_[key];
if (existingDestination != null) {
existingDestination.capabilities = destination.capabilities;
return existingDestination;
} else {
this.insertDestination(destination);
return null;
}
},
......@@ -418,10 +427,11 @@ cr.define('print_preview', function() {
* @private
*/
insertDestination_: function(destination) {
var existingDestination = this.destinationMap_[destination.id];
var key = this.getDestinationKey_(destination.origin, destination.id);
var existingDestination = this.destinationMap_[key];
if (existingDestination == null) {
this.destinations_.push(destination);
this.destinationMap_[destination.id] = destination;
this.destinationMap_[key] = destination;
return true;
} else if (existingDestination.connectionStatus ==
print_preview.Destination.ConnectionStatus.UNKNOWN &&
......@@ -499,7 +509,10 @@ cr.define('print_preview', function() {
*/
onLocalDestinationCapabilitiesSet_: function(event) {
var destinationId = event.settingsInfo['printerId'];
var destination = this.destinationMap_[destinationId];
var key =
this.getDestinationKey_(print_preview.Destination.Origin.LOCAL,
destinationId);
var destination = this.destinationMap_[key];
var capabilities = print_preview.LocalCapabilitiesParser.parse(
event.settingsInfo);
if (destination) {
......@@ -537,7 +550,8 @@ cr.define('print_preview', function() {
console.error('Failed to get print capabilities for printer ' +
event.destinationId);
if (this.isInAutoSelectMode_ &&
this.initialDestinationId_ == event.destinationId) {
this.matchInitialDestinationStrict_(event.destinationId,
event.destinationOrigin)) {
assert(this.destinations_.length > 0,
'No destinations were loaded when failed to get initial ' +
'destination');
......@@ -593,7 +607,8 @@ cr.define('print_preview', function() {
*/
onCloudPrintPrinterFailed_: function(event) {
if (this.isInAutoSelectMode_ &&
this.initialDestinationId_ == event.destinationId) {
this.matchInitialDestinationStrict_(event.destinationId,
event.destinationOrigin)) {
console.error('Could not find initial printer: ' + event.destinationId);
assert(this.destinations_.length > 0,
'No destinations were loaded when failed to get initial ' +
......@@ -625,6 +640,41 @@ cr.define('print_preview', function() {
assert(this.destinations_.length > 0,
'No destinations were loaded before auto-select timeout expired');
this.selectDestination(this.destinations_[0]);
},
// TODO(vitalybuka): Remove three next functions replacing Destination.id
// and Destination.origin by complex ID.
/**
* Returns key to be used with {@code destinationMap_}.
* @param {!print_preview.Destination.Origin} origin Destination origin.
* @return {!string} id Destination id.
* @private
*/
getDestinationKey_: function(origin, id) {
return origin + '/' + id;
},
/**
* @param {?string} id Id of the destination.
* @param {?string} origin Oring of the destination.
* @return {boolean} Whether a initial destination matches provided.
* @private
*/
matchInitialDestination_: function(id, origin) {
return this.initialDestinationId_ == null ||
this.initialDestinationOrigin_ == null ||
this.matchInitialDestinationStrict_(id, origin);
},
/**
* @param {?string} id Id of the destination.
* @param {?string} origin Oring of the destination.
* @return {boolean} Whether destination is the same as initial.
* @private
*/
matchInitialDestinationStrict_: function(id, origin) {
return id == this.initialDestinationId_ &&
origin == this.initialDestinationOrigin_;
}
};
......
......@@ -18,7 +18,7 @@ cr.define('print_preview', function() {
return new print_preview.Destination(
destinationInfo.deviceName,
print_preview.Destination.Type.LOCAL,
print_preview.Destination.AuthType.LOCAL,
print_preview.Destination.Origin.LOCAL,
destinationInfo.printerName,
false /*isRecent*/,
print_preview.Destination.ConnectionStatus.ONLINE);
......
......@@ -399,6 +399,8 @@ cr.define('print_preview', function() {
var getCapsFailEvent = new cr.Event(
NativeLayer.EventType.GET_CAPABILITIES_FAIL);
getCapsFailEvent.destinationId = destinationId;
getCapsFailEvent.destinationOrigin =
print_preview.Destination.Origin.LOCAL;
this.dispatchEvent(getCapsFailEvent);
},
......
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