Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / app_state.js
bloba604e9d8e4436003cbc20844285507d624f20fa3
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}
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_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',
45     DPI: 'dpi',
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'
56   };
58   /**
59    * Current version of the app state. This value helps to understand how to
60    * parse earlier versions of the app state.
61    * @type {number}
62    * @const
63    * @private
64    */
65   AppState.VERSION_ = 2;
67   /**
68    * Name of C++ layer function to persist app state.
69    * @type {string}
70    * @const
71    * @private
72    */
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];
79     },
81     /** @return {?string} Account the selected destination is registered for. */
82     get selectedDestinationAccount() {
83       return this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT];
84     },
86     /**
87      * @return {?print_preview.Destination.Origin<string>} Origin of the
88      *     selected destination.
89      */
90     get selectedDestinationOrigin() {
91       return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
92     },
94     /** @return {?print_preview.Cdd} CDD of the selected destination. */
95     get selectedDestinationCapabilities() {
96       return this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES];
97     },
99     /** @return {?string} Name of the selected destination. */
100     get selectedDestinationName() {
101       return this.state_[AppState.Field.SELECTED_DESTINATION_NAME];
102     },
104     /**
105      * @return {?string} Extension ID associated with the selected destination.
106      */
107     get selectedDestinationExtensionId() {
108       return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID];
109     },
111     /**
112      * @return {?string} Extension name associated with the selected
113      *     destination.
114      */
115     get selectedDestinationExtensionName() {
116       return this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME];
117     },
119     /** @return {boolean} Whether the GCP promotion has been dismissed. */
120     get isGcpPromoDismissed() {
121       return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
122     },
124     /**
125      * @param {!print_preview.AppState.Field} field App state field to check if
126      *     set.
127      * @return {boolean} Whether a field has been set in the app state.
128      */
129     hasField: function(field) {
130       return this.state_.hasOwnProperty(field);
131     },
133     /**
134      * @param {!print_preview.AppState.Field} field App state field to get.
135      * @return {?} Value of the app state field.
136      */
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;
141       } else {
142         return this.state_[field];
143       }
144     },
146     /**
147      * Initializes the app state from a serialized string returned by the native
148      * layer.
149      * @param {?string} serializedAppStateStr Serialized string representation
150      *     of the app state.
151      * @param {?string} systemDefaultDestinationId ID of the system default
152      *     destination.
153      */
154     init: function(serializedAppStateStr, systemDefaultDestinationId) {
155       if (serializedAppStateStr) {
156         var state = JSON.parse(serializedAppStateStr);
157         if (state[AppState.Field.VERSION] == AppState.VERSION_) {
158           this.state_ = state;
159         }
160       } else {
161         // Set some state defaults.
162         this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
163       }
164       // Default to system destination, if no destination was selected.
165       if (!this.state_[AppState.Field.SELECTED_DESTINATION_ID] ||
166           !this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]) {
167         if (systemDefaultDestinationId) {
168           this.state_[AppState.Field.SELECTED_DESTINATION_ID] =
169               systemDefaultDestinationId;
170           this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] =
171               print_preview.Destination.Origin.LOCAL;
172           this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = '';
173         }
174       }
175     },
177     /**
178      * Sets to initialized state. Now object will accept persist requests.
179      */
180     setInitialized: function() {
181       this.isInitialized_ = true;
182     },
184     /**
185      * Persists the given value for the given field.
186      * @param {!print_preview.AppState.Field} field Field to persist.
187      * @param {?} value Value of field to persist.
188      */
189     persistField: function(field, value) {
190       if (!this.isInitialized_)
191         return;
192       if (field == AppState.Field.CUSTOM_MARGINS) {
193         this.state_[field] = value ? value.serialize() : null;
194       } else {
195         this.state_[field] = value;
196       }
197       this.persist_();
198     },
200     /**
201      * Persists the selected destination.
202      * @param {!print_preview.Destination} dest Destination to persist.
203      */
204     persistSelectedDestination: function(dest) {
205       if (!this.isInitialized_)
206         return;
207       this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
208       this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = dest.account;
209       this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
210       this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES] =
211           dest.capabilities;
212       this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName;
213       this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID] =
214           dest.extensionId;
215       this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME] =
216           dest.extensionName;
217       this.persist_();
218     },
220     /**
221      * Persists whether the GCP promotion has been dismissed.
222      * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
223      *     dismissed.
224      */
225     persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
226       if (!this.isInitialized_)
227         return;
228       this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
229       this.persist_();
230     },
232     /**
233      * Calls into the native layer to persist the application state.
234      * @private
235      */
236     persist_: function() {
237       chrome.send(AppState.NATIVE_FUNCTION_NAME_,
238                   [JSON.stringify(this.state_)]);
239     }
240   };
242   return {
243     AppState: AppState
244   };