| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/extensions/api/networking_cast/networking_cast_api.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "chrome/common/extensions/api/networking_cast.h" |
| 11 #include "extensions/browser/api/extensions_api_client.h" |
| 12 #include "extensions/browser/api/networking_private/networking_cast_delegate.h" |
| 13 |
| 14 #if defined(OS_CHROMEOS) |
| 15 #include "chromeos/network/network_device_handler.h" |
| 16 #include "chromeos/network/network_handler.h" |
| 17 #include "third_party/cros_system_api/dbus/shill/dbus-constants.h" |
| 18 #endif |
| 19 |
| 20 namespace private_api = extensions::api::networking_private; |
| 21 namespace cast_api = extensions::api::networking_cast; |
| 22 |
| 23 namespace extensions { |
| 24 |
| 25 namespace { |
| 26 |
| 27 #if defined(OS_CHROMEOS) |
| 28 // Parses TDLS status returned by network handler to networking_cast TDLS status |
| 29 // type. |
| 30 cast_api::TDLSStatus ParseTDLSStatus(const std::string& status) { |
| 31 if (status == shill::kTDLSConnectedState) |
| 32 return cast_api::TDLS_STATUS_CONNECTED; |
| 33 if (status == shill::kTDLSNonexistentState) |
| 34 return cast_api::TDLS_STATUS_NONEXISTENT; |
| 35 if (status == shill::kTDLSDisabledState) |
| 36 return cast_api::TDLS_STATUS_DISABLED; |
| 37 if (status == shill::kTDLSDisconnectedState) |
| 38 return cast_api::TDLS_STATUS_DISCONNECTED; |
| 39 if (status == shill::kTDLSUnknownState) |
| 40 return cast_api::TDLS_STATUS_UNKNOWN; |
| 41 |
| 42 NOTREACHED() << "Unknown TDLS status " << status; |
| 43 return cast_api::TDLS_STATUS_UNKNOWN; |
| 44 } |
| 45 #endif |
| 46 |
| 47 std::unique_ptr<private_api::VerificationProperties> |
| 48 AsPrivateApiVerificaitonProperties( |
| 49 const cast_api::VerificationProperties& properties) { |
| 50 std::unique_ptr<base::DictionaryValue> cast_properties_dict = |
| 51 properties.ToValue(); |
| 52 CHECK(cast_properties_dict); |
| 53 return private_api::VerificationProperties::FromValue(*cast_properties_dict); |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
| 58 NetworkingCastVerifyDestinationFunction:: |
| 59 ~NetworkingCastVerifyDestinationFunction() {} |
| 60 |
| 61 ExtensionFunction::ResponseAction |
| 62 NetworkingCastVerifyDestinationFunction::Run() { |
| 63 std::unique_ptr<cast_api::VerifyDestination::Params> params = |
| 64 cast_api::VerifyDestination::Params::Create(*args_); |
| 65 EXTENSION_FUNCTION_VALIDATE(params); |
| 66 |
| 67 NetworkingCastDelegate* delegate = |
| 68 ExtensionsAPIClient::Get()->GetNetworkingCastDelegate(); |
| 69 std::unique_ptr<private_api::VerificationProperties> private_api_properties = |
| 70 AsPrivateApiVerificaitonProperties(params->properties); |
| 71 delegate->VerifyDestination( |
| 72 *private_api_properties, |
| 73 base::Bind(&NetworkingCastVerifyDestinationFunction::Success, this), |
| 74 base::Bind(&NetworkingCastVerifyDestinationFunction::Failure, this)); |
| 75 |
| 76 // VerifyDestination might respond synchronously, e.g. in tests. |
| 77 return did_respond() ? AlreadyResponded() : RespondLater(); |
| 78 } |
| 79 |
| 80 void NetworkingCastVerifyDestinationFunction::Success(bool result) { |
| 81 Respond(ArgumentList(cast_api::VerifyDestination::Results::Create(result))); |
| 82 } |
| 83 |
| 84 void NetworkingCastVerifyDestinationFunction::Failure( |
| 85 const std::string& error) { |
| 86 Respond(Error(error)); |
| 87 } |
| 88 |
| 89 NetworkingCastVerifyAndEncryptCredentialsFunction:: |
| 90 ~NetworkingCastVerifyAndEncryptCredentialsFunction() {} |
| 91 |
| 92 ExtensionFunction::ResponseAction |
| 93 NetworkingCastVerifyAndEncryptCredentialsFunction::Run() { |
| 94 std::unique_ptr<cast_api::VerifyAndEncryptCredentials::Params> params = |
| 95 cast_api::VerifyAndEncryptCredentials::Params::Create(*args_); |
| 96 EXTENSION_FUNCTION_VALIDATE(params); |
| 97 |
| 98 NetworkingCastDelegate* delegate = |
| 99 ExtensionsAPIClient::Get()->GetNetworkingCastDelegate(); |
| 100 std::unique_ptr<private_api::VerificationProperties> private_api_properties = |
| 101 AsPrivateApiVerificaitonProperties(params->properties); |
| 102 delegate->VerifyAndEncryptCredentials( |
| 103 params->network_guid, *private_api_properties, |
| 104 base::Bind(&NetworkingCastVerifyAndEncryptCredentialsFunction::Success, |
| 105 this), |
| 106 base::Bind(&NetworkingCastVerifyAndEncryptCredentialsFunction::Failure, |
| 107 this)); |
| 108 |
| 109 // VerifyAndEncryptCredentials might respond synchronously, e.g. in tests. |
| 110 return did_respond() ? AlreadyResponded() : RespondLater(); |
| 111 } |
| 112 |
| 113 void NetworkingCastVerifyAndEncryptCredentialsFunction::Success( |
| 114 const std::string& result) { |
| 115 Respond(ArgumentList( |
| 116 cast_api::VerifyAndEncryptCredentials::Results::Create(result))); |
| 117 } |
| 118 |
| 119 void NetworkingCastVerifyAndEncryptCredentialsFunction::Failure( |
| 120 const std::string& error) { |
| 121 Respond(Error(error)); |
| 122 } |
| 123 |
| 124 NetworkingCastVerifyAndEncryptDataFunction:: |
| 125 ~NetworkingCastVerifyAndEncryptDataFunction() {} |
| 126 |
| 127 ExtensionFunction::ResponseAction |
| 128 NetworkingCastVerifyAndEncryptDataFunction::Run() { |
| 129 std::unique_ptr<cast_api::VerifyAndEncryptData::Params> params = |
| 130 cast_api::VerifyAndEncryptData::Params::Create(*args_); |
| 131 EXTENSION_FUNCTION_VALIDATE(params); |
| 132 |
| 133 NetworkingCastDelegate* delegate = |
| 134 ExtensionsAPIClient::Get()->GetNetworkingCastDelegate(); |
| 135 std::unique_ptr<private_api::VerificationProperties> private_api_properties = |
| 136 AsPrivateApiVerificaitonProperties(params->properties); |
| 137 delegate->VerifyAndEncryptData( |
| 138 params->data, *private_api_properties, |
| 139 base::Bind(&NetworkingCastVerifyAndEncryptDataFunction::Success, this), |
| 140 base::Bind(&NetworkingCastVerifyAndEncryptDataFunction::Failure, this)); |
| 141 |
| 142 // VerifyAndEncryptData might respond synchronously, e.g. in tests. |
| 143 return did_respond() ? AlreadyResponded() : RespondLater(); |
| 144 } |
| 145 |
| 146 void NetworkingCastVerifyAndEncryptDataFunction::Success( |
| 147 const std::string& result) { |
| 148 Respond( |
| 149 ArgumentList(cast_api::VerifyAndEncryptData::Results::Create(result))); |
| 150 } |
| 151 |
| 152 void NetworkingCastVerifyAndEncryptDataFunction::Failure( |
| 153 const std::string& error) { |
| 154 Respond(Error(error)); |
| 155 } |
| 156 |
| 157 NetworkingCastSetWifiTDLSEnabledStateFunction:: |
| 158 ~NetworkingCastSetWifiTDLSEnabledStateFunction() {} |
| 159 |
| 160 ExtensionFunction::ResponseAction |
| 161 NetworkingCastSetWifiTDLSEnabledStateFunction::Run() { |
| 162 std::unique_ptr<cast_api::SetWifiTDLSEnabledState::Params> params = |
| 163 cast_api::SetWifiTDLSEnabledState::Params::Create(*args_); |
| 164 EXTENSION_FUNCTION_VALIDATE(params); |
| 165 |
| 166 #if defined(OS_CHROMEOS) |
| 167 chromeos::NetworkHandler::Get()->network_device_handler()->SetWifiTDLSEnabled( |
| 168 params->ip_or_mac_address, params->enabled, |
| 169 base::Bind(&NetworkingCastSetWifiTDLSEnabledStateFunction::Success, this), |
| 170 base::Bind(&NetworkingCastSetWifiTDLSEnabledStateFunction::Failure, |
| 171 this)); |
| 172 |
| 173 // SetWifiTDLSEnabled might respond synchronously, e.g. in tests. |
| 174 return did_respond() ? AlreadyResponded() : RespondLater(); |
| 175 #else |
| 176 return RespondNow(Error("Not supported")); |
| 177 #endif |
| 178 } |
| 179 |
| 180 #if defined(OS_CHROMEOS) |
| 181 void NetworkingCastSetWifiTDLSEnabledStateFunction::Success( |
| 182 const std::string& result) { |
| 183 Respond(ArgumentList(cast_api::SetWifiTDLSEnabledState::Results::Create( |
| 184 ParseTDLSStatus(result)))); |
| 185 } |
| 186 |
| 187 void NetworkingCastSetWifiTDLSEnabledStateFunction::Failure( |
| 188 const std::string& error, |
| 189 std::unique_ptr<base::DictionaryValue> error_data) { |
| 190 Respond(Error(error)); |
| 191 } |
| 192 #endif |
| 193 |
| 194 NetworkingCastGetWifiTDLSStatusFunction:: |
| 195 ~NetworkingCastGetWifiTDLSStatusFunction() {} |
| 196 |
| 197 ExtensionFunction::ResponseAction |
| 198 NetworkingCastGetWifiTDLSStatusFunction::Run() { |
| 199 std::unique_ptr<cast_api::GetWifiTDLSStatus::Params> params = |
| 200 cast_api::GetWifiTDLSStatus::Params::Create(*args_); |
| 201 EXTENSION_FUNCTION_VALIDATE(params); |
| 202 |
| 203 #if defined(OS_CHROMEOS) |
| 204 chromeos::NetworkHandler::Get()->network_device_handler()->GetWifiTDLSStatus( |
| 205 params->ip_or_mac_address, |
| 206 base::Bind(&NetworkingCastGetWifiTDLSStatusFunction::Success, this), |
| 207 base::Bind(&NetworkingCastGetWifiTDLSStatusFunction::Failure, this)); |
| 208 |
| 209 // GetWifiTDLSStatus might respond synchronously, e.g. in tests. |
| 210 return did_respond() ? AlreadyResponded() : RespondLater(); |
| 211 #else |
| 212 return RespondNow(Error("Not supported")); |
| 213 #endif |
| 214 } |
| 215 |
| 216 #if defined(OS_CHROMEOS) |
| 217 void NetworkingCastGetWifiTDLSStatusFunction::Success( |
| 218 const std::string& result) { |
| 219 Respond(ArgumentList( |
| 220 cast_api::GetWifiTDLSStatus::Results::Create(ParseTDLSStatus(result)))); |
| 221 } |
| 222 |
| 223 void NetworkingCastGetWifiTDLSStatusFunction::Failure( |
| 224 const std::string& error, |
| 225 std::unique_ptr<base::DictionaryValue> error_data) { |
| 226 Respond(Error(error)); |
| 227 } |
| 228 #endif |
| 229 |
| 230 } // namespace extensions |
| OLD | NEW |