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