1// Copyright (c) 2013 The Chromium OS 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// chromeTesting.Networking provides wrappers around chrome.networkingPrivate 6// functions. The result of each asynchronous call can be accessed through 7// chromeTesting.networking.callStatus, which is a dictionary of the form: 8// { 9// <function name>: { 10// "status": <STATUS_PENDING|STATUS_SUCCESS|STATUS_FAILURE>, 11// "result": <Return value or null>, 12// "error": <Error message or null>, 13// }, 14// ... 15// } 16 17function Networking() { 18 this.callStatus = {}; 19} 20 21// Returns false if a call |function_name| is pending, otherwise sets up 22// the function dictionary and sets the status to STATUS_PENDING. 23Networking.prototype._setupFunctionCall = function(function_name) { 24 if (this.callStatus[function_name] == null) 25 this.callStatus[function_name] = {}; 26 if (this.callStatus[function_name].status == chromeTesting.STATUS_PENDING) 27 return false; 28 this.callStatus[function_name].status = chromeTesting.STATUS_PENDING; 29 return true; 30}; 31 32Networking.prototype._setResult = function(function_name, result_value) { 33 var error = chrome.runtime.lastError; 34 if (error) { 35 this.callStatus[function_name].status = chromeTesting.STATUS_FAILURE; 36 this.callStatus[function_name].result = null; 37 this.callStatus[function_name].error = error.message; 38 } else { 39 this.callStatus[function_name].status = chromeTesting.STATUS_SUCCESS; 40 this.callStatus[function_name].result = result_value; 41 this.callStatus[function_name].error = null; 42 } 43}; 44 45Networking.prototype.getEnabledNetworkDevices = function() { 46 if (!this._setupFunctionCall("getEnabledNetworkDevices")) 47 return; 48 var self = this; 49 chrome.networkingPrivate.getEnabledNetworkTypes(function(networkTypes) { 50 self._setResult("getEnabledNetworkDevices", networkTypes); 51 }); 52}; 53 54Networking.prototype.enableNetworkDevice = function(type) { 55 if (!this._setupFunctionCall("enableNetworkDevice")) 56 return; 57 var self = this; 58 chrome.networkingPrivate.enableNetworkType(type); 59}; 60 61Networking.prototype.disableNetworkDevice = function(type) { 62 if (!this._setupFunctionCall("disableNetworkDevice")) 63 return; 64 var self = this; 65 chrome.networkingPrivate.disableNetworkType(type); 66}; 67 68Networking.prototype.requestNetworkScan = function() { 69 if (!this._setupFunctionCall("requestNetworkScan")) 70 return; 71 var self = this; 72 chrome.networkingPrivate.requestNetworkScan(); 73}; 74 75Networking.prototype.createNetwork = function(shared, properties) { 76 if (!this._setupFunctionCall("createNetwork")) 77 return; 78 var self = this; 79 chrome.networkingPrivate.createNetwork(shared, properties, function(guid) { 80 self._setResult("createNetwork", guid); 81 }); 82}; 83 84Networking.prototype.setProperties = function(guid, properties) { 85 if (!this._setupFunctionCall("setProperties")) 86 return; 87 var self = this; 88 chrome.networkingPrivate.setProperties(guid, properties, function() { 89 self._setResult("setProperties", null); 90 }); 91}; 92 93Networking.prototype.findNetworks = function(type) { 94 if (!this._setupFunctionCall("findNetworks")) 95 return; 96 var self = this; 97 chrome.networkingPrivate.getVisibleNetworks(type, function(networks) { 98 self._setResult("findNetworks", networks); 99 }); 100}; 101 102Networking.prototype.getNetworks = function(properties) { 103 if (!this._setupFunctionCall("getNetworks")) 104 return; 105 var self = this; 106 chrome.networkingPrivate.getNetworks(properties, function(networkList) { 107 self._setResult("getNetworks", networkList); 108 }); 109}; 110 111Networking.prototype.getNetworkInfo = function(networkId) { 112 if (!this._setupFunctionCall("getNetworkInfo")) 113 return; 114 var self = this; 115 chrome.networkingPrivate.getProperties(networkId, function(networkInfo) { 116 self._setResult("getNetworkInfo", networkInfo); 117 }); 118}; 119 120Networking.prototype.connectToNetwork = function(networkId) { 121 if (!this._setupFunctionCall("connectToNetwork")) 122 return; 123 var self = this; 124 chrome.networkingPrivate.startConnect(networkId, function() { 125 self._setResult("connectToNetwork", null); 126 }); 127}; 128 129Networking.prototype.disconnectFromNetwork = function(networkId) { 130 if (!this._setupFunctionCall("disconnectFromNetwork")) 131 return; 132 var self = this; 133 chrome.networkingPrivate.startDisconnect(networkId, function() { 134 self._setResult("disconnectFromNetwork", null); 135 }); 136}; 137 138Networking.prototype.setWifiTDLSEnabledState = function(ip_or_mac, enable) { 139 if (!this._setupFunctionCall("setWifiTDLSEnabledState")) 140 return; 141 var self = this; 142 chrome.networkingPrivate.setWifiTDLSEnabledState( 143 ip_or_mac, enable, function(result) { 144 self._setResult("setWifiTDLSEnabledState", result); 145 }); 146}; 147 148Networking.prototype.getWifiTDLSStatus = function(ip_or_mac) { 149 if (!this._setupFunctionCall("getWifiTDLSStatus")) 150 return; 151 var self = this; 152 chrome.networkingPrivate.getWifiTDLSStatus(ip_or_mac, 153 function(TDLSStatus) { 154 self._setResult("getWifiTDLSStatus", TDLSStatus); 155 }); 156}; 157 158Networking.prototype.getCaptivePortalStatus = function(networkPath) { 159 if (!this._setupFunctionCall("getCaptivePortalStatus")) 160 return; 161 var self = this; 162 chrome.networkingPrivate.getCaptivePortalStatus(networkPath, 163 function(CaptivePortalStatus) { 164 self._setResult("getCaptivePortalStatus", CaptivePortalStatus); 165 }); 166}; 167 168 169Networking.prototype.onNetworkListChanged = function() { 170 if (!this._setupFunctionCall("onNetworkListChanged")) 171 return; 172 var self = this; 173 chrome.networkingPrivate.onNetworkListChanged.addListener( 174 function(changes) { 175 self._setResult("onNetworkListChanged", changes); 176 }); 177}; 178 179Networking.prototype.onPortalDetectionCompleted = function() { 180 if (!this._setupFunctionCall("onPortalDetectionCompleted")) 181 return; 182 var self = this; 183 chrome.networkingPrivate.onPortalDetectionCompleted.addListener( 184 function(networkPath, state) { 185 self._setResult("onPortalDetectionCompleted", networkPath); 186 }); 187}; 188 189var chromeTesting = { 190 STATUS_PENDING: "chrome-test-call-status-pending", 191 STATUS_SUCCESS: "chrome-test-call-status-success", 192 STATUS_FAILURE: "chrome-test-call-status-failure", 193 networking: new Networking() 194}; 195