| 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 #ifndef COMPONENTS_WIFI_SYNC_WIFI_CREDENTIAL_H_ | 5 #ifndef COMPONENTS_WIFI_SYNC_WIFI_CREDENTIAL_H_ |
| 6 #define COMPONENTS_WIFI_SYNC_WIFI_CREDENTIAL_H_ | 6 #define COMPONENTS_WIFI_SYNC_WIFI_CREDENTIAL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "components/wifi_sync/wifi_security_class.h" | 15 #include "components/wifi_sync/wifi_security_class.h" |
| 15 | 16 |
| 17 namespace base { |
| 18 class DictionaryValue; |
| 19 } |
| 20 |
| 16 namespace wifi_sync { | 21 namespace wifi_sync { |
| 17 | 22 |
| 18 // A container to hold the information required to locate and connect | 23 // A container to hold the information required to locate and connect |
| 19 // to a WiFi network. | 24 // to a WiFi network. |
| 20 class WifiCredential final { // final because the class is copyable | 25 class WifiCredential final { // final because the class is copyable |
| 21 public: | 26 public: |
| 27 // Per IEEE 802.11-2012 (Sec 8.4.2.2), the only requirement on SSIDs |
| 28 // is that they are between 0 and 32 bytes in length |
| 29 // (inclusive). There are no restrictions on the values of those |
| 30 // bytes. The SSID is not, e.g., required to be encoded as UTF-8. |
| 22 using SsidBytes = std::vector<uint8_t>; | 31 using SsidBytes = std::vector<uint8_t>; |
| 23 using CredentialSet = std::set< | 32 using CredentialSet = std::set< |
| 24 WifiCredential, | 33 WifiCredential, |
| 25 bool(*)(const WifiCredential&a, const WifiCredential& b)>; | 34 bool(*)(const WifiCredential&a, const WifiCredential& b)>; |
| 26 | 35 |
| 27 // Constructs a credential with the given |ssid|, |security_class|, | 36 ~WifiCredential(); |
| 37 |
| 38 // Creates a WifiCredential with the given |ssid|, |security_class|, |
| 28 // and |passphrase|. No assumptions are made about the input | 39 // and |passphrase|. No assumptions are made about the input |
| 29 // encoding of |ssid|. The passphrase must be valid UTF-8. | 40 // encoding of |ssid|. |passphrase|, however, must be valid |
| 30 WifiCredential(const SsidBytes& ssid, | 41 // UTF-8. Returns NULL if the parameters are invalid. |
| 31 WifiSecurityClass security_class, | 42 static scoped_ptr<WifiCredential> Create( |
| 32 const std::string& passphrase); | 43 const SsidBytes& ssid, |
| 33 ~WifiCredential(); | 44 WifiSecurityClass security_class, |
| 45 const std::string& passphrase); |
| 34 | 46 |
| 35 const SsidBytes& ssid() const { return ssid_; } | 47 const SsidBytes& ssid() const { return ssid_; } |
| 36 WifiSecurityClass security_class() const { return security_class_; } | 48 WifiSecurityClass security_class() const { return security_class_; } |
| 37 const std::string& passphrase() const { return passphrase_; } | 49 const std::string& passphrase() const { return passphrase_; } |
| 38 | 50 |
| 51 // Returns a dictionary which represents this WifiCredential as ONC |
| 52 // properties. The resulting dictionary can be used, e.g, to |
| 53 // configure a new network using |
| 54 // chromeos::NetworkConfigurationHandler::CreateConfiguration. Due |
| 55 // to limitations in ONC, this operation fails if ssid() is not |
| 56 // valid UTF-8. In case of failure, returns a scoped_ptr with value |
| 57 // nullptr. |
| 58 scoped_ptr<base::DictionaryValue> ToOncProperties() const; |
| 59 |
| 39 // Returns a string representation of the credential, for debugging | 60 // Returns a string representation of the credential, for debugging |
| 40 // purposes. The string will not include the credential's passphrase. | 61 // purposes. The string will not include the credential's passphrase. |
| 41 std::string ToString() const; | 62 std::string ToString() const; |
| 42 | 63 |
| 43 // Returns true if credential |a| comes before credential |b|. | 64 // Returns true if credential |a| comes before credential |b|. |
| 44 static bool IsLessThan(const WifiCredential& a, const WifiCredential& b); | 65 static bool IsLessThan(const WifiCredential& a, const WifiCredential& b); |
| 45 | 66 |
| 46 // Returns an empty set of WifiCredentials, with the IsLessThan | 67 // Returns an empty set of WifiCredentials, with the IsLessThan |
| 47 // ordering function plumbed in. | 68 // ordering function plumbed in. |
| 48 static CredentialSet MakeSet(); | 69 static CredentialSet MakeSet(); |
| 49 | 70 |
| 71 // Returns |ssid| as an SsidBytes instance. This convenience |
| 72 // function simplifies some tests, which need to instantiate |
| 73 // SsidBytes from string literals. |
| 74 static SsidBytes MakeSsidBytesForTest(const std::string& ssid); |
| 75 |
| 50 private: | 76 private: |
| 77 // Constructs a credential with the given |ssid|, |security_class|, |
| 78 // and |passphrase|. |
| 79 WifiCredential(const SsidBytes& ssid, |
| 80 WifiSecurityClass security_class, |
| 81 const std::string& passphrase); |
| 82 |
| 51 // The WiFi network's SSID. | 83 // The WiFi network's SSID. |
| 52 const SsidBytes ssid_; | 84 const SsidBytes ssid_; |
| 53 // The WiFi network's security class (e.g. WEP, PSK). | 85 // The WiFi network's security class (e.g. WEP, PSK). |
| 54 const WifiSecurityClass security_class_; | 86 const WifiSecurityClass security_class_; |
| 55 // The passphrase for connecting to the network. | 87 // The passphrase for connecting to the network. |
| 56 const std::string passphrase_; | 88 const std::string passphrase_; |
| 57 }; | 89 }; |
| 58 | 90 |
| 59 } // namespace wifi_sync | 91 } // namespace wifi_sync |
| 60 | 92 |
| 61 #endif // COMPONENTS_WIFI_SYNC_WIFI_CREDENTIAL_H_ | 93 #endif // COMPONENTS_WIFI_SYNC_WIFI_CREDENTIAL_H_ |
| OLD | NEW |