Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / app_state.js
bloba221f7a5e722def026dccc0d24378d85a7a88464
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         try {
157           var state = JSON.parse(serializedAppStateStr);
158           if (state[AppState.Field.VERSION] == AppState.VERSION_) {
159             this.state_ = state;
160           }
161         } catch(e) {
162           console.error('Unable to parse state: ' + e);
163           // Proceed with default state.
164         }
165       } else {
166         // Set some state defaults.
167         this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
168       }
169       // Default to system destination, if no destination was selected.
170       if (!this.state_[AppState.Field.SELECTED_DESTINATION_ID] ||
171           !this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]) {
172         if (systemDefaultDestinationId) {
173           this.state_[AppState.Field.SELECTED_DESTINATION_ID] =
174               systemDefaultDestinationId;
175           this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] =
176               print_preview.Destination.Origin.LOCAL;
177           this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = '';
178         }
179       }
180     },
182     /**
183      * Sets to initialized state. Now object will accept persist requests.
184      */
185     setInitialized: function() {
186       this.isInitialized_ = true;
187     },
189     /**
190      * Persists the given value for the given field.
191      * @param {!print_preview.AppState.Field} field Field to persist.
192      * @param {?} value Value of field to persist.
193      */
194     persistField: function(field, value) {
195       if (!this.isInitialized_)
196         return;
197       if (field == AppState.Field.CUSTOM_MARGINS) {
198         this.state_[field] = value ? value.serialize() : null;
199       } else {
200         this.state_[field] = value;
201       }
202       this.persist_();
203     },
205     /**
206      * Persists the selected destination.
207      * @param {!print_preview.Destination} dest Destination to persist.
208      */
209     persistSelectedDestination: function(dest) {
210       if (!this.isInitialized_)
211         return;
212       this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
213       this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = dest.account;
214       this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
215       this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES] =
216           dest.capabilities;
217       this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName;
218       this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_ID] =
219           dest.extensionId;
220       this.state_[AppState.Field.SELECTED_DESTINATION_EXTENSION_NAME] =
221           dest.extensionName;
222       this.persist_();
223     },
225     /**
226      * Persists whether the GCP promotion has been dismissed.
227      * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
228      *     dismissed.
229      */
230     persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
231       if (!this.isInitialized_)
232         return;
233       this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
234       this.persist_();
235     },
237     /**
238      * Calls into the native layer to persist the application state.
239      * @private
240      */
241     persist_: function() {
242       chrome.send(AppState.NATIVE_FUNCTION_NAME_,
243                   [JSON.stringify(this.state_)]);
244     }
245   };
247   return {
248     AppState: AppState
249   };