OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
6 #define CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 6 #define CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 12 matching lines...) Expand all Loading... |
23 // This is a wrapper around a leveldb::mojom::LevelDBDatabase. Multiple | 23 // This is a wrapper around a leveldb::mojom::LevelDBDatabase. Multiple |
24 // interface | 24 // interface |
25 // pointers can be bound to the same object. The wrapper adds a couple of | 25 // pointers can be bound to the same object. The wrapper adds a couple of |
26 // features not found directly in leveldb. | 26 // features not found directly in leveldb. |
27 // 1) Adds the given prefix, if any, to all keys. This allows the sharing of one | 27 // 1) Adds the given prefix, if any, to all keys. This allows the sharing of one |
28 // database across many, possibly untrusted, consumers and ensuring that they | 28 // database across many, possibly untrusted, consumers and ensuring that they |
29 // can't access each other's values. | 29 // can't access each other's values. |
30 // 2) Enforces a max_size constraint. | 30 // 2) Enforces a max_size constraint. |
31 // 3) Informs observers when values scoped by prefix are modified. | 31 // 3) Informs observers when values scoped by prefix are modified. |
32 // 4) Throttles requests to avoid overwhelming the disk. | 32 // 4) Throttles requests to avoid overwhelming the disk. |
33 class LevelDBWrapperImpl : public mojom::LevelDBWrapper { | 33 class CONTENT_EXPORT LevelDBWrapperImpl : public mojom::LevelDBWrapper { |
34 public: | 34 public: |
35 // |no_bindings_callback| will be called when this object has no more | 35 // |no_bindings_callback| will be called when this object has no more |
36 // bindings. | 36 // bindings. |
37 LevelDBWrapperImpl(leveldb::mojom::LevelDBDatabase* database, | 37 LevelDBWrapperImpl(leveldb::mojom::LevelDBDatabase* database, |
38 const std::string& prefix, | 38 const std::string& prefix, |
39 size_t max_size, | 39 size_t max_size, |
40 base::TimeDelta default_commit_delay, | 40 base::TimeDelta default_commit_delay, |
41 int max_bytes_per_hour, | 41 int max_bytes_per_hour, |
42 int max_commits_per_hour, | 42 int max_commits_per_hour, |
43 const base::Closure& no_bindings_callback); | 43 const base::Closure& no_bindings_callback); |
44 ~LevelDBWrapperImpl() override; | 44 ~LevelDBWrapperImpl() override; |
45 | 45 |
46 void Bind(mojom::LevelDBWrapperRequest request); | 46 void Bind(mojom::LevelDBWrapperRequest request); |
47 void AddObserver(mojom::LevelDBObserverPtr observer); | 47 void AddObserver(mojom::LevelDBObserverPtr observer); |
48 | 48 |
49 // Commence aggressive flushing. This should be called early during startup, | 49 // Commence aggressive flushing. This should be called early during startup, |
50 // before any localStorage writing. Currently scheduled writes will not be | 50 // before any localStorage writing. Currently scheduled writes will not be |
51 // rescheduled and will be flushed at the scheduled time after which | 51 // rescheduled and will be flushed at the scheduled time after which |
52 // aggressive flushing will commence. | 52 // aggressive flushing will commence. |
53 static void EnableAggressiveCommitDelay(); | 53 static void EnableAggressiveCommitDelay(); |
54 | 54 |
55 private: | 55 private: |
| 56 friend class LevelDBWrapperImplTest; |
| 57 |
56 using ValueMap = std::map<std::vector<uint8_t>, std::vector<uint8_t>>; | 58 using ValueMap = std::map<std::vector<uint8_t>, std::vector<uint8_t>>; |
57 using ChangedValueMap = | 59 using ChangedValueMap = |
58 std::map<std::vector<uint8_t>, base::Optional<std::vector<uint8_t>>>; | 60 std::map<std::vector<uint8_t>, base::Optional<std::vector<uint8_t>>>; |
59 | 61 |
60 // Used to rate limit commits. | 62 // Used to rate limit commits. |
61 class RateLimiter { | 63 class RateLimiter { |
62 public: | 64 public: |
63 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); | 65 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); |
64 | 66 |
65 void add_samples(size_t samples) { samples_ += samples; } | 67 void add_samples(size_t samples) { samples_ += samples; } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 void OnConnectionError(); | 109 void OnConnectionError(); |
108 void LoadMap(const base::Closure& completion_callback); | 110 void LoadMap(const base::Closure& completion_callback); |
109 void OnLoadComplete(leveldb::mojom::DatabaseError status, | 111 void OnLoadComplete(leveldb::mojom::DatabaseError status, |
110 std::vector<leveldb::mojom::KeyValuePtr> data); | 112 std::vector<leveldb::mojom::KeyValuePtr> data); |
111 void CreateCommitBatchIfNeeded(); | 113 void CreateCommitBatchIfNeeded(); |
112 void StartCommitTimer(); | 114 void StartCommitTimer(); |
113 base::TimeDelta ComputeCommitDelay() const; | 115 base::TimeDelta ComputeCommitDelay() const; |
114 void CommitChanges(); | 116 void CommitChanges(); |
115 void OnCommitComplete(leveldb::mojom::DatabaseError error); | 117 void OnCommitComplete(leveldb::mojom::DatabaseError error); |
116 | 118 |
117 std::string prefix_; | 119 std::vector<uint8_t> prefix_; |
118 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; | 120 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; |
119 mojo::InterfacePtrSet<mojom::LevelDBObserver> observers_; | 121 mojo::InterfacePtrSet<mojom::LevelDBObserver> observers_; |
120 base::Closure no_bindings_callback_; | 122 base::Closure no_bindings_callback_; |
121 leveldb::mojom::LevelDBDatabase* database_; | 123 leveldb::mojom::LevelDBDatabase* database_; |
122 std::unique_ptr<ValueMap> map_; | 124 std::unique_ptr<ValueMap> map_; |
123 std::vector<base::Closure> on_load_complete_tasks_; | 125 std::vector<base::Closure> on_load_complete_tasks_; |
124 size_t bytes_used_; | 126 size_t bytes_used_; |
125 size_t max_size_; | 127 size_t max_size_; |
126 base::TimeTicks start_time_; | 128 base::TimeTicks start_time_; |
127 base::TimeDelta default_commit_delay_; | 129 base::TimeDelta default_commit_delay_; |
128 RateLimiter data_rate_limiter_; | 130 RateLimiter data_rate_limiter_; |
129 RateLimiter commit_rate_limiter_; | 131 RateLimiter commit_rate_limiter_; |
130 int commit_batches_in_flight_ = 0; | 132 int commit_batches_in_flight_ = 0; |
131 std::unique_ptr<CommitBatch> commit_batch_; | 133 std::unique_ptr<CommitBatch> commit_batch_; |
132 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; | 134 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; |
133 | 135 |
134 static bool s_aggressive_flushing_enabled_; | 136 static bool s_aggressive_flushing_enabled_; |
135 | 137 |
136 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); | 138 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); |
137 }; | 139 }; |
138 | 140 |
139 } // namespace content | 141 } // namespace content |
140 | 142 |
141 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 143 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
OLD | NEW |