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