Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6806)

Unified Diff: chrome/browser/resources/print_preview/data/app_state.js

Issue 2346153002: Save most recent 3 destinations across multiple sessions (Closed)
Patch Set: Factor out to helper functions, small fixes Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/destination_store.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/print_preview/data/app_state.js
diff --git a/chrome/browser/resources/print_preview/data/app_state.js b/chrome/browser/resources/print_preview/data/app_state.js
index b782f4ea3d1543fc1b8edb23ba6fdb842778570c..69663aade2dd9963b3180f0de7dc116d540aeb86 100644
--- a/chrome/browser/resources/print_preview/data/app_state.js
+++ b/chrome/browser/resources/print_preview/data/app_state.js
@@ -29,18 +29,25 @@ cr.define('print_preview', function() {
};
/**
+ * Number of recent print destinations to store across browser sessions.
+ * @const {number}
+ */
+ AppState.NUM_DESTINATIONS_ = 3;
+
+
+ /**
* Enumeration of field names for serialized app state.
* @enum {string}
*/
AppState.Field = {
VERSION: 'version',
- SELECTED_DESTINATION_ID: 'selectedDestinationId',
- SELECTED_DESTINATION_ACCOUNT: 'selectedDestinationAccount',
- SELECTED_DESTINATION_ORIGIN: 'selectedDestinationOrigin',
- SELECTED_DESTINATION_CAPABILITIES: 'selectedDestinationCapabilities',
- SELECTED_DESTINATION_NAME: 'selectedDestinationName',
- SELECTED_DESTINATION_EXTENSION_ID: 'selectedDestinationExtensionId',
- SELECTED_DESTINATION_EXTENSION_NAME: 'selectedDestinationExtensionName',
+ RECENT_DESTINATION_IDS: 'recentDestinationIds',
+ RECENT_DESTINATION_ACCOUNTS: 'recentDestinationAccounts',
+ RECENT_DESTINATION_ORIGINS: 'recentDestinationOrigins',
+ RECENT_DESTINATION_CAPABILITIES: 'recentDestinationCapabilities',
+ RECENT_DESTINATION_NAMES: 'recentDestinationNames',
+ RECENT_DESTINATION_EXTENSION_IDS: 'recentDestinationExtensionIds',
+ RECENT_DESTINATION_EXTENSION_NAMES: 'recentDestinationExtensionNames',
IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
DPI: 'dpi',
MEDIA_SIZE: 'mediaSize',
@@ -74,14 +81,29 @@ cr.define('print_preview', function() {
AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
AppState.prototype = {
+
+ /**
+ * Helper function to get the most recent value of one of the destination
+ * fields in the app state.
+ * @param {?print_preview.AppState.field} The state field to return the
dpapad 2016/09/19 22:16:19 Type annotation is missing the parameter name, and
rbpotter 2016/09/20 23:47:37 Done.
+ * value of.
+ * @return {?string or ?print_preview.Cdd} The most recent value of the
dpapad 2016/09/19 22:16:19 This is not a valid type annotation. I am not sure
rbpotter 2016/09/20 23:47:37 Done - I did not realize there was actually a comp
+ * destination state field. ?print_preview.Cdd if the state field is
+ * RECENT_DESTINATION_CAPABILIITIES, ?string otherwise.
+ */
+ getStateValue_: function(fieldName) {
+ return (this.state_[fieldName] && this.state_[fieldName].length > 0) ?
+ this.state_[fieldName][0] : null;
+ },
+
/** @return {?string} ID of the selected destination. */
get selectedDestinationId() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_ID];
+ return this.getStateValue_(AppState.Field.RECENT_DESTINATION_IDS);
},
/** @return {?string} Account the selected destination is registered for. */
get selectedDestinationAccount() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT];
+ return this.getStateValue_(AppState.Field.RECENT_DESTINATION_ACCOUNTS);
},
/**
@@ -89,24 +111,26 @@ cr.define('print_preview', function() {
* selected destination.
*/
get selectedDestinationOrigin() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
+ return this.getStateValue_(AppState.Field.RECENT_DESTINATION_ORIGINS);
},
/** @return {?print_preview.Cdd} CDD of the selected destination. */
get selectedDestinationCapabilities() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES];
+ return this.getStateValue_(
+ AppState.Field.RECENT_DESTINATION_CAPABILITIES);
},
/** @return {?string} Name of the selected destination. */
get selectedDestinationName() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_NAME];
+ return this.getStateValue_(AppState.Field.RECENT_DESTINATION_NAMES);
},
/**
* @return {?string} Extension ID associated with the selected destination.
*/
get selectedDestinationExtensionId() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID];
+ return this.getStateValue_(
+ AppState.Field.RECENT_DESTINATION_EXTENSION_IDS);
},
/**
@@ -114,7 +138,52 @@ cr.define('print_preview', function() {
* destination.
*/
get selectedDestinationExtensionName() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME];
+ return this.getStateValue_(
+ AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES);
+ },
+
+ /** @return {?Array<string>} IDs of the recent destinations. */
+ get recentDestinationIds() {
+ return this.state_[AppState.Field.RECENT_DESTINATION_IDS];
+ },
+
+ /**
+ * @return {?Array<string>} Accounts the recent destinations are registered
+ * for.
+ */
+ get recentDestinationAccounts() {
+ return this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS];
+ },
+
+ /** @return {?Array<string>} Origins of the recent destinations. */
+ get recentDestinationOrigins() {
+ return this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS];
+ },
+
+ /** @return {?Array<print_preview.Cdd>} CDDs of the recent destinations. */
+ get recentDestinationCapabilities() {
+ return this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES];
+ },
+
+ /** @return {?Array<string>} Names of the recent destinations. */
+ get recentDestinationNames() {
+ return this.state_[AppState.Field.RECENT_DESTINATION_NAMES];
+ },
+
+ /**
+ * @return {?Array<string>} Extension IDs associated with the recent
+ * destinations.
+ */
+ get recentDestinationExtensionIds() {
+ return this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS];
+ },
+
+ /**
+ * @return {?Array<string>} Extension names associated with the recent
+ * destinations.
+ */
+ get recentDestinationExtensionNames() {
+ return this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES];
},
/** @return {boolean} Whether the GCP promotion has been dismissed. */
@@ -145,6 +214,25 @@ cr.define('print_preview', function() {
},
/**
+ * Helper function to set up the recent destination fields in case they are
+ * empty or only contain one value.
+ * @param {print_preview.AppState.field} stateFieldName The state field to
dpapad 2016/09/19 22:16:19 !print_preview.AppState.Field
rbpotter 2016/09/20 23:47:37 Done.
+ * be set up.
+ */
+ setUpState_: function(stateFieldName) {
+ if (!this.state_[stateFieldName]) {
dpapad 2016/09/19 22:16:19 Is this check robust? Could this.state_[stateField
rbpotter 2016/09/20 23:47:37 Done - fields other than IDs could be a single ele
+ this.state_[stateFieldName] = [];
+ } else if (!(this.state_[stateFieldName] instanceof Array)) {
+ var tmp = this.state_[stateFieldName];
+ this.state_[stateFieldName][0] = tmp;
dpapad 2016/09/19 22:16:19 This assignment seems a odd but also wrong. Are yo
rbpotter 2016/09/20 23:47:37 Done - yes, I was trying to convert the value to a
+ } else if (this.state_[stateFieldName].length >
+ AppState.NUM_DESTINATIONS_) {
+ this.state_[stateFieldName].splice(AppState.NUM_DESTINATIONS_,
+ this.state_[stateFieldName].length - AppState.NUM_DESTINATIONS_);
+ }
+ },
+
+ /**
* Initializes the app state from a serialized string returned by the native
* layer.
* @param {?string} serializedAppStateStr Serialized string representation
@@ -165,6 +253,15 @@ cr.define('print_preview', function() {
// Set some state defaults.
this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
}
+
+ this.setUpState_(AppState.Field.RECENT_DESTINATION_IDS);
+ this.setUpState_(AppState.Field.RECENT_DESTINATION_ACCOUNTS);
+ this.setUpState_(AppState.Field.RECENT_DESTINATION_ORIGINS);
+ this.setUpState_(AppState.Field.RECENT_DESTINATION_CAPABILITIES);
+ this.setUpState_(AppState.Field.RECENT_DESTINATION_NAMES);
+ this.setUpState_(AppState.Field.RECENT_DESTINATION_EXTENSION_IDS);
+ this.setUpState_(AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES);
+
dpapad 2016/09/19 22:16:19 Nit: Remove blank line.
rbpotter 2016/09/20 23:47:37 Done.
},
/**
@@ -191,22 +288,121 @@ cr.define('print_preview', function() {
},
/**
+ * Shifts the desired recent destination field's values as needed and places
+ * the most recent value, destVal, in position 0 in the array.
+ * @param {number} indexFound the index where the destination already exists
+ * in the array, or -1 if it is not in the array.
+ * @param {!print_preview.AppState.Field} fieldName the field array to
+ * adjust
+ * @param {string or print_preview.Cdd} the value that should be added as
+ * the most recent value in the array.
+ */
+ shiftStateField_: function(indexFound, fieldName, destVal) {
+ if (indexFound == -1 &&
+ this.state_[fieldName].length == AppState.NUM_DESTINATIONS_)
+ indexFound = AppState.NUM_DESTINATIONS_ - 1;
+ if (indexFound != -1)
+ this.state_[fieldName].splice(indexFound, 1);
+ this.state_[fieldName].reverse();
dpapad 2016/09/19 22:16:19 No need to reverse, push, reverse. You can insert
rbpotter 2016/09/20 23:47:37 Done.
+ this.state_[fieldName].push(destVal);
+ this.state_[fieldName].reverse();
+ },
+
+ /**
* Persists the selected destination.
* @param {!print_preview.Destination} dest Destination to persist.
*/
persistSelectedDestination: function(dest) {
rbpotter 2016/09/19 21:16:19 Does this function (with the helper above) look be
- if (!this.isInitialized_)
+ if (!this.isInitialized_ || !dest)
+ return;
+
+ // Determine if this destination is already in the recent destinations,
+ // and where in the array it is located.
+ var idIndexFound = this.state_[
+ AppState.Field.RECENT_DESTINATION_IDS].indexOf(dest.id);
+ var originIndexFound = this.state_[
+ AppState.Field.RECENT_DESTINATION_ORIGINS].indexOf(dest.origin);
+
+ if (idIndexFound != originIndexFound ||
+ idIndexFound >= AppState.NUM_DESTINATIONS_)
+ idIndexFound = -1;
+
+ if (idIndexFound == 0) {
+ this.persist_();
return;
- this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
- this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = dest.account;
- this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
- this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES] =
- dest.capabilities;
- this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName;
- this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID] =
- dest.extensionId;
- this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME] =
- dest.extensionName;
+ }
+
+ // Shift all the destination state fields so that the are always ordered
+ // from most recent (entry 0) to least recent.
+ this.shiftStateField_(idIndexFound,
+ AppState.Field.RECENT_DESTINATION_IDS, dest.id);
+ this.shiftStateField_(idIndexFound,
+ AppState.Field.RECENT_DESTINATION_ACCOUNTS, dest.account || '');
+ this.shiftStateField_(idIndexFound,
+ AppState.Field.RECENT_DESTINATION_ORIGINS, dest.origin);
+ this.shiftStateField_(idIndexFound,
+ AppState.Field.RECENT_DESTINATION_CAPABILITIES,
+ dest.capabilities);
+ this.shiftStateField_(idIndexFound,
+ AppState.Field.RECENT_DESTINATION_NAMES, dest.name || '');
+ this.shiftStateField_(idIndexFound,
+ AppState.Field.RECENT_DESTINATION_EXTENSION_IDS,
+ dest.extension_id || '');
+ this.shiftStateField_(idIndexFound,
+ AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES,
+ dest.extension_name || '');
+
+ /*
+
+ // Shift destinations so that they are always ordered from most recent
+ // (entry 0) to least recent (entry AppState.NUM_DESTINATIONS_ -1).
+ for (var i = AppState.NUM_DESTINATIONS - 1; i > 0; i--) {
+ var shiftDestination = true;
+ // If the selected destination is one of the destinations more recent
+ // than destination i there is no need to move destination i-1 to i, as
+ // only the more recent destinations need to be reordered and the ith
+ // entry remains the ith most recently used.
+ for (var j = i - 1; j >= 0; j--) {
+ if (!this.state_[AppState.Field.RECENT_DESTINATION_IDS] ||
+ dest.id == this.state_[AppState.Field.RECENT_DESTINATION_IDS][j])
+ shiftDestination = false;
+ }
+ // Shift destination i-1 to i.
+ if (shiftDestination) {
+ this.state_[AppState.Field.RECENT_DESTINATION_IDS][i] =
+ this.state_[AppState.Field.RECENT_DESTINATION_IDS][i - 1];
+ this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][i] =
+ this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][i - 1];
+ this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][i] =
+ this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][i - 1];
+ this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][i] =
+ this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][
+ i - 1];
+ this.state_[AppState.Field.RECENT_DESTINATION_NAMES][i] =
+ this.state_[AppState.Field.RECENT_DESTINATION_NAMES][i - 1];
+ this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][i] =
+ this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][
+ i - 1];
+ this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][i] =
+ this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][
+ i - 1];
+ }
+ }
+ // Set the most recent destination (currently selected destination) to
+ // dest.
+ this.state_[AppState.Field.RECENT_DESTINATION_IDS][0] = dest.id;
+ this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][0] =
+ dest.account || '';
+ this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][0] =
+ dest.origin;
+ this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][0] =
+ dest.capabilities || '';
+ this.state_[AppState.Field.RECENT_DESTINATION_NAMES][0] =
+ dest.name || '';
+ this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][0] =
+ dest.extensionId || '';
+ this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][0] =
+ dest.extensionName || '';*/
this.persist_();
},
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/destination_store.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698