• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5cr.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_ACCOUNT: 'selectedDestinationAccount',
39    SELECTED_DESTINATION_ORIGIN: 'selectedDestinationOrigin',
40    SELECTED_DESTINATION_CAPABILITIES: 'selectedDestinationCapabilities',
41    SELECTED_DESTINATION_NAME: 'selectedDestinationName',
42    IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
43    MEDIA_SIZE: 'mediaSize',
44    MARGINS_TYPE: 'marginsType',
45    CUSTOM_MARGINS: 'customMargins',
46    IS_COLOR_ENABLED: 'isColorEnabled',
47    IS_DUPLEX_ENABLED: 'isDuplexEnabled',
48    IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
49    IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
50    IS_COLLATE_ENABLED: 'isCollateEnabled',
51    IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled',
52    VENDOR_OPTIONS: 'vendorOptions'
53  };
54
55  /**
56   * Current version of the app state. This value helps to understand how to
57   * parse earlier versions of the app state.
58   * @type {number}
59   * @const
60   * @private
61   */
62  AppState.VERSION_ = 2;
63
64  /**
65   * Name of C++ layer function to persist app state.
66   * @type {string}
67   * @const
68   * @private
69   */
70  AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';
71
72  AppState.prototype = {
73    /** @return {?string} ID of the selected destination. */
74    get selectedDestinationId() {
75      return this.state_[AppState.Field.SELECTED_DESTINATION_ID];
76    },
77
78    /** @return {?string} Account the selected destination is registered for. */
79    get selectedDestinationAccount() {
80      return this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT];
81    },
82
83    /** @return {?string} Origin of the selected destination. */
84    get selectedDestinationOrigin() {
85      return this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN];
86    },
87
88    /** @return {?print_preview.Cdd} CDD of the selected destination. */
89    get selectedDestinationCapabilities() {
90      return this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES];
91    },
92
93    /** @return {?string} Name of the selected destination. */
94    get selectedDestinationName() {
95      return this.state_[AppState.Field.SELECTED_DESTINATION_NAME];
96    },
97
98    /** @return {boolean} Whether the GCP promotion has been dismissed. */
99    get isGcpPromoDismissed() {
100      return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
101    },
102
103    /**
104     * @param {!print_preview.AppState.Field} field App state field to check if
105     *     set.
106     * @return {boolean} Whether a field has been set in the app state.
107     */
108    hasField: function(field) {
109      return this.state_.hasOwnProperty(field);
110    },
111
112    /**
113     * @param {!print_preview.AppState.Field} field App state field to get.
114     * @return {Object} Value of the app state field.
115     */
116    getField: function(field) {
117      if (field == AppState.Field.CUSTOM_MARGINS) {
118        return this.state_[field] ?
119            print_preview.Margins.parse(this.state_[field]) : null;
120      } else {
121        return this.state_[field];
122      }
123    },
124
125    /**
126     * Initializes the app state from a serialized string returned by the native
127     * layer.
128     * @param {?string} serializedAppStateStr Serialized string representation
129     *     of the app state.
130     * @param {?string} systemDefaultDestinationId ID of the system default
131     *     destination.
132     */
133    init: function(serializedAppStateStr, systemDefaultDestinationId) {
134      if (serializedAppStateStr) {
135        var state = JSON.parse(serializedAppStateStr);
136        if (state[AppState.Field.VERSION] == AppState.VERSION_) {
137          this.state_ = state;
138        }
139      } else {
140        // Set some state defaults.
141        this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
142      }
143      // Default to system destination, if no destination was selected.
144      if (!this.state_[AppState.Field.SELECTED_DESTINATION_ID] ||
145          !this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]) {
146        if (systemDefaultDestinationId) {
147          this.state_[AppState.Field.SELECTED_DESTINATION_ID] =
148              systemDefaultDestinationId;
149          this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] =
150              print_preview.Destination.Origin.LOCAL;
151          this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = '';
152        }
153      }
154    },
155
156    /**
157     * Sets to initialized state. Now object will accept persist requests.
158     */
159    setInitialized: function() {
160      this.isInitialized_ = true;
161    },
162
163    /**
164     * Persists the given value for the given field.
165     * @param {!print_preview.AppState.Field} field Field to persist.
166     * @param {Object} value Value of field to persist.
167     */
168    persistField: function(field, value) {
169      if (!this.isInitialized_)
170        return;
171      if (field == AppState.Field.CUSTOM_MARGINS) {
172        this.state_[field] = value ? value.serialize() : null;
173      } else {
174        this.state_[field] = value;
175      }
176      this.persist_();
177    },
178
179    /**
180     * Persists the selected destination.
181     * @param {!print_preview.Destination} dest Destination to persist.
182     */
183    persistSelectedDestination: function(dest) {
184      if (!this.isInitialized_)
185        return;
186      this.state_[AppState.Field.SELECTED_DESTINATION_ID] = dest.id;
187      this.state_[AppState.Field.SELECTED_DESTINATION_ACCOUNT] = dest.account;
188      this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN] = dest.origin;
189      this.state_[AppState.Field.SELECTED_DESTINATION_CAPABILITIES] =
190          dest.capabilities;
191      this.state_[AppState.Field.SELECTED_DESTINATION_NAME] = dest.displayName;
192      this.persist_();
193    },
194
195    /**
196     * Persists whether the GCP promotion has been dismissed.
197     * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
198     *     dismissed.
199     */
200    persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
201      if (!this.isInitialized_)
202        return;
203      this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
204      this.persist_();
205    },
206
207    /**
208     * Calls into the native layer to persist the application state.
209     * @private
210     */
211    persist_: function() {
212      chrome.send(AppState.NATIVE_FUNCTION_NAME_,
213                  [JSON.stringify(this.state_)]);
214    }
215  };
216
217  return {
218    AppState: AppState
219  };
220});
221