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.
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.
16 * Return the property associated with a key (which may reference a
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.
24 getValueFromProperties: function(properties, key) {
26 console.error('Empty key');
29 var dot = key.indexOf('.');
31 var key1 = key.substring(0, dot);
32 var key2 = key.substring(dot + 1);
33 var subobject = properties[key1];
35 return this.getValueFromProperties(subobject, key2);
37 return properties[key];
41 * Generate a unique id for 'callback' and store it for future retrieval.
43 * @param {function} callback The associated callback.
44 * @return {integer} The id of the callback.
49 storeCallback_: function(callback) {
50 var id = this.callbackId++;
51 this.callbackMap[id] = callback;
56 * Retrieve the callback associated with |id| and remove it from the map.
58 * @param {integer} id The id of the callback.
59 * @return {function} The associated callback.
62 retrieveCallback_: function(id) {
63 var callback = this.callbackMap[id];
64 delete this.callbackMap[id];
69 * Callback invoked by Chrome.
71 * @param {Array} args A list of arguments passed to the callback. The first
72 * entry must be the callbackId passed to chrome.send.
74 chromeCallbackSuccess: function(args) {
75 var callbackId = args.shift();
76 var callback = this.retrieveCallback_(callbackId);
79 callback.apply(null, args);
81 console.error('Callback not found for id: ' + callbackId);
85 * Error Callback invoked by Chrome. Sets lastError and logs to the console.
87 * @param {Args} args A list of arguments. The first entry must be the
88 * callbackId passed to chrome.send.
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);
99 callback.apply(null, null);
103 * Implement networkingPrivate.getProperties. See networking_private.json.
105 * @param {string} guid The guid identifying the network.
106 * @param {function()} callback The callback to call on completion.
108 getProperties: function(guid, callback) {
109 var callbackId = this.storeCallback_(callback);
110 chrome.send('networkConfig.getProperties', [callbackId, guid]);
114 * Implement networkingPrivate.getManagedProperties. See
115 * networking_private.json.
117 * @param {string} guid The guid identifying the network.
118 * @param {function()} callback The callback to call on completion.
120 getManagedProperties: function(guid, callback) {
121 var callbackId = this.storeCallback_(callback);
122 chrome.send('networkConfig.getManagedProperties', [callbackId, guid]);
126 * Implement networkingPrivate.getNetworks. See networking_private.json.
128 * @param {string} guid The guid identifying the network.
129 * @param {function()} callback The callback to call on completion.
131 getNetworks: function(filter, callback) {
132 var callbackId = this.storeCallback_(callback);
133 chrome.send('networkConfig.getNetworks', [callbackId, filter]);
137 * Debugging method to get raw Shill properties
139 * @param {string} guid The guid identifying the network.
140 * @param {function()} callback The callback to call on completion.
142 getShillProperties: function(guid, callback) {
143 var callbackId = this.storeCallback_(callback);
144 chrome.send('networkConfig.getShillProperties', [callbackId, guid]);