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 IS_SELECTED_DESTINATION_LOCAL: 'isSelectedDestinationLocal', // Deprecated
40 IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
41 MARGINS_TYPE: 'marginsType',
42 CUSTOM_MARGINS: 'customMargins',
43 IS_COLOR_ENABLED: 'isColorEnabled',
44 IS_DUPLEX_ENABLED: 'isDuplexEnabled',
45 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
46 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
47 IS_COLLATE_ENABLED: 'isCollateEnabled',
48 IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled'
52 * Current version of the app state. This value helps to understand how to
53 * parse earlier versions of the app state.
58 AppState.VERSION_ = 2;
61 * Name of C++ layer function to persist app state.
66 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
68 AppState.prototype = {
69 /** @return {?string} ID of the selected destination. */
70 get selectedDestinationId() {
71 return this.state_[AppState.Field.SELECTED_DESTINATION_ID];
74 /** @return {?string} Origin of the selected destination. */
75 get selectedDestinationOrigin() {
76 return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
79 /** @return {boolean} Whether the GCP promotion has been dismissed. */
80 get isGcpPromoDismissed() {
81 return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
85 * @param {!print_preview.AppState.Field} field App state field to check if
87 * @return {boolean} Whether a field has been set in the app state.
89 hasField: function(field) {
90 return this.state_.hasOwnProperty(field);
94 * @param {!print_preview.AppState.Field} field App state field to get.
95 * @return {Object} Value of the app state field.
97 getField: function(field) {
98 if (field == AppState.Field.CUSTOM_MARGINS) {
99 return this.state_[field] ?
100 print_preview.Margins.parse(this.state_[field]) : null;
102 return this.state_[field];
107 * Initializes the app state from a serialized string returned by the native
109 * @param {?string} serializedAppStateStr Serialized string representation
112 init: function(serializedAppStateStr) {
113 if (serializedAppStateStr) {
114 var state = JSON.parse(serializedAppStateStr);
115 if (state[AppState.Field.VERSION] == AppState.VERSION_) {
116 if (state.hasOwnProperty(
117 AppState.Field.IS_SELECTED_DESTINATION_LOCAL)) {
118 state[AppState.Field.SELECTED_DESTINATION_ORIGIN] =
119 state[AppState.Field.IS_SELECTED_DESTINATION_LOCAL] ?
120 print_preview.Destination.Origin.LOCAL :
121 print_preview.Destination.Origin.COOKIES;
122 delete state[AppState.Field.IS_SELECTED_DESTINATION_LOCAL];
127 // Set some state defaults.
128 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
133 * Sets to initialized state. Now object will accept persist requests.
135 setInitialized: function() {
136 this.isInitialized_ = true;
140 * Persists the given value for the given field.
141 * @param {!print_preview.AppState.Field} field Field to persist.
142 * @param {Object} value Value of field to persist.
144 persistField: function(field, value) {
145 if (!this.isInitialized_)
147 if (field == AppState.Field.CUSTOM_MARGINS) {
148 this.state_[field] = value ? value.serialize() : null;
150 this.state_[field] = value;
156 * Persists the selected destination.
157 * @param {!print_preview.Destination} dest Destination to persist.
159 persistSelectedDestination: function(dest) {
160 if (!this.isInitialized_)
162 this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
163 this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
168 * Persists whether the GCP promotion has been dismissed.
169 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
172 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
173 if (!this.isInitialized_)
175 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
180 * Calls into the native layer to persist the application state.
183 persist_: function() {
184 chrome.send(AppState.NATIVE_FUNCTION_NAME_,
185 [JSON.stringify(this.state_)]);