• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.networking.onc
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.networking.onc.getDeviceStates(function(deviceState) {
50    // De-dupe network types by using a dictionary.
51    // TODO(stevenjb): remove the de-dupe logic here after crbug.com/679945 is
52    // fixed.
53    var networkTypes = {};
54    deviceState.forEach(function(device) {
55      if (device.State == 'Enabled')
56        networkTypes[device.Type] = true;
57    });
58    self._setResult("getEnabledNetworkDevices", Object.keys(networkTypes));
59  });
60};
61
62Networking.prototype.enableNetworkDevice = function(type) {
63  if (!this._setupFunctionCall("enableNetworkDevice"))
64    return;
65  var self = this;
66  chrome.networking.onc.enableNetworkType(type);
67};
68
69Networking.prototype.disableNetworkDevice = function(type) {
70  if (!this._setupFunctionCall("disableNetworkDevice"))
71    return;
72  var self = this;
73  chrome.networking.onc.disableNetworkType(type);
74};
75
76Networking.prototype.requestNetworkScan = function() {
77  if (!this._setupFunctionCall("requestNetworkScan"))
78    return;
79  var self = this;
80  chrome.networking.onc.requestNetworkScan();
81};
82
83Networking.prototype.createNetwork = function(shared, properties) {
84  if (!this._setupFunctionCall("createNetwork"))
85    return;
86  var self = this;
87  chrome.networking.onc.createNetwork(shared, properties, function(guid) {
88    self._setResult("createNetwork", guid);
89  });
90};
91
92Networking.prototype.setProperties = function(guid, properties) {
93  if (!this._setupFunctionCall("setProperties"))
94    return;
95  var self = this;
96  chrome.networking.onc.setProperties(guid, properties, function() {
97    self._setResult("setProperties", null);
98  });
99};
100
101Networking.prototype.findNetworks = function(type) {
102  if (!this._setupFunctionCall("findNetworks"))
103    return;
104  var self = this;
105  chrome.networking.onc.getNetworks({
106    visible: true,
107    networkType: type
108  }, function(networks) {
109    self._setResult("findNetworks", networks);
110  });
111};
112
113Networking.prototype.getNetworks = function(properties) {
114  if (!this._setupFunctionCall("getNetworks"))
115    return;
116  var self = this;
117  chrome.networking.onc.getNetworks(properties, function(networkList) {
118    self._setResult("getNetworks", networkList);
119  });
120};
121
122Networking.prototype.getNetworkInfo = function(networkId) {
123  if (!this._setupFunctionCall("getNetworkInfo"))
124    return;
125  var self = this;
126  chrome.networking.onc.getProperties(networkId, function(networkInfo) {
127    self._setResult("getNetworkInfo", networkInfo);
128  });
129};
130
131Networking.prototype.connectToNetwork = function(networkId) {
132  if (!this._setupFunctionCall("connectToNetwork"))
133    return;
134  var self = this;
135  chrome.networking.onc.startConnect(networkId, function() {
136    self._setResult("connectToNetwork", null);
137  });
138};
139
140Networking.prototype.disconnectFromNetwork = function(networkId) {
141  if (!this._setupFunctionCall("disconnectFromNetwork"))
142    return;
143  var self = this;
144  chrome.networking.onc.startDisconnect(networkId, function() {
145    self._setResult("disconnectFromNetwork", null);
146  });
147};
148
149Networking.prototype.setWifiTDLSEnabledState = function(ip_or_mac, enable) {
150  if (!this._setupFunctionCall("setWifiTDLSEnabledState"))
151    return;
152  var self = this;
153  chrome.networking.onc.setWifiTDLSEnabledState(
154      ip_or_mac, enable, function(result) {
155    self._setResult("setWifiTDLSEnabledState", result);
156  });
157};
158
159Networking.prototype.getWifiTDLSStatus = function(ip_or_mac) {
160  if (!this._setupFunctionCall("getWifiTDLSStatus"))
161    return;
162  var self = this;
163  chrome.networking.onc.getWifiTDLSStatus(ip_or_mac,
164      function(TDLSStatus) {
165    self._setResult("getWifiTDLSStatus", TDLSStatus);
166  });
167};
168
169Networking.prototype.getCaptivePortalStatus = function(networkPath) {
170  if (!this._setupFunctionCall("getCaptivePortalStatus"))
171    return;
172  var self = this;
173  chrome.networking.onc.getCaptivePortalStatus(networkPath,
174      function(CaptivePortalStatus) {
175    self._setResult("getCaptivePortalStatus", CaptivePortalStatus);
176  });
177};
178
179
180Networking.prototype.onNetworkListChanged = function() {
181  if (!this._setupFunctionCall("onNetworkListChanged"))
182    return;
183  var self = this;
184  chrome.networking.onc.onNetworkListChanged.addListener(
185      function(changes) {
186    self._setResult("onNetworkListChanged", changes);
187  });
188};
189
190Networking.prototype.onPortalDetectionCompleted = function() {
191  if (!this._setupFunctionCall("onPortalDetectionCompleted"))
192    return;
193  var self = this;
194  chrome.networking.onc.onPortalDetectionCompleted.addListener(
195      function(networkPath, state) {
196    self._setResult("onPortalDetectionCompleted", networkPath);
197  });
198};
199
200var chromeTesting = {
201  STATUS_PENDING: "chrome-test-call-status-pending",
202  STATUS_SUCCESS: "chrome-test-call-status-success",
203  STATUS_FAILURE: "chrome-test-call-status-failure",
204  networking: new Networking()
205};
206