1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_API_STORAGE_SETTINGS_STORAGE_QUOTA_ENFORCER_H_
6 #define EXTENSIONS_BROWSER_API_STORAGE_SETTINGS_STORAGE_QUOTA_ENFORCER_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/weak_ptr.h"
10 #include "extensions/browser/value_store/value_store.h"
12 namespace extensions {
14 // Enforces total quota and a per-setting quota in bytes, and a maximum number
15 // of setting keys, for a delegate storage area.
16 class SettingsStorageQuotaEnforcer : public ValueStore {
19 // The total quota in bytes.
22 // The quota for each individual item in bytes.
23 size_t quota_bytes_per_item;
25 // The maximum number of items allowed.
29 SettingsStorageQuotaEnforcer(const Limits& limits, ValueStore* delegate);
31 virtual ~SettingsStorageQuotaEnforcer();
33 // ValueStore implementation.
34 virtual size_t GetBytesInUse(const std::string& key) OVERRIDE;
35 virtual size_t GetBytesInUse(const std::vector<std::string>& keys) OVERRIDE;
36 virtual size_t GetBytesInUse() OVERRIDE;
37 virtual ReadResult Get(const std::string& key) OVERRIDE;
38 virtual ReadResult Get(const std::vector<std::string>& keys) OVERRIDE;
39 virtual ReadResult Get() OVERRIDE;
40 virtual WriteResult Set(
42 const std::string& key,
43 const base::Value& value) OVERRIDE;
44 virtual WriteResult Set(
45 WriteOptions options, const base::DictionaryValue& values) OVERRIDE;
46 virtual WriteResult Remove(const std::string& key) OVERRIDE;
47 virtual WriteResult Remove(const std::vector<std::string>& keys) OVERRIDE;
48 virtual WriteResult Clear() OVERRIDE;
49 virtual bool Restore() OVERRIDE;
50 virtual bool RestoreKey(const std::string& key) OVERRIDE;
52 ValueStore* get_delegate_for_test() { return delegate_.get(); }
55 // Calculate the current usage for the database.
56 void CalculateUsage();
58 // Limits configuration.
61 // The delegate storage area.
62 scoped_ptr<ValueStore> const delegate_;
64 // Total bytes in used by |delegate_|. Includes both key lengths and
65 // JSON-encoded values.
68 // Map of item key to its size, including the key itself.
69 std::map<std::string, size_t> used_per_setting_;
71 DISALLOW_COPY_AND_ASSIGN(SettingsStorageQuotaEnforcer);
74 } // namespace extensions
76 #endif // EXTENSIONS_BROWSER_API_STORAGE_SETTINGS_STORAGE_QUOTA_ENFORCER_H_