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/extensions/extension_function_registry.h" |
| 9 #include "chrome/common/extensions/api/networking_private.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 "chromeos/network/onc/onc_constants.h" |
| 14 #include "chromeos/network/onc/onc_signature.h" |
| 15 #include "chromeos/network/onc/onc_translation_tables.h" |
| 16 #include "chromeos/network/onc/onc_translator.h" |
| 17 #include "dbus/object_path.h" |
| 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 19 |
| 20 using namespace chromeos; |
| 21 namespace api = extensions::api::networking_private; |
| 22 |
| 23 namespace { |
| 24 |
| 25 // An error returned when no valid services were found. |
| 26 const char kInvalidResponseError[] = "Error.invalidResponse"; |
| 27 |
| 28 // This creates a new ONC dictionary that only contains the information we're |
| 29 // interested in passing on to JavaScript. |
| 30 scoped_ptr<api::NetworkProperties> CreateFilteredResult( |
| 31 const base::DictionaryValue& properties) { |
| 32 scoped_ptr<base::DictionaryValue> onc_properties( |
| 33 onc::TranslateShillServiceToONCPart( |
| 34 properties, |
| 35 &onc::kNetworkConfigurationSignature)); |
| 36 |
| 37 // Now we filter it so we only include properties that we care about for this |
| 38 // interface. |
| 39 static const char* const desired_fields[] = { |
| 40 onc::network_config::kWiFi, |
| 41 onc::network_config::kName, |
| 42 onc::network_config::kGUID, |
| 43 onc::network_config::kType, |
| 44 onc::network_config::kConnectionState, |
| 45 }; |
| 46 |
| 47 scoped_ptr<api::NetworkProperties> filtered_result( |
| 48 new api::NetworkProperties); |
| 49 for (size_t i = 0; i < arraysize(desired_fields); ++i) { |
| 50 base::Value* value; |
| 51 if (onc_properties->Get(desired_fields[i], &value)) |
| 52 filtered_result->additional_properties.Set(desired_fields[i], |
| 53 value->DeepCopy()); |
| 54 } |
| 55 |
| 56 return filtered_result.Pass(); |
| 57 } |
| 58 |
| 59 class ResultList : public base::RefCounted<ResultList> { |
| 60 public: |
| 61 typedef base::Callback<void(const std::string& error, |
| 62 scoped_ptr<base::ListValue>)> ResultCallback; |
| 63 |
| 64 ResultList(const std::string& type, const ResultCallback& callback) |
| 65 : callback_(callback) { |
| 66 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( |
| 67 base::Bind(&ResultList::ManagerPropertiesCallback, this, type)); |
| 68 } |
| 69 |
| 70 scoped_ptr<base::ListValue> GetResults() { |
| 71 return api::GetVisibleNetworks::Results::Create(list_); |
| 72 } |
| 73 |
| 74 private: |
| 75 friend class base::RefCounted<ResultList>; |
| 76 |
| 77 ~ResultList() { |
| 78 callback_.Run(std::string(), GetResults()); |
| 79 } |
| 80 |
| 81 void Append(api::NetworkProperties* value) { |
| 82 list_.push_back(linked_ptr<api::NetworkProperties>(value)); |
| 83 } |
| 84 |
| 85 // Receives the result of a call to GetProperties on the Shill Manager API. |
| 86 void ManagerPropertiesCallback(const std::string& network_type, |
| 87 chromeos::DBusMethodCallStatus call_status, |
| 88 const base::DictionaryValue& result); |
| 89 |
| 90 // Receives the result of a call to GetProperties on the Shill Service API. |
| 91 void ServicePropertiesCallback(const std::string& service_path, |
| 92 const std::string& network_type, |
| 93 chromeos::DBusMethodCallStatus call_status, |
| 94 const base::DictionaryValue& result); |
| 95 |
| 96 std::vector<linked_ptr<api::NetworkProperties> > list_; |
| 97 ResultCallback callback_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(ResultList); |
| 100 }; |
| 101 |
| 102 // For each of the available services, fire off a request for its properties. |
| 103 void ResultList::ManagerPropertiesCallback( |
| 104 const std::string& network_type, |
| 105 DBusMethodCallStatus call_status, |
| 106 const base::DictionaryValue& result) { |
| 107 const base::ListValue* available_services; |
| 108 if (!result.GetList(flimflam::kServicesProperty, &available_services)) { |
| 109 LOG(ERROR) |
| 110 << "ShillManagerClient::GetProperties returned malformed service list."; |
| 111 callback_.Run(kInvalidResponseError, make_scoped_ptr(new base::ListValue)); |
| 112 return; |
| 113 } |
| 114 // If there just are no services, return an empty list. |
| 115 if (available_services->GetSize() == 0) { |
| 116 callback_.Run(std::string(), make_scoped_ptr(new base::ListValue)); |
| 117 return; |
| 118 } |
| 119 for (base::ListValue::const_iterator iter = available_services->begin(); |
| 120 iter != available_services->end(); ++iter) { |
| 121 std::string service_path; |
| 122 if (!(*iter)->GetAsString(&service_path)) { |
| 123 LOG(ERROR) |
| 124 << "ShillManagerClient::GetProperties returned malformed service."; |
| 125 continue; |
| 126 } |
| 127 |
| 128 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( |
| 129 dbus::ObjectPath(service_path), |
| 130 base::Bind( |
| 131 &ResultList::ServicePropertiesCallback, |
| 132 this, |
| 133 service_path, |
| 134 network_type)); |
| 135 } |
| 136 } |
| 137 |
| 138 void ResultList::ServicePropertiesCallback( |
| 139 const std::string& service_path, |
| 140 const std::string& network_type, |
| 141 DBusMethodCallStatus call_status, |
| 142 const base::DictionaryValue& result) { |
| 143 if (call_status == DBUS_METHOD_CALL_SUCCESS) { |
| 144 scoped_ptr<api::NetworkProperties> filtered_result( |
| 145 CreateFilteredResult(result)); |
| 146 |
| 147 std::string onc_type; |
| 148 if (filtered_result->additional_properties.GetString( |
| 149 onc::network_config::kType, &onc_type) && |
| 150 (onc_type == network_type || |
| 151 network_type == onc::network_type::kAllTypes)) { |
| 152 // TODO(gspencer): For now the "GUID" we send back is going to look |
| 153 // remarkably like the service path. Once this code starts using the |
| 154 // NetworkStateHandler instead of Shill directly, we should remove |
| 155 // this line so that we're sending back the actual GUID. The |
| 156 // JavaScript shouldn't care: this ID is opaque to it, and it |
| 157 // shouldn't store it anywhere. |
| 158 filtered_result->additional_properties.SetString( |
| 159 onc::network_config::kGUID, service_path); |
| 160 |
| 161 Append(filtered_result.release()); |
| 162 } |
| 163 } |
| 164 } |
| 165 |
| 166 } // namespace |
| 167 |
| 168 //////////////////////////////////////////////////////////////////////////////// |
| 169 // NetworkingPrivateGetPropertiesFunction |
| 170 |
| 171 NetworkingPrivateGetPropertiesFunction:: |
| 172 ~NetworkingPrivateGetPropertiesFunction() { |
| 173 } |
| 174 |
| 175 bool NetworkingPrivateGetPropertiesFunction::RunImpl() { |
| 176 scoped_ptr<api::GetProperties::Params> params = |
| 177 api::GetProperties::Params::Create(*args_); |
| 178 EXTENSION_FUNCTION_VALIDATE(params); |
| 179 |
| 180 // TODO(gspencer): Currently we're using the service path as the |
| 181 // |network_guid|. Eventually this should be using the real GUID. |
| 182 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( |
| 183 dbus::ObjectPath(params->network_guid), |
| 184 base::Bind(&NetworkingPrivateGetPropertiesFunction::ResultCallback, |
| 185 this)); |
| 186 return true; |
| 187 } |
| 188 |
| 189 void NetworkingPrivateGetPropertiesFunction::ResultCallback( |
| 190 DBusMethodCallStatus call_status, |
| 191 const base::DictionaryValue& result) { |
| 192 scoped_ptr<api::NetworkProperties> filtered_result( |
| 193 CreateFilteredResult(result)); |
| 194 results_ = api::GetProperties::Results::Create(*filtered_result); |
| 195 SendResponse(true); |
| 196 } |
| 197 |
| 198 //////////////////////////////////////////////////////////////////////////////// |
| 199 // NetworkingPrivateGetVisibleNetworksFunction |
| 200 |
| 201 NetworkingPrivateGetVisibleNetworksFunction:: |
| 202 ~NetworkingPrivateGetVisibleNetworksFunction() { |
| 203 } |
| 204 |
| 205 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { |
| 206 scoped_ptr<api::GetVisibleNetworks::Params> params = |
| 207 api::GetVisibleNetworks::Params::Create(*args_); |
| 208 EXTENSION_FUNCTION_VALIDATE(params); |
| 209 |
| 210 scoped_refptr<ResultList> result_list(new ResultList( |
| 211 api::GetVisibleNetworks::Params::ToString(params->type), |
| 212 base::Bind( |
| 213 &NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback, |
| 214 this))); |
| 215 return true; |
| 216 } |
| 217 |
| 218 void NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback( |
| 219 const std::string& error, |
| 220 scoped_ptr<base::ListValue> result_list) { |
| 221 if (!error.empty()) { |
| 222 error_ = error; |
| 223 SendResponse(false); |
| 224 } else { |
| 225 results_.reset(result_list.release()); |
| 226 SendResponse(true); |
| 227 } |
| 228 } |
| 229 |
| 230 //////////////////////////////////////////////////////////////////////////////// |
| 231 // NetworkingPrivateStartConnectFunction |
| 232 |
| 233 NetworkingPrivateStartConnectFunction:: |
| 234 ~NetworkingPrivateStartConnectFunction() { |
| 235 } |
| 236 |
| 237 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { |
| 238 SendResponse(true); |
| 239 } |
| 240 |
| 241 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( |
| 242 const std::string& error_name, |
| 243 const std::string& error_message) { |
| 244 error_ = error_name; |
| 245 SendResponse(false); |
| 246 } |
| 247 |
| 248 bool NetworkingPrivateStartConnectFunction::RunImpl() { |
| 249 scoped_ptr<api::StartConnect::Params> params = |
| 250 api::StartConnect::Params::Create(*args_); |
| 251 EXTENSION_FUNCTION_VALIDATE(params); |
| 252 |
| 253 // TODO(gspencer): For now, the "GUID" we receive from the JavaScript is going |
| 254 // to be the service path. Fix this so it actually looks up the service path |
| 255 // from the GUID once we're using the NetworkStateHandler. |
| 256 std::string service_path = params->network_guid; |
| 257 |
| 258 DBusThreadManager::Get()->GetShillServiceClient()->Connect( |
| 259 dbus::ObjectPath(service_path), |
| 260 base::Bind( |
| 261 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, |
| 262 this), |
| 263 base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed, |
| 264 this)); |
| 265 return true; |
| 266 } |
| 267 |
| 268 //////////////////////////////////////////////////////////////////////////////// |
| 269 // NetworkingPrivateStartDisconnectFunction |
| 270 |
| 271 NetworkingPrivateStartDisconnectFunction:: |
| 272 ~NetworkingPrivateStartDisconnectFunction() { |
| 273 } |
| 274 |
| 275 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { |
| 276 SendResponse(true); |
| 277 } |
| 278 |
| 279 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( |
| 280 const std::string& error_name, |
| 281 const std::string& error_message) { |
| 282 error_ = error_name; |
| 283 SendResponse(false); |
| 284 } |
| 285 |
| 286 bool NetworkingPrivateStartDisconnectFunction::RunImpl() { |
| 287 scoped_ptr<api::StartDisconnect::Params> params = |
| 288 api::StartDisconnect::Params::Create(*args_); |
| 289 EXTENSION_FUNCTION_VALIDATE(params); |
| 290 |
| 291 // TODO(gspencer): Currently the |network_guid| parameter is storing the |
| 292 // service path. Convert to using the actual GUID when we start using |
| 293 // the NetworkStateHandler. |
| 294 DBusThreadManager::Get()->GetShillServiceClient()->Connect( |
| 295 dbus::ObjectPath(params->network_guid), |
| 296 base::Bind( |
| 297 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, |
| 298 this), |
| 299 base::Bind( |
| 300 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, |
| 301 this)); |
| 302 return true; |
| 303 } |
OLD | NEW |