- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / data / app_state.js
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.
4
5 cr.define('print_preview', function() {
6   'use strict';
7
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;
21
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   };
30
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     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'
49   };
50
51   /**
52    * Current version of the app state. This value helps to understand how to
53    * parse earlier versions of the app state.
54    * @type {number}
55    * @const
56    * @private
57    */
58   AppState.VERSION_ = 2;
59
60   /**
61    * Name of C++ layer function to persist app state.
62    * @type {string}
63    * @const
64    * @private
65    */
66   AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
67
68   AppState.prototype = {
69     /** @return {?string} ID of the selected destination. */
70     get selectedDestinationId() {
71       return this.state_[AppState.Field.SELECTED_DESTINATION_ID];
72     },
73
74     /** @return {?string} Origin of the selected destination. */
75     get selectedDestinationOrigin() {
76       return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
77     },
78
79     /** @return {boolean} Whether the GCP promotion has been dismissed. */
80     get isGcpPromoDismissed() {
81       return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
82     },
83
84     /**
85      * @param {!print_preview.AppState.Field} field App state field to check if
86      *     set.
87      * @return {boolean} Whether a field has been set in the app state.
88      */
89     hasField: function(field) {
90       return this.state_.hasOwnProperty(field);
91     },
92
93     /**
94      * @param {!print_preview.AppState.Field} field App state field to get.
95      * @return {Object} Value of the app state field.
96      */
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;
101       } else {
102         return this.state_[field];
103       }
104     },
105
106     /**
107      * Initializes the app state from a serialized string returned by the native
108      * layer.
109      * @param {?string} serializedAppStateStr Serialized string representation
110      *     of the app state.
111      */
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];
123           }
124           this.state_ = state;
125         }
126       } else {
127         // Set some state defaults.
128         this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
129       }
130     },
131
132     /**
133      * Sets to initialized state. Now object will accept persist requests.
134      */
135     setInitialized: function() {
136       this.isInitialized_ = true;
137     },
138
139     /**
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.
143      */
144     persistField: function(field, value) {
145       if (!this.isInitialized_)
146         return;
147       if (field == AppState.Field.CUSTOM_MARGINS) {
148         this.state_[field] = value ? value.serialize() : null;
149       } else {
150         this.state_[field] = value;
151       }
152       this.persist_();
153     },
154
155     /**
156      * Persists the selected destination.
157      * @param {!print_preview.Destination} dest Destination to persist.
158      */
159     persistSelectedDestination: function(dest) {
160       if (!this.isInitialized_)
161         return;
162       this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
163       this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
164       this.persist_();
165     },
166
167     /**
168      * Persists whether the GCP promotion has been dismissed.
169      * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
170      *     dismissed.
171      */
172     persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
173       if (!this.isInitialized_)
174         return;
175       this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
176       this.persist_();
177     },
178
179     /**
180      * Calls into the native layer to persist the application state.
181      * @private
182      */
183     persist_: function() {
184       chrome.send(AppState.NATIVE_FUNCTION_NAME_,
185                   [JSON.stringify(this.state_)]);
186     }
187   };
188
189   return {
190     AppState: AppState
191   };
192 });