Chromium Code Reviews| 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 scoped_ptr<base::DictionaryValue> WifiCredential::ToOncProperties() const { | |
| 20 const std::string ssid_utf8(ssid().begin(), ssid().end()); | |
| 21 // TODO(quiche): Remove this test, once ONC suports non-UTF-8 SSIDs. | |
| 22 // crbug.com/432546. | |
| 23 if (!base::StreamingUtf8Validator::Validate(ssid_utf8)) { | |
| 24 LOG(ERROR) << "SSID is not valid UTF-8"; | |
| 25 return nullptr; | |
| 26 } | |
| 27 | |
| 28 std::string onc_security; | |
| 29 if (!WifiSecurityClassToOncSecurityString(security_class(), &onc_security)) { | |
| 30 NOTREACHED() << "Failed to convert SecurityClass with value " | |
| 31 << security_class(); | |
| 32 return nullptr; | |
| 33 } | |
| 34 | |
| 35 auto onc_properties = make_scoped_ptr(new base::DictionaryValue()); | |
|
mukesh agrawal
2015/01/09 02:00:18
I think auto is appropriate here, since the rest o
erikwright (departed)
2015/01/09 14:58:20
This would be more typically done without the make
mukesh agrawal
2015/01/09 19:00:02
Done.
| |
| 36 onc_properties->Set(onc::toplevel_config::kType, | |
| 37 new base::StringValue(onc::network_type::kWiFi)); | |
| 38 // TODO(quiche): Switch to the HexSSID property, once ONC fully supports it. | |
| 39 // crbug.com/432546. | |
| 40 onc_properties->Set(onc::network_config::WifiProperty(onc::wifi::kSSID), | |
| 41 new base::StringValue(ssid_utf8)); | |
| 42 onc_properties->Set(onc::network_config::WifiProperty(onc::wifi::kSecurity), | |
| 43 new base::StringValue(onc_security)); | |
| 44 if (WifiSecurityClassSupportsPassphrases(security_class())) { | |
| 45 onc_properties->Set( | |
| 46 onc::network_config::WifiProperty(onc::wifi::kPassphrase), | |
| 47 new base::StringValue(passphrase())); | |
| 48 } | |
| 49 return onc_properties; | |
| 50 } | |
| 51 | |
| 28 std::string WifiCredential::ToString() const { | 52 std::string WifiCredential::ToString() const { |
| 29 return base::StringPrintf( | 53 return base::StringPrintf( |
| 30 "[SSID (hex): %s, SecurityClass: %d]", | 54 "[SSID (hex): %s, SecurityClass: %d]", |
| 31 base::HexEncode(&ssid_.front(), ssid_.size()).c_str(), | 55 base::HexEncode(&ssid_.front(), ssid_.size()).c_str(), |
| 32 security_class_); // Passphrase deliberately omitted. | 56 security_class_); // Passphrase deliberately omitted. |
| 33 } | 57 } |
| 34 | 58 |
| 35 // static | 59 // static |
| 60 scoped_ptr<WifiCredential> WifiCredential::Create( | |
| 61 const SsidBytes& ssid, | |
| 62 WifiSecurityClass security_class, | |
| 63 const std::string& passphrase) { | |
| 64 if (security_class == SECURITY_CLASS_INVALID) { | |
| 65 LOG(ERROR) << "SecurityClass is invalid."; | |
| 66 return nullptr; | |
| 67 } | |
| 68 | |
| 69 if (!base::StreamingUtf8Validator::Validate(passphrase)) { | |
| 70 LOG(ERROR) << "Passphrase is not valid UTF-8"; | |
| 71 return nullptr; | |
| 72 } | |
| 73 | |
| 74 return make_scoped_ptr(new WifiCredential(ssid, security_class, passphrase)); | |
| 75 } | |
| 76 | |
| 77 // static | |
| 36 bool WifiCredential::IsLessThan( | 78 bool WifiCredential::IsLessThan( |
| 37 const WifiCredential& a, const WifiCredential& b) { | 79 const WifiCredential& a, const WifiCredential& b) { |
| 38 return a.ssid_ < b.ssid_ || | 80 return a.ssid_ < b.ssid_ || |
| 39 a.security_class_< b.security_class_ || | 81 a.security_class_< b.security_class_ || |
| 40 a.passphrase_ < b.passphrase_; | 82 a.passphrase_ < b.passphrase_; |
| 41 } | 83 } |
| 42 | 84 |
| 43 // static | 85 // static |
| 44 WifiCredential::CredentialSet WifiCredential::MakeSet() { | 86 WifiCredential::CredentialSet WifiCredential::MakeSet() { |
| 45 return CredentialSet(WifiCredential::IsLessThan); | 87 return CredentialSet(WifiCredential::IsLessThan); |
| 46 } | 88 } |
| 47 | 89 |
| 90 // static | |
| 91 WifiCredential::SsidBytes WifiCredential::MakeSsidBytesForTest( | |
| 92 const std::string& ssid) { | |
| 93 return SsidBytes(ssid.begin(), ssid.end()); | |
| 94 } | |
| 95 | |
| 96 // Private methods. | |
| 97 | |
| 98 WifiCredential::WifiCredential( | |
| 99 const std::vector<unsigned char>& ssid, | |
| 100 WifiSecurityClass security_class, | |
| 101 const std::string& passphrase) | |
| 102 : ssid_(ssid), | |
| 103 security_class_(security_class), | |
| 104 passphrase_(passphrase) { | |
| 105 } | |
| 106 | |
| 48 } // namespace wifi_sync | 107 } // namespace wifi_sync |
| OLD | NEW |