Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / app_state.js
blob9f424d631d6fcbe1b9eb15c4cdb90db8f2a6e09f
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() {
6   'use strict';
8   /**
9    * Object used to get and persist the print preview application state.
10    * @constructor
11    */
12   function AppState() {
13     /**
14      * Internal representation of application state.
15      * @type {Object.<string: Object>}
16      * @private
17      */
18     this.state_ = {};
19     this.state_[AppState.Field.VERSION] = AppState.VERSION_;
20     this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true;
22     /**
23      * Whether the app state has been initialized. The app state will ignore all
24      * writes until it has been initialized.
25      * @type {boolean}
26      * @private
27      */
28     this.isInitialized_ = false;
29   };
31   /**
32    * Enumeration of field names for serialized app state.
33    * @enum {string}
34    */
35   AppState.Field = {
36     VERSION: 'version',
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'
51   };
53   /**
54    * Current version of the app state. This value helps to understand how to
55    * parse earlier versions of the app state.
56    * @type {number}
57    * @const
58    * @private
59    */
60   AppState.VERSION_ = 2;
62   /**
63    * Name of C++ layer function to persist app state.
64    * @type {string}
65    * @const
66    * @private
67    */
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];
74     },
76     /** @return {?string} Origin of the selected destination. */
77     get selectedDestinationOrigin() {
78       return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
79     },
81     /** @return {?print_preview.Cdd} CDD of the selected destination. */
82     get selectedDestinationCapabilities() {
83       return this.state_[AppState.Field.SELECTED_DESTINATION_CAPBILITIES];
84     },
86     /** @return {?string} Name of the selected destination. */
87     get selectedDestinationName() {
88       return this.state_[AppState.Field.SELECTED_DESTINATION_NAME];
89     },
91     /** @return {boolean} Whether the GCP promotion has been dismissed. */
92     get isGcpPromoDismissed() {
93       return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
94     },
96     /**
97      * @param {!print_preview.AppState.Field} field App state field to check if
98      *     set.
99      * @return {boolean} Whether a field has been set in the app state.
100      */
101     hasField: function(field) {
102       return this.state_.hasOwnProperty(field);
103     },
105     /**
106      * @param {!print_preview.AppState.Field} field App state field to get.
107      * @return {Object} Value of the app state field.
108      */
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;
113       } else {
114         return this.state_[field];
115       }
116     },
118     /**
119      * Initializes the app state from a serialized string returned by the native
120      * layer.
121      * @param {?string} serializedAppStateStr Serialized string representation
122      *     of the app state.
123      */
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];
135           }
136           this.state_ = state;
137         }
138       } else {
139         // Set some state defaults.
140         this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
141       }
142     },
144     /**
145      * Sets to initialized state. Now object will accept persist requests.
146      */
147     setInitialized: function() {
148       this.isInitialized_ = true;
149     },
151     /**
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.
155      */
156     persistField: function(field, value) {
157       if (!this.isInitialized_)
158         return;
159       if (field == AppState.Field.CUSTOM_MARGINS) {
160         this.state_[field] = value ? value.serialize() : null;
161       } else {
162         this.state_[field] = value;
163       }
164       this.persist_();
165     },
167     /**
168      * Persists the selected destination.
169      * @param {!print_preview.Destination} dest Destination to persist.
170      */
171     persistSelectedDestination: function(dest) {
172       if (!this.isInitialized_)
173         return;
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] =
177           dest.capabilities;
178       this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName;
179       this.persist_();
180     },
182     /**
183      * Persists whether the GCP promotion has been dismissed.
184      * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
185      *     dismissed.
186      */
187     persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
188       if (!this.isInitialized_)
189         return;
190       this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
191       this.persist_();
192     },
194     /**
195      * Calls into the native layer to persist the application state.
196      * @private
197      */
198     persist_: function() {
199       chrome.send(AppState.NATIVE_FUNCTION_NAME_,
200                   [JSON.stringify(this.state_)]);
201     }
202   };
204   return {
205     AppState: AppState
206   };