| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" // for OVERRIDE | 11 #include "base/compiler_specific.h" // for OVERRIDE |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/chromeos/cros/network_parser.h" | 13 #include "chrome/browser/chromeos/cros/network_parser.h" |
| 15 | 14 |
| 16 namespace base { | 15 namespace base { |
| 17 class DictionaryValue; | 16 class DictionaryValue; |
| 18 class ListValue; | 17 class ListValue; |
| 19 class Value; | 18 class Value; |
| 20 } | 19 } |
| 21 | 20 |
| 22 namespace chromeos { | 21 namespace chromeos { |
| 23 | 22 |
| 24 // This is a simple representation of the signature of an ONC typed | |
| 25 // field, used in validation and translation. It could be extended | |
| 26 // to include more complex rules of when the field is required/optional, | |
| 27 // as well as to handle "enum" types, which are strings with a small | |
| 28 // static set of possible values. | |
| 29 struct OncValueSignature { | |
| 30 const char* field; | |
| 31 PropertyIndex index; | |
| 32 base::Value::Type type; | |
| 33 }; | |
| 34 | |
| 35 // This is the network parser that parses the data from an Open Network | 23 // This is the network parser that parses the data from an Open Network |
| 36 // Configuration (ONC) file. ONC files are in JSON format that describes | 24 // Configuration (ONC) file. ONC files are in JSON format that describes |
| 37 // networks. We will use this parser to parse the ONC JSON blob. | 25 // networks. We will use this parser to parse the ONC JSON blob. |
| 38 // | 26 // |
| 39 // For ONC file format, see: http://dev.chromium.org/chromium-os/ | 27 // For ONC file format, see: http://dev.chromium.org/chromium-os/ |
| 40 // chromiumos-design-docs/open-network-configuration | 28 // chromiumos-design-docs/open-network-configuration |
| 41 class OncNetworkParser : public NetworkParser { | 29 class OncNetworkParser : public NetworkParser { |
| 42 public: | 30 public: |
| 43 typedef bool (*ParserPointer)(OncNetworkParser*, | |
| 44 PropertyIndex, | |
| 45 const base::Value&, | |
| 46 Network*); | |
| 47 | |
| 48 explicit OncNetworkParser(const std::string& onc_blob); | 31 explicit OncNetworkParser(const std::string& onc_blob); |
| 49 virtual ~OncNetworkParser(); | 32 virtual ~OncNetworkParser(); |
| 50 static const EnumMapper<PropertyIndex>* property_mapper(); | 33 static const EnumMapper<PropertyIndex>* property_mapper(); |
| 51 | 34 |
| 52 // Returns the number of networks in the "NetworkConfigs" list. | 35 // Returns the number of networks in the "NetworkConfigs" list. |
| 53 int GetNetworkConfigsSize() const; | 36 int GetNetworkConfigsSize() const; |
| 54 | 37 |
| 55 // Returns the number of certificates in the "Certificates" list. | 38 // Returns the number of certificates in the "Certificates" list. |
| 56 int GetCertificatesSize() const; | 39 int GetCertificatesSize() const; |
| 57 | 40 |
| 58 // Call to create the network by parsing network config in the nth position. | 41 // Call to create the network by parsing network config in the nth position. |
| 59 // (0-based). Returns NULL if there's a parse error or if n is out of range. | 42 // (0-based). Returns NULL if there's a parse error or if n is out of range. |
| 60 Network* ParseNetwork(int n); | 43 Network* ParseNetwork(int n); |
| 61 | 44 |
| 62 // Call to parse and import the nth certificate in the certificate | 45 // Call to parse and import the nth certificate in the certificate |
| 63 // list. Returns false on failure. | 46 // list. Returns false on failure. |
| 64 bool ParseCertificate(int n); | 47 bool ParseCertificate(int n); |
| 65 | 48 |
| 66 virtual Network* CreateNetworkFromInfo(const std::string& service_path, | 49 virtual Network* CreateNetworkFromInfo(const std::string& service_path, |
| 67 const base::DictionaryValue& info) OVERRIDE; | 50 const base::DictionaryValue& info) OVERRIDE; |
| 68 | 51 |
| 69 // Parses a nested ONC object with the given mapper and parser function. | |
| 70 // If Value is not the proper type or there is an error in parsing | |
| 71 // any individual field, VLOGs diagnostics, and returns false. | |
| 72 bool ParseNestedObject(Network* network, | |
| 73 const std::string& onc_type, | |
| 74 const base::Value& value, | |
| 75 OncValueSignature* signature, | |
| 76 ParserPointer parser); | |
| 77 | |
| 78 const std::string& parse_error() const { return parse_error_; } | 52 const std::string& parse_error() const { return parse_error_; } |
| 79 | 53 |
| 80 protected: | 54 protected: |
| 81 OncNetworkParser(); | 55 OncNetworkParser(); |
| 82 | 56 |
| 83 virtual Network* CreateNewNetwork(ConnectionType type, | 57 virtual Network* CreateNewNetwork(ConnectionType type, |
| 84 const std::string& service_path) OVERRIDE; | 58 const std::string& service_path) OVERRIDE; |
| 85 virtual ConnectionType ParseType(const std::string& type) OVERRIDE; | 59 virtual ConnectionType ParseType(const std::string& type) OVERRIDE; |
| 86 virtual ConnectionType ParseTypeFromDictionary( | 60 virtual ConnectionType ParseTypeFromDictionary( |
| 87 const base::DictionaryValue& info) OVERRIDE; | 61 const base::DictionaryValue& info) OVERRIDE; |
| 88 | 62 |
| 89 // Returns the type string from the dictionary of network values. | 63 // Returns the type string from the dictionary of network values. |
| 90 std::string GetTypeFromDictionary(const base::DictionaryValue& info); | 64 std::string GetTypeFromDictionary(const base::DictionaryValue& info); |
| 91 | 65 |
| 92 // Returns the GUID string from the dictionary of network values. | 66 // Returns the GUID string from the dictionary of network values. |
| 93 std::string GetGuidFromDictionary(const base::DictionaryValue& info); | 67 std::string GetGuidFromDictionary(const base::DictionaryValue& info); |
| 94 | 68 |
| 95 // Parse a field's value in the NetworkConfiguration object. | |
| 96 static bool ParseNetworkConfigurationValue(OncNetworkParser* parser, | |
| 97 PropertyIndex index, | |
| 98 const base::Value& value, | |
| 99 Network* network); | |
| 100 | |
| 101 // Issue a diagnostic and return false if a type mismatch is found. | |
| 102 static bool CheckNetworkType(Network* network, | |
| 103 ConnectionType expected, | |
| 104 const std::string& onc_type); | |
| 105 | |
| 106 private: | 69 private: |
| 107 bool ParseServerOrCaCertificate( | 70 bool ParseServerOrCaCertificate( |
| 108 int cert_index, | 71 int cert_index, |
| 109 const std::string& cert_type, | 72 const std::string& cert_type, |
| 110 base::DictionaryValue* certificate); | 73 base::DictionaryValue* certificate); |
| 111 bool ParseClientCertificate( | 74 bool ParseClientCertificate( |
| 112 int cert_index, | 75 int cert_index, |
| 113 base::DictionaryValue* certificate); | 76 base::DictionaryValue* certificate); |
| 114 | 77 |
| 115 // Error message from the JSON parser, if applicable. | 78 // Error message from the JSON parser, if applicable. |
| 116 std::string parse_error_; | 79 std::string parse_error_; |
| 117 | 80 |
| 118 scoped_ptr<base::DictionaryValue> root_dict_; | 81 scoped_ptr<base::DictionaryValue> root_dict_; |
| 119 base::ListValue* network_configs_; | 82 base::ListValue* network_configs_; |
| 120 base::ListValue* certificates_; | 83 base::ListValue* certificates_; |
| 121 | 84 |
| 122 DISALLOW_COPY_AND_ASSIGN(OncNetworkParser); | 85 DISALLOW_COPY_AND_ASSIGN(OncNetworkParser); |
| 123 }; | 86 }; |
| 124 | 87 |
| 125 // Base for wireless networks. | 88 // Base for wireless networks. |
| 126 class OncWirelessNetworkParser : public OncNetworkParser { | 89 class OncWirelessNetworkParser : public OncNetworkParser { |
| 127 public: | 90 public: |
| 128 OncWirelessNetworkParser(); | 91 OncWirelessNetworkParser(); |
| 129 virtual ~OncWirelessNetworkParser(); | 92 virtual ~OncWirelessNetworkParser(); |
| 130 | 93 virtual bool ParseValue(PropertyIndex index, |
| 94 const base::Value& value, |
| 95 Network* network) OVERRIDE; |
| 131 private: | 96 private: |
| 132 DISALLOW_COPY_AND_ASSIGN(OncWirelessNetworkParser); | 97 DISALLOW_COPY_AND_ASSIGN(OncWirelessNetworkParser); |
| 133 }; | 98 }; |
| 134 | 99 |
| 135 // Class for parsing Wi-Fi networks. | |
| 136 class OncWifiNetworkParser : public OncWirelessNetworkParser { | 100 class OncWifiNetworkParser : public OncWirelessNetworkParser { |
| 137 public: | 101 public: |
| 138 OncWifiNetworkParser(); | 102 OncWifiNetworkParser(); |
| 139 virtual ~OncWifiNetworkParser(); | 103 virtual ~OncWifiNetworkParser(); |
| 140 static bool ParseWifiValue(OncNetworkParser* parser, | 104 virtual bool ParseValue(PropertyIndex index, |
| 141 PropertyIndex index, | 105 const base::Value& value, |
| 142 const base::Value& value, | 106 Network* network) OVERRIDE; |
| 143 Network* wifi_network); | |
| 144 | |
| 145 protected: | 107 protected: |
| 146 static bool ParseEAPValue(OncNetworkParser* parser, | 108 bool ParseEAPValue(PropertyIndex index, |
| 147 PropertyIndex index, | 109 const base::Value& value, |
| 148 const base::Value& value, | 110 WifiNetwork* wifi_network); |
| 149 Network* wifi_network); | 111 ConnectionSecurity ParseSecurity(const std::string& security); |
| 150 static ConnectionSecurity ParseSecurity(const std::string& security); | 112 EAPMethod ParseEAPMethod(const std::string& method); |
| 151 static EAPMethod ParseEAPMethod(const std::string& method); | 113 EAPPhase2Auth ParseEAPPhase2Auth(const std::string& auth); |
| 152 static EAPPhase2Auth ParseEAPPhase2Auth(const std::string& auth); | |
| 153 | |
| 154 private: | 114 private: |
| 155 DISALLOW_COPY_AND_ASSIGN(OncWifiNetworkParser); | 115 DISALLOW_COPY_AND_ASSIGN(OncWifiNetworkParser); |
| 156 }; | 116 }; |
| 157 | 117 |
| 158 // Class for parsing virtual private networks. | |
| 159 class OncVirtualNetworkParser : public OncNetworkParser { | 118 class OncVirtualNetworkParser : public OncNetworkParser { |
| 160 public: | 119 public: |
| 161 OncVirtualNetworkParser(); | 120 OncVirtualNetworkParser(); |
| 162 virtual ~OncVirtualNetworkParser(); | 121 virtual ~OncVirtualNetworkParser(); |
| 122 virtual bool ParseValue(PropertyIndex index, |
| 123 const base::Value& value, |
| 124 Network* network) OVERRIDE; |
| 163 virtual bool UpdateNetworkFromInfo( | 125 virtual bool UpdateNetworkFromInfo( |
| 164 const base::DictionaryValue& info, Network* network) OVERRIDE; | 126 const base::DictionaryValue& info, Network* network) OVERRIDE; |
| 165 static bool ParseVPNValue(OncNetworkParser* parser, | |
| 166 PropertyIndex index, | |
| 167 const base::Value& value, | |
| 168 Network* network); | |
| 169 | |
| 170 // network_library combines provider type and authentication type | |
| 171 // (L2TP-IPsec with PSK vs with Certificates). This function | |
| 172 // takes a provider type and adds an authentication type to return | |
| 173 // the updated provider type. | |
| 174 static ProviderType UpdateProviderTypeWithAuthType( | |
| 175 ProviderType provider, | |
| 176 const std::string& auth_type); | |
| 177 | |
| 178 // This function takes a provider type (which includes authentication | |
| 179 // type) and returns the canonical provider type from it. For instance | |
| 180 // for L2TP-IPsec, the PSK provider type is the canonical one. | |
| 181 static ProviderType GetCanonicalProviderType(ProviderType provider_type); | |
| 182 | |
| 183 static ProviderType ParseProviderType(const std::string& type); | |
| 184 | |
| 185 protected: | 127 protected: |
| 186 static bool ParseIPsecValue(OncNetworkParser* parser, | 128 bool ParseProviderValue(PropertyIndex index, |
| 187 PropertyIndex index, | 129 const base::Value& value, |
| 188 const base::Value& value, | 130 VirtualNetwork* network); |
| 189 Network* network); | 131 ProviderType ParseProviderType(const std::string& type); |
| 190 static bool ParseL2TPValue(OncNetworkParser* parser, | |
| 191 PropertyIndex index, | |
| 192 const base::Value& value, | |
| 193 Network* network); | |
| 194 static bool ParseOpenVPNValue(OncNetworkParser* parser, | |
| 195 PropertyIndex index, | |
| 196 const base::Value& value, | |
| 197 Network* network); | |
| 198 | |
| 199 private: | 132 private: |
| 200 DISALLOW_COPY_AND_ASSIGN(OncVirtualNetworkParser); | 133 DISALLOW_COPY_AND_ASSIGN(OncVirtualNetworkParser); |
| 201 }; | 134 }; |
| 202 | 135 |
| 203 } // namespace chromeos | 136 } // namespace chromeos |
| 204 | 137 |
| 205 #endif // CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ | 138 #endif // CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ |
| OLD | NEW |