Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 // This object provides a similar API to chrome.networkingPrivate. It simulates | |
| 8 // the extension callback model by storing callbacks in a member object and | |
| 9 // invoking them when the corresponding method is called by Chrome in response | |
| 10 // to a chrome.send call. | |
| 11 | |
| 12 var networkConfig = { | |
| 13 // ONC types | |
| 14 Type: { | |
| 15 All: 'All', | |
| 16 Cellular: 'Cellular', | |
| 17 Ethernet: 'Ethernet', | |
| 18 VPN: 'VPN', | |
| 19 WiFi: 'WiFi' | |
| 20 }, | |
| 21 | |
| 22 // ONC Network properties | |
| 23 ONC: { | |
| 24 Cellular: { | |
| 25 NetworkTechnology: 'Cellular.NetworkTechnology', | |
| 26 ActivationState: 'Cellular.ActivationState', | |
| 27 RoamingState: 'Cellular.RoamingState', | |
| 28 OutOfCredits: 'Cellular.OutOfCredits' | |
| 29 }, | |
| 30 Connectable: 'Connectable', | |
| 31 ConnectionState: 'ConnectionState', | |
| 32 EAP: { | |
| 33 EAP: 'EAP.EAP' | |
| 34 }, | |
| 35 ErrorState: 'ErrorState', | |
| 36 GUID: 'GUID', | |
| 37 Name: 'Name', | |
| 38 OncSource: 'onc_source', // TODO(stevenjb): Replace with 'Policies'. | |
| 39 Profile: 'Profile', | |
| 40 WiFi: { | |
| 41 Security: 'WiFi.Security', | |
| 42 SignalStrength: 'WiFi.SignalStrength' | |
| 43 }, | |
| 44 ServicePath: 'ServicePath', | |
| 45 Type: 'Type' | |
| 46 }, | |
| 47 | |
| 48 /** | |
| 49 * Return the property associated with a key (which may reference a | |
| 50 * sub-object). | |
| 51 * | |
| 52 * @param {Object} properties The object containing the network properties | |
| 53 * @param {string} key The ONC key for the property | |
| 54 * @return {Mixed} The value associated with the property | |
| 55 */ | |
| 56 getValueFromProperties: function(properties, key) { | |
| 57 if (!key) { | |
| 58 console.error('Empty key'); | |
| 59 return undefined; | |
| 60 } | |
| 61 var dot = key.indexOf('.'); | |
| 62 if (dot > 0) { | |
| 63 var key1 = key.substring(0, dot); | |
| 64 var key2 = key.substring(dot + 1); | |
| 65 var subobject = properties[key1]; | |
| 66 if (subobject) | |
| 67 return subobject[key2]; | |
| 68 } | |
| 69 return properties[key]; | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * Generate a unique id for 'callback' and store it for future retrieval. | |
| 74 * | |
| 75 * @param {function} callback The associated callback | |
| 76 * @return {integer} The id of the callback | |
| 77 */ | |
| 78 callbackId: 1, | |
| 79 callbackMap: {}, | |
| 80 storeCallback: function(callback) { | |
| 81 var id = networkConfig.callbackId++; | |
| 82 networkConfig.callbackMap[id] = callback; | |
| 83 return id; | |
| 84 }, | |
| 85 | |
| 86 /** | |
| 87 * Retrieve the callback associated with |id| and remove it from the map. | |
| 88 * | |
| 89 * @param {integer} id The id of the callback | |
| 90 * @return {function} The associated callback | |
| 91 */ | |
| 92 retrieveCallback: function(id) { | |
| 93 var callback = networkConfig.callbackMap[id]; | |
| 94 delete networkConfig.callbackMap[id]; | |
| 95 return callback; | |
| 96 }, | |
| 97 | |
| 98 /** | |
| 99 * Callback invoked by Chrome | |
| 100 * | |
| 101 * @param {string} data A list of arguments passed to the callback. The first | |
|
pneubeck (no reviews)
2014/05/08 09:21:43
nit: data -> args
(similarly below)
stevenjb
2014/05/21 20:22:44
Done.
| |
| 102 * entry must be the callbackId passed to chrome.send. | |
| 103 */ | |
| 104 chromeCallbackSuccess: function(data) { | |
| 105 var callbackId = data.shift(); | |
| 106 var callback = networkConfig.retrieveCallback(callbackId); | |
| 107 if (callback) | |
| 108 callback.apply(null, data); | |
| 109 else | |
| 110 console.error('Callback not found for id: ' + callbackId); | |
| 111 }, | |
| 112 | |
| 113 /** | |
| 114 * Error Callback invoked by Chrome | |
| 115 * | |
| 116 * @param {string} data A list of arguments. The first entry must be the | |
| 117 * callbackId passed to chrome.send. | |
| 118 */ | |
| 119 chromeCallbackError: function(data) { | |
| 120 var callbackId = data.shift(); | |
| 121 var error = data.shift(); | |
| 122 // Get the callback so that it is deleted from the map. | |
| 123 networkConfig.retrieveCallback(callbackId); | |
| 124 console.error('Callback error: "' + error + '" for id: ' + callbackId); | |
| 125 }, | |
| 126 | |
| 127 /** | |
| 128 * Implement networkingPrivate.getProperties | |
| 129 * | |
| 130 * @param {string} type The type of networks to return | |
| 131 */ | |
| 132 getProperties: function(guid, callback) { | |
| 133 var callbackId = networkConfig.storeCallback(callback); | |
| 134 chrome.send('networkConfig.getProperties', [callbackId, guid]); | |
| 135 }, | |
| 136 | |
| 137 /** | |
| 138 * Implement networkingPrivate.getVisibleNetworks | |
| 139 * | |
| 140 * @param {string} type The type of networks to return | |
| 141 */ | |
| 142 getVisibleNetworks: function(type, callback) { | |
| 143 var callbackId = networkConfig.storeCallback(callback); | |
| 144 chrome.send('networkConfig.getVisibleNetworks', [callbackId, type]); | |
| 145 } | |
| 146 | |
| 147 }; | |
| OLD | NEW |