summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/print_preview/data/app_state.js
blob: fb5b95530dac70f9043aca234423a7a6c1c78f43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('print_preview', function() {
  'use strict';

  /**
   * Object used to represent a recent destination in the app state.
   * @constructor
   */
  function RecentDestination(destination) {
    /**
     * ID of the RecentDestination.
     * @type {string}
     */
    this.id = destination.id;

    /**
     * Origin of the RecentDestination.
     * @type {string}
     */
    this.origin = destination.origin;

    /**
     * Account the RecentDestination is registered for.
     * @type {string}
     */
    this.account = destination.account || '';

    /**
     * CDD of the RecentDestination.
     * @type {print_preview.Cdd}
     */
    this.capabilities = destination.capabilities;

    /**
     * Name of the RecentDestination.
     * @type {string}
     */
    this.name = destination.name || '';

    /**
     * Extension ID associated with the RecentDestination.
     * @type {string}
     */
    this.extensionId = destination.extension_id || '';

    /**
     * Extension name associated with the RecentDestination.
     * @type {string}
     */
    this.extensionName = destination.extension_name || '';
  };

  /**
   * Object used to get and persist the print preview application state.
   * @constructor
   */
  function AppState() {
    /**
     * Internal representation of application state.
     * @type {Object}
     * @private
     */
    this.state_ = {};
    this.state_[AppState.Field.VERSION] = AppState.VERSION_;
    this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true;
    this.state_[AppState.Field.RECENT_DESTINATIONS] = [];

    /**
     * Whether the app state has been initialized. The app state will ignore all
     * writes until it has been initialized.
     * @type {boolean}
     * @private
     */
    this.isInitialized_ = false;
  };

  /**
   * Number of recent print destinations to store across browser sessions.
   * @const {number}
   */
  AppState.NUM_DESTINATIONS_ = 3;


  /**
   * Enumeration of field names for serialized app state.
   * @enum {string}
   */
  AppState.Field = {
    VERSION: 'version',
    RECENT_DESTINATIONS: 'recentDestinations',
    IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed',
    DPI: 'dpi',
    MEDIA_SIZE: 'mediaSize',
    MARGINS_TYPE: 'marginsType',
    CUSTOM_MARGINS: 'customMargins',
    IS_COLOR_ENABLED: 'isColorEnabled',
    IS_DUPLEX_ENABLED: 'isDuplexEnabled',
    IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
    IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
    IS_COLLATE_ENABLED: 'isCollateEnabled',
    IS_FIT_TO_PAGE_ENABLED: 'isFitToPageEnabled',
    IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled',
    SCALING: 'scaling',
    VENDOR_OPTIONS: 'vendorOptions'
  };

  /**
   * Current version of the app state. This value helps to understand how to
   * parse earlier versions of the app state.
   * @type {number}
   * @const
   * @private
   */
  AppState.VERSION_ = 2;

  /**
   * Name of C++ layer function to persist app state.
   * @type {string}
   * @const
   * @private
   */
  AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState';

  AppState.prototype = {
    /**
     * @return {?RecentDestination} The most recent destination, which is
     *     currently the selected destination.
     */
    get selectedDestination() {
      return (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 0) ?
          this.state_[AppState.Field.RECENT_DESTINATIONS][0] : null;
    },

    /**
     * @return {?Array<!RecentDestination>} The AppState.NUM_DESTINATIONS_ most
     *     recent destinations.
     */
    get recentDestinations() {
      return this.state_[AppState.Field.RECENT_DESTINATIONS];
    },

    /** @return {boolean} Whether the GCP promotion has been dismissed. */
    get isGcpPromoDismissed() {
      return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED];
    },

    /**
     * @param {!print_preview.AppState.Field} field App state field to check if
     *     set.
     * @return {boolean} Whether a field has been set in the app state.
     */
    hasField: function(field) {
      return this.state_.hasOwnProperty(field);
    },

    /**
     * @param {!print_preview.AppState.Field} field App state field to get.
     * @return {?} Value of the app state field.
     */
    getField: function(field) {
      if (field == AppState.Field.CUSTOM_MARGINS) {
        return this.state_[field] ?
            print_preview.Margins.parse(this.state_[field]) : null;
      } else {
        return this.state_[field];
      }
    },

    /**
     * Initializes the app state from a serialized string returned by the native
     * layer.
     * @param {?string} serializedAppStateStr Serialized string representation
     *     of the app state.
     */
    init: function(serializedAppStateStr) {
      if (serializedAppStateStr) {
        try {
          var state = JSON.parse(serializedAppStateStr);
          if (state[AppState.Field.VERSION] == AppState.VERSION_) {
            this.state_ = state;
          }
        } catch(e) {
          console.error('Unable to parse state: ' + e);
          // Proceed with default state.
        }
      } else {
        // Set some state defaults.
        this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false;
        this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
      }
      if (!this.state_[AppState.Field.RECENT_DESTINATIONS]) {
        this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
      } else if (!(this.state_[AppState.Field.RECENT_DESTINATIONS] instanceof
            Array)) {
        var tmp = this.state_[AppState.Field.RECENT_DESTINATIONS];
        this.state_[AppState.Field.RECENT_DESTINATIONS] = [tmp];
      } else if (!this.state_[AppState.Field.RECENT_DESTINATIONS][0] ||
          !this.state_[AppState.Field.RECENT_DESTINATIONS][0].id) {
        // read in incorrectly
        this.state_[AppState.Field.RECENT_DESTINATIONS] = [];
      } else if (this.state_[AppState.Field.RECENT_DESTINATIONS].length >
          AppState.NUM_DESTINATIONS_) {
        this.state_[AppState.Field.RECENT_DESTINATIONS].length =
            AppState.NUM_DESTINATIONS_;
      }
      if (!loadTimeData.getBoolean('scalingEnabled'))
        this.state_[AppState.Field.SCALING] = 100;

    },

    /**
     * Sets to initialized state. Now object will accept persist requests.
     */
    setInitialized: function() {
      this.isInitialized_ = true;
    },

    /**
     * Persists the given value for the given field.
     * @param {!print_preview.AppState.Field} field Field to persist.
     * @param {?} value Value of field to persist.
     */
    persistField: function(field, value) {
      if (!this.isInitialized_)
        return;
      if (field == AppState.Field.CUSTOM_MARGINS) {
        this.state_[field] = value ? value.serialize() : null;
      } else {
        this.state_[field] = value;
      }
      this.persist_();
    },

    /**
     * Persists the selected destination.
     * @param {!print_preview.Destination} dest Destination to persist.
     */
    persistSelectedDestination: function(dest) {
      if (!this.isInitialized_)
        return;

      // Determine if this destination is already in the recent destinations,
      // and where in the array it is located.
      var newDestination = new RecentDestination(dest);
      var indexFound = this.state_[
          AppState.Field.RECENT_DESTINATIONS].findIndex(function(recent) {
            return (newDestination.id == recent.id &&
                    newDestination.origin == recent.origin);
          });

      // No change
      if (indexFound == 0) {
        this.persist_();
        return;
      }

      // Shift the array so that the nth most recent destination is located at
      // index n.
      if (indexFound == -1 &&
          this.state_[AppState.Field.RECENT_DESTINATIONS].length ==
          AppState.NUM_DESTINATIONS_) {
        indexFound = AppState.NUM_DESTINATIONS_ - 1;
      }
      if (indexFound != -1)
        this.state_[AppState.Field.RECENT_DESTINATIONS].splice(indexFound, 1);

      // Add the most recent destination
      this.state_[AppState.Field.RECENT_DESTINATIONS].splice(
          0, 0, newDestination);

      this.persist_();
    },

    /**
     * Persists whether the GCP promotion has been dismissed.
     * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been
     *     dismissed.
     */
    persistIsGcpPromoDismissed: function(isGcpPromoDismissed) {
      if (!this.isInitialized_)
        return;
      this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed;
      this.persist_();
    },

    /**
     * Calls into the native layer to persist the application state.
     * @private
     */
    persist_: function() {
      chrome.send(AppState.NATIVE_FUNCTION_NAME_,
                  [JSON.stringify(this.state_)]);
    }
  };

  return {
    AppState: AppState
  };
});