15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)cr.define('print_preview', function() {
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  'use strict';
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /**
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * A data store that stores destinations and dispatches events when the data
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * store changes.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   *     destinations.
13c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch   * @param {!print_preview.UserInfo} userInfo User information repository.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @param {!print_preview.AppState} appState Application state.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @constructor
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @extends {cr.EventTarget}
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   */
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  function DestinationStore(nativeLayer, userInfo, appState) {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cr.EventTarget.call(this);
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Used to fetch local print destinations.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {!print_preview.NativeLayer}
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.nativeLayer_ = nativeLayer;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
29c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch     * User information repository.
30c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch     * @type {!print_preview.UserInfo}
31c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch     * @private
32c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch     */
33c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    this.userInfo_ = userInfo;
34c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
35c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    /**
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Used to load and persist the selected destination.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {!print_preview.AppState}
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.appState_ = appState;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * Used to track metrics.
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     * @type {!print_preview.DestinationSearchMetricsContext}
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * @private
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     */
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    this.metrics_ = new print_preview.DestinationSearchMetricsContext();
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /**
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Internal backing store for the data store.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {!Array.<!print_preview.Destination>}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.destinations_ = [];
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * Cache used for constant lookup of destinations by origin and id.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {object.<string, !print_preview.Destination>}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.destinationMap_ = {};
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Currently selected destination.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {print_preview.Destination}
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.selectedDestination_ = null;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Whether the destination store will auto select the destination that
720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * matches the last used destination stored in appState_.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {boolean}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.isInAutoSelectMode_ = false;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Event tracker used to track event listeners of the destination store.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {!EventTracker}
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.tracker_ = new EventTracker();
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * Whether PDF printer is enabled. It's disabled, for example, in App Kiosk
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * mode.
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * @type {boolean}
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * @private
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     */
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    this.pdfPrinterEnabled_ = false;
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    /**
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Used to fetch cloud-based print destinations.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {print_preview.CloudPrintInterface}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.cloudPrintInterface_ = null;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Maps user account to the list of origins for which destinations are
1020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * already loaded.
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @type {!Object.<string, Array.<print_preview.Destination.Origin>>}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
1060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    this.loadedCloudOrigins_ = {};
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * ID of a timeout after the initial destination ID is set. If no inserted
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * destination matches the initial destination ID after the specified
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * timeout, the first destination in the store will be automatically
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * selected.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {?number}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.autoSelectTimeout_ = null;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Whether a search for local destinations is in progress.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @type {boolean}
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.isLocalDestinationSearchInProgress_ = false;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * Whether the destination store has already loaded or is loading all local
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * destinations.
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @type {boolean}
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @private
130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
131f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    this.hasLoadedAllLocalDestinations_ = false;
132f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
133f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * Whether a search for privet destinations is in progress.
135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @type {boolean}
136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @private
137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    this.isPrivetDestinationSearchInProgress_ = false;
139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
140a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    /**
141a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     * Whether the destination store has already loaded or is loading all privet
142a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     * destinations.
143a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     * @type {boolean}
144a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     * @private
145a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     */
146a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    this.hasLoadedAllPrivetDestinations_ = false;
147a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /**
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * ID of a timeout after the start of a privet search to end that privet
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * search.
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * @type {?number}
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * @private
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     */
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    this.privetSearchTimeout_ = null;
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /**
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * MDNS service name of destination that we are waiting to register.
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * @type {?string}
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * @private
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     */
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    this.waitForRegisterDestination_ = null;
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.addEventListeners_();
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this.reset_();
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /**
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * Event types dispatched by the data store.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @enum {string}
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   */
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DestinationStore.EventType = {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DESTINATION_SEARCH_DONE:
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        'print_preview.DestinationStore.DESTINATION_SEARCH_DONE',
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DESTINATION_SEARCH_STARTED:
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED',
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT',
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DESTINATIONS_INSERTED:
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        'print_preview.DestinationStore.DESTINATIONS_INSERTED',
179a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    CACHED_SELECTED_DESTINATION_INFO_READY:
180a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        'print_preview.DestinationStore.CACHED_SELECTED_DESTINATION_INFO_READY',
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SELECTED_DESTINATION_CAPABILITIES_READY:
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        'print_preview.DestinationStore.SELECTED_DESTINATION_CAPABILITIES_READY'
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /**
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * Delay in milliseconds before the destination store ignores the initial
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * destination ID and just selects any printer (since the initial destination
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * was not found).
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @type {number}
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @const
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @private
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   */
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DestinationStore.AUTO_SELECT_TIMEOUT_ = 15000;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /**
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   * Amount of time spent searching for privet destination, in milliseconds.
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   * @type {number}
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   * @const
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   * @private
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   */
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DestinationStore.PRIVET_SEARCH_DURATION_ = 2000;
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /**
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)   * Localizes printer capabilities.
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)   * @param {!Object} capabilities Printer capabilities to localize.
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)   * @return {!Object} Localized capabilities.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   * @private
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   */
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DestinationStore.localizeCapabilities_ = function(capabilities) {
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    var mediaSize = capabilities.printer.media_size;
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (mediaSize) {
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      var mediaDisplayNames = {
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        'ISO_A4': 'A4',
214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        'ISO_A3': 'A3',
215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        'NA_LETTER': 'Letter',
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        'NA_LEGAL': 'Legal',
217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        'NA_LEDGER': 'Tabloid'
218cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      };
219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      for (var i = 0, media; media = mediaSize.option[i]; i++) {
220f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        media.custom_display_name =
221f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            media.custom_display_name ||
222f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            mediaDisplayNames[media.name] ||
223f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            media.name;
224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
225cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
226cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return capabilities;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DestinationStore.prototype = {
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    __proto__: cr.EventTarget.prototype,
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
2330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {string=} opt_account Account to filter destinations by. When
2340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     omitted, all destinations are returned.
2350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @return {!Array.<!print_preview.Destination>} List of destinations
2360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     accessible by the {@code account}.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
2380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    destinations: function(opt_account) {
2390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (opt_account) {
2400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return this.destinations_.filter(function(destination) {
2410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          return !destination.account || destination.account == opt_account;
2420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        });
2430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      } else {
2440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return this.destinations_.slice(0);
2450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @return {print_preview.Destination} The currently selected destination or
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *     {@code null} if none is selected.
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    get selectedDestination() {
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return this.selectedDestination_;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    /** @return {boolean} Whether destination selection is pending or not. */
257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    get isAutoSelectDestinationInProgress() {
258116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return this.selectedDestination_ == null &&
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          this.autoSelectTimeout_ != null;
260116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    },
261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @return {boolean} Whether a search for local destinations is in progress.
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    get isLocalDestinationSearchInProgress() {
266f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return this.isLocalDestinationSearchInProgress_ ||
267f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        this.isPrivetDestinationSearchInProgress_;
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @return {boolean} Whether a search for cloud destinations is in progress.
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    get isCloudDestinationSearchInProgress() {
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return this.cloudPrintInterface_ &&
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             this.cloudPrintInterface_.isCloudDestinationSearchInProgress;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Initializes the destination store. Sets the initially selected
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * destination. If any inserted destinations match this ID, that destination
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * will be automatically selected. This method must be called after the
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * print_preview.AppState has been initialized.
283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     * @param {boolean} isInAppKioskMode Whether the print preview is in App
284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch     *     Kiosk mode.
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
286116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    init: function(isInAppKioskMode) {
287116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this.pdfPrinterEnabled_ = !isInAppKioskMode;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.isInAutoSelectMode_ = true;
289116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this.createLocalPdfPrintDestination_();
2900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (!this.appState_.selectedDestinationId ||
2910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          !this.appState_.selectedDestinationOrigin) {
292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        this.selectDefaultDestination_();
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
2940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        var key = this.getDestinationKey_(
2950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            this.appState_.selectedDestinationOrigin,
2960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            this.appState_.selectedDestinationId,
2970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            this.appState_.selectedDestinationAccount);
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        var candidate = this.destinationMap_[key];
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (candidate != null) {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.selectDestination(candidate);
3010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        } else if (this.appState_.selectedDestinationOrigin ==
302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   print_preview.Destination.Origin.LOCAL) {
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.nativeLayer_.startGetLocalDestinationCapabilities(
3040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              this.appState_.selectedDestinationId);
305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else if (this.cloudPrintInterface_ &&
3060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                   (this.appState_.selectedDestinationOrigin ==
3070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        print_preview.Destination.Origin.COOKIES ||
3080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                    this.appState_.selectedDestinationOrigin ==
3090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        print_preview.Destination.Origin.DEVICE)) {
3100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.cloudPrintInterface_.printer(
3110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              this.appState_.selectedDestinationId,
3120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              this.appState_.selectedDestinationOrigin,
3130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              this.appState_.selectedDestinationAccount);
3140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        } else if (this.appState_.selectedDestinationOrigin ==
315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                   print_preview.Destination.Origin.PRIVET) {
316f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          // TODO(noamsml): Resolve a specific printer instead of listing all
317f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          // privet printers in this case.
318f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          this.nativeLayer_.startGetPrivetDestinations();
319a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
320a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          var destinationName = this.appState_.selectedDestinationName || '';
321a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
322a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          // Create a fake selectedDestination_ that is not actually in the
323a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          // destination store. When the real destination is created, this
324a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          // destination will be overwritten.
325a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          this.selectedDestination_ = new print_preview.Destination(
3260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              this.appState_.selectedDestinationId,
327a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              print_preview.Destination.Type.LOCAL,
328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              print_preview.Destination.Origin.PRIVET,
329a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              destinationName,
330a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              false /*isRecent*/,
331a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              print_preview.Destination.ConnectionStatus.ONLINE);
332a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          this.selectedDestination_.capabilities =
333a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              this.appState_.selectedDestinationCapabilities;
334a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
335a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          cr.dispatchSimpleEvent(
336a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            this,
337a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY);
338f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
339116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          this.selectDefaultDestination_();
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Sets the destination store's Google Cloud Print interface.
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *     to set.
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    setCloudPrintInterface: function(cloudPrintInterface) {
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.cloudPrintInterface_ = cloudPrintInterface;
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.cloudPrintInterface_,
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          cloudprint.CloudPrintInterface.EventType.SEARCH_DONE,
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.onCloudPrintSearchDone_.bind(this));
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.cloudPrintInterface_,
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          cloudprint.CloudPrintInterface.EventType.SEARCH_FAILED,
3580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.onCloudPrintSearchDone_.bind(this));
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.cloudPrintInterface_,
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          cloudprint.CloudPrintInterface.EventType.PRINTER_DONE,
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.onCloudPrintPrinterDone_.bind(this));
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.cloudPrintInterface_,
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED,
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.onCloudPrintPrinterFailed_.bind(this));
3671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      this.tracker_.add(
3681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          this.cloudPrintInterface_,
3691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          cloudprint.CloudPrintInterface.EventType.PROCESS_INVITE_DONE,
3701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          this.onCloudPrintProcessInviteDone_.bind(this));
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @return {boolean} Whether only default cloud destinations have been
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *     loaded.
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    hasOnlyDefaultCloudDestinations: function() {
3780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // TODO: Move the logic to print_preview.
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return this.destinations_.every(function(dest) {
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return dest.isLocal ||
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            dest.id == print_preview.Destination.GooglePromotedId.DOCS ||
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            dest.id == print_preview.Destination.GooglePromotedId.FEDEX;
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      });
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
3871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @param {!print_preview.Destination} destination Destination to select.
3881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selectDestination: function(destination) {
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.isInAutoSelectMode_ = false;
391116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // When auto select expires, DESTINATION_SELECT event has to be dispatched
392116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // anyway (see isAutoSelectDestinationInProgress() logic).
393116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (this.autoSelectTimeout_) {
394116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        clearTimeout(this.autoSelectTimeout_);
395116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        this.autoSelectTimeout_ = null;
396116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      } else if (destination == this.selectedDestination_) {
3970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return;
3980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
3990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (destination == null) {
4000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.selectedDestination_ = null;
4010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        cr.dispatchSimpleEvent(
4020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            this, DestinationStore.EventType.DESTINATION_SELECT);
4030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return;
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // Update and persist selected destination.
4060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      this.selectedDestination_ = destination;
4070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      this.selectedDestination_.isRecent = true;
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX &&
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          !destination.isTosAccepted) {
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assert(this.cloudPrintInterface_ != null,
4110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch               'Selected FedEx destination, but GCP API is not available');
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        destination.isTosAccepted = true;
4130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.cloudPrintInterface_.updatePrinterTosAcceptance(destination, true);
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.appState_.persistSelectedDestination(this.selectedDestination_);
4160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // Adjust metrics.
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (destination.cloudID &&
4180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.destinations_.some(function(otherDestination) {
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            return otherDestination.cloudID == destination.cloudID &&
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                otherDestination != destination;
4210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          })) {
4225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        this.metrics_.record(destination.isPrivet ?
4235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            print_preview.Metrics.DestinationSearchBucket.
4245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                PRIVET_DUPLICATE_SELECTED :
4255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            print_preview.Metrics.DestinationSearchBucket.
4265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                CLOUD_DUPLICATE_SELECTED);
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
4280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // Notify about selected destination change.
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cr.dispatchSimpleEvent(
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this, DestinationStore.EventType.DESTINATION_SELECT);
4310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // Request destination capabilities, of not known yet.
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (destination.capabilities == null) {
433f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        if (destination.isPrivet) {
434f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          this.nativeLayer_.startGetPrivetDestinationCapabilities(
435f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)              destination.id);
436f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
437f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        else if (destination.isLocal) {
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.nativeLayer_.startGetLocalDestinationCapabilities(
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              destination.id);
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        } else {
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          assert(this.cloudPrintInterface_ != null,
4420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 'Cloud destination selected, but GCP is not enabled');
4430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.cloudPrintInterface_.printer(
4440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              destination.id, destination.origin, destination.account);
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        cr.dispatchSimpleEvent(
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            this,
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY);
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
4540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Selects 'Save to PDF' destination (since it always exists).
4550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @private
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
4570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    selectDefaultDestination_: function() {
458116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      var saveToPdfKey = this.getDestinationKey_(
4590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          print_preview.Destination.Origin.LOCAL,
4600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
461116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          '');
462116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this.selectDestination(
463116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          this.destinationMap_[saveToPdfKey] || this.destinations_[0] || null);
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /** Initiates loading of local print destinations. */
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    startLoadLocalDestinations: function() {
468f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (!this.hasLoadedAllLocalDestinations_) {
469f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        this.hasLoadedAllLocalDestinations_ = true;
470f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        this.nativeLayer_.startGetLocalDestinations();
471f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        this.isLocalDestinationSearchInProgress_ = true;
472f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        cr.dispatchSimpleEvent(
473f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
474f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      }
475f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
476f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
477f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /** Initiates loading of privet print destinations. */
478f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    startLoadPrivetDestinations: function() {
479a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (!this.hasLoadedAllPrivetDestinations_) {
480a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        this.isPrivetDestinationSearchInProgress_ = true;
481a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        this.nativeLayer_.startGetPrivetDestinations();
482a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        cr.dispatchSimpleEvent(
483a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        this.privetSearchTimeout_ = setTimeout(
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            this.endPrivetPrinterSearch_.bind(this),
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            DestinationStore.PRIVET_SEARCH_DURATION_);
487a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      }
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /**
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     * Initiates loading of cloud destinations.
492c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch     * @param {print_preview.Destination.Origin=} opt_origin Search destinations
493c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch     *     for the specified origin only.
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
495c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    startLoadCloudDestinations: function(opt_origin) {
4960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (this.cloudPrintInterface_ != null) {
4970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || [];
4980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (origins.length == 0 ||
4990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            (opt_origin && origins.indexOf(opt_origin) < 0)) {
5000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.cloudPrintInterface_.search(
5010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              this.userInfo_.activeUser, opt_origin);
5020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          cr.dispatchSimpleEvent(
5030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
5040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
5050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
5060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    },
5070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /** Requests load of COOKIE based cloud destinations. */
5090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    reloadUserCookieBasedDestinations: function() {
5100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || [];
5110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (origins.indexOf(print_preview.Destination.Origin.COOKIES) >= 0) {
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        cr.dispatchSimpleEvent(
5130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
5140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      } else {
5150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.startLoadCloudDestinations(
5160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            print_preview.Destination.Origin.COOKIES);
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /** Initiates loading of all known destination types. */
5211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    startLoadAllDestinations: function() {
5221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      this.startLoadCloudDestinations();
5231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      this.startLoadLocalDestinations();
5241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      this.startLoadPrivetDestinations();
5251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    },
5261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
5285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * Wait for a privet device to be registered.
5295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     */
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    waitForRegister: function(id) {
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.nativeLayer_.startGetPrivetDestinations();
5325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.waitForRegisterDestination_ = id;
5335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    },
5345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /**
5360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Inserts {@code destination} to the data store and dispatches a
5370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * DESTINATIONS_INSERTED event.
5380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {!print_preview.Destination} destination Print destination to
5390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     insert.
5400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @private
5410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
5420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    insertDestination_: function(destination) {
5430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (this.insertIntoStore_(destination)) {
5440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.destinationsInserted_(destination);
5450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
5460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    },
5470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
5490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Inserts multiple {@code destinations} to the data store and dispatches
5500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * single DESTINATIONS_INSERTED event.
5510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {!Array.<print_preview.Destination>} destinations Print
5520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     destinations to insert.
5530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @private
5540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
5550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    insertDestinations_: function(destinations) {
5560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      var inserted = false;
5570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      destinations.forEach(function(destination) {
5580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        inserted = this.insertIntoStore_(destination) || inserted;
5590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }, this);
5600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (inserted) {
5610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.destinationsInserted_();
5620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
5630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    },
5640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
5660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to
5670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * update selected destination to match {@code appState_} settings.
5680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {print_preview.Destination=} opt_destination The only destination
5690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     that was changed or skipped if possibly more than one destination was
5700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     changed. Used as a hint to limit destination search scope in
5710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     {@code isInAutoSelectMode_).
5720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
5730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    destinationsInserted_: function(opt_destination) {
5740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      cr.dispatchSimpleEvent(
5750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this, DestinationStore.EventType.DESTINATIONS_INSERTED);
5760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (this.isInAutoSelectMode_) {
5770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        var destinationsToSearch =
5780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            opt_destination && [opt_destination] || this.destinations_;
5790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        destinationsToSearch.some(function(destination) {
5800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          if (this.matchPersistedDestination_(destination)) {
5810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            this.selectDestination(destination);
5820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            return true;
5830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          }
5840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }, this);
5850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
5860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    },
5870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
5890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Updates an existing print destination with capabilities and display name
5900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * information. If the destination doesn't already exist, it will be added.
5910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {!print_preview.Destination} destination Destination to update.
5920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @return {!print_preview.Destination} The existing destination that was
5930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     updated or {@code null} if it was the new destination.
5940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @private
5950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
5960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    updateDestination_: function(destination) {
5970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      assert(destination.constructor !== Array, 'Single printer expected');
5980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      var existingDestination = this.destinationMap_[this.getKey_(destination)];
5990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (existingDestination != null) {
6000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        existingDestination.capabilities = destination.capabilities;
6010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      } else {
6020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.insertDestination_(destination);
6030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
6040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (existingDestination == this.selectedDestination_ ||
6060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          destination == this.selectedDestination_) {
6070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.appState_.persistSelectedDestination(this.selectedDestination_);
6080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        cr.dispatchSimpleEvent(
6090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            this,
6100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY);
6110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
6120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return existingDestination;
6140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    },
6150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
6175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * Called when the search for Privet printers is done.
6185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     * @private
6195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     */
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    endPrivetPrinterSearch_: function() {
6215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.nativeLayer_.stopGetPrivetDestinations();
6225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.isPrivetDestinationSearchInProgress_ = false;
6235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.hasLoadedAllPrivetDestinations_ = true;
6245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      cr.dispatchSimpleEvent(
6255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
6265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    },
6275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /**
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Inserts a destination into the store without dispatching any events.
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @return {boolean} Whether the inserted destination was not already in the
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *     store.
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
6340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    insertIntoStore_: function(destination) {
6350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      var key = this.getKey_(destination);
636c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      var existingDestination = this.destinationMap_[key];
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (existingDestination == null) {
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        this.destinations_.push(destination);
639c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        this.destinationMap_[key] = destination;
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return true;
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else if (existingDestination.connectionStatus ==
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     print_preview.Destination.ConnectionStatus.UNKNOWN &&
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 destination.connectionStatus !=
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     print_preview.Destination.ConnectionStatus.UNKNOWN) {
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        existingDestination.connectionStatus = destination.connectionStatus;
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return true;
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Binds handlers to events.
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    addEventListeners_: function() {
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.nativeLayer_,
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET,
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.onLocalDestinationsSet_.bind(this));
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.nativeLayer_,
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          print_preview.NativeLayer.EventType.CAPABILITIES_SET,
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.onLocalDestinationCapabilitiesSet_.bind(this));
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.nativeLayer_,
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL,
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.onGetCapabilitiesFail_.bind(this));
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.tracker_.add(
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.nativeLayer_,
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD,
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.onDestinationsReload_.bind(this));
673f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      this.tracker_.add(
674f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          this.nativeLayer_,
675f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          print_preview.NativeLayer.EventType.PRIVET_PRINTER_CHANGED,
676f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          this.onPrivetPrinterAdded_.bind(this));
677f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      this.tracker_.add(
678f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          this.nativeLayer_,
679f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET,
680f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          this.onPrivetCapabilitiesSet_.bind(this));
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
684cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * Creates a local PDF print destination.
685cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @return {!print_preview.Destination} Created print destination.
686cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     * @private
687cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)     */
688cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    createLocalPdfPrintDestination_: function() {
689116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // TODO(alekseys): Create PDF printer in the native code and send its
690116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // capabilities back with other local printers.
691116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (this.pdfPrinterEnabled_) {
692116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        this.insertDestination_(new print_preview.Destination(
693116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
694116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            print_preview.Destination.Type.LOCAL,
695116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            print_preview.Destination.Origin.LOCAL,
6961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            loadTimeData.getString('printToPDF'),
697116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            false /*isRecent*/,
698116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            print_preview.Destination.ConnectionStatus.ONLINE));
699116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
700cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    },
701cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
702cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    /**
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Resets the state of the destination store to its initial state.
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reset_: function() {
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.destinations_ = [];
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.destinationMap_ = {};
7090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      this.selectDestination(null);
7100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      this.loadedCloudOrigins_ = {};
711f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      this.hasLoadedAllLocalDestinations_ = false;
712c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
713116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      clearTimeout(this.autoSelectTimeout_);
714116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this.autoSelectTimeout_ = setTimeout(
715116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          this.selectDefaultDestination_.bind(this),
716116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          DestinationStore.AUTO_SELECT_TIMEOUT_);
717c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    },
718c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
719c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    /**
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Called when the local destinations have been got from the native layer.
7211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @param {Event} event Contains the local destinations.
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    onLocalDestinationsSet_: function(event) {
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      var localDestinations = event.destinationInfos.map(function(destInfo) {
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return print_preview.LocalDestinationParser.parse(destInfo);
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      });
7280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      this.insertDestinations_(localDestinations);
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.isLocalDestinationSearchInProgress_ = false;
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cr.dispatchSimpleEvent(
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Called when the native layer retrieves the capabilities for the selected
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * local destination. Updates the destination with new capabilities if the
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * destination already exists, otherwise it creates a new destination and
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * then updates its capabilities.
7394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     * @param {Event} event Contains the capabilities of the local print
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *     destination.
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    onLocalDestinationCapabilitiesSet_: function(event) {
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      var destinationId = event.settingsInfo['printerId'];
7450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      var key = this.getDestinationKey_(
7460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          print_preview.Destination.Origin.LOCAL,
7470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          destinationId,
7480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          '');
749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      var destination = this.destinationMap_[key];
75046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      var capabilities = DestinationStore.localizeCapabilities_(
75146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          event.settingsInfo.capabilities);
752cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // Special case for PDF printer (until local printers capabilities are
753cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // reported in CDD format too).
754cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      if (destinationId ==
755cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) {
756cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (destination) {
75746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          destination.capabilities = capabilities;
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
760cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (destination) {
761cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          // In case there were multiple capabilities request for this local
762cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          // destination, just ignore the later ones.
763cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          if (destination.capabilities != null) {
764cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            return;
765cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          }
766cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          destination.capabilities = capabilities;
767cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        } else {
768cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          // TODO(rltoscano): This makes the assumption that the "deviceName" is
769cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          // the same as "printerName". We should include the "printerName" in
770cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          // the response. See http://crbug.com/132831.
771cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          destination = print_preview.LocalDestinationParser.parse(
772cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              {deviceName: destinationId, printerName: destinationId});
773cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          destination.capabilities = capabilities;
774cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          this.insertDestination_(destination);
775cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        }
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (this.selectedDestination_ &&
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this.selectedDestination_.id == destinationId) {
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        cr.dispatchSimpleEvent(this,
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               DestinationStore.EventType.
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   SELECTED_DESTINATION_CAPABILITIES_READY);
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Called when a request to get a local destination's print capabilities
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * fails. If the destination is the initial destination, auto-select another
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * destination instead.
7894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     * @param {Event} event Contains the destination ID that failed.
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    onGetCapabilitiesFail_: function(event) {
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      console.error('Failed to get print capabilities for printer ' +
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    event.destinationId);
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (this.isInAutoSelectMode_ &&
7960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.sameAsPersistedDestination_(event.destinationId,
7970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                           event.destinationOrigin)) {
7980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.selectDefaultDestination_();
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
8030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Called when the /search call completes, either successfully or not.
8040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * In case of success, stores fetched destinations.
8050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {Event} event Contains the request result.
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    onCloudPrintSearchDone_: function(event) {
8090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (event.printers) {
8100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.insertDestinations_(event.printers);
8110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
8120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (event.searchDone) {
8130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        var origins = this.loadedCloudOrigins_[event.user] || [];
8140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (origins.indexOf(event.origin) < 0) {
8150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.loadedCloudOrigins_[event.user] = origins.concat([event.origin]);
8160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        }
8170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cr.dispatchSimpleEvent(
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Called when /printer call completes. Updates the specified destination's
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * print capabilities.
8254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     * @param {Event} event Contains detailed information about the
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *     destination.
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    onCloudPrintPrinterDone_: function(event) {
8300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      this.updateDestination_(event.printer);
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Called when the Google Cloud Print interface fails to lookup a
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * destination. Selects another destination if the failed destination was
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * the initial destination.
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @param {object} event Contains the ID of the destination that was failed
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *     to be looked up.
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    onCloudPrintPrinterFailed_: function(event) {
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (this.isInAutoSelectMode_ &&
8430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          this.sameAsPersistedDestination_(event.destinationId,
8440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                           event.destinationOrigin)) {
8450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        console.error(
8460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            'Failed to fetch last used printer caps: ' + event.destinationId);
8470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.selectDefaultDestination_();
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /**
8521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * Called when printer sharing invitation was processed successfully.
8531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @param {Event} event Contains detailed information about the invite and
8541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     *     newly accepted destination (if known).
8551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @private
8561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
8571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    onCloudPrintProcessInviteDone_: function(event) {
8581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      if (event.accept && event.printer) {
8591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        // Hint the destination list to promote this new destination.
8601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        event.printer.isRecent = true;
8611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        this.insertDestination_(event.printer);
8621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      }
8631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    },
8641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
8651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
866f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * Called when a Privet printer is added to the local network.
867f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @param {object} event Contains information about the added printer.
868f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @private
869f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
870f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    onPrivetPrinterAdded_: function(event) {
8715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (event.printer.serviceName == this.waitForRegisterDestination_ &&
8725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          !event.printer.isUnregistered) {
8735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        this.waitForRegisterDestination_ = null;
8745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        this.onDestinationsReload_();
8755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      } else {
8760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.insertDestinations_(
877a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            print_preview.PrivetDestinationParser.parse(event.printer));
8785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
879f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
880f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
881f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
882f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * Called when capabilities for a privet printer are set.
883f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @param {object} event Contains the capabilities and printer ID.
884f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @private
885f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
886f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    onPrivetCapabilitiesSet_: function(event) {
887f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      var destinationId = event.printerId;
8885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      var destinations =
8895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          print_preview.PrivetDestinationParser.parse(event.printer);
8905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      destinations.forEach(function(dest) {
8915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        dest.capabilities = event.capabilities;
8920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        this.updateDestination_(dest);
8935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }, this);
894f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
895f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
896f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * Called from native layer after the user was requested to sign in, and did
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * so successfully.
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * @private
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    onDestinationsReload_: function() {
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.reset_();
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this.isInAutoSelectMode_ = true;
904116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this.createLocalPdfPrintDestination_();
9051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      this.startLoadAllDestinations();
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
908c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(vitalybuka): Remove three next functions replacing Destination.id
909c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    //    and Destination.origin by complex ID.
910c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /**
911c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * Returns key to be used with {@code destinationMap_}.
912c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * @param {!print_preview.Destination.Origin} origin Destination origin.
9130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @return {string} id Destination id.
9140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @return {string} account User account destination is registered for.
915c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * @private
916c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     */
9170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    getDestinationKey_: function(origin, id, account) {
9180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return origin + '/' + id + '/' + account;
919c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
920c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
921c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /**
9220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * Returns key to be used with {@code destinationMap_}.
9230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {!print_preview.Destination} destination Destination.
9240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @private
9250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     */
9260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    getKey_: function(destination) {
9270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return this.getDestinationKey_(
9280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          destination.origin, destination.id, destination.account);
9290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    },
9300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
9310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    /**
9320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @param {!print_preview.Destination} destination Destination to match.
9330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     * @return {boolean} Whether {@code destination} matches the last user
9340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     *     selected one.
935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * @private
936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     */
9370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    matchPersistedDestination_: function(destination) {
9380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return !this.appState_.selectedDestinationId ||
9390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch             !this.appState_.selectedDestinationOrigin ||
9400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch             this.sameAsPersistedDestination_(
9410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 destination.id, destination.origin);
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
944c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /**
945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * @param {?string} id Id of the destination.
946c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * @param {?string} origin Oring of the destination.
947c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * @return {boolean} Whether destination is the same as initial.
948c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     * @private
949c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)     */
9500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    sameAsPersistedDestination_: function(id, origin) {
9510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return id == this.appState_.selectedDestinationId &&
9520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch             origin == this.appState_.selectedDestinationOrigin;
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Export
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return {
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DestinationStore: DestinationStore
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)});
961