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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 cr.define('print_preview', function() { 5 cr.define('print_preview', function() {
6 'use strict'; 6 'use strict';
7 7
8 /** 8 /**
9 * Object used to get and persist the print preview application state. 9 * Object used to represent a recent destination in the app state.
10 * @constructor 10 * @constructor
11 */ 11 */
12 function RecentDestination(destination) {
13 this.id_ = destination.id;
14 this.origin_ = destination.origin;
15 this.account_ = destination.account || '';
16 this.capabilities_ = destination.capabilities;
17 this.name_ = destination.name || '';
18 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.
19 this.extension_name_ = destination.extension_name || '';
20 };
21
22 /**
23 * Object used to get and persist the print preview application state.
24 * @constructor
25 */
12 function AppState() { 26 function AppState() {
13 /** 27 /**
14 * Internal representation of application state. 28 * Internal representation of application state.
15 * @type {Object} 29 * @type {Object}
16 * @private 30 * @private
17 */ 31 */
18 this.state_ = {}; 32 this.state_ = {};
19 this.state_[AppState.Field.VERSION] = AppState.VERSION_; 33 this.state_[AppState.Field.VERSION] = AppState.VERSION_;
20 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true; 34 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true;
35 this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
21 36
22 /** 37 /**
23 * Whether the app state has been initialized. The app state will ignore all 38 * Whether the app state has been initialized. The app state will ignore all
24 * writes until it has been initialized. 39 * writes until it has been initialized.
25 * @type {boolean} 40 * @type {boolean}
26 * @private 41 * @private
27 */ 42 */
28 this.isInitialized_ = false; 43 this.isInitialized_ = false;
29 }; 44 };
30 45
31 /** 46 /**
47 * Number of recent print destinations to store across browser sessions.
48 * @const {number}
49 */
50 AppState.NUM_DESTINATIONS_ = 3;
51
52
53 /**
32 * Enumeration of field names for serialized app state. 54 * Enumeration of field names for serialized app state.
33 * @enum {string} 55 * @enum {string}
34 */ 56 */
35 AppState.Field = { 57 AppState.Field = {
36 VERSION: 'version', 58 VERSION: 'version',
37 SELECTED_DESTINATION_ID: 'selectedDestinationId', 59 RECENT_DESTINATIONS: 'recentDestinations',
38 SELECTED_DESTINATION_ACCOUNT: 'selectedDestinationAccount',
39 SELECTED_DESTINATION_ORIGIN: 'selectedDestinationOrigin',
40 SELECTED_DESTINATION_CAPABILITIES: 'selectedDestinationCapabilities',
41 SELECTED_DESTINATION_NAME: 'selectedDestinationName',
42 SELECTED_DESTINATION_EXTENSION_ID: 'selectedDestinationExtensionId',
43 SELECTED_DESTINATION_EXTENSION_NAME: 'selectedDestinationExtensionName',
44 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed', 60 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
45 DPI: 'dpi', 61 DPI: 'dpi',
46 MEDIA_SIZE: 'mediaSize', 62 MEDIA_SIZE: 'mediaSize',
47 MARGINS_TYPE: 'marginsType', 63 MARGINS_TYPE: 'marginsType',
48 CUSTOM_MARGINS: 'customMargins', 64 CUSTOM_MARGINS: 'customMargins',
49 IS_COLOR_ENABLED: 'isColorEnabled', 65 IS_COLOR_ENABLED: 'isColorEnabled',
50 IS_DUPLEX_ENABLED: 'isDuplexEnabled', 66 IS_DUPLEX_ENABLED: 'isDuplexEnabled',
51 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled', 67 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
52 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled', 68 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
53 IS_COLLATE_ENABLED: 'isCollateEnabled', 69 IS_COLLATE_ENABLED: 'isCollateEnabled',
(...skipping 13 matching lines...) Expand all
67 83
68 /** 84 /**
69 * Name of C++ layer function to persist app state. 85 * Name of C++ layer function to persist app state.
70 * @type {string} 86 * @type {string}
71 * @const 87 * @const
72 * @private 88 * @private
73 */ 89 */
74 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; 90 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
75 91
76 AppState.prototype = { 92 AppState.prototype = {
93
94 /**
95 * Helper function to get the most recent destination.
96 * @return {?RecentDestination} The most recent value of the
97 * destination.
98 */
99 getSelectedDestination_: function() {
100 return (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 0) ?
101 this.state_[AppState.Field.RECENT_DESTINATIONS][0] : null;
102 },
103
77 /** @return {?string} ID of the selected destination. */ 104 /** @return {?string} ID of the selected destination. */
78 get selectedDestinationId() { 105 get selectedDestinationId() {
79 return this.state_[AppState.Field.SELECTED_DESTINATION_ID]; 106 return this.getSelectedDestination_ ?
107 this.getSelectedDestination_.id : null;
80 }, 108 },
81 109
82 /** @return {?string} Account the selected destination is registered for. */ 110 /** @return {?string} Account the selected destination is registered for. */
83 get selectedDestinationAccount() { 111 get selectedDestinationAccount() {
84 return this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT]; 112 return this.getSelectedDestination_ ?
113 this.getSelectedDestination_.account : null;
85 }, 114 },
86 115
87 /** 116 /**
88 * @return {?print_preview.Destination.Origin<string>} Origin of the 117 * @return {?print_preview.Destination.Origin<string>} Origin of the
89 * selected destination. 118 * selected destination.
90 */ 119 */
91 get selectedDestinationOrigin() { 120 get selectedDestinationOrigin() {
92 return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]; 121 return this.getSelectedDestination_ ?
122 this.getSelectedDestination_.origin : null;
93 }, 123 },
94 124
95 /** @return {?print_preview.Cdd} CDD of the selected destination. */ 125 /** @return {?print_preview.Cdd} CDD of the selected destination. */
96 get selectedDestinationCapabilities() { 126 get selectedDestinationCapabilities() {
97 return this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES]; 127 return this.getSelectedDestination_ ?
128 this.getSelectedDestination_.capabilities : null;
98 }, 129 },
99 130
100 /** @return {?string} Name of the selected destination. */ 131 /** @return {?string} Name of the selected destination. */
101 get selectedDestinationName() { 132 get selectedDestinationName() {
102 return this.state_[AppState.Field.SELECTED_DESTINATION_NAME]; 133 return this.getSelectedDestination_ ?
134 this.getSelectedDestination_.name : null;
103 }, 135 },
104 136
105 /** 137 /**
106 * @return {?string} Extension ID associated with the selected destination. 138 * @return {?string} Extension ID associated with the selected destination.
107 */ 139 */
108 get selectedDestinationExtensionId() { 140 get selectedDestinationExtensionId() {
109 return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID]; 141 return this.getSelectedDestination_ ?
142 this.getSelectedDestination_.extension_id : null;
110 }, 143 },
111 144
112 /** 145 /**
113 * @return {?string} Extension name associated with the selected 146 * @return {?string} Extension name associated with the selected
114 * destination. 147 * destination.
115 */ 148 */
116 get selectedDestinationExtensionName() { 149 get selectedDestinationExtensionName() {
117 return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME]; 150 return this.getSelectedDestination_ ?
151 this.getSelectedDestination_.extension_name : null;
152 },
153
154 /**
155 * @return {?RecentDestination} The most recent destination, which is
156 * currently the selected destination.
157 */
158 get selectedDestination() {
159 return this.getSelectedDestination_;
160 },
161
162 /**
163 * @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.
164 * recent destinations.
165 */
166 get recentDestinations() {
167 return this.state_[AppState.Field.RECENT_DESTINATIONS];
118 }, 168 },
119 169
120 /** @return {boolean} Whether the GCP promotion has been dismissed. */ 170 /** @return {boolean} Whether the GCP promotion has been dismissed. */
121 get isGcpPromoDismissed() { 171 get isGcpPromoDismissed() {
122 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED]; 172 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
123 }, 173 },
124 174
125 /** 175 /**
126 * @param {!print_preview.AppState.Field} field App state field to check if 176 * @param {!print_preview.AppState.Field} field App state field to check if
127 * set. 177 * set.
(...skipping 29 matching lines...) Expand all
157 if (state[AppState.Field.VERSION] == AppState.VERSION_) { 207 if (state[AppState.Field.VERSION] == AppState.VERSION_) {
158 this.state_ = state; 208 this.state_ = state;
159 } 209 }
160 } catch(e) { 210 } catch(e) {
161 console.error('Unable to parse state: ' + e); 211 console.error('Unable to parse state: ' + e);
162 // Proceed with default state. 212 // Proceed with default state.
163 } 213 }
164 } else { 214 } else {
165 // Set some state defaults. 215 // Set some state defaults.
166 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; 216 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
217 this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
218 }
219 if (!this.state_[AppState.Field.RECENT_DESTINATIONS])
220 this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
221 else if (!(this.state_[AppState.Field.RECENT_DESTINATIONS] instanceof
222 Array)) {
223 var tmp = this.state_[AppState.Field.RECENT_DESTINATIONS];
224 this.state_[AppState.Field.RECENT_DESTINATIONS] = [tmp];
225 } else if (this.state_[AppState.Field.RECENT_DESTINATIONS].length >
226 AppState.NUM_DESTINATIONS_) {
227 this.state_[AppState.Field.RECENT_DESTINATIONS].length =
228 AppState.NUM_DESTINATIONS_;
167 } 229 }
168 }, 230 },
169 231
170 /** 232 /**
171 * Sets to initialized state. Now object will accept persist requests. 233 * Sets to initialized state. Now object will accept persist requests.
172 */ 234 */
173 setInitialized: function() { 235 setInitialized: function() {
174 this.isInitialized_ = true; 236 this.isInitialized_ = true;
175 }, 237 },
176 238
(...skipping 13 matching lines...) Expand all
190 this.persist_(); 252 this.persist_();
191 }, 253 },
192 254
193 /** 255 /**
194 * Persists the selected destination. 256 * Persists the selected destination.
195 * @param {!print_preview.Destination} dest Destination to persist. 257 * @param {!print_preview.Destination} dest Destination to persist.
196 */ 258 */
197 persistSelectedDestination: function(dest) { 259 persistSelectedDestination: function(dest) {
198 if (!this.isInitialized_) 260 if (!this.isInitialized_)
199 return; 261 return;
200 this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id; 262
201 this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = dest.account; 263 // Determine if this destination is already in the recent destinations,
202 this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin; 264 // and where in the array it is located.
203 this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES] = 265 var newDestination = new RecentDestination(dest);
204 dest.capabilities; 266 var indexFound = this.state_[
205 this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName; 267 AppState.Field.RECENT_DESTINATIONS].findIndex(function(recent) {
206 this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID] = 268 return (this.id_ == recent.id_ && this.origin_ == recent.origin_);
207 dest.extensionId; 269 }, 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.
208 this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME] = 270
209 dest.extensionName; 271 // No change
272 if (indexFound == 0) {
273 this.persist_();
274 return;
275 }
276
277 // Shift the array so that the nth most recent destination is located at
278 // index n.
279 if (indexFound == -1 &&
280 this.state_[AppState.Field.RECENT_DESTINATIONS].length ==
281 AppState.NUM_DESTINATIONS_) {
282 indexFound = AppState.NUM_DESTINATIONS_ - 1;
283 }
284 if (indexFound != -1)
285 this.state_[AppState.Field.RECENT_DESTINATIONS].splice(indexFound, 1);
286
287 // Add the most recent destination
288 this.state_[AppState.Field.RECENT_DESTINATIONS].splice(0, 0,
289 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.
290
210 this.persist_(); 291 this.persist_();
211 }, 292 },
212 293
213 /** 294 /**
214 * Persists whether the GCP promotion has been dismissed. 295 * Persists whether the GCP promotion has been dismissed.
215 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been 296 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
216 * dismissed. 297 * dismissed.
217 */ 298 */
218 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) { 299 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
219 if (!this.isInitialized_) 300 if (!this.isInitialized_)
220 return; 301 return;
221 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed; 302 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
222 this.persist_(); 303 this.persist_();
223 }, 304 },
224 305
225 /** 306 /**
226 * Calls into the native layer to persist the application state. 307 * Calls into the native layer to persist the application state.
227 * @private 308 * @private
228 */ 309 */
229 persist_: function() { 310 persist_: function() {
230 chrome.send(AppState.NATIVE_FUNCTION_NAME_, 311 chrome.send(AppState.NATIVE_FUNCTION_NAME_,
231 [JSON.stringify(this.state_)]); 312 [JSON.stringify(this.state_)]);
232 } 313 }
233 }; 314 };
234 315
235 return { 316 return {
236 AppState: AppState 317 AppState: AppState
237 }; 318 };
238 }); 319 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698