Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / components / wifi_sync / wifi_credential.cc
blob490cd7b0a9ffb4e278cde77125e564fec395f766
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"
14 namespace wifi_sync {
16 WifiCredential::~WifiCredential() {
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;
29 if (!base::StreamingUtf8Validator::Validate(passphrase)) {
30 LOG(ERROR) << "Passphrase is not valid UTF-8";
31 return nullptr;
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.
40 // crbug.com/432546.
41 if (!base::StreamingUtf8Validator::Validate(ssid_utf8)) {
42 LOG(ERROR) << "SSID is not valid UTF-8";
43 return nullptr;
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());
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()));
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.
78 // static
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_;
86 // static
87 WifiCredential::CredentialSet WifiCredential::MakeSet() {
88 return CredentialSet(WifiCredential::IsLessThan);
91 // static
92 WifiCredential::SsidBytes WifiCredential::MakeSsidBytesForTest(
93 const std::string& ssid) {
94 return SsidBytes(ssid.begin(), ssid.end());
97 // Private methods.
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) {
108 } // namespace wifi_sync