1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview
7 * Class handling saving and restoring of per-host options.
8 */
9
10'use strict';
11
12/** @suppress {duplicate} */
13var remoting = remoting || {};
14
15/** @type {Object} */
16remoting.HostSettings = {};
17
18/**
19 * Load the settings for the specified host. Settings are returned as a
20 * dictionary of (name, value) pairs.
21 *
22 * @param {string} hostId The host identifer for which to load options.
23 * @param {function(Object):void} callback Callback to which the
24 *     current settings for the host are passed. If there are no settings,
25 *     then an empty dictionary is passed.
26 * @return {void} Nothing.
27 */
28remoting.HostSettings.load = function(hostId, callback) {
29  /**
30   * @param {Object} requestedHost
31   * @param {Object} allHosts
32   * @return {void} Nothing.
33   */
34  var onDone = function(requestedHost, allHosts) {
35    callback(requestedHost);
36  };
37  remoting.HostSettings.loadInternal_(hostId, onDone);
38};
39
40/**
41 * Save the settings for the specified hosts. Existing settings with the same
42 * names will be overwritten; settings not currently saved will be created.
43 *
44 * @param {string} hostId The host identifer for which to save options.
45 * @param {Object} options The options to save, expressed as a dictionary of
46 *     (name, value) pairs.
47 * @param {function():void=} opt_callback Optional completion callback.
48 * @return {void} Nothing.
49 */
50remoting.HostSettings.save = function(hostId, options, opt_callback) {
51  /**
52   * @param {Object} requestedHost
53   * @param {Object} allHosts
54   * @return {void} Nothing.
55   */
56  var onDone = function(requestedHost, allHosts) {
57    for (var option in options) {
58      requestedHost[option] = options[option];
59    }
60    allHosts[hostId] = requestedHost;
61    var newSettings = {};
62    newSettings[remoting.HostSettings.KEY_] = JSON.stringify(allHosts);
63    chrome.storage.local.set(newSettings, opt_callback);
64  };
65  remoting.HostSettings.loadInternal_(hostId, onDone);
66};
67
68/**
69 * Helper function for both load and save.
70 *
71 * @param {string} hostId The host identifer for which to load options.
72 * @param {function(Object, Object):void} callback Callback to which the
73 *     current settings for the specified host and for all hosts are passed.
74 * @return {void} Nothing.
75 */
76remoting.HostSettings.loadInternal_ = function(hostId, callback) {
77  /**
78   * @param {Object.<string>} allHosts The current options for all hosts.
79   * @return {void} Nothing.
80   */
81  var onDone = function(allHosts) {
82    var result = {};
83    try {
84      var hosts = allHosts[remoting.HostSettings.KEY_];
85      if (hosts) {
86        result = jsonParseSafe(hosts);
87        if (typeof(result) != 'object') {
88          console.error("Error loading host settings: Not an object");
89          result = {};
90        } else if (/** @type {Object} */ (result).hasOwnProperty(hostId) &&
91                   typeof(result[hostId]) == 'object') {
92          callback(result[hostId], result);
93          return;
94        }
95      }
96    } catch (err) {
97      var typedErr = /** @type {*} */ (err);
98      console.error('Error loading host settings:', typedErr);
99    }
100    callback({}, /** @type {Object} */ (result));
101  };
102  chrome.storage.local.get(remoting.HostSettings.KEY_, onDone);
103};
104
105/** @type {string} @private */
106remoting.HostSettings.KEY_ = 'remoting-host-options';