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. | |
|
xiyuan
2014/05/21 21:22:28
nit: Convert this to a @fileoverview jsdoc.
stevenjb
2014/05/23 21:50:59
Done.
| |
| 11 | |
| 12 var networkConfig = { | |
| 13 /** | |
| 14 * Return the property associated with a key (which may reference a | |
| 15 * sub-object). | |
| 16 * | |
| 17 * @param {Object} properties The object containing the network properties | |
|
xiyuan
2014/05/21 21:22:28
nit: end with a '.'
stevenjb
2014/05/23 21:50:59
Done.
| |
| 18 * @param {string} key The ONC key for the property | |
|
pneubeck (no reviews)
2014/05/22 16:24:20
nit: mention an example compound key like 'WiFi.Si
stevenjb
2014/05/23 21:50:59
Done.
| |
| 19 * @return {Mixed} The value associated with the property | |
|
xiyuan
2014/05/21 21:22:28
nit: Mixed is not a proper type name. Is it more l
stevenjb
2014/05/23 21:50:59
I copied this from somewhere else. It could be {(u
xiyuan
2014/05/23 22:12:19
In this case, let's use {*}.
stevenjb
2014/05/27 15:58:35
Done.
| |
| 20 */ | |
| 21 getValueFromProperties: function(properties, key) { | |
| 22 if (!key) { | |
| 23 console.error('Empty key'); | |
| 24 return undefined; | |
| 25 } | |
| 26 var dot = key.indexOf('.'); | |
| 27 if (dot > 0) { | |
| 28 var key1 = key.substring(0, dot); | |
| 29 var key2 = key.substring(dot + 1); | |
| 30 var subobject = properties[key1]; | |
| 31 if (subobject) | |
| 32 return this.getValueFromProperties(subobject, key2); | |
| 33 } | |
| 34 return properties[key]; | |
|
pneubeck (no reviews)
2014/05/22 16:24:20
IIRC you wanted to have some error logging / throw
stevenjb
2014/05/23 21:50:59
I was originally trying to enforce valid ONC names
| |
| 35 }, | |
| 36 | |
| 37 /** | |
| 38 * Generate a unique id for 'callback' and store it for future retrieval. | |
| 39 * | |
| 40 * @param {function} callback The associated callback | |
| 41 * @return {integer} The id of the callback | |
| 42 */ | |
| 43 callbackId: 1, | |
| 44 callbackMap: {}, | |
| 45 storeCallback: function(callback) { | |
|
pneubeck (no reviews)
2014/05/22 16:24:20
(this could also be resolved in a follow up)
this
stevenjb
2014/05/23 21:50:59
Renamed _, added @private.
Not sure what you mean
pneubeck (no reviews)
2014/05/24 13:38:53
Yes, it can keep state, because the argument to cr
stevenjb
2014/05/27 15:58:35
I'm still not entirely clear on how this pattern w
| |
| 46 var id = networkConfig.callbackId++; | |
|
xiyuan
2014/05/21 21:22:28
nit: networkConfig -> this here and other places.
stevenjb
2014/05/23 21:50:59
Done.
| |
| 47 networkConfig.callbackMap[id] = callback; | |
| 48 return id; | |
| 49 }, | |
| 50 | |
| 51 /** | |
| 52 * Retrieve the callback associated with |id| and remove it from the map. | |
| 53 * | |
| 54 * @param {integer} id The id of the callback | |
| 55 * @return {function} The associated callback | |
| 56 */ | |
| 57 retrieveCallback: function(id) { | |
| 58 var callback = networkConfig.callbackMap[id]; | |
| 59 delete networkConfig.callbackMap[id]; | |
| 60 return callback; | |
| 61 }, | |
| 62 | |
| 63 /** | |
| 64 * Callback invoked by Chrome | |
| 65 * | |
| 66 * @param {string} args A list of arguments passed to the callback. The first | |
|
xiyuan
2014/05/21 21:22:28
nit: string -> Array
stevenjb
2014/05/23 21:50:59
Done.
| |
| 67 * entry must be the callbackId passed to chrome.send. | |
| 68 */ | |
| 69 chromeCallbackSuccess: function(args) { | |
| 70 var callbackId = args.shift(); | |
| 71 var callback = networkConfig.retrieveCallback(callbackId); | |
| 72 networkConfig.lastError = ''; | |
| 73 if (callback) | |
| 74 callback.apply(null, args); | |
| 75 else | |
| 76 console.error('Callback not found for id: ' + callbackId); | |
| 77 }, | |
| 78 | |
| 79 /** | |
| 80 * Error Callback invoked by Chrome. Sets lastError and logs to the console. | |
| 81 * | |
| 82 * @param {string} args A list of arguments. The first entry must be the | |
|
xiyuan
2014/05/21 21:22:28
nit: string -> Array
stevenjb
2014/05/23 21:50:59
Done.
| |
| 83 * callbackId passed to chrome.send. | |
| 84 */ | |
| 85 lastError: '', | |
| 86 chromeCallbackError: function(args) { | |
| 87 var callbackId = args.shift(); | |
| 88 networkConfig.lastError = args.shift(); | |
| 89 console.error('Callback error: "' + networkConfig.lastError); | |
| 90 // We still invoke the callback, but with null args. The callback should | |
| 91 // check networkConfig.lastError and handle that. | |
| 92 var callback = networkConfig.retrieveCallback(callbackId); | |
| 93 if (callback) | |
| 94 callback.apply(null, null); | |
| 95 }, | |
| 96 | |
| 97 /** | |
| 98 * Implement networkingPrivate.getProperties | |
| 99 * | |
| 100 * @param {string} type The type of networks to return | |
|
xiyuan
2014/05/21 21:22:28
This document needs to be updated.
stevenjb
2014/05/23 21:50:59
Done.
| |
| 101 */ | |
| 102 getProperties: function(guid, callback) { | |
| 103 var callbackId = networkConfig.storeCallback(callback); | |
| 104 chrome.send('networkConfig.getProperties', [callbackId, guid]); | |
| 105 }, | |
| 106 | |
| 107 /** | |
| 108 * Implement networkingPrivate.getNetworks | |
| 109 * | |
| 110 * @param {string} type The type of networks to return | |
|
xiyuan
2014/05/21 21:22:28
This document needs to be updated.
stevenjb
2014/05/23 21:50:59
Done.
| |
| 111 */ | |
| 112 getNetworks: function(filter, callback) { | |
| 113 var callbackId = networkConfig.storeCallback(callback); | |
| 114 chrome.send('networkConfig.getNetworks', [callbackId, filter]); | |
| 115 } | |
| 116 | |
|
xiyuan
2014/05/21 21:22:28
nit: nuke empty line
stevenjb
2014/05/23 21:50:59
Done.
| |
| 117 }; | |
| OLD | NEW |