Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 5 | #ifndef SHILL_STATIC_IP_PARAMETERS_H_ |
| 6 | #define SHILL_STATIC_IP_PARAMETERS_H_ |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/memory/ref_counted.h> |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 12 | #include <base/logging.h> |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 13 | |
| 14 | #include "shill/ipconfig.h" |
| 15 | #include "shill/key_value_store.h" |
| 16 | #include "shill/property_store.h" |
| 17 | |
| 18 | namespace shill { |
| 19 | class StoreInterface; |
| 20 | |
| 21 | // Holder for static IP parameters. Includes methods for reading and |
| 22 | // displaying values over a control API, methods for loading and |
| 23 | // storing this to a persistent store, as well as applying these |
| 24 | // parameters to an IPConfig object. |
| 25 | class StaticIPParameters { |
| 26 | public: |
| 27 | StaticIPParameters(); |
| 28 | virtual ~StaticIPParameters(); |
| 29 | |
| 30 | // Take a property store and add static IP parameters to them. |
| 31 | void PlumbPropertyStore(PropertyStore *store); |
| 32 | |
| 33 | // Load static IP parameters from a persistent store with id |storage_id|. |
| 34 | void Load(StoreInterface *storage, const std::string &storage_id); |
| 35 | |
| 36 | // Save static IP parameters to a persistent store with id |storage_id|. |
| 37 | void Save(StoreInterface *storage, const std::string &storage_id); |
| 38 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 39 | // Apply static IP parameters to an IPConfig properties object, and save |
| 40 | // their original values. |
| 41 | void ApplyTo(IPConfig::Properties *props); |
| 42 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 43 | // Restore IP parameters from |saved_args_| to |props|, then clear |
| 44 | // |saved_args_|. |
| 45 | void RestoreTo(IPConfig::Properties *props); |
| 46 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 47 | // Remove any saved parameters from a previous call to ApplyTo(). |
| 48 | void ClearSavedParameters(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 49 | |
| 50 | // Return whether configuration parameters contain an address property. |
| 51 | bool ContainsAddress() const; |
| 52 | |
| 53 | private: |
| 54 | friend class StaticIPParametersTest; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 55 | FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithStatic); |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 56 | FRIEND_TEST(StaticIpParametersTest, SavedParameters); |
Peter Qiu | 08bf508 | 2014-09-08 15:57:12 -0700 | [diff] [blame^] | 57 | FRIEND_TEST(StaticIpParametersTest, SavedParametersDict); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 58 | |
| 59 | struct Property { |
| 60 | enum Type { |
| 61 | kTypeInt32, |
| 62 | kTypeString, |
| 63 | // Properties of type "Strings" are stored as a comma-separated list |
| 64 | // in the control interface and in the profile, but are stored as a |
| 65 | // vector of strings in the IPConfig properties. |
| 66 | kTypeStrings |
| 67 | }; |
| 68 | |
| 69 | const char *name; |
| 70 | Type type; |
| 71 | }; |
| 72 | |
| 73 | static const char kConfigKeyPrefix[]; |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 74 | static const char kSavedConfigKeyPrefix[]; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 75 | static const Property kProperties[]; |
| 76 | |
| 77 | // These functions try to retrieve the argument |property| out of the |
| 78 | // KeyValueStore in |args_|. If that value exists, overwrite |value_out| |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 79 | // with its contents, and save the previous value into |saved_args_|. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 80 | void ApplyInt(const std::string &property, int32_t *value_out); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 81 | void ApplyString(const std::string &property, std::string *value_out); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 82 | void ApplyStrings(const std::string &property, |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 83 | std::vector<std::string> *value_out); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 84 | |
| 85 | void ClearMappedProperty(const size_t &index, Error *error); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 86 | void ClearMappedSavedProperty(const size_t &index, Error *error); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 87 | int32_t GetMappedInt32Property(const size_t &index, Error *error); |
| 88 | int32_t GetMappedSavedInt32Property(const size_t &index, Error *error); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 89 | std::string GetMappedStringProperty(const size_t &index, Error *error); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 90 | std::string GetMappedSavedStringProperty(const size_t &index, Error *error); |
Peter Qiu | 08bf508 | 2014-09-08 15:57:12 -0700 | [diff] [blame^] | 91 | std::string GetMappedStringsProperty(const size_t &index, Error *error); |
| 92 | std::string GetMappedSavedStringsProperty(const size_t &index, Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 93 | bool SetMappedInt32Property( |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 94 | const size_t &index, const int32_t &value, Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 95 | bool SetMappedSavedInt32Property( |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 96 | const size_t &index, const int32_t &value, Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 97 | bool SetMappedStringProperty( |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 98 | const size_t &index, const std::string &value, Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 99 | bool SetMappedSavedStringProperty( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 100 | const size_t &index, const std::string &value, Error *error); |
Peter Qiu | 08bf508 | 2014-09-08 15:57:12 -0700 | [diff] [blame^] | 101 | bool SetMappedStringsProperty( |
| 102 | const size_t &index, const std::string &value, Error *error); |
| 103 | bool SetMappedSavedStringsProperty( |
| 104 | const size_t &index, const std::string &value, Error *error); |
| 105 | |
| 106 | KeyValueStore GetSavedIPConfig(Error *error); |
| 107 | KeyValueStore GetStaticIPConfig(Error *error); |
| 108 | bool SetStaticIPConfig(const KeyValueStore &value, Error *error); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 109 | |
| 110 | KeyValueStore args_; |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 111 | KeyValueStore saved_args_; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 112 | |
| 113 | DISALLOW_COPY_AND_ASSIGN(StaticIPParameters); |
| 114 | }; |
| 115 | |
| 116 | } // namespace shill |
| 117 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 118 | #endif // SHILL_STATIC_IP_PARAMETERS_H_ |