1// Copyright 2014 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'use strict'; 6 7/** 8 * @fileoverview This object provides a similar API to chrome.networkingPrivate. 9 * It simulates the extension callback model by storing callbacks in a member 10 * object and invoking them when the corresponding method is called by Chrome in 11 * response to a chrome.send call. 12 */ 13 14var networkConfig = { 15 /** 16 * Return the property associated with a key (which may reference a 17 * sub-object). 18 * 19 * @param {Object} properties The object containing the network properties. 20 * @param {string} key The ONC key for the property. May refer to a nested 21 * propety, e.g. 'WiFi.Security'. 22 * @return {*} The value associated with the property. 23 */ 24 getValueFromProperties: function(properties, key) { 25 if (!key) { 26 console.error('Empty key'); 27 return undefined; 28 } 29 var dot = key.indexOf('.'); 30 if (dot > 0) { 31 var key1 = key.substring(0, dot); 32 var key2 = key.substring(dot + 1); 33 var subobject = properties[key1]; 34 if (subobject) 35 return this.getValueFromProperties(subobject, key2); 36 } 37 return properties[key]; 38 }, 39 40 /** 41 * Generate a unique id for 'callback' and store it for future retrieval. 42 * 43 * @param {function} callback The associated callback. 44 * @return {integer} The id of the callback. 45 * @private 46 */ 47 callbackId: 1, 48 callbackMap: {}, 49 storeCallback_: function(callback) { 50 var id = this.callbackId++; 51 this.callbackMap[id] = callback; 52 return id; 53 }, 54 55 /** 56 * Retrieve the callback associated with |id| and remove it from the map. 57 * 58 * @param {integer} id The id of the callback. 59 * @return {function} The associated callback. 60 * @private 61 */ 62 retrieveCallback_: function(id) { 63 var callback = this.callbackMap[id]; 64 delete this.callbackMap[id]; 65 return callback; 66 }, 67 68 /** 69 * Callback invoked by Chrome. 70 * 71 * @param {Array} args A list of arguments passed to the callback. The first 72 * entry must be the callbackId passed to chrome.send. 73 */ 74 chromeCallbackSuccess: function(args) { 75 var callbackId = args.shift(); 76 var callback = this.retrieveCallback_(callbackId); 77 this.lastError = ''; 78 if (callback) 79 callback.apply(null, args); 80 else 81 console.error('Callback not found for id: ' + callbackId); 82 }, 83 84 /** 85 * Error Callback invoked by Chrome. Sets lastError and logs to the console. 86 * 87 * @param {Args} args A list of arguments. The first entry must be the 88 * callbackId passed to chrome.send. 89 */ 90 lastError: '', 91 chromeCallbackError: function(args) { 92 var callbackId = args.shift(); 93 this.lastError = args.shift(); 94 console.error('Callback error: "' + this.lastError); 95 // We still invoke the callback, but with null args. The callback should 96 // check this.lastError and handle that. 97 var callback = this.retrieveCallback_(callbackId); 98 if (callback) 99 callback.apply(null, null); 100 }, 101 102 /** 103 * Implement networkingPrivate.getProperties. See networking_private.json. 104 * 105 * @param {string} guid The guid identifying the network. 106 * @param {function()} callback The callback to call on completion. 107 */ 108 getProperties: function(guid, callback) { 109 var callbackId = this.storeCallback_(callback); 110 chrome.send('networkConfig.getProperties', [callbackId, guid]); 111 }, 112 113 /** 114 * Implement networkingPrivate.getManagedProperties. See 115 * networking_private.json. 116 * 117 * @param {string} guid The guid identifying the network. 118 * @param {function()} callback The callback to call on completion. 119 */ 120 getManagedProperties: function(guid, callback) { 121 var callbackId = this.storeCallback_(callback); 122 chrome.send('networkConfig.getManagedProperties', [callbackId, guid]); 123 }, 124 125 /** 126 * Implement networkingPrivate.getNetworks. See networking_private.json. 127 * 128 * @param {string} guid The guid identifying the network. 129 * @param {function()} callback The callback to call on completion. 130 */ 131 getNetworks: function(filter, callback) { 132 var callbackId = this.storeCallback_(callback); 133 chrome.send('networkConfig.getNetworks', [callbackId, filter]); 134 }, 135 136 /** 137 * Debugging method to get raw Shill properties 138 * 139 * @param {string} guid The guid identifying the network. 140 * @param {function()} callback The callback to call on completion. 141 */ 142 getShillProperties: function(guid, callback) { 143 var callbackId = this.storeCallback_(callback); 144 chrome.send('networkConfig.getShillProperties', [callbackId, guid]); 145 }, 146 147}; 148