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 when a new network is encountered. Returns NULL upon failure. |
| 112 virtual Network* CreateNetworkFromInfo(const std::string& service_path, |
| 113 const DictionaryValue& info); |
| 114 |
| 115 // Called when an existing network is has new information that needs |
| 116 // to be updated. Returns false upon failure. |
| 117 virtual bool UpdateNetworkFromInfo(const DictionaryValue& info, |
| 118 Network* network); |
| 119 |
| 120 // Called when an individual attribute of an existing network has |
| 121 // changed. |index| is a return value that supplies the appropriate |
| 122 // property index for the given key. |index| is filled in even if |
| 123 // the update fails. Returns false upon failure. |
| 124 virtual bool UpdateStatus(const std::string& key, |
| 125 const Value& value, |
| 126 Network* network, |
| 127 PropertyIndex* index); |
| 128 protected: |
| 129 // The NetworkParser does not take ownership of the |mapper|. |
| 130 explicit NetworkParser(const EnumMapper<PropertyIndex>* mapper); |
| 131 virtual ~NetworkParser(); |
| 132 |
| 133 virtual bool ParseValue(PropertyIndex index, |
| 134 const Value& value, |
| 135 Network* network) = 0; |
| 136 virtual ConnectionType ParseType(const std::string& type) = 0; |
| 137 virtual ConnectionType ParseTypeFromDictionary( |
| 138 const DictionaryValue& info) = 0; |
| 139 virtual ConnectionMode ParseMode(const std::string& mode) = 0; |
| 140 virtual ConnectionState ParseState(const std::string& state) = 0; |
| 141 virtual ConnectionError ParseError(const std::string& error) = 0; |
| 142 |
| 143 const EnumMapper<PropertyIndex>& mapper() const { |
| 144 return *mapper_; |
| 145 } |
| 146 |
| 147 private: |
| 148 const EnumMapper<PropertyIndex>* mapper_; |
| 149 DISALLOW_COPY_AND_ASSIGN(NetworkParser); |
| 150 }; |
| 151 |
| 152 } // namespace chromeos |
| 153 |
| 154 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ |
OLD | NEW |