Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / storage / settings_storage_quota_enforcer.h
blob297f9e1089a2525f0176af02a06067001e2b8205
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 {
17 public:
18 struct Limits {
19 // The total quota in bytes.
20 size_t quota_bytes;
22 // The quota for each individual item in bytes.
23 size_t quota_bytes_per_item;
25 // The maximum number of items allowed.
26 size_t max_items;
29 SettingsStorageQuotaEnforcer(const Limits& limits, ValueStore* delegate);
31 ~SettingsStorageQuotaEnforcer() override;
33 // ValueStore implementation.
34 size_t GetBytesInUse(const std::string& key) override;
35 size_t GetBytesInUse(const std::vector<std::string>& keys) override;
36 size_t GetBytesInUse() override;
37 ReadResult Get(const std::string& key) override;
38 ReadResult Get(const std::vector<std::string>& keys) override;
39 ReadResult Get() override;
40 WriteResult Set(WriteOptions options,
41 const std::string& key,
42 const base::Value& value) override;
43 WriteResult Set(WriteOptions options,
44 const base::DictionaryValue& values) override;
45 WriteResult Remove(const std::string& key) override;
46 WriteResult Remove(const std::vector<std::string>& keys) override;
47 WriteResult Clear() override;
48 bool Restore() override;
49 bool RestoreKey(const std::string& key) override;
51 ValueStore* get_delegate_for_test() { return delegate_.get(); }
53 private:
54 // Calculate the current usage for the database.
55 void CalculateUsage();
57 // Limits configuration.
58 const Limits limits_;
60 // The delegate storage area.
61 scoped_ptr<ValueStore> const delegate_;
63 // Total bytes in used by |delegate_|. Includes both key lengths and
64 // JSON-encoded values.
65 size_t used_total_;
67 // Map of item key to its size, including the key itself.
68 std::map<std::string, size_t> used_per_setting_;
70 DISALLOW_COPY_AND_ASSIGN(SettingsStorageQuotaEnforcer);
73 } // namespace extensions
75 #endif // EXTENSIONS_BROWSER_API_STORAGE_SETTINGS_STORAGE_QUOTA_ENFORCER_H_