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

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: 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
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/data/destination_store.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 get and persist the print preview application state.
10 * @constructor 10 * @constructor
11 */ 11 */
12 function AppState() { 12 function AppState() {
13 /** 13 /**
14 * Internal representation of application state. 14 * Internal representation of application state.
15 * @type {Object} 15 * @type {Object}
16 * @private 16 * @private
17 */ 17 */
18 this.state_ = {}; 18 this.state_ = {};
19 this.state_[AppState.Field.VERSION] = AppState.VERSION_; 19 this.state_[AppState.Field.VERSION] = AppState.VERSION_;
20 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true; 20 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true;
21 21 this.numDestinations_ = 3;
dpapad 2016/09/17 01:53:02 Let's add type annotation to this. @private {numb
rbpotter 2016/09/19 21:16:19 Done.
22 /** 22 /**
23 * Whether the app state has been initialized. The app state will ignore all 23 * Whether the app state has been initialized. The app state will ignore all
24 * writes until it has been initialized. 24 * writes until it has been initialized.
25 * @type {boolean} 25 * @type {boolean}
26 * @private 26 * @private
27 */ 27 */
28 this.isInitialized_ = false; 28 this.isInitialized_ = false;
29 }; 29 };
30 30
31 /** 31 /**
32 * Enumeration of field names for serialized app state. 32 * Enumeration of field names for serialized app state.
33 * @enum {string} 33 * @enum {string}
34 */ 34 */
35 AppState.Field = { 35 AppState.Field = {
36 VERSION: 'version', 36 VERSION: 'version',
37 SELECTED_DESTINATION_ID: 'selectedDestinationId', 37 RECENT_DESTINATION_IDS: 'recentDestinationIds',
38 SELECTED_DESTINATION_ACCOUNT: 'selectedDestinationAccount', 38 RECENT_DESTINATION_ACCOUNTS: 'recentDestinationAccounts',
39 SELECTED_DESTINATION_ORIGIN: 'selectedDestinationOrigin', 39 RECENT_DESTINATION_ORIGINS: 'recentDestinationOrigins',
40 SELECTED_DESTINATION_CAPABILITIES: 'selectedDestinationCapabilities', 40 RECENT_DESTINATION_CAPABILITIES: 'recentDestinationCapabilities',
41 SELECTED_DESTINATION_NAME: 'selectedDestinationName', 41 RECENT_DESTINATION_NAMES: 'recentDestinationNames',
42 SELECTED_DESTINATION_EXTENSION_ID: 'selectedDestinationExtensionId', 42 RECENT_DESTINATION_EXTENSION_IDS: 'recentDestinationExtensionIds',
43 SELECTED_DESTINATION_EXTENSION_NAME: 'selectedDestinationExtensionName', 43 RECENT_DESTINATION_EXTENSION_NAMES: 'recentDestinationExtensionNames',
44 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed', 44 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
45 DPI: 'dpi', 45 DPI: 'dpi',
46 MEDIA_SIZE: 'mediaSize', 46 MEDIA_SIZE: 'mediaSize',
47 MARGINS_TYPE: 'marginsType', 47 MARGINS_TYPE: 'marginsType',
48 CUSTOM_MARGINS: 'customMargins', 48 CUSTOM_MARGINS: 'customMargins',
49 IS_COLOR_ENABLED: 'isColorEnabled', 49 IS_COLOR_ENABLED: 'isColorEnabled',
50 IS_DUPLEX_ENABLED: 'isDuplexEnabled', 50 IS_DUPLEX_ENABLED: 'isDuplexEnabled',
51 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled', 51 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
52 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled', 52 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
53 IS_COLLATE_ENABLED: 'isCollateEnabled', 53 IS_COLLATE_ENABLED: 'isCollateEnabled',
(...skipping 13 matching lines...) Expand all
67 67
68 /** 68 /**
69 * Name of C++ layer function to persist app state. 69 * Name of C++ layer function to persist app state.
70 * @type {string} 70 * @type {string}
71 * @const 71 * @const
72 * @private 72 * @private
73 */ 73 */
74 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; 74 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
75 75
76 AppState.prototype = { 76 AppState.prototype = {
77 /** @return {?string} ID of the selected destination. */ 77 /** @return {?string} ID of the selected destination. */
dpapad 2016/09/17 01:53:02 The type annotation here seems no longer accurate.
rbpotter 2016/09/19 21:16:19 Done.
78 get selectedDestinationId() { 78 get selectedDestinationId() {
79 return this.state_[AppState.Field.SELECTED_DESTINATION_ID]; 79 if (this.state_[AppState.Field.RECENT_DESTINATION_IDS] &&
80 this.state_[AppState.Field.RECENT_DESTINATION_IDS].length > 0)
81 return this.state_[AppState.Field.RECENT_DESTINATION_IDS][0];
82 else
83 return '';
80 }, 84 },
81 85
82 /** @return {?string} Account the selected destination is registered for. */ 86 /** @return {?string} Account the selected destination is registered for. */
83 get selectedDestinationAccount() { 87 get selectedDestinationAccount() {
84 return this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT]; 88 if (this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS] &&
dpapad 2016/09/17 01:53:02 This logic is repeated 7 times. Can you package it
rbpotter 2016/09/19 21:16:18 Done.
89 this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS].length > 0)
90 return this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][0];
91 else
92 return '';
85 }, 93 },
86 94
87 /** 95 /**
88 * @return {?print_preview.Destination.Origin<string>} Origin of the 96 * @return {?print_preview.Destination.Origin<string>} Origin of the
89 * selected destination. 97 * selected destination.
90 */ 98 */
91 get selectedDestinationOrigin() { 99 get selectedDestinationOrigin() {
92 return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]; 100 if (this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS] &&
101 this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS].length > 0)
102 return this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][0];
103 else
104 return '';
93 }, 105 },
94 106
95 /** @return {?print_preview.Cdd} CDD of the selected destination. */ 107 /** @return {?print_preview.Cdd} CDD of the selected destination. */
96 get selectedDestinationCapabilities() { 108 get selectedDestinationCapabilities() {
97 return this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES]; 109 if (this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES] &&
110 this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES].length >
111 0)
112 return this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][0];
113 else
114 return null;
98 }, 115 },
99 116
100 /** @return {?string} Name of the selected destination. */ 117 /** @return {?string} Name of the selected destination. */
101 get selectedDestinationName() { 118 get selectedDestinationName() {
102 return this.state_[AppState.Field.SELECTED_DESTINATION_NAME]; 119 if (this.state_[AppState.Field.RECENT_DESTINATION_NAMES] &&
120 this.state_[AppState.Field.RECENT_DESTINATION_NAMES].length > 0)
121 return this.state_[AppState.Field.RECENT_DESTINATION_NAMES][0];
122 else
123 return '';
103 }, 124 },
104 125
105 /** 126 /**
106 * @return {?string} Extension ID associated with the selected destination. 127 * @return {?string} Extension ID associated with the selected destination.
107 */ 128 */
108 get selectedDestinationExtensionId() { 129 get selectedDestinationExtensionId() {
109 return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID]; 130 if (this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS] &&
131 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS].length >
132 0)
133 return this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][0];
134 else
135 return '';
110 }, 136 },
111 137
112 /** 138 /**
113 * @return {?string} Extension name associated with the selected 139 * @return {?string} Extension name associated with the selected
114 * destination. 140 * destination.
115 */ 141 */
116 get selectedDestinationExtensionName() { 142 get selectedDestinationExtensionName() {
117 return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME]; 143 if (this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES] &&
144 this.state_[
145 AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES].length > 0)
146 return this.state_[
147 AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][0];
148 else
149 return '';
150 },
151
152 /** @return {?array of strings} IDs of the recent destinations. */
dpapad 2016/09/17 01:53:02 @return {?Array<string>} IDs of the recent destina
rbpotter 2016/09/19 21:16:19 Done.
153 get recentDestinationIds() {
154 return this.state_[AppState.Field.RECENT_DESTINATION_IDS];
155 },
156
157 /**
158 * @return {?array of strings} Accounts the recent destinations are
159 * registered for.
160 */
161 get recentDestinationAccounts() {
162 return this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS];
163 },
164
165 /**
166 * @return {?array of print_preview.Destination.Origin<string>} Origins of
167 * the recent destinations.
168 */
169 get recentDestinationOrigins() {
170 return this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS];
171 },
172
173 /**
174 * @return {?array of print_preview.Cdds} CDDs of the recent
175 * destinations.
176 */
177 get recentDestinationCapabilities() {
178 return this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES];
179 },
180
181 /** @return {?array of strings} Names of the recent destinations. */
182 get recentDestinationNames() {
183 return this.state_[AppState.Field.RECENT_DESTINATION_NAMES];
184 },
185
186 /**
187 * @return {?array of strings} Extension IDs associated with the recent
188 * destinations.
189 */
190 get recentDestinationExtensionIds() {
191 return this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS];
192 },
193
194 /**
195 * @return {?array of strings} Extension names associated with the recent
196 * destinations.
197 */
198 get recentDestinationExtensionNames() {
199 return this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES];
118 }, 200 },
119 201
120 /** @return {boolean} Whether the GCP promotion has been dismissed. */ 202 /** @return {boolean} Whether the GCP promotion has been dismissed. */
121 get isGcpPromoDismissed() { 203 get isGcpPromoDismissed() {
122 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED]; 204 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
123 }, 205 },
124 206
125 /** 207 /**
126 * @param {!print_preview.AppState.Field} field App state field to check if 208 * @param {!print_preview.AppState.Field} field App state field to check if
127 * set. 209 * set.
(...skipping 30 matching lines...) Expand all
158 this.state_ = state; 240 this.state_ = state;
159 } 241 }
160 } catch(e) { 242 } catch(e) {
161 console.error('Unable to parse state: ' + e); 243 console.error('Unable to parse state: ' + e);
162 // Proceed with default state. 244 // Proceed with default state.
163 } 245 }
164 } else { 246 } else {
165 // Set some state defaults. 247 // Set some state defaults.
166 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; 248 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
167 } 249 }
250
251 // Deal with empty or single element cases.
252 if (!this.state_[AppState.Field.RECENT_DESTINATION_IDS])
dpapad 2016/09/17 01:53:02 Is there a way to package redundant logic into a h
rbpotter 2016/09/19 21:16:18 Done.
253 this.state_[AppState.Field.RECENT_DESTINATION_IDS] = [];
254 else if (this.state_[
255 AppState.Field.RECENT_DESTINATION_IDS].constructor != Array) {
dpapad 2016/09/17 01:53:02 instanceof is more suited for this type of checks.
rbpotter 2016/09/19 21:16:19 Done.
256 var tmp = this.state_[AppState.Field.RECENT_DESTINATION_IDS];
257 this.state_[AppState.Field.RECENT_DESTINATION_IDS][0] = tmp;
258 }
259
260 if (!this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS])
261 this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS] = [];
262 else if (this.state_[
263 AppState.Field.RECENT_DESTINATION_ACCOUNTS].constructor != Array) {
264 var tmp = this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS];
265 this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][0] = tmp;
266 }
267
268 if (!this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS])
269 this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS] = [];
270 else if (this.state_[
271 AppState.Field.RECENT_DESTINATION_ORIGINS].constructor != Array) {
272 var tmp = this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS];
273 this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][0] = tmp;
274 }
275
276 if (!this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES])
277 this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES] = [];
278 else if (this.state_[
279 AppState.Field.RECENT_DESTINATION_CAPABILITIES].constructor !=
280 Array) {
281 var tmp = this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES];
282 this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][0] = tmp;
283 }
284
285 if (!this.state_[AppState.Field.RECENT_DESTINATION_NAMES])
286 this.state_[AppState.Field.RECENT_DESTINATION_NAMES] = [];
287 else if (this.state_[
288 AppState.Field.RECENT_DESTINATION_NAMES].constructor != Array) {
289 var tmp = this.state_[AppState.Field.RECENT_DESTINATION_NAMES];
290 this.state_[AppState.Field.RECENT_DESTINATION_NAMES][0] = tmp;
291 }
292
293 if (!this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS])
294 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS] = [];
295 else if (this.state_[
296 AppState.Field.RECENT_DESTINATION_EXTENSION_IDS].constructor !=
297 Array) {
298 var tmp = this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS];
299 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][0] = tmp;
300 }
301
302 if (!this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES])
303 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES] = [];
304 else if (this.state_[
305 AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES].constructor !=
306 Array) {
307 var tmp = this.state_[
308 AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES];
309 this.state_[
310 AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][0] = tmp;
311 }
312
313
168 }, 314 },
169 315
170 /** 316 /**
171 * Sets to initialized state. Now object will accept persist requests. 317 * Sets to initialized state. Now object will accept persist requests.
172 */ 318 */
173 setInitialized: function() { 319 setInitialized: function() {
174 this.isInitialized_ = true; 320 this.isInitialized_ = true;
175 }, 321 },
176 322
177 /** 323 /**
(...skipping 12 matching lines...) Expand all
190 this.persist_(); 336 this.persist_();
191 }, 337 },
192 338
193 /** 339 /**
194 * Persists the selected destination. 340 * Persists the selected destination.
195 * @param {!print_preview.Destination} dest Destination to persist. 341 * @param {!print_preview.Destination} dest Destination to persist.
196 */ 342 */
197 persistSelectedDestination: function(dest) { 343 persistSelectedDestination: function(dest) {
198 if (!this.isInitialized_) 344 if (!this.isInitialized_)
199 return; 345 return;
200 this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id; 346
201 this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = dest.account; 347 for (var i = this.numDestinations_ - 1; i > 0; i--) {
dpapad 2016/09/17 01:53:02 Can you explain a bit what this logic does? Perhap
rbpotter 2016/09/19 21:16:19 Acknowledged - see new code in the next patchset.
202 this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin; 348 var shiftDestination = true;
203 this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES] = 349 for (var j = i - 1; j >= 0; j--) {
204 dest.capabilities; 350 if (!this.state_[AppState.Field.RECENT_DESTINATION_IDS] ||
205 this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName; 351 dest.id == this.state_[AppState.Field.RECENT_DESTINATION_IDS][j])
206 this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID] = 352 shiftDestination = false;
207 dest.extensionId; 353 }
208 this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME] = 354 if (shiftDestination) {
209 dest.extensionName; 355 this.state_[AppState.Field.RECENT_DESTINATION_IDS][i] =
356 this.state_[AppState.Field.RECENT_DESTINATION_IDS][i - 1];
357 this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][i] =
358 this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][i - 1];
359 this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][i] =
360 this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][i - 1];
361 this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][i] =
362 this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][
363 i - 1];
364 this.state_[AppState.Field.RECENT_DESTINATION_NAMES][i] =
365 this.state_[AppState.Field.RECENT_DESTINATION_NAMES][i - 1];
366 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][i] =
367 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][
368 i - 1];
369 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][i] =
370 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][
371 i - 1];
372 }
373 }
374 this.state_[AppState.Field.RECENT_DESTINATION_IDS][0] = dest.id;
375 this.state_[AppState.Field.RECENT_DESTINATION_ACCOUNTS][0] =
376 dest.account || '';
377 this.state_[AppState.Field.RECENT_DESTINATION_ORIGINS][0] =
378 dest.origin;
379 this.state_[AppState.Field.RECENT_DESTINATION_CAPABILITIES][0] =
380 dest.capabilities || '';
381 this.state_[AppState.Field.RECENT_DESTINATION_NAMES][0] =
382 dest.name || '';
383 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_IDS][0] =
384 dest.extensionId || '';
385 this.state_[AppState.Field.RECENT_DESTINATION_EXTENSION_NAMES][0] =
386 dest.extensionName || '';
210 this.persist_(); 387 this.persist_();
211 }, 388 },
212 389
213 /** 390 /**
214 * Persists whether the GCP promotion has been dismissed. 391 * Persists whether the GCP promotion has been dismissed.
215 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been 392 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
216 * dismissed. 393 * dismissed.
217 */ 394 */
218 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) { 395 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
219 if (!this.isInitialized_) 396 if (!this.isInitialized_)
220 return; 397 return;
221 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed; 398 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
222 this.persist_(); 399 this.persist_();
223 }, 400 },
224 401
225 /** 402 /**
226 * Calls into the native layer to persist the application state. 403 * Calls into the native layer to persist the application state.
227 * @private 404 * @private
228 */ 405 */
229 persist_: function() { 406 persist_: function() {
230 chrome.send(AppState.NATIVE_FUNCTION_NAME_, 407 chrome.send(AppState.NATIVE_FUNCTION_NAME_,
231 [JSON.stringify(this.state_)]); 408 [JSON.stringify(this.state_)]);
232 } 409 }
233 }; 410 };
234 411
235 return { 412 return {
236 AppState: AppState 413 AppState: AppState
237 }; 414 };
238 }); 415 });
OLDNEW
« 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