| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/wifi_sync/wifi_credential.h" | 5 #include "components/wifi_sync/wifi_credential.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include "base/i18n/streaming_utf8_validator.h" |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/logging.h" | 8 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/values.h" |
| 12 #include "components/onc/onc_constants.h" |
| 13 | 13 |
| 14 namespace wifi_sync { | 14 namespace wifi_sync { |
| 15 | 15 |
| 16 WifiCredential::WifiCredential( | |
| 17 const std::vector<unsigned char>& ssid, | |
| 18 WifiSecurityClass security_class, | |
| 19 const std::string& passphrase) | |
| 20 : ssid_(ssid), | |
| 21 security_class_(security_class), | |
| 22 passphrase_(passphrase) { | |
| 23 } | |
| 24 | |
| 25 WifiCredential::~WifiCredential() { | 16 WifiCredential::~WifiCredential() { |
| 26 } | 17 } |
| 27 | 18 |
| 19 // static |
| 20 scoped_ptr<WifiCredential> WifiCredential::Create( |
| 21 const SsidBytes& ssid, |
| 22 WifiSecurityClass security_class, |
| 23 const std::string& passphrase) { |
| 24 if (security_class == SECURITY_CLASS_INVALID) { |
| 25 LOG(ERROR) << "SecurityClass is invalid."; |
| 26 return nullptr; |
| 27 } |
| 28 |
| 29 if (!base::StreamingUtf8Validator::Validate(passphrase)) { |
| 30 LOG(ERROR) << "Passphrase is not valid UTF-8"; |
| 31 return nullptr; |
| 32 } |
| 33 |
| 34 return make_scoped_ptr(new WifiCredential(ssid, security_class, passphrase)); |
| 35 } |
| 36 |
| 37 scoped_ptr<base::DictionaryValue> WifiCredential::ToOncProperties() const { |
| 38 const std::string ssid_utf8(ssid().begin(), ssid().end()); |
| 39 // TODO(quiche): Remove this test, once ONC suports non-UTF-8 SSIDs. |
| 40 // crbug.com/432546. |
| 41 if (!base::StreamingUtf8Validator::Validate(ssid_utf8)) { |
| 42 LOG(ERROR) << "SSID is not valid UTF-8"; |
| 43 return nullptr; |
| 44 } |
| 45 |
| 46 std::string onc_security; |
| 47 if (!WifiSecurityClassToOncSecurityString(security_class(), &onc_security)) { |
| 48 NOTREACHED() << "Failed to convert SecurityClass with value " |
| 49 << security_class(); |
| 50 return make_scoped_ptr(new base::DictionaryValue()); |
| 51 } |
| 52 |
| 53 scoped_ptr<base::DictionaryValue> onc_properties( |
| 54 new base::DictionaryValue()); |
| 55 onc_properties->Set(onc::toplevel_config::kType, |
| 56 new base::StringValue(onc::network_type::kWiFi)); |
| 57 // TODO(quiche): Switch to the HexSSID property, once ONC fully supports it. |
| 58 // crbug.com/432546. |
| 59 onc_properties->Set(onc::network_config::WifiProperty(onc::wifi::kSSID), |
| 60 new base::StringValue(ssid_utf8)); |
| 61 onc_properties->Set(onc::network_config::WifiProperty(onc::wifi::kSecurity), |
| 62 new base::StringValue(onc_security)); |
| 63 if (WifiSecurityClassSupportsPassphrases(security_class())) { |
| 64 onc_properties->Set( |
| 65 onc::network_config::WifiProperty(onc::wifi::kPassphrase), |
| 66 new base::StringValue(passphrase())); |
| 67 } |
| 68 return onc_properties; |
| 69 } |
| 70 |
| 28 std::string WifiCredential::ToString() const { | 71 std::string WifiCredential::ToString() const { |
| 29 return base::StringPrintf( | 72 return base::StringPrintf( |
| 30 "[SSID (hex): %s, SecurityClass: %d]", | 73 "[SSID (hex): %s, SecurityClass: %d]", |
| 31 base::HexEncode(&ssid_.front(), ssid_.size()).c_str(), | 74 base::HexEncode(&ssid_.front(), ssid_.size()).c_str(), |
| 32 security_class_); // Passphrase deliberately omitted. | 75 security_class_); // Passphrase deliberately omitted. |
| 33 } | 76 } |
| 34 | 77 |
| 35 // static | 78 // static |
| 36 bool WifiCredential::IsLessThan( | 79 bool WifiCredential::IsLessThan( |
| 37 const WifiCredential& a, const WifiCredential& b) { | 80 const WifiCredential& a, const WifiCredential& b) { |
| 38 return a.ssid_ < b.ssid_ || | 81 return a.ssid_ < b.ssid_ || |
| 39 a.security_class_< b.security_class_ || | 82 a.security_class_< b.security_class_ || |
| 40 a.passphrase_ < b.passphrase_; | 83 a.passphrase_ < b.passphrase_; |
| 41 } | 84 } |
| 42 | 85 |
| 43 // static | 86 // static |
| 44 WifiCredential::CredentialSet WifiCredential::MakeSet() { | 87 WifiCredential::CredentialSet WifiCredential::MakeSet() { |
| 45 return CredentialSet(WifiCredential::IsLessThan); | 88 return CredentialSet(WifiCredential::IsLessThan); |
| 46 } | 89 } |
| 47 | 90 |
| 91 // static |
| 92 WifiCredential::SsidBytes WifiCredential::MakeSsidBytesForTest( |
| 93 const std::string& ssid) { |
| 94 return SsidBytes(ssid.begin(), ssid.end()); |
| 95 } |
| 96 |
| 97 // Private methods. |
| 98 |
| 99 WifiCredential::WifiCredential( |
| 100 const std::vector<unsigned char>& ssid, |
| 101 WifiSecurityClass security_class, |
| 102 const std::string& passphrase) |
| 103 : ssid_(ssid), |
| 104 security_class_(security_class), |
| 105 passphrase_(passphrase) { |
| 106 } |
| 107 |
| 48 } // namespace wifi_sync | 108 } // namespace wifi_sync |
| OLD | NEW |