Chromium Code Reviews| OLD | NEW |
|---|---|
| (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. | |
|
stevenjb
2013/01/17 00:04:30
I thought we were going to use ONC properties for
Greg Spencer (Chromium)
2013/01/17 00:10:23
Good point on the ONC, I'll switch it to that form
| |
| 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)) | |
| 40 filtered_result->SetString(kName, name); | |
| 41 std::string type; | |
| 42 if (properties.GetString(flimflam::kTypeProperty, &type)) | |
| 43 filtered_result->SetString(kType, type); | |
| 44 std::string state; | |
| 45 if (properties.GetString(flimflam::kStateProperty, &state)) { | |
| 46 if (state == flimflam::kStateReady || state == flimflam::kStateOnline) { | |
|
stevenjb
2013/01/17 00:04:30
This should use NetworkState::StateIsConnected()
| |
| 47 filtered_result->SetString(kStatus, "connected"); | |
| 48 } else if (state == flimflam::kStateAssociation || | |
| 49 state == flimflam::kStatePortal || | |
| 50 state == flimflam::kStateConfiguration) { | |
|
stevenjb
2013/01/17 00:04:30
This should use NetworkState::StateIsConnecting()
| |
| 51 filtered_result->SetString(kStatus, "connecting"); | |
|
stevenjb
2013/01/17 00:04:30
This should also be a const defined above
| |
| 52 } else { | |
| 53 filtered_result->SetString(kStatus, "notConnected"); | |
|
stevenjb
2013/01/17 00:04:30
ditto
| |
| 54 } | |
| 55 } | |
| 56 | |
| 57 return filtered_result.release(); | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 | |
| 63 //////////////////////////////////////////////////////////////////////////////// | |
| 64 // NetworkingGetPropertiesFunction | |
| 65 | |
| 66 NetworkingGetPropertiesFunction::~NetworkingGetPropertiesFunction() {} | |
|
stevenjb
2013/01/17 00:04:30
nit: I generally see/prefer } on a second line out
Greg Spencer (Chromium)
2013/01/18 01:20:30
Done.
| |
| 67 | |
| 68 bool NetworkingGetPropertiesFunction::RunImpl() { | |
| 69 std::string service_path; | |
| 70 if (!args_->GetString(0, &service_path)) | |
| 71 return false; | |
| 72 | |
| 73 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
| 74 dbus::ObjectPath(service_path), base::Bind( | |
| 75 &NetworkingGetPropertiesFunction::ResultCallback, this)); | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 void NetworkingGetPropertiesFunction::ResultCallback( | |
| 80 DBusMethodCallStatus call_status, | |
| 81 const base::DictionaryValue& result) { | |
| 82 scoped_ptr<base::DictionaryValue> filtered_result( | |
| 83 CreateFilteredResult(result)); | |
| 84 | |
| 85 SetResult(filtered_result.release()); | |
| 86 SendResponse(true); | |
| 87 } | |
| 88 | |
| 89 //////////////////////////////////////////////////////////////////////////////// | |
| 90 // NetworkingGetVisibleNetworksFunction::ScopedDecrementer | |
| 91 | |
| 92 NetworkingGetVisibleNetworksFunction::ScopedDecrementer::ScopedDecrementer( | |
| 93 NetworkingGetVisibleNetworksFunction* parent, | |
| 94 scoped_refptr<NetworkingGetVisibleNetworksFunction::ResultList> result_list) | |
| 95 : parent_(parent), result_list_(result_list) { | |
|
stevenjb
2013/01/17 00:04:30
align, separate line per member
Greg Spencer (Chromium)
2013/01/18 01:20:30
Done.
| |
| 96 } | |
| 97 | |
| 98 NetworkingGetVisibleNetworksFunction::ScopedDecrementer::~ScopedDecrementer() { | |
| 99 result_list_->count--; | |
|
stevenjb
2013/01/17 00:04:30
pre-decrement
Greg Spencer (Chromium)
2013/01/18 01:20:30
Done.
| |
| 100 if (result_list_->count == 0) { | |
| 101 parent_->SetResult(result_list_->list.release()); | |
| 102 parent_->SendResponse(true); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 //////////////////////////////////////////////////////////////////////////////// | |
| 107 // NetworkingGetVisibleNetworksFunction | |
| 108 | |
| 109 NetworkingGetVisibleNetworksFunction::~NetworkingGetVisibleNetworksFunction() {} | |
| 110 | |
| 111 bool NetworkingGetVisibleNetworksFunction::RunImpl() { | |
| 112 std::string network_type; | |
| 113 if (!args_->GetString(0, &network_type)) | |
| 114 return false; | |
| 115 | |
| 116 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( | |
| 117 base::Bind( | |
| 118 &NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback, | |
| 119 this, | |
| 120 network_type)); | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 // For each of the available services, fire off a request for its properties. | |
| 125 void NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback( | |
| 126 const std::string& network_type, | |
| 127 DBusMethodCallStatus call_status, | |
| 128 const base::DictionaryValue& result) { | |
| 129 const base::ListValue* available_services; | |
| 130 if (!result.GetList(flimflam::kServicesProperty, &available_services)) { | |
| 131 error_ = "Error.noServices"; | |
| 132 SendResponse(false); | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 scoped_refptr<ResultList> results(new ResultList); | |
| 137 results->count = available_services->GetSize(); | |
| 138 for (size_t i = 0; i < available_services->GetSize(); ++i) { | |
| 139 std::string service_path; | |
| 140 available_services->GetString(i, &service_path); | |
| 141 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
| 142 dbus::ObjectPath(service_path), | |
| 143 base::Bind( | |
| 144 &NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback, | |
| 145 this, | |
| 146 service_path, | |
| 147 network_type, | |
| 148 results)); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 // If this network is of the appropriate type, add it to the results and | |
| 153 // decrement the count. If the count hits zero, then send the result. | |
| 154 void NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback( | |
| 155 const std::string& service_path, | |
| 156 const std::string& network_type, | |
| 157 scoped_refptr<ResultList> result_list, | |
| 158 DBusMethodCallStatus call_status, | |
| 159 const base::DictionaryValue& result) { | |
| 160 // We must decrement the count each time we get a callback, regardless of the | |
| 161 // outcome. | |
| 162 ScopedDecrementer decrementer(this, result_list); | |
| 163 | |
| 164 if (call_status != DBUS_METHOD_CALL_SUCCESS) | |
| 165 return; | |
| 166 | |
| 167 std::string type; | |
| 168 if (!result.GetString(flimflam::kTypeProperty, &type)) | |
| 169 return; | |
| 170 | |
| 171 if (type == network_type || network_type == kNetworkTypeAll) { | |
| 172 scoped_ptr<base::DictionaryValue> network_properties( | |
| 173 CreateFilteredResult(result)); | |
| 174 network_properties->SetString(kNetworkId, service_path); | |
| 175 result_list->list->Append(network_properties.release()); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 //////////////////////////////////////////////////////////////////////////////// | |
| 180 // NetworkingRequestConnectFunction | |
| 181 | |
| 182 NetworkingRequestConnectFunction::~NetworkingRequestConnectFunction() {} | |
| 183 | |
| 184 void NetworkingRequestConnectFunction::ConnectSuccess() { | |
| 185 SendResponse(true); | |
| 186 } | |
| 187 | |
| 188 void NetworkingRequestConnectFunction::ConnectFailed( | |
| 189 const std::string& errorName, | |
| 190 const std::string& errorMessage) { | |
| 191 error_ = errorName; | |
| 192 SendResponse(false); | |
| 193 } | |
| 194 | |
| 195 bool NetworkingRequestConnectFunction::RunImpl() { | |
| 196 std::string service_path; | |
| 197 if (!args_->GetString(0, &service_path)) | |
| 198 return false; | |
| 199 | |
| 200 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
| 201 dbus::ObjectPath(service_path), | |
| 202 base::Bind(&NetworkingRequestConnectFunction::ConnectSuccess, this), | |
| 203 base::Bind(&NetworkingRequestConnectFunction::ConnectFailed, this)); | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 //////////////////////////////////////////////////////////////////////////////// | |
| 208 // NetworkingRequestDisconnectFunction | |
| 209 | |
| 210 NetworkingRequestDisconnectFunction::~NetworkingRequestDisconnectFunction() {} | |
| 211 | |
| 212 void NetworkingRequestDisconnectFunction::ConnectSuccess() { | |
| 213 SendResponse(true); | |
|
stevenjb
2013/01/17 00:04:30
Note: this does not mean that we connected, just t
Greg Spencer (Chromium)
2013/01/18 01:20:30
I documented it. The Javascript can wait for a pr
| |
| 214 } | |
| 215 | |
| 216 void NetworkingRequestDisconnectFunction::ConnectFailed( | |
| 217 const std::string& errorName, | |
| 218 const std::string& errorMessage) { | |
| 219 error_ = errorName; | |
| 220 SendResponse(false); | |
| 221 } | |
| 222 | |
| 223 bool NetworkingRequestDisconnectFunction::RunImpl() { | |
| 224 std::string service_path; | |
| 225 if (!args_->GetString(0, &service_path)) | |
| 226 return false; | |
| 227 | |
| 228 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
| 229 dbus::ObjectPath(service_path), | |
| 230 base::Bind(&NetworkingRequestDisconnectFunction::ConnectSuccess, this), | |
| 231 base::Bind(&NetworkingRequestDisconnectFunction::ConnectFailed, this)); | |
| 232 return true; | |
| 233 } | |
| 234 | |
| 235 //////////////////////////////////////////////////////////////////////////////// | |
| 236 // NetworkingPrivateAPI | |
| 237 | |
| 238 NetworkingPrivateAPI::NetworkingPrivateAPI(Profile* profile) { | |
| 239 ExtensionFunctionRegistry* registry = | |
| 240 ExtensionFunctionRegistry::GetInstance(); | |
| 241 registry->RegisterFunction<NetworkingGetPropertiesFunction>(); | |
| 242 registry->RegisterFunction<NetworkingGetVisibleNetworksFunction>(); | |
| 243 registry->RegisterFunction<NetworkingRequestConnectFunction>(); | |
| 244 registry->RegisterFunction<NetworkingRequestDisconnectFunction>(); | |
| 245 } | |
| 246 | |
| 247 NetworkingPrivateAPI::~NetworkingPrivateAPI() { | |
| 248 } | |
| 249 | |
| 250 void NetworkingPrivateAPI::Shutdown() { | |
| 251 } | |
| 252 | |
| 253 // static | |
| 254 NetworkingPrivateAPI* NetworkingPrivateAPI::Get(Profile* profile) { | |
| 255 return NetworkingPrivateAPIFactory::GetForProfile(profile); | |
| 256 } | |
| 257 | |
| 258 } // namespace chromeos | |
| OLD | NEW |