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_ORIGIN: 'selectedDestinationOrigin',
39 SELECTED_DESTINATION_CAPABILITIES: 'selectedDestinationCapabilities',
40 SELECTED_DESTINATION_NAME: 'selectedDestinationName',
41 IS_SELECTED_DESTINATION_LOCAL: 'isSelectedDestinationLocal', // Deprecated
42 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
43 MARGINS_TYPE: 'marginsType',
44 CUSTOM_MARGINS: 'customMargins',
45 IS_COLOR_ENABLED: 'isColorEnabled',
46 IS_DUPLEX_ENABLED: 'isDuplexEnabled',
47 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
48 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
49 IS_COLLATE_ENABLED: 'isCollateEnabled',
50 IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled'
54 * Current version of the app state. This value helps to understand how to
55 * parse earlier versions of the app state.
60 AppState.VERSION_ = 2;
63 * Name of C++ layer function to persist app state.
68 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
70 AppState.prototype = {
71 /** @return {?string} ID of the selected destination. */
72 get selectedDestinationId() {
73 return this.state_[AppState.Field.SELECTED_DESTINATION_ID];
76 /** @return {?string} Origin of the selected destination. */
77 get selectedDestinationOrigin() {
78 return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
81 /** @return {?print_preview.Cdd} CDD of the selected destination. */
82 get selectedDestinationCapabilities() {
83 return this.state_[AppState.Field.SELECTED_DESTINATION_CAPBILITIES];
86 /** @return {?string} Name of the selected destination. */
87 get selectedDestinationName() {
88 return this.state_[AppState.Field.SELECTED_DESTINATION_NAME];
91 /** @return {boolean} Whether the GCP promotion has been dismissed. */
92 get isGcpPromoDismissed() {
93 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
97 * @param {!print_preview.AppState.Field} field App state field to check if
99 * @return {boolean} Whether a field has been set in the app state.
101 hasField: function(field) {
102 return this.state_.hasOwnProperty(field);
106 * @param {!print_preview.AppState.Field} field App state field to get.
107 * @return {Object} Value of the app state field.
109 getField: function(field) {
110 if (field == AppState.Field.CUSTOM_MARGINS) {
111 return this.state_[field] ?
112 print_preview.Margins.parse(this.state_[field]) : null;
114 return this.state_[field];
119 * Initializes the app state from a serialized string returned by the native
121 * @param {?string} serializedAppStateStr Serialized string representation
124 init: function(serializedAppStateStr) {
125 if (serializedAppStateStr) {
126 var state = JSON.parse(serializedAppStateStr);
127 if (state[AppState.Field.VERSION] == AppState.VERSION_) {
128 if (state.hasOwnProperty(
129 AppState.Field.IS_SELECTED_DESTINATION_LOCAL)) {
130 state[AppState.Field.SELECTED_DESTINATION_ORIGIN] =
131 state[AppState.Field.IS_SELECTED_DESTINATION_LOCAL] ?
132 print_preview.Destination.Origin.LOCAL :
133 print_preview.Destination.Origin.COOKIES;
134 delete state[AppState.Field.IS_SELECTED_DESTINATION_LOCAL];
139 // Set some state defaults.
140 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
145 * Sets to initialized state. Now object will accept persist requests.
147 setInitialized: function() {
148 this.isInitialized_ = true;
152 * Persists the given value for the given field.
153 * @param {!print_preview.AppState.Field} field Field to persist.
154 * @param {Object} value Value of field to persist.
156 persistField: function(field, value) {
157 if (!this.isInitialized_)
159 if (field == AppState.Field.CUSTOM_MARGINS) {
160 this.state_[field] = value ? value.serialize() : null;
162 this.state_[field] = value;
168 * Persists the selected destination.
169 * @param {!print_preview.Destination} dest Destination to persist.
171 persistSelectedDestination: function(dest) {
172 if (!this.isInitialized_)
174 this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
175 this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
176 this.state_[AppState.Field.SELECTED_DESTINATION_CAPBILITIES] =
178 this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName;
183 * Persists whether the GCP promotion has been dismissed.
184 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
187 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
188 if (!this.isInitialized_)
190 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
195 * Calls into the native layer to persist the application state.
198 persist_: function() {
199 chrome.send(AppState.NATIVE_FUNCTION_NAME_,
200 [JSON.stringify(this.state_)]);