| Index: components/wifi_sync/wifi_credential.cc
|
| diff --git a/components/wifi_sync/wifi_credential.cc b/components/wifi_sync/wifi_credential.cc
|
| index 73fbaf27f96b34281b4f6606c4f959bde6d58305..490cd7b0a9ffb4e278cde77125e564fec395f766 100644
|
| --- a/components/wifi_sync/wifi_credential.cc
|
| +++ b/components/wifi_sync/wifi_credential.cc
|
| @@ -4,25 +4,68 @@
|
|
|
| #include "components/wifi_sync/wifi_credential.h"
|
|
|
| -#include <limits>
|
| -#include <string>
|
| -
|
| +#include "base/i18n/streaming_utf8_validator.h"
|
| #include "base/logging.h"
|
| #include "base/strings/string_number_conversions.h"
|
| #include "base/strings/stringprintf.h"
|
| +#include "base/values.h"
|
| +#include "components/onc/onc_constants.h"
|
|
|
| namespace wifi_sync {
|
|
|
| -WifiCredential::WifiCredential(
|
| - const std::vector<unsigned char>& ssid,
|
| +WifiCredential::~WifiCredential() {
|
| +}
|
| +
|
| +// static
|
| +scoped_ptr<WifiCredential> WifiCredential::Create(
|
| + const SsidBytes& ssid,
|
| WifiSecurityClass security_class,
|
| - const std::string& passphrase)
|
| - : ssid_(ssid),
|
| - security_class_(security_class),
|
| - passphrase_(passphrase) {
|
| + const std::string& passphrase) {
|
| + if (security_class == SECURITY_CLASS_INVALID) {
|
| + LOG(ERROR) << "SecurityClass is invalid.";
|
| + return nullptr;
|
| + }
|
| +
|
| + if (!base::StreamingUtf8Validator::Validate(passphrase)) {
|
| + LOG(ERROR) << "Passphrase is not valid UTF-8";
|
| + return nullptr;
|
| + }
|
| +
|
| + return make_scoped_ptr(new WifiCredential(ssid, security_class, passphrase));
|
| }
|
|
|
| -WifiCredential::~WifiCredential() {
|
| +scoped_ptr<base::DictionaryValue> WifiCredential::ToOncProperties() const {
|
| + const std::string ssid_utf8(ssid().begin(), ssid().end());
|
| + // TODO(quiche): Remove this test, once ONC suports non-UTF-8 SSIDs.
|
| + // crbug.com/432546.
|
| + if (!base::StreamingUtf8Validator::Validate(ssid_utf8)) {
|
| + LOG(ERROR) << "SSID is not valid UTF-8";
|
| + return nullptr;
|
| + }
|
| +
|
| + std::string onc_security;
|
| + if (!WifiSecurityClassToOncSecurityString(security_class(), &onc_security)) {
|
| + NOTREACHED() << "Failed to convert SecurityClass with value "
|
| + << security_class();
|
| + return make_scoped_ptr(new base::DictionaryValue());
|
| + }
|
| +
|
| + scoped_ptr<base::DictionaryValue> onc_properties(
|
| + new base::DictionaryValue());
|
| + onc_properties->Set(onc::toplevel_config::kType,
|
| + new base::StringValue(onc::network_type::kWiFi));
|
| + // TODO(quiche): Switch to the HexSSID property, once ONC fully supports it.
|
| + // crbug.com/432546.
|
| + onc_properties->Set(onc::network_config::WifiProperty(onc::wifi::kSSID),
|
| + new base::StringValue(ssid_utf8));
|
| + onc_properties->Set(onc::network_config::WifiProperty(onc::wifi::kSecurity),
|
| + new base::StringValue(onc_security));
|
| + if (WifiSecurityClassSupportsPassphrases(security_class())) {
|
| + onc_properties->Set(
|
| + onc::network_config::WifiProperty(onc::wifi::kPassphrase),
|
| + new base::StringValue(passphrase()));
|
| + }
|
| + return onc_properties;
|
| }
|
|
|
| std::string WifiCredential::ToString() const {
|
| @@ -45,4 +88,21 @@ WifiCredential::CredentialSet WifiCredential::MakeSet() {
|
| return CredentialSet(WifiCredential::IsLessThan);
|
| }
|
|
|
| +// static
|
| +WifiCredential::SsidBytes WifiCredential::MakeSsidBytesForTest(
|
| + const std::string& ssid) {
|
| + return SsidBytes(ssid.begin(), ssid.end());
|
| +}
|
| +
|
| +// Private methods.
|
| +
|
| +WifiCredential::WifiCredential(
|
| + const std::vector<unsigned char>& ssid,
|
| + WifiSecurityClass security_class,
|
| + const std::string& passphrase)
|
| + : ssid_(ssid),
|
| + security_class_(security_class),
|
| + passphrase_(passphrase) {
|
| +}
|
| +
|
| } // namespace wifi_sync
|
|
|