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" |
13 #include "chrome/browser/chromeos/cros/network_parser.h" | 14 #include "chrome/browser/chromeos/cros/network_parser.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class DictionaryValue; | 17 class DictionaryValue; |
17 class ListValue; | 18 class ListValue; |
18 class Value; | 19 class Value; |
19 } | 20 } |
20 | 21 |
21 namespace chromeos { | 22 namespace chromeos { |
22 | 23 |
| 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 |
23 // This is the network parser that parses the data from an Open Network | 35 // This is the network parser that parses the data from an Open Network |
24 // Configuration (ONC) file. ONC files are in JSON format that describes | 36 // Configuration (ONC) file. ONC files are in JSON format that describes |
25 // networks. We will use this parser to parse the ONC JSON blob. | 37 // networks. We will use this parser to parse the ONC JSON blob. |
26 // | 38 // |
27 // For ONC file format, see: http://dev.chromium.org/chromium-os/ | 39 // For ONC file format, see: http://dev.chromium.org/chromium-os/ |
28 // chromiumos-design-docs/open-network-configuration | 40 // chromiumos-design-docs/open-network-configuration |
29 class OncNetworkParser : public NetworkParser { | 41 class OncNetworkParser : public NetworkParser { |
30 public: | 42 public: |
| 43 typedef bool (*ParserPointer)(OncNetworkParser*, |
| 44 PropertyIndex, |
| 45 const base::Value&, |
| 46 Network*); |
| 47 |
31 explicit OncNetworkParser(const std::string& onc_blob); | 48 explicit OncNetworkParser(const std::string& onc_blob); |
32 virtual ~OncNetworkParser(); | 49 virtual ~OncNetworkParser(); |
33 static const EnumMapper<PropertyIndex>* property_mapper(); | 50 static const EnumMapper<PropertyIndex>* property_mapper(); |
34 | 51 |
35 // Returns the number of networks in the "NetworkConfigs" list. | 52 // Returns the number of networks in the "NetworkConfigs" list. |
36 int GetNetworkConfigsSize() const; | 53 int GetNetworkConfigsSize() const; |
37 | 54 |
38 // Returns the number of certificates in the "Certificates" list. | 55 // Returns the number of certificates in the "Certificates" list. |
39 int GetCertificatesSize() const; | 56 int GetCertificatesSize() const; |
40 | 57 |
41 // Call to create the network by parsing network config in the nth position. | 58 // Call to create the network by parsing network config in the nth position. |
42 // (0-based). Returns NULL if there's a parse error or if n is out of range. | 59 // (0-based). Returns NULL if there's a parse error or if n is out of range. |
43 Network* ParseNetwork(int n); | 60 Network* ParseNetwork(int n); |
44 | 61 |
45 // Call to parse and import the nth certificate in the certificate | 62 // Call to parse and import the nth certificate in the certificate |
46 // list. Returns false on failure. | 63 // list. Returns false on failure. |
47 bool ParseCertificate(int n); | 64 bool ParseCertificate(int n); |
48 | 65 |
49 virtual Network* CreateNetworkFromInfo(const std::string& service_path, | 66 virtual Network* CreateNetworkFromInfo(const std::string& service_path, |
50 const base::DictionaryValue& info) OVERRIDE; | 67 const base::DictionaryValue& info) OVERRIDE; |
51 | 68 |
| 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 |
52 const std::string& parse_error() const { return parse_error_; } | 78 const std::string& parse_error() const { return parse_error_; } |
53 | 79 |
54 protected: | 80 protected: |
55 OncNetworkParser(); | 81 OncNetworkParser(); |
56 | 82 |
57 virtual Network* CreateNewNetwork(ConnectionType type, | 83 virtual Network* CreateNewNetwork(ConnectionType type, |
58 const std::string& service_path) OVERRIDE; | 84 const std::string& service_path) OVERRIDE; |
59 virtual ConnectionType ParseType(const std::string& type) OVERRIDE; | 85 virtual ConnectionType ParseType(const std::string& type) OVERRIDE; |
60 virtual ConnectionType ParseTypeFromDictionary( | 86 virtual ConnectionType ParseTypeFromDictionary( |
61 const base::DictionaryValue& info) OVERRIDE; | 87 const base::DictionaryValue& info) OVERRIDE; |
62 | 88 |
63 // Returns the type string from the dictionary of network values. | 89 // Returns the type string from the dictionary of network values. |
64 std::string GetTypeFromDictionary(const base::DictionaryValue& info); | 90 std::string GetTypeFromDictionary(const base::DictionaryValue& info); |
65 | 91 |
66 // Returns the GUID string from the dictionary of network values. | 92 // Returns the GUID string from the dictionary of network values. |
67 std::string GetGuidFromDictionary(const base::DictionaryValue& info); | 93 std::string GetGuidFromDictionary(const base::DictionaryValue& info); |
68 | 94 |
| 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 |
69 private: | 106 private: |
70 bool ParseServerOrCaCertificate( | 107 bool ParseServerOrCaCertificate( |
71 int cert_index, | 108 int cert_index, |
72 const std::string& cert_type, | 109 const std::string& cert_type, |
73 base::DictionaryValue* certificate); | 110 base::DictionaryValue* certificate); |
74 bool ParseClientCertificate( | 111 bool ParseClientCertificate( |
75 int cert_index, | 112 int cert_index, |
76 base::DictionaryValue* certificate); | 113 base::DictionaryValue* certificate); |
77 | 114 |
78 // Error message from the JSON parser, if applicable. | 115 // Error message from the JSON parser, if applicable. |
79 std::string parse_error_; | 116 std::string parse_error_; |
80 | 117 |
81 scoped_ptr<base::DictionaryValue> root_dict_; | 118 scoped_ptr<base::DictionaryValue> root_dict_; |
82 base::ListValue* network_configs_; | 119 base::ListValue* network_configs_; |
83 base::ListValue* certificates_; | 120 base::ListValue* certificates_; |
84 | 121 |
85 DISALLOW_COPY_AND_ASSIGN(OncNetworkParser); | 122 DISALLOW_COPY_AND_ASSIGN(OncNetworkParser); |
86 }; | 123 }; |
87 | 124 |
88 // Base for wireless networks. | 125 // Base for wireless networks. |
89 class OncWirelessNetworkParser : public OncNetworkParser { | 126 class OncWirelessNetworkParser : public OncNetworkParser { |
90 public: | 127 public: |
91 OncWirelessNetworkParser(); | 128 OncWirelessNetworkParser(); |
92 virtual ~OncWirelessNetworkParser(); | 129 virtual ~OncWirelessNetworkParser(); |
93 virtual bool ParseValue(PropertyIndex index, | 130 |
94 const base::Value& value, | |
95 Network* network) OVERRIDE; | |
96 private: | 131 private: |
97 DISALLOW_COPY_AND_ASSIGN(OncWirelessNetworkParser); | 132 DISALLOW_COPY_AND_ASSIGN(OncWirelessNetworkParser); |
98 }; | 133 }; |
99 | 134 |
| 135 // Class for parsing Wi-Fi networks. |
100 class OncWifiNetworkParser : public OncWirelessNetworkParser { | 136 class OncWifiNetworkParser : public OncWirelessNetworkParser { |
101 public: | 137 public: |
102 OncWifiNetworkParser(); | 138 OncWifiNetworkParser(); |
103 virtual ~OncWifiNetworkParser(); | 139 virtual ~OncWifiNetworkParser(); |
104 virtual bool ParseValue(PropertyIndex index, | 140 static bool ParseWifiValue(OncNetworkParser* parser, |
105 const base::Value& value, | 141 PropertyIndex index, |
106 Network* network) OVERRIDE; | 142 const base::Value& value, |
| 143 Network* wifi_network); |
| 144 |
107 protected: | 145 protected: |
108 bool ParseEAPValue(PropertyIndex index, | 146 static bool ParseEAPValue(OncNetworkParser* parser, |
109 const base::Value& value, | 147 PropertyIndex index, |
110 WifiNetwork* wifi_network); | 148 const base::Value& value, |
111 ConnectionSecurity ParseSecurity(const std::string& security); | 149 Network* wifi_network); |
112 EAPMethod ParseEAPMethod(const std::string& method); | 150 static ConnectionSecurity ParseSecurity(const std::string& security); |
113 EAPPhase2Auth ParseEAPPhase2Auth(const std::string& auth); | 151 static EAPMethod ParseEAPMethod(const std::string& method); |
| 152 static EAPPhase2Auth ParseEAPPhase2Auth(const std::string& auth); |
| 153 |
114 private: | 154 private: |
115 DISALLOW_COPY_AND_ASSIGN(OncWifiNetworkParser); | 155 DISALLOW_COPY_AND_ASSIGN(OncWifiNetworkParser); |
116 }; | 156 }; |
117 | 157 |
| 158 // Class for parsing virtual private networks. |
118 class OncVirtualNetworkParser : public OncNetworkParser { | 159 class OncVirtualNetworkParser : public OncNetworkParser { |
119 public: | 160 public: |
120 OncVirtualNetworkParser(); | 161 OncVirtualNetworkParser(); |
121 virtual ~OncVirtualNetworkParser(); | 162 virtual ~OncVirtualNetworkParser(); |
122 virtual bool ParseValue(PropertyIndex index, | |
123 const base::Value& value, | |
124 Network* network) OVERRIDE; | |
125 virtual bool UpdateNetworkFromInfo( | 163 virtual bool UpdateNetworkFromInfo( |
126 const base::DictionaryValue& info, Network* network) OVERRIDE; | 164 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 |
127 protected: | 185 protected: |
128 bool ParseProviderValue(PropertyIndex index, | 186 static bool ParseIPsecValue(OncNetworkParser* parser, |
129 const base::Value& value, | 187 PropertyIndex index, |
130 VirtualNetwork* network); | 188 const base::Value& value, |
131 ProviderType ParseProviderType(const std::string& type); | 189 Network* network); |
| 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 |
132 private: | 199 private: |
133 DISALLOW_COPY_AND_ASSIGN(OncVirtualNetworkParser); | 200 DISALLOW_COPY_AND_ASSIGN(OncVirtualNetworkParser); |
134 }; | 201 }; |
135 | 202 |
136 } // namespace chromeos | 203 } // namespace chromeos |
137 | 204 |
138 #endif // CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ | 205 #endif // CHROME_BROWSER_CHROMEOS_CROS_ONC_NETWORK_PARSER_H_ |
OLD | NEW |