OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <map> | |
11 | |
12 #include "base/scoped_ptr.h" | |
13 #include "chrome/browser/chromeos/cros/network_library.h" | |
14 | |
15 class Value; | |
16 | |
17 namespace chromeos { | |
18 | |
19 class NetworkDevice; | |
20 | |
21 // This turns an array of string-to-enum-value mappings into a class | |
22 // that can cache the mapping and do quick lookups using an actual map | |
23 // class. Usage is something like: | |
24 // | |
25 // const char kKey1[] = "key1"; | |
26 // const char kKey2[] = "key2"; | |
27 // | |
28 // enum EnumFoo { | |
29 // UNKNOWN = 0, | |
30 // FOO = 1, | |
31 // BAR = 2, | |
32 // }; | |
33 // | |
34 // const EnumMapper<EnumFoo>::Pair index_table[] = { | |
35 // { kKey1, FOO }, | |
36 // { kKey2, BAR }, | |
37 // }; | |
38 // | |
39 // EnumMapper<EnumFoo> mapper(index_table, arraysize(index_table), UNKNOWN); | |
40 // EnumFoo value = mapper.Get(kKey1); // Returns FOO. | |
41 // EnumFoo value = mapper.Get('boo'); // Returns UNKNOWN. | |
42 template <typename EnumType> | |
43 class EnumMapper { | |
44 public: | |
45 struct Pair { | |
46 const char* key; | |
47 const EnumType value; | |
48 }; | |
49 | |
50 EnumMapper(const Pair* list, size_t num_entries, EnumType unknown) | |
51 : unknown_value_(unknown) { | |
52 for (size_t i = 0; i < num_entries; ++i, ++list) | |
53 enum_map_[list->key] = list->value; | |
54 } | |
55 | |
56 EnumType Get(const std::string& type) const { | |
57 EnumMapConstIter iter = enum_map_.find(type); | |
58 if (iter != enum_map_.end()) | |
59 return iter->second; | |
60 return unknown_value_; | |
61 } | |
62 | |
63 private: | |
64 typedef typename std::map<std::string, EnumType> EnumMap; | |
65 typedef typename EnumMap::const_iterator EnumMapConstIter; | |
66 EnumMap enum_map_; | |
67 EnumType unknown_value_; | |
68 DISALLOW_COPY_AND_ASSIGN(EnumMapper); | |
69 }; | |
70 | |
71 // This takes a Value of a particular form, and maps the keys in the | |
72 // dictionary to a NetworkDevice object to initialize it properly. | |
73 // Subclasses of this can then customize its methods to parse either | |
74 // libcros (flimflam) data or network setup information obtained from | |
75 // policy or setup file import depending on the EnumMapper supplied. | |
76 class NetworkDeviceParser { | |
77 public: | |
78 virtual NetworkDevice* CreateDeviceFromInfo(const std::string& device_path, | |
79 const DictionaryValue& info); | |
80 virtual bool UpdateDeviceFromInfo(const DictionaryValue& info, | |
81 NetworkDevice* device); | |
82 virtual bool UpdateStatus(const std::string& key, | |
83 const Value& value, | |
84 NetworkDevice* device, | |
85 PropertyIndex* index); | |
86 protected: | |
87 // The NetworkDeviceParser does not take ownership of the |mapper|. | |
88 explicit NetworkDeviceParser(const EnumMapper<PropertyIndex>* mapper); | |
89 virtual ~NetworkDeviceParser(); | |
90 | |
91 virtual bool ParseValue(PropertyIndex index, | |
92 const Value& value, | |
93 NetworkDevice* device) = 0; | |
94 virtual ConnectionType ParseType(const std::string& type) = 0; | |
95 | |
96 const EnumMapper<PropertyIndex>& mapper() const { | |
97 return *mapper_; | |
98 } | |
99 | |
100 private: | |
101 const EnumMapper<PropertyIndex>* mapper_; | |
102 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceParser); | |
103 }; | |
104 | |
105 // This takes a Value of a particular form, and uses the keys in the | |
106 // dictionary to create Network (WiFiNetwork, EthernetNetwork, etc.) | |
107 // objects and initialize them properly. Subclasses of this can then | |
108 // customize its methods to parse other forms of input dictionaries. | |
109 class NetworkParser { | |
110 public: | |
111 // Called from the NetworkLibrary when a new network is encountered. | |
stevenjb
2011/08/05 18:46:58
More generic description (since it might be used f
| |
112 virtual Network* CreateNetworkFromInfo(const std::string& service_path, | |
113 const DictionaryValue& info); | |
114 | |
stevenjb
2011/08/05 18:46:58
Should probably comment the following methods also
Greg Spencer (Chromium)
2011/08/11 22:19:32
Done.
| |
115 virtual bool UpdateNetworkFromInfo(const DictionaryValue& info, | |
116 Network* network); | |
117 | |
118 virtual bool UpdateStatus(const std::string& key, | |
119 const Value& value, | |
120 Network* network, | |
121 PropertyIndex* index); | |
122 protected: | |
123 // The NetworkParser does not take ownership of the |mapper|. | |
124 explicit NetworkParser(const EnumMapper<PropertyIndex>* mapper); | |
125 virtual ~NetworkParser(); | |
126 | |
127 static Network* CreateNewNetwork(ConnectionType type, | |
128 const std::string& service_path); | |
stevenjb
2011/08/05 18:46:58
Does this need to be a static member, or could it
Greg Spencer (Chromium)
2011/08/11 22:19:32
Huh. I guess I changed things. It used to be nee
| |
129 | |
130 virtual bool ParseValue(PropertyIndex index, | |
131 const Value& value, | |
132 Network* network) = 0; | |
133 virtual ConnectionType ParseType(const std::string& type) = 0; | |
134 virtual ConnectionType ParseTypeFromDictionary( | |
135 const DictionaryValue& info) = 0; | |
136 virtual ConnectionMode ParseMode(const std::string& mode) = 0; | |
137 virtual ConnectionState ParseState(const std::string& state) = 0; | |
138 virtual ConnectionError ParseError(const std::string& error) = 0; | |
139 | |
140 const EnumMapper<PropertyIndex>& mapper() const { | |
141 return *mapper_; | |
142 } | |
143 | |
144 private: | |
145 const EnumMapper<PropertyIndex>* mapper_; | |
146 DISALLOW_COPY_AND_ASSIGN(NetworkParser); | |
147 }; | |
148 | |
149 } // namespace chromeos | |
150 | |
151 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ | |
OLD | NEW |