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

Side by Side Diff: chrome/browser/resources/print_preview/data/app_state.js

Issue 2862203002: Print Preview: Fix data/ errors (Closed)
Patch Set: Fix destination resolver Created 3 years, 7 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.exportPath('print_preview');
6
7 /**
8 * Enumeration of field names for serialized app state.
9 * @enum {string}
10 */
11 print_preview.AppStateField = {
12 VERSION: 'version',
13 RECENT_DESTINATIONS: 'recentDestinations',
14 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
15 DPI: 'dpi',
16 MEDIA_SIZE: 'mediaSize',
17 MARGINS_TYPE: 'marginsType',
18 CUSTOM_MARGINS: 'customMargins',
19 IS_COLOR_ENABLED: 'isColorEnabled',
20 IS_DUPLEX_ENABLED: 'isDuplexEnabled',
21 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
22 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
23 IS_COLLATE_ENABLED: 'isCollateEnabled',
24 IS_FIT_TO_PAGE_ENABLED: 'isFitToPageEnabled',
25 IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled',
26 SCALING: 'scaling',
27 VENDOR_OPTIONS: 'vendorOptions'
28 };
29
30 /**
31 * Object used to represent a recent destination in the app state.
32 * @constructor
33 * @struct
34 */
35 function RecentDestination(destination) {
36 /**
37 * ID of the RecentDestination.
38 * @type {string}
39 */
40 this.id = destination.id;
41
42 /**
43 * Origin of the RecentDestination.
44 * @type {string}
45 */
46 this.origin = destination.origin;
47
48 /**
49 * Account the RecentDestination is registered for.
50 * @type {string}
51 */
52 this.account = destination.account || '';
53
54 /**
55 * CDD of the RecentDestination.
56 * @type {print_preview.Cdd}
57 */
58 this.capabilities = destination.capabilities;
59
60 /**
61 * Name of the RecentDestination.
62 * @type {string}
63 */
64 this.name = destination.name || '';
65
66 /**
67 * Extension ID associated with the RecentDestination.
68 * @type {string}
69 */
70 this.extensionId = destination.extension_id || '';
71
72 /**
73 * Extension name associated with the RecentDestination.
74 * @type {string}
75 */
76 this.extensionName = destination.extension_name || '';
77 }
78
5 cr.define('print_preview', function() { 79 cr.define('print_preview', function() {
6 'use strict'; 80 'use strict';
7 81
8 /** 82 /**
9 * Object used to represent a recent destination in the app state.
10 * @constructor
11 */
12 function RecentDestination(destination) {
13 /**
14 * ID of the RecentDestination.
15 * @type {string}
16 */
17 this.id = destination.id;
18
19 /**
20 * Origin of the RecentDestination.
21 * @type {string}
22 */
23 this.origin = destination.origin;
24
25 /**
26 * Account the RecentDestination is registered for.
27 * @type {string}
28 */
29 this.account = destination.account || '';
30
31 /**
32 * CDD of the RecentDestination.
33 * @type {print_preview.Cdd}
34 */
35 this.capabilities = destination.capabilities;
36
37 /**
38 * Name of the RecentDestination.
39 * @type {string}
40 */
41 this.name = destination.name || '';
42
43 /**
44 * Extension ID associated with the RecentDestination.
45 * @type {string}
46 */
47 this.extensionId = destination.extension_id || '';
48
49 /**
50 * Extension name associated with the RecentDestination.
51 * @type {string}
52 */
53 this.extensionName = destination.extension_name || '';
54 };
55
56 /**
57 * Object used to get and persist the print preview application state. 83 * Object used to get and persist the print preview application state.
58 * @constructor 84 * @constructor
59 */ 85 */
60 function AppState() { 86 function AppState() {
61 /** 87 /**
62 * Internal representation of application state. 88 * Internal representation of application state.
63 * @type {Object} 89 * @type {Object}
64 * @private 90 * @private
65 */ 91 */
66 this.state_ = {}; 92 this.state_ = {};
67 this.state_[AppState.Field.VERSION] = AppState.VERSION_; 93 this.state_[print_preview.AppStateField.VERSION] = AppState.VERSION_;
68 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true; 94 this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = true;
69 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; 95 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [];
70 96
71 /** 97 /**
72 * Whether the app state has been initialized. The app state will ignore all 98 * Whether the app state has been initialized. The app state will ignore all
73 * writes until it has been initialized. 99 * writes until it has been initialized.
74 * @type {boolean} 100 * @type {boolean}
75 * @private 101 * @private
76 */ 102 */
77 this.isInitialized_ = false; 103 this.isInitialized_ = false;
78 }; 104 }
79 105
80 /** 106 /**
81 * Number of recent print destinations to store across browser sessions. 107 * Number of recent print destinations to store across browser sessions.
82 * @const {number} 108 * @const {number}
83 */ 109 */
84 AppState.NUM_DESTINATIONS_ = 3; 110 AppState.NUM_DESTINATIONS_ = 3;
85 111
86
87 /**
88 * Enumeration of field names for serialized app state.
89 * @enum {string}
90 */
91 AppState.Field = {
92 VERSION: 'version',
93 RECENT_DESTINATIONS: 'recentDestinations',
94 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
95 DPI: 'dpi',
96 MEDIA_SIZE: 'mediaSize',
97 MARGINS_TYPE: 'marginsType',
98 CUSTOM_MARGINS: 'customMargins',
99 IS_COLOR_ENABLED: 'isColorEnabled',
100 IS_DUPLEX_ENABLED: 'isDuplexEnabled',
101 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
102 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
103 IS_COLLATE_ENABLED: 'isCollateEnabled',
104 IS_FIT_TO_PAGE_ENABLED: 'isFitToPageEnabled',
105 IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled',
106 SCALING: 'scaling',
107 VENDOR_OPTIONS: 'vendorOptions'
108 };
109
110 /** 112 /**
111 * Current version of the app state. This value helps to understand how to 113 * Current version of the app state. This value helps to understand how to
112 * parse earlier versions of the app state. 114 * parse earlier versions of the app state.
113 * @type {number} 115 * @type {number}
114 * @const 116 * @const
115 * @private 117 * @private
116 */ 118 */
117 AppState.VERSION_ = 2; 119 AppState.VERSION_ = 2;
118 120
119 /** 121 /**
120 * Name of C++ layer function to persist app state. 122 * Name of C++ layer function to persist app state.
121 * @type {string} 123 * @type {string}
122 * @const 124 * @const
123 * @private 125 * @private
124 */ 126 */
125 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; 127 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
126 128
127 AppState.prototype = { 129 AppState.prototype = {
128 /** 130 /**
129 * @return {?RecentDestination} The most recent destination, which is 131 * @return {?RecentDestination} The most recent destination,
130 * currently the selected destination. 132 * which is currently the selected destination.
131 */ 133 */
132 get selectedDestination() { 134 get selectedDestination() {
133 return (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 0) ? 135 return (this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].
134 this.state_[AppState.Field.RECENT_DESTINATIONS][0] : null; 136 length > 0) ?
137 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0] :
138 null;
135 }, 139 },
136 140
137 /** 141 /**
138 * @return {boolean} Whether the selected destination is valid. 142 * @return {boolean} Whether the selected destination is valid.
139 */ 143 */
140 isSelectedDestinationValid: function() { 144 isSelectedDestinationValid: function() {
141 return this.selectedDestination && 145 return !!this.selectedDestination &&
142 this.selectedDestination.id && 146 !!this.selectedDestination.id &&
143 this.selectedDestination.origin; 147 !!this.selectedDestination.origin;
144 }, 148 },
145 149
146 /** 150 /**
147 * @return {?Array<!RecentDestination>} The AppState.NUM_DESTINATIONS_ most 151 * @return {?Array<!RecentDestination>} The
148 * recent destinations. 152 * AppState.NUM_DESTINATIONS_ most recent destinations.
149 */ 153 */
150 get recentDestinations() { 154 get recentDestinations() {
151 return this.state_[AppState.Field.RECENT_DESTINATIONS]; 155 return this.state_[print_preview.AppStateField.RECENT_DESTINATIONS];
152 }, 156 },
153 157
154 /** @return {boolean} Whether the GCP promotion has been dismissed. */ 158 /** @return {boolean} Whether the GCP promotion has been dismissed. */
155 get isGcpPromoDismissed() { 159 get isGcpPromoDismissed() {
156 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED]; 160 return this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED];
157 }, 161 },
158 162
159 /** 163 /**
160 * @param {!print_preview.AppState.Field} field App state field to check if 164 * @param {!print_preview.AppStateField} field App state field to check if
161 * set. 165 * set.
162 * @return {boolean} Whether a field has been set in the app state. 166 * @return {boolean} Whether a field has been set in the app state.
163 */ 167 */
164 hasField: function(field) { 168 hasField: function(field) {
165 return this.state_.hasOwnProperty(field); 169 return this.state_.hasOwnProperty(field);
166 }, 170 },
167 171
168 /** 172 /**
169 * @param {!print_preview.AppState.Field} field App state field to get. 173 * @param {!print_preview.AppStateField} field App state field to get.
170 * @return {?} Value of the app state field. 174 * @return {?} Value of the app state field.
171 */ 175 */
172 getField: function(field) { 176 getField: function(field) {
173 if (field == AppState.Field.CUSTOM_MARGINS) { 177 if (field == print_preview.AppStateField.CUSTOM_MARGINS) {
174 return this.state_[field] ? 178 return this.state_[field] ?
175 print_preview.Margins.parse(this.state_[field]) : null; 179 print_preview.Margins.parse(this.state_[field]) : null;
176 } else { 180 } else {
177 return this.state_[field]; 181 return this.state_[field];
178 } 182 }
179 }, 183 },
180 184
181 /** 185 /**
182 * Initializes the app state from a serialized string returned by the native 186 * Initializes the app state from a serialized string returned by the native
183 * layer. 187 * layer.
184 * @param {?string} serializedAppStateStr Serialized string representation 188 * @param {?string} serializedAppStateStr Serialized string representation
185 * of the app state. 189 * of the app state.
186 */ 190 */
187 init: function(serializedAppStateStr) { 191 init: function(serializedAppStateStr) {
188 if (serializedAppStateStr) { 192 if (serializedAppStateStr) {
189 try { 193 try {
190 var state = JSON.parse(serializedAppStateStr); 194 var state = JSON.parse(serializedAppStateStr);
191 if (state[AppState.Field.VERSION] == AppState.VERSION_) { 195 if (state[print_preview.AppStateField.VERSION] == AppState.VERSION_) {
192 this.state_ = state; 196 this.state_ = /** @type {Object} */(state);
193 } 197 }
194 } catch(e) { 198 } catch(e) {
195 console.error('Unable to parse state: ' + e); 199 console.error('Unable to parse state: ' + e);
196 // Proceed with default state. 200 // Proceed with default state.
197 } 201 }
198 } else { 202 } else {
199 // Set some state defaults. 203 // Set some state defaults.
200 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; 204 this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = false;
201 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; 205 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [];
202 } 206 }
203 if (!this.state_[AppState.Field.RECENT_DESTINATIONS]) { 207 if (!this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]) {
204 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; 208 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [];
205 } else if (!(this.state_[AppState.Field.RECENT_DESTINATIONS] instanceof 209 } else if (!(this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]
206 Array)) { 210 instanceof Array)) {
207 var tmp = this.state_[AppState.Field.RECENT_DESTINATIONS]; 211 var tmp = this.state_[print_preview.AppStateField.RECENT_DESTINATIONS];
208 this.state_[AppState.Field.RECENT_DESTINATIONS] = [tmp]; 212 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [tmp];
209 } else if (!this.state_[AppState.Field.RECENT_DESTINATIONS][0] || 213 } else if (!this.state_[
210 !this.state_[AppState.Field.RECENT_DESTINATIONS][0].id) { 214 print_preview.AppStateField.RECENT_DESTINATIONS][0] ||
215 !this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0].id) {
211 // read in incorrectly 216 // read in incorrectly
212 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; 217 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [];
213 } else if (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 218 } else if (this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].
214 AppState.NUM_DESTINATIONS_) { 219 length > AppState.NUM_DESTINATIONS_) {
215 this.state_[AppState.Field.RECENT_DESTINATIONS].length = 220 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length =
216 AppState.NUM_DESTINATIONS_; 221 AppState.NUM_DESTINATIONS_;
217 } 222 }
218 }, 223 },
219 224
220 /** 225 /**
221 * Sets to initialized state. Now object will accept persist requests. 226 * Sets to initialized state. Now object will accept persist requests.
222 */ 227 */
223 setInitialized: function() { 228 setInitialized: function() {
224 this.isInitialized_ = true; 229 this.isInitialized_ = true;
225 }, 230 },
226 231
227 /** 232 /**
228 * Persists the given value for the given field. 233 * Persists the given value for the given field.
229 * @param {!print_preview.AppState.Field} field Field to persist. 234 * @param {!print_preview.AppStateField} field Field to persist.
230 * @param {?} value Value of field to persist. 235 * @param {?} value Value of field to persist.
231 */ 236 */
232 persistField: function(field, value) { 237 persistField: function(field, value) {
233 if (!this.isInitialized_) 238 if (!this.isInitialized_)
234 return; 239 return;
235 if (field == AppState.Field.CUSTOM_MARGINS) { 240 if (field == print_preview.AppStateField.CUSTOM_MARGINS) {
236 this.state_[field] = value ? value.serialize() : null; 241 this.state_[field] = value ? value.serialize() : null;
237 } else { 242 } else {
238 this.state_[field] = value; 243 this.state_[field] = value;
239 } 244 }
240 this.persist_(); 245 this.persist_();
241 }, 246 },
242 247
243 /** 248 /**
244 * Persists the selected destination. 249 * Persists the selected destination.
245 * @param {!print_preview.Destination} dest Destination to persist. 250 * @param {!print_preview.Destination} dest Destination to persist.
246 */ 251 */
247 persistSelectedDestination: function(dest) { 252 persistSelectedDestination: function(dest) {
248 if (!this.isInitialized_) 253 if (!this.isInitialized_)
249 return; 254 return;
250 255
251 // Determine if this destination is already in the recent destinations, 256 // Determine if this destination is already in the recent destinations,
252 // and where in the array it is located. 257 // and where in the array it is located.
253 var newDestination = new RecentDestination(dest); 258 var newDestination = new RecentDestination(dest);
254 var indexFound = this.state_[ 259 var indexFound = this.state_[
255 AppState.Field.RECENT_DESTINATIONS].findIndex(function(recent) { 260 print_preview.AppStateField.RECENT_DESTINATIONS].findIndex(
256 return (newDestination.id == recent.id && 261 function(recent) {
257 newDestination.origin == recent.origin); 262 return (newDestination.id == recent.id &&
258 }); 263 newDestination.origin == recent.origin);
264 });
259 265
260 // No change 266 // No change
261 if (indexFound == 0) { 267 if (indexFound == 0) {
262 this.persist_(); 268 this.persist_();
263 return; 269 return;
264 } 270 }
265 271
266 // Shift the array so that the nth most recent destination is located at 272 // Shift the array so that the nth most recent destination is located at
267 // index n. 273 // index n.
268 if (indexFound == -1 && 274 if (indexFound == -1 &&
269 this.state_[AppState.Field.RECENT_DESTINATIONS].length == 275 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length ==
270 AppState.NUM_DESTINATIONS_) { 276 AppState.NUM_DESTINATIONS_) {
271 indexFound = AppState.NUM_DESTINATIONS_ - 1; 277 indexFound = AppState.NUM_DESTINATIONS_ - 1;
272 } 278 }
273 if (indexFound != -1) 279 if (indexFound != -1)
274 this.state_[AppState.Field.RECENT_DESTINATIONS].splice(indexFound, 1); 280 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice(
281 indexFound, 1);
275 282
276 // Add the most recent destination 283 // Add the most recent destination
277 this.state_[AppState.Field.RECENT_DESTINATIONS].splice( 284 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice(
278 0, 0, newDestination); 285 0, 0, newDestination);
279 286
280 this.persist_(); 287 this.persist_();
281 }, 288 },
282 289
283 /** 290 /**
284 * Persists whether the GCP promotion has been dismissed. 291 * Persists whether the GCP promotion has been dismissed.
285 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been 292 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
286 * dismissed. 293 * dismissed.
287 */ 294 */
288 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) { 295 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
289 if (!this.isInitialized_) 296 if (!this.isInitialized_)
290 return; 297 return;
291 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed; 298 this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] =
299 isGcpPromoDismissed;
292 this.persist_(); 300 this.persist_();
293 }, 301 },
294 302
295 /** 303 /**
296 * Calls into the native layer to persist the application state. 304 * Calls into the native layer to persist the application state.
297 * @private 305 * @private
298 */ 306 */
299 persist_: function() { 307 persist_: function() {
300 chrome.send(AppState.NATIVE_FUNCTION_NAME_, 308 chrome.send(AppState.NATIVE_FUNCTION_NAME_,
301 [JSON.stringify(this.state_)]); 309 [JSON.stringify(this.state_)]);
302 } 310 }
303 }; 311 };
304 312
305 return { 313 return {
306 AppState: AppState 314 AppState: AppState
307 }; 315 };
308 }); 316 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698