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.
7 * Class handling saving and restoring of per-host options.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
16 remoting.HostSettings = {};
19 * Load the settings for the specified host. Settings are returned as a
20 * dictionary of (name, value) pairs.
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.
28 remoting.HostSettings.load = function(hostId, callback) {
30 * @param {Object} requestedHost
31 * @param {Object} allHosts
32 * @return {void} Nothing.
34 var onDone = function(requestedHost, allHosts) {
35 callback(requestedHost);
37 remoting.HostSettings.loadInternal_(hostId, onDone);
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.
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.
50 remoting.HostSettings.save = function(hostId, options, opt_callback) {
52 * @param {Object} requestedHost
53 * @param {Object} allHosts
54 * @return {void} Nothing.
56 var onDone = function(requestedHost, allHosts) {
57 for (var option in options) {
58 requestedHost[option] = options[option];
60 allHosts[hostId] = requestedHost;
62 newSettings[remoting.HostSettings.KEY_] = JSON.stringify(allHosts);
63 chrome.storage.local.set(newSettings, opt_callback);
65 remoting.HostSettings.loadInternal_(hostId, onDone);
69 * Helper function for both load and save.
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.
76 remoting.HostSettings.loadInternal_ = function(hostId, callback) {
78 * @param {Object<string>} allHosts The current options for all hosts.
79 * @return {void} Nothing.
81 var onDone = function(allHosts) {
84 var hosts = allHosts[remoting.HostSettings.KEY_];
86 result = base.jsonParseSafe(hosts);
87 if (typeof(result) != 'object') {
88 console.error("Error loading host settings: Not an object");
90 } else if (/** @type {Object} */ (result).hasOwnProperty(hostId) &&
91 typeof(result[hostId]) == 'object') {
92 callback(result[hostId], result);
96 } catch (/** @type {*} */ err) {
97 console.error('Error loading host settings:', err);
99 callback({}, /** @type {Object} */ (result));
101 chrome.storage.local.get(remoting.HostSettings.KEY_, onDone);
104 /** @type {string} @private */
105 remoting.HostSettings.KEY_ = 'remoting-host-options';