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
3 // found in the LICENSE file.
5 cr.define('print_preview', function() {
9 * Object used to get and persist the print preview application state.
14 * Internal representation of application state.
15 * @type {Object.<string: Object>}
19 this.state_[AppState.Field.VERSION] = AppState.VERSION_;
20 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true;
23 * Whether the app state has been initialized. The app state will ignore all
24 * writes until it has been initialized.
28 this.isInitialized_ = false;
32 * Enumeration of field names for serialized app state.
37 SELECTED_DESTINATION_ID: 'selectedDestinationId',
38 SELECTED_DESTINATION_ACCOUNT: 'selectedDestinationAccount',
39 SELECTED_DESTINATION_ORIGIN: 'selectedDestinationOrigin',
40 SELECTED_DESTINATION_CAPABILITIES: 'selectedDestinationCapabilities',
41 SELECTED_DESTINATION_NAME: 'selectedDestinationName',
42 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
43 MEDIA_SIZE: 'mediaSize',
44 MARGINS_TYPE: 'marginsType',
45 CUSTOM_MARGINS: 'customMargins',
46 IS_COLOR_ENABLED: 'isColorEnabled',
47 IS_DUPLEX_ENABLED: 'isDuplexEnabled',
48 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
49 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
50 IS_COLLATE_ENABLED: 'isCollateEnabled',
51 IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled'
55 * Current version of the app state. This value helps to understand how to
56 * parse earlier versions of the app state.
61 AppState.VERSION_ = 2;
64 * Name of C++ layer function to persist app state.
69 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
71 AppState.prototype = {
72 /** @return {?string} ID of the selected destination. */
73 get selectedDestinationId() {
74 return this.state_[AppState.Field.SELECTED_DESTINATION_ID];
77 /** @return {?string} Account the selected destination is registered for. */
78 get selectedDestinationAccount() {
79 return this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT];
82 /** @return {?string} Origin of the selected destination. */
83 get selectedDestinationOrigin() {
84 return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
87 /** @return {?print_preview.Cdd} CDD of the selected destination. */
88 get selectedDestinationCapabilities() {
89 return this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES];
92 /** @return {?string} Name of the selected destination. */
93 get selectedDestinationName() {
94 return this.state_[AppState.Field.SELECTED_DESTINATION_NAME];
97 /** @return {boolean} Whether the GCP promotion has been dismissed. */
98 get isGcpPromoDismissed() {
99 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
103 * @param {!print_preview.AppState.Field} field App state field to check if
105 * @return {boolean} Whether a field has been set in the app state.
107 hasField: function(field) {
108 return this.state_.hasOwnProperty(field);
112 * @param {!print_preview.AppState.Field} field App state field to get.
113 * @return {Object} Value of the app state field.
115 getField: function(field) {
116 if (field == AppState.Field.CUSTOM_MARGINS) {
117 return this.state_[field] ?
118 print_preview.Margins.parse(this.state_[field]) : null;
120 return this.state_[field];
125 * Initializes the app state from a serialized string returned by the native
127 * @param {?string} serializedAppStateStr Serialized string representation
129 * @param {?string} systemDefaultDestinationId ID of the system default
132 init: function(serializedAppStateStr, systemDefaultDestinationId) {
133 if (serializedAppStateStr) {
134 var state = JSON.parse(serializedAppStateStr);
135 if (state[AppState.Field.VERSION] == AppState.VERSION_) {
139 // Set some state defaults.
140 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
142 // Default to system destination, if no destination was selected.
143 if (!this.state_[AppState.Field.SELECTED_DESTINATION_ID] ||
144 !this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]) {
145 if (systemDefaultDestinationId) {
146 this.state_[AppState.Field.SELECTED_DESTINATION_ID] =
147 systemDefaultDestinationId;
148 this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] =
149 print_preview.Destination.Origin.LOCAL;
155 * Sets to initialized state. Now object will accept persist requests.
157 setInitialized: function() {
158 this.isInitialized_ = true;
162 * Persists the given value for the given field.
163 * @param {!print_preview.AppState.Field} field Field to persist.
164 * @param {Object} value Value of field to persist.
166 persistField: function(field, value) {
167 if (!this.isInitialized_)
169 if (field == AppState.Field.CUSTOM_MARGINS) {
170 this.state_[field] = value ? value.serialize() : null;
172 this.state_[field] = value;
178 * Persists the selected destination.
179 * @param {!print_preview.Destination} dest Destination to persist.
181 persistSelectedDestination: function(dest) {
182 if (!this.isInitialized_)
184 this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
185 this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = dest.account;
186 this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
187 this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES] =
189 this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName;
194 * Persists whether the GCP promotion has been dismissed.
195 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
198 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
199 if (!this.isInitialized_)
201 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
206 * Calls into the native layer to persist the application state.
209 persist_: function() {
210 chrome.send(AppState.NATIVE_FUNCTION_NAME_,
211 [JSON.stringify(this.state_)]);