Home | History | Annotate | Download | only in value_store
      1 // Copyright (c) 2012 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_VALUE_STORE_VALUE_STORE_H_
      6 #define CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/values.h"
     13 #include "chrome/browser/value_store/value_store_change.h"
     14 
     15 // Interface for a storage area for Value objects.
     16 class ValueStore {
     17  public:
     18   // The result of a read operation (Get).
     19   class ReadResultType {
     20    public:
     21     // Ownership of |settings| taken.
     22     explicit ReadResultType(base::DictionaryValue* settings);
     23     explicit ReadResultType(const std::string& error);
     24     ~ReadResultType();
     25 
     26     // Gets the settings read from the storage. Note that this represents
     27     // the root object. If you request the value for key "foo", that value will
     28     // be in |settings.foo|.
     29     // Must only be called if HasError() is false.
     30     scoped_ptr<base::DictionaryValue>& settings();
     31 
     32     // Gets whether the operation failed.
     33     bool HasError() const;
     34 
     35     // Gets the error message describing the failure.
     36     // Must only be called if HasError() is true.
     37     const std::string& error() const;
     38 
     39    private:
     40     scoped_ptr<base::DictionaryValue> settings_;
     41     const std::string error_;
     42 
     43     DISALLOW_COPY_AND_ASSIGN(ReadResultType);
     44   };
     45   typedef scoped_ptr<ReadResultType> ReadResult;
     46 
     47   // The result of a write operation (Set/Remove/Clear).
     48   class WriteResultType {
     49    public:
     50     // Ownership of |changes| taken.
     51     explicit WriteResultType(ValueStoreChangeList* changes);
     52     explicit WriteResultType(const std::string& error);
     53     ~WriteResultType();
     54 
     55     // Gets the list of changes to the settings which resulted from the write.
     56     // Must only be called if HasError() is false.
     57     const ValueStoreChangeList& changes() const;
     58 
     59     // Gets whether the operation failed.
     60     bool HasError() const;
     61 
     62     // Gets the error message describing the failure.
     63     // Must only be called if HasError() is true.
     64     const std::string& error() const;
     65 
     66    private:
     67     const scoped_ptr<ValueStoreChangeList> changes_;
     68     const std::string error_;
     69 
     70     DISALLOW_COPY_AND_ASSIGN(WriteResultType);
     71   };
     72   typedef scoped_ptr<WriteResultType> WriteResult;
     73 
     74   // Options for write operations.
     75   enum WriteOptionsValues {
     76     // Callers should usually use this.
     77     DEFAULTS = 0,
     78 
     79     // Ignore any quota restrictions.
     80     IGNORE_QUOTA = 1<<1,
     81 
     82     // Don't generate the changes for a WriteResult.
     83     NO_GENERATE_CHANGES = 1<<2,
     84 
     85     // Don't check the old value before writing a new value. This will also
     86     // result in an empty |old_value| in the WriteResult::changes list.
     87     NO_CHECK_OLD_VALUE = 1<<3
     88   };
     89   typedef int WriteOptions;
     90 
     91   virtual ~ValueStore() {}
     92 
     93   // Helpers for making a Read/WriteResult.
     94   template<typename T>
     95   static ReadResult MakeReadResult(T arg) {
     96     return ReadResult(new ReadResultType(arg));
     97   }
     98 
     99   template<typename T>
    100   static WriteResult MakeWriteResult(T arg) {
    101     return WriteResult(new WriteResultType(arg));
    102   }
    103 
    104   // Gets the amount of space being used by a single value, in bytes.
    105   // Note: The GetBytesInUse methods are only used by extension settings at the
    106   // moment. If these become more generally useful, the
    107   // SettingsStorageQuotaEnforcer and WeakUnlimitedSettingsStorage classes
    108   // should be moved to the value_store directory.
    109   virtual size_t GetBytesInUse(const std::string& key) = 0;
    110 
    111   // Gets the total amount of space being used by multiple values, in bytes.
    112   virtual size_t GetBytesInUse(const std::vector<std::string>& keys) = 0;
    113 
    114   // Gets the total amount of space being used by this storage area, in bytes.
    115   virtual size_t GetBytesInUse() = 0;
    116 
    117   // Gets a single value from storage.
    118   virtual ReadResult Get(const std::string& key) = 0;
    119 
    120   // Gets multiple values from storage.
    121   virtual ReadResult Get(const std::vector<std::string>& keys) = 0;
    122 
    123   // Gets all values from storage.
    124   virtual ReadResult Get() = 0;
    125 
    126   // Sets a single key to a new value.
    127   virtual WriteResult Set(WriteOptions options,
    128                           const std::string& key,
    129                           const base::Value& value) = 0;
    130 
    131   // Sets multiple keys to new values.
    132   virtual WriteResult Set(
    133       WriteOptions options, const base::DictionaryValue& values) = 0;
    134 
    135   // Removes a key from the storage.
    136   virtual WriteResult Remove(const std::string& key) = 0;
    137 
    138   // Removes multiple keys from the storage.
    139   virtual WriteResult Remove(const std::vector<std::string>& keys) = 0;
    140 
    141   // Clears the storage.
    142   virtual WriteResult Clear() = 0;
    143 };
    144 
    145 #endif  // CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_
    146