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.
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 SELECTED_DESTINATION_EXTENSION_ID
: 'selectedDestinationExtensionId',
43 SELECTED_DESTINATION_EXTENSION_NAME
: 'selectedDestinationExtensionName',
44 IS_GCP_PROMO_DISMISSED
: 'isGcpPromoDismissed',
46 MEDIA_SIZE
: 'mediaSize',
47 MARGINS_TYPE
: 'marginsType',
48 CUSTOM_MARGINS
: 'customMargins',
49 IS_COLOR_ENABLED
: 'isColorEnabled',
50 IS_DUPLEX_ENABLED
: 'isDuplexEnabled',
51 IS_HEADER_FOOTER_ENABLED
: 'isHeaderFooterEnabled',
52 IS_LANDSCAPE_ENABLED
: 'isLandscapeEnabled',
53 IS_COLLATE_ENABLED
: 'isCollateEnabled',
54 IS_CSS_BACKGROUND_ENABLED
: 'isCssBackgroundEnabled',
55 VENDOR_OPTIONS
: 'vendorOptions'
59 * Current version of the app state. This value helps to understand how to
60 * parse earlier versions of the app state.
65 AppState
.VERSION_
= 2;
68 * Name of C++ layer function to persist app state.
73 AppState
.NATIVE_FUNCTION_NAME_
= 'saveAppState';
75 AppState
.prototype = {
76 /** @return {?string} ID of the selected destination. */
77 get selectedDestinationId() {
78 return this.state_
[AppState
.Field
.SELECTED_DESTINATION_ID
];
81 /** @return {?string} Account the selected destination is registered for. */
82 get selectedDestinationAccount() {
83 return this.state_
[AppState
.Field
.SELECTED_DESTINATION_ACCOUNT
];
87 * @return {?print_preview.Destination.Origin<string>} Origin of the
88 * selected destination.
90 get selectedDestinationOrigin() {
91 return this.state_
[AppState
.Field
.SELECTED_DESTINATION_ORIGIN
];
94 /** @return {?print_preview.Cdd} CDD of the selected destination. */
95 get selectedDestinationCapabilities() {
96 return this.state_
[AppState
.Field
.SELECTED_DESTINATION_CAPABILITIES
];
99 /** @return {?string} Name of the selected destination. */
100 get selectedDestinationName() {
101 return this.state_
[AppState
.Field
.SELECTED_DESTINATION_NAME
];
105 * @return {?string} Extension ID associated with the selected destination.
107 get selectedDestinationExtensionId() {
108 return this.state_
[AppState
.Field
.SELECTED_DESTINATION_EXTENSION_ID
];
112 * @return {?string} Extension name associated with the selected
115 get selectedDestinationExtensionName() {
116 return this.state_
[AppState
.Field
.SELECTED_DESTINATION_EXTENSION_NAME
];
119 /** @return {boolean} Whether the GCP promotion has been dismissed. */
120 get isGcpPromoDismissed() {
121 return this.state_
[AppState
.Field
.IS_GCP_PROMO_DISMISSED
];
125 * @param {!print_preview.AppState.Field} field App state field to check if
127 * @return {boolean} Whether a field has been set in the app state.
129 hasField: function(field
) {
130 return this.state_
.hasOwnProperty(field
);
134 * @param {!print_preview.AppState.Field} field App state field to get.
135 * @return {?} Value of the app state field.
137 getField: function(field
) {
138 if (field
== AppState
.Field
.CUSTOM_MARGINS
) {
139 return this.state_
[field
] ?
140 print_preview
.Margins
.parse(this.state_
[field
]) : null;
142 return this.state_
[field
];
147 * Initializes the app state from a serialized string returned by the native
149 * @param {?string} serializedAppStateStr Serialized string representation
151 * @param {?string} systemDefaultDestinationId ID of the system default
154 init: function(serializedAppStateStr
, systemDefaultDestinationId
) {
155 if (serializedAppStateStr
) {
157 var state
= JSON
.parse(serializedAppStateStr
);
158 if (state
[AppState
.Field
.VERSION
] == AppState
.VERSION_
) {
162 console
.error('Unable to parse state: ' + e
);
163 // Proceed with default state.
166 // Set some state defaults.
167 this.state_
[AppState
.Field
.IS_GCP_PROMO_DISMISSED
] = false;
169 // Default to system destination, if no destination was selected.
170 if (!this.state_
[AppState
.Field
.SELECTED_DESTINATION_ID
] ||
171 !this.state_
[AppState
.Field
.SELECTED_DESTINATION_ORIGIN
]) {
172 if (systemDefaultDestinationId
) {
173 this.state_
[AppState
.Field
.SELECTED_DESTINATION_ID
] =
174 systemDefaultDestinationId
;
175 this.state_
[AppState
.Field
.SELECTED_DESTINATION_ORIGIN
] =
176 print_preview
.Destination
.Origin
.LOCAL
;
177 this.state_
[AppState
.Field
.SELECTED_DESTINATION_ACCOUNT
] = '';
183 * Sets to initialized state. Now object will accept persist requests.
185 setInitialized: function() {
186 this.isInitialized_
= true;
190 * Persists the given value for the given field.
191 * @param {!print_preview.AppState.Field} field Field to persist.
192 * @param {?} value Value of field to persist.
194 persistField: function(field
, value
) {
195 if (!this.isInitialized_
)
197 if (field
== AppState
.Field
.CUSTOM_MARGINS
) {
198 this.state_
[field
] = value
? value
.serialize() : null;
200 this.state_
[field
] = value
;
206 * Persists the selected destination.
207 * @param {!print_preview.Destination} dest Destination to persist.
209 persistSelectedDestination: function(dest
) {
210 if (!this.isInitialized_
)
212 this.state_
[AppState
.Field
.SELECTED_DESTINATION_ID
] = dest
.id
;
213 this.state_
[AppState
.Field
.SELECTED_DESTINATION_ACCOUNT
] = dest
.account
;
214 this.state_
[AppState
.Field
.SELECTED_DESTINATION_ORIGIN
] = dest
.origin
;
215 this.state_
[AppState
.Field
.SELECTED_DESTINATION_CAPABILITIES
] =
217 this.state_
[AppState
.Field
.SELECTED_DESTINATION_NAME
] = dest
.displayName
;
218 this.state_
[AppState
.Field
.SELECTED_DESTINATION_EXTENSION_ID
] =
220 this.state_
[AppState
.Field
.SELECTED_DESTINATION_EXTENSION_NAME
] =
226 * Persists whether the GCP promotion has been dismissed.
227 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
230 persistIsGcpPromoDismissed: function(isGcpPromoDismissed
) {
231 if (!this.isInitialized_
)
233 this.state_
[AppState
.Field
.IS_GCP_PROMO_DISMISSED
] = isGcpPromoDismissed
;
238 * Calls into the native layer to persist the application state.
241 persist_: function() {
242 chrome
.send(AppState
.NATIVE_FUNCTION_NAME_
,
243 [JSON
.stringify(this.state_
)]);