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

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

Issue 2346153002: Save most recent 3 destinations across multiple sessions (Closed)
Patch Set: Change to array of objects 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
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..0a53f70a7b5f7796cf52daf9dfe0afb31620e477 100644
--- a/chrome/browser/resources/print_preview/data/app_state.js
+++ b/chrome/browser/resources/print_preview/data/app_state.js
@@ -6,9 +6,23 @@ cr.define('print_preview', function() {
'use strict';
/**
- * Object used to get and persist the print preview application state.
+ * Object used to represent a recent destination in the app state.
* @constructor
*/
+ function RecentDestination(destination) {
+ this.id_ = destination.id;
+ this.origin_ = destination.origin;
+ this.account_ = destination.account || '';
+ this.capabilities_ = destination.capabilities;
+ this.name_ = destination.name || '';
+ this.extension_id_ = destination.extension_id || '';
dpapad 2016/09/21 20:44:18 Nit: Per JS styleguide naming, this should be this
rbpotter 2016/09/21 23:19:14 Done.
+ this.extension_name_ = destination.extension_name || '';
+ };
+
+ /**
+ * Object used to get and persist the print preview application state.
+ * @constructor
+ */
function AppState() {
/**
* Internal representation of application state.
@@ -18,6 +32,7 @@ cr.define('print_preview', function() {
this.state_ = {};
this.state_[AppState.Field.VERSION] = AppState.VERSION_;
this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true;
+ this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
/**
* Whether the app state has been initialized. The app state will ignore all
@@ -29,18 +44,19 @@ 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_DESTINATIONS: 'recentDestinations',
IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
DPI: 'dpi',
MEDIA_SIZE: 'mediaSize',
@@ -74,14 +90,27 @@ cr.define('print_preview', function() {
AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
AppState.prototype = {
+
+ /**
+ * Helper function to get the most recent destination.
+ * @return {?RecentDestination} The most recent value of the
+ * destination.
+ */
+ getSelectedDestination_: function() {
+ return (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 0) ?
+ this.state_[AppState.Field.RECENT_DESTINATIONS][0] : null;
+ },
+
/** @return {?string} ID of the selected destination. */
get selectedDestinationId() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_ID];
+ return this.getSelectedDestination_ ?
+ this.getSelectedDestination_.id : null;
},
/** @return {?string} Account the selected destination is registered for. */
get selectedDestinationAccount() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT];
+ return this.getSelectedDestination_ ?
+ this.getSelectedDestination_.account : null;
},
/**
@@ -89,24 +118,28 @@ cr.define('print_preview', function() {
* selected destination.
*/
get selectedDestinationOrigin() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
+ return this.getSelectedDestination_ ?
+ this.getSelectedDestination_.origin : null;
},
/** @return {?print_preview.Cdd} CDD of the selected destination. */
get selectedDestinationCapabilities() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES];
+ return this.getSelectedDestination_ ?
+ this.getSelectedDestination_.capabilities : null;
},
/** @return {?string} Name of the selected destination. */
get selectedDestinationName() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_NAME];
+ return this.getSelectedDestination_ ?
+ this.getSelectedDestination_.name : null;
},
/**
* @return {?string} Extension ID associated with the selected destination.
*/
get selectedDestinationExtensionId() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID];
+ return this.getSelectedDestination_ ?
+ this.getSelectedDestination_.extension_id : null;
},
/**
@@ -114,7 +147,24 @@ cr.define('print_preview', function() {
* destination.
*/
get selectedDestinationExtensionName() {
- return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME];
+ return this.getSelectedDestination_ ?
+ this.getSelectedDestination_.extension_name : null;
+ },
+
+ /**
+ * @return {?RecentDestination} The most recent destination, which is
+ * currently the selected destination.
+ */
+ get selectedDestination() {
+ return this.getSelectedDestination_;
+ },
+
+ /**
+ * @return {?Array<RecentDestination>} The AppState.NUM_DESTINATIONS_ most
dpapad 2016/09/21 20:44:18 Nit: ?Array<!RecentDestination>
rbpotter 2016/09/21 23:19:15 Done.
+ * recent destinations.
+ */
+ get recentDestinations() {
+ return this.state_[AppState.Field.RECENT_DESTINATIONS];
},
/** @return {boolean} Whether the GCP promotion has been dismissed. */
@@ -164,6 +214,18 @@ cr.define('print_preview', function() {
} else {
// Set some state defaults.
this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
+ this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
+ }
+ if (!this.state_[AppState.Field.RECENT_DESTINATIONS])
+ this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
+ else if (!(this.state_[AppState.Field.RECENT_DESTINATIONS] instanceof
+ Array)) {
+ var tmp = this.state_[AppState.Field.RECENT_DESTINATIONS];
+ this.state_[AppState.Field.RECENT_DESTINATIONS] = [tmp];
+ } else if (this.state_[AppState.Field.RECENT_DESTINATIONS].length >
+ AppState.NUM_DESTINATIONS_) {
+ this.state_[AppState.Field.RECENT_DESTINATIONS].length =
+ AppState.NUM_DESTINATIONS_;
}
},
@@ -197,16 +259,35 @@ cr.define('print_preview', function() {
persistSelectedDestination: function(dest) {
if (!this.isInitialized_)
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;
+
+ // Determine if this destination is already in the recent destinations,
+ // and where in the array it is located.
+ var newDestination = new RecentDestination(dest);
+ var indexFound = this.state_[
+ AppState.Field.RECENT_DESTINATIONS].findIndex(function(recent) {
+ return (this.id_ == recent.id_ && this.origin_ == recent.origin_);
+ }, newDestination);
dpapad 2016/09/21 20:44:18 No need to use newDestination as "this" here. Just
rbpotter 2016/09/21 23:19:15 Done.
+
+ // No change
+ if (indexFound == 0) {
+ this.persist_();
+ return;
+ }
+
+ // Shift the array so that the nth most recent destination is located at
+ // index n.
+ if (indexFound == -1 &&
+ this.state_[AppState.Field.RECENT_DESTINATIONS].length ==
+ AppState.NUM_DESTINATIONS_) {
+ indexFound = AppState.NUM_DESTINATIONS_ - 1;
+ }
+ if (indexFound != -1)
+ this.state_[AppState.Field.RECENT_DESTINATIONS].splice(indexFound, 1);
+
+ // Add the most recent destination
+ this.state_[AppState.Field.RECENT_DESTINATIONS].splice(0, 0,
+ newDestination);
dpapad 2016/09/21 20:44:18 Nit: Probably more readable to break line right af
rbpotter 2016/09/21 23:19:15 Done.
+
this.persist_();
},

Powered by Google App Engine
This is Rietveld 408576698