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} */ 13 var remoting = remoting || {}; 14 15 /** @type {Object} */ 16 remoting.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 */ 28 remoting.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 */ 50 remoting.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 */ 76 remoting.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 result = jsonParseSafe(allHosts[remoting.HostSettings.KEY_]); 85 if (typeof(result) != 'object') { 86 console.error("Error loading host settings: Not an object"); 87 result = {}; 88 } else if (/** @type {Object} */ (result).hasOwnProperty(hostId) && 89 typeof(result[hostId]) == 'object') { 90 callback(result[hostId], result); 91 return; 92 } 93 } catch (err) { 94 var typedErr = /** @type {*} */ (err); 95 console.error('Error loading host settings:', typedErr); 96 } 97 callback({}, /** @type {Object} */ (result)); 98 }; 99 chrome.storage.local.get(remoting.HostSettings.KEY_, onDone); 100 }; 101 102 /** @type {string} @private */ 103 remoting.HostSettings.KEY_ = 'remoting-host-options';