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.
5 #include "components/wifi_sync/wifi_credential.h"
7 #include "base/i18n/streaming_utf8_validator.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/values.h"
12 #include "components/onc/onc_constants.h"
16 WifiCredential::~WifiCredential() {
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.";
29 if (!base::StreamingUtf8Validator::Validate(passphrase
)) {
30 LOG(ERROR
) << "Passphrase is not valid UTF-8";
34 return make_scoped_ptr(new WifiCredential(ssid
, security_class
, passphrase
));
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.
41 if (!base::StreamingUtf8Validator::Validate(ssid_utf8
)) {
42 LOG(ERROR
) << "SSID is not valid UTF-8";
46 std::string onc_security
;
47 if (!WifiSecurityClassToOncSecurityString(security_class(), &onc_security
)) {
48 NOTREACHED() << "Failed to convert SecurityClass with value "
50 return make_scoped_ptr(new base::DictionaryValue());
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.
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())) {
65 onc::network_config::WifiProperty(onc::wifi::kPassphrase
),
66 new base::StringValue(passphrase()));
68 return onc_properties
;
71 std::string
WifiCredential::ToString() const {
72 return base::StringPrintf(
73 "[SSID (hex): %s, SecurityClass: %d]",
74 base::HexEncode(&ssid_
.front(), ssid_
.size()).c_str(),
75 security_class_
); // Passphrase deliberately omitted.
79 bool WifiCredential::IsLessThan(
80 const WifiCredential
& a
, const WifiCredential
& b
) {
81 return a
.ssid_
< b
.ssid_
||
82 a
.security_class_
< b
.security_class_
||
83 a
.passphrase_
< b
.passphrase_
;
87 WifiCredential::CredentialSet
WifiCredential::MakeSet() {
88 return CredentialSet(WifiCredential::IsLessThan
);
92 WifiCredential::SsidBytes
WifiCredential::MakeSsidBytesForTest(
93 const std::string
& ssid
) {
94 return SsidBytes(ssid
.begin(), ssid
.end());
99 WifiCredential::WifiCredential(
100 const std::vector
<unsigned char>& ssid
,
101 WifiSecurityClass security_class
,
102 const std::string
& passphrase
)
104 security_class_(security_class
),
105 passphrase_(passphrase
) {
108 } // namespace wifi_sync