Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2011 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #ifndef SHILL_STORE_INTERFACE_H_
     18 #define SHILL_STORE_INTERFACE_H_
     19 
     20 #include <set>
     21 #include <string>
     22 #include <vector>
     23 
     24 namespace shill {
     25 
     26 class KeyValueStore;
     27 
     28 // An interface to a persistent store implementation.
     29 class StoreInterface {
     30  public:
     31   virtual ~StoreInterface() {}
     32 
     33   // Returns true if the store exists and is non-empty.
     34   virtual bool IsNonEmpty() const = 0;
     35 
     36   // Opens the store. Returns true on success. The effects of
     37   // re-opening an open store are undefined. The effects of calling a
     38   // getter or setter on an unopened store are also undefined.
     39   virtual bool Open() = 0;
     40 
     41   // Closes the store and flushes it to persistent storage. Returns
     42   // true on success. Note that the store is considered closed even if
     43   // Close returns false. The effects of closing and already closed
     44   // store are undefined.
     45   virtual bool Close() = 0;
     46 
     47   // Flush current in-memory data to disk.
     48   virtual bool Flush() = 0;
     49 
     50   // Mark the underlying file store as corrupted, moving the data file
     51   // to a new filename.  This will prevent the file from being re-opened
     52   // the next time Open() is called.
     53   virtual bool MarkAsCorrupted() = 0;
     54 
     55   // Returns a set of all groups contained in the store.
     56   virtual std::set<std::string> GetGroups() const = 0;
     57 
     58   // Returns the names of all groups that contain the named |key|.
     59   virtual std::set<std::string> GetGroupsWithKey(
     60       const std::string& key) const = 0;
     61 
     62   // Returns the names of all groups that contain the named |properties|.
     63   // Only the Bool, Int and String properties are checked.
     64   virtual std::set<std::string> GetGroupsWithProperties(
     65       const KeyValueStore& properties) const = 0;
     66 
     67   // Returns true if the store contains |group|, false otherwise.
     68   virtual bool ContainsGroup(const std::string& group) const = 0;
     69 
     70   // Deletes |group|:|key|. Returns true on success. It is an error to
     71   // delete from a group that does not exist. It is, however,
     72   // permitted to delete a non-existent key from a group that does
     73   // exist.
     74   virtual bool DeleteKey(const std::string& group, const std::string& key) = 0;
     75 
     76   // Deletes |group|. Returns true on success. It is not an error to
     77   // delete a group that does not exist.
     78   virtual bool DeleteGroup(const std::string& group) = 0;
     79 
     80   // Sets a descriptive header on the key file.
     81   virtual bool SetHeader(const std::string& header) = 0;
     82 
     83   // Gets a string |value| associated with |group|:|key|. Returns true on
     84   // success and false on failure (including when |group|:|key| is not present
     85   // in the store).  It is not an error to pass NULL as |value| to simply
     86   // test for the presence of this value.
     87   virtual bool GetString(const std::string& group,
     88                          const std::string& key,
     89                          std::string* value) const = 0;
     90 
     91   // Associates |group|:|key| with a string |value|. Returns true on success,
     92   // false otherwise.
     93   virtual bool SetString(const std::string& group,
     94                          const std::string& key,
     95                          const std::string& value) = 0;
     96 
     97   // Gets a boolean |value| associated with |group|:|key|. Returns true on
     98   // success and false on failure (including when the |group|:|key| is not
     99   // present in the store).  It is not an error to pass NULL as |value| to
    100   // simply test for the presence of this value.
    101 
    102   virtual bool GetBool(const std::string& group,
    103                        const std::string& key,
    104                        bool* value) const = 0;
    105 
    106   // Associates |group|:|key| with a boolean |value|. Returns true on success,
    107   // false otherwise.
    108   virtual bool SetBool(const std::string& group,
    109                        const std::string& key,
    110                        bool value) = 0;
    111 
    112   // Gets a integer |value| associated with |group|:|key|. Returns true on
    113   // success and false on failure (including when the |group|:|key| is not
    114   // present in the store).  It is not an error to pass NULL as |value| to
    115   // simply test for the presence of this value.
    116   virtual bool GetInt(const std::string& group,
    117                       const std::string& key,
    118                       int* value) const = 0;
    119 
    120   // Associates |group|:|key| with an integer |value|. Returns true on success,
    121   // false otherwise.
    122   virtual bool SetInt(const std::string& group,
    123                       const std::string& key,
    124                       int value) = 0;
    125 
    126   // Gets a 64-bit unsigned integer |value| associated with |group|:|key|.
    127   // Returns true on success and false on failure (including when the
    128   // |group|:|key| is not present in the store).  It is not an error to
    129   // pass NULL as |value| to simply test for the presence of this value.
    130   virtual bool GetUint64(const std::string& group,
    131                          const std::string& key,
    132                          uint64_t* value) const = 0;
    133 
    134   // Associates |group|:|key| with a 64-bit unsigned integer |value|. Returns
    135   // true on success, false otherwise.
    136   virtual bool SetUint64(const std::string& group,
    137                          const std::string& key,
    138                          uint64_t value) = 0;
    139 
    140   // Gets a string list |value| associated with |group|:|key|. Returns true on
    141   // success and false on failure (including when |group|:|key| is not present
    142   // in the store).  It is not an error to pass NULL as |value| to simply test
    143   // for the presence of this value.
    144   virtual bool GetStringList(const std::string& group,
    145                              const std::string& key,
    146                              std::vector<std::string>* value) const = 0;
    147 
    148   // Associates |group|:|key| with a string list |value|. Returns true on
    149   // success, false otherwise.
    150   virtual bool SetStringList(const std::string& group,
    151                              const std::string& key,
    152                              const std::vector<std::string>& value) = 0;
    153 
    154   // Gets and decrypts string |value| associated with |group|:|key|. Returns
    155   // true on success and false on failure (including when |group|:|key| is not
    156   // present in the store).  It is not an error to pass NULL as |value| to
    157   // simply test for the presence of this value.
    158   virtual bool GetCryptedString(const std::string& group,
    159                                 const std::string& key,
    160                                 std::string* value) = 0;
    161 
    162   // Associates |group|:|key| with a string |value| after encrypting it. Returns
    163   // true on success, false otherwise.
    164   virtual bool SetCryptedString(const std::string& group,
    165                                 const std::string& key,
    166                                 const std::string& value) = 0;
    167 };
    168 
    169 }  // namespace shill
    170 
    171 #endif  // SHILL_STORE_INTERFACE_H_
    172