Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: chrome/browser/chromeos/extensions/networking_private_api.cc

Issue 11975015: This adds a private extension API to use for simple networking (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed self review nits Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 #include "chrome/browser/chromeos/extensions/networking_private_api.h"
6
7 #include "base/bind_helpers.h"
8 #include "chrome/browser/chromeos/extensions/networking_private_api_factory.h"
9 #include "chrome/browser/extensions/extension_function_registry.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_manager_client.h"
12 #include "chromeos/dbus/shill_service_client.h"
13 #include "dbus/object_path.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
15
16 namespace chromeos {
17
18 namespace {
19
20 // These are keys used by the JavaScript code to access information in the
21 // result.
22 const char kBssid[] = "bssid";
23 const char kName[] = "name";
24 const char kNetworkId[] = "networkId";
25 const char kNetworkTypeAll[] = "all";
26 const char kStatus[] = "status";
27 const char kType[] = "type";
28
29 // This creates a new dictionary that only contains the information we're
30 // interested in passing on to JavaScript.
31 base::DictionaryValue* CreateFilteredResult(
32 const base::DictionaryValue& properties) {
33 scoped_ptr<base::DictionaryValue> filtered_result(new base::DictionaryValue);
34
35 std::string bssid;
36 if (properties.GetString(flimflam::kWifiBSsid, &bssid))
37 filtered_result->SetString(kBssid, bssid);
38 std::string name;
39 if (properties.GetString(flimflam::kNameProperty, &name)) {
mazda 2013/01/16 22:08:52 nit: braces are not needed
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
40 filtered_result->SetString(kName, name);
41 }
42 std::string type;
43 if (properties.GetString(flimflam::kTypeProperty, &type))
44 filtered_result->SetString(kType, type);
45 std::string state;
46 if (properties.GetString(flimflam::kStateProperty, &state)) {
47 if (state == flimflam::kStateReady || state == flimflam::kStateOnline) {
48 filtered_result->SetString(kStatus, "connected");
49 } else if (state == flimflam::kStateAssociation ||
50 state == flimflam::kStatePortal ||
51 state == flimflam::kStateConfiguration) {
52 filtered_result->SetString(kStatus, "connecting");
53 } else {
54 filtered_result->SetString(kStatus, "notConnected");
55 }
56 }
57
58 return filtered_result.release();
59 }
60
61 } // namespace
62
63
64 ////////////////////////////////////////////////////////////////////////////////
65 // NetworkingGetPropertiesFunction
66
67 NetworkingGetPropertiesFunction::~NetworkingGetPropertiesFunction() {}
68
69 bool NetworkingGetPropertiesFunction::RunImpl() {
70 std::string service_path;
71 if (!args_->GetString(0, &service_path))
72 return false;
mazda 2013/01/16 22:08:52 nit: indent with two spaces
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
73
74 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
75 dbus::ObjectPath(service_path), base::Bind(
76 &NetworkingGetPropertiesFunction::ResultCallback, this));
77 return true;
78 }
79
80 void NetworkingGetPropertiesFunction::ResultCallback(
81 DBusMethodCallStatus call_status,
82 const base::DictionaryValue& result) {
83 scoped_ptr<base::DictionaryValue> filtered_result(
84 CreateFilteredResult(result));
85
86 SetResult(filtered_result.release());
87 SendResponse(true);
88 }
89
90 ////////////////////////////////////////////////////////////////////////////////
91 // NetworkingGetVisibleNetworksFunction::ScopedDecrementer
92
93 NetworkingGetVisibleNetworksFunction::ScopedDecrementer::ScopedDecrementer(
94 NetworkingGetVisibleNetworksFunction* parent,
95 scoped_refptr<NetworkingGetVisibleNetworksFunction::ResultList> result_list)
96 : parent_(parent), result_list_(result_list) {
97 }
98
99 NetworkingGetVisibleNetworksFunction::ScopedDecrementer::~ScopedDecrementer() {
100 result_list_->count--;
101 if (result_list_->count == 0) {
102 parent_->SetResult(result_list_->list.release());
103 parent_->SendResponse(true);
104 }
105 }
106
107 ////////////////////////////////////////////////////////////////////////////////
108 // NetworkingGetVisibleNetworksFunction
109
110 NetworkingGetVisibleNetworksFunction::~NetworkingGetVisibleNetworksFunction() {}
111
112 bool NetworkingGetVisibleNetworksFunction::RunImpl() {
113 std::string network_type;
114 if (!args_->GetString(0, &network_type))
115 return false;
mazda 2013/01/16 22:08:52 nit: indent with two spaces
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
116
117 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(
118 base::Bind(
119 &NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback,
120 this,
121 network_type));
122 return true;
123 }
124
125 // For each of the available services, fire off a request for its properties.
126 void NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback(
127 const std::string& network_type,
128 DBusMethodCallStatus call_status,
129 const base::DictionaryValue& result) {
130 const base::ListValue* available_services;
131 if (!result.GetList(flimflam::kServicesProperty, &available_services)) {
132 error_ = "Error.noServices";
133 SendResponse(false);
134 return;
135 }
136
137 scoped_refptr<ResultList> results(new ResultList);
138 results->count = available_services->GetSize();
139 for (size_t i = 0; i < available_services->GetSize(); ++i) {
140 std::string service_path;
141 available_services->GetString(i, &service_path);
142 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
143 dbus::ObjectPath(service_path),
144 base::Bind(
145 &NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback,
146 this,
147 service_path,
148 network_type,
149 results));
150 }
151 }
152
153 // If this network is of the appropriate type, add it to the results and
154 // decrement the count. If the count hits zero, then send the result.
155 void NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback(
156 const std::string& service_path,
157 const std::string& network_type,
158 scoped_refptr<ResultList> result_list,
159 DBusMethodCallStatus call_status,
160 const base::DictionaryValue& result) {
161 // We must decrement the count each time we get a callback, regardless of the
162 // outcome.
163 ScopedDecrementer decrementer(this, result_list);
164
165 if (call_status != DBUS_METHOD_CALL_SUCCESS)
166 return;
167
168 std::string type;
169 if (!result.GetString(flimflam::kTypeProperty, &type))
170 return;
171
172 if (type == network_type || network_type == kNetworkTypeAll) {
173 scoped_ptr<base::DictionaryValue> network_properties(
174 CreateFilteredResult(result));
175 network_properties->SetString(kNetworkId, service_path);
176 result_list->list->Append(network_properties.release());
177 }
178 }
179
180 ////////////////////////////////////////////////////////////////////////////////
181 // NetworkingRequestConnectFunction
182
183 NetworkingRequestConnectFunction::~NetworkingRequestConnectFunction() {}
184
185 void NetworkingRequestConnectFunction::ConnectSuccess() {
186 SendResponse(true);
187 }
188
189 void NetworkingRequestConnectFunction::ConnectFailed(
190 const std::string& errorName,
191 const std::string& errorMessage) {
192 error_ = errorName;
193 SendResponse(false);
194 }
195
196 bool NetworkingRequestConnectFunction::RunImpl() {
197 std::string service_path;
198 if (!args_->GetString(0, &service_path))
199 return false;
mazda 2013/01/16 22:08:52 nit: indent with two spaces
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
200
201 DBusThreadManager::Get()->GetShillServiceClient()->Connect(
202 dbus::ObjectPath(service_path),
mazda 2013/01/16 22:08:52 nit: indent with four spaces
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
203 base::Bind(&NetworkingRequestConnectFunction::ConnectSuccess, this),
204 base::Bind(&NetworkingRequestConnectFunction::ConnectFailed, this));
205 return true;
206 }
207
208 ////////////////////////////////////////////////////////////////////////////////
209 // NetworkingRequestDisconnectFunction
210
211 NetworkingRequestDisconnectFunction::~NetworkingRequestDisconnectFunction() {}
212
213 void NetworkingRequestDisconnectFunction::ConnectSuccess() {
214 SendResponse(true);
215 }
216
217 void NetworkingRequestDisconnectFunction::ConnectFailed(
218 const std::string& errorName,
219 const std::string& errorMessage) {
220 error_ = errorName;
221 SendResponse(false);
222 }
223
224 bool NetworkingRequestDisconnectFunction::RunImpl() {
225 std::string service_path;
226 if (!args_->GetString(0, &service_path))
227 return false;
mazda 2013/01/16 22:08:52 nit: indent with two spaces
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
228
229 DBusThreadManager::Get()->GetShillServiceClient()->Connect(
230 dbus::ObjectPath(service_path),
mazda 2013/01/16 22:08:52 nit: indent with four spaces
Greg Spencer (Chromium) 2013/01/16 22:29:17 Done.
231 base::Bind(&NetworkingRequestDisconnectFunction::ConnectSuccess, this),
232 base::Bind(&NetworkingRequestDisconnectFunction::ConnectFailed, this));
233 return true;
234 }
235
236 ////////////////////////////////////////////////////////////////////////////////
237 // NetworkingPrivateAPI
238
239 NetworkingPrivateAPI::NetworkingPrivateAPI(Profile* profile) {
240 ExtensionFunctionRegistry* registry =
241 ExtensionFunctionRegistry::GetInstance();
242 registry->RegisterFunction<NetworkingGetPropertiesFunction>();
243 registry->RegisterFunction<NetworkingGetVisibleNetworksFunction>();
244 registry->RegisterFunction<NetworkingRequestConnectFunction>();
245 registry->RegisterFunction<NetworkingRequestDisconnectFunction>();
246 }
247
248 NetworkingPrivateAPI::~NetworkingPrivateAPI() {
249 }
250
251 void NetworkingPrivateAPI::Shutdown() {
252 }
253
254 // static
255 NetworkingPrivateAPI* NetworkingPrivateAPI::Get(Profile* profile) {
256 return NetworkingPrivateAPIFactory::GetForProfile(profile);
257 }
258
259 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698