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 14 matching lines...) Expand all Loading... |
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 CONTENT_EXPORT LevelDBWrapperImpl : public mojom::LevelDBWrapper { | 33 class CONTENT_EXPORT LevelDBWrapperImpl : public mojom::LevelDBWrapper { |
34 public: | 34 public: |
| 35 using PrepareToCommitCallback = |
| 36 base::Callback<std::vector<leveldb::mojom::BatchedOperationPtr>()>; |
| 37 |
35 // |no_bindings_callback| will be called when this object has no more | 38 // |no_bindings_callback| will be called when this object has no more |
36 // bindings and all pending modifications have been processed. | 39 // bindings and all pending modifications have been processed. |
37 LevelDBWrapperImpl(leveldb::mojom::LevelDBDatabase* database, | 40 LevelDBWrapperImpl(leveldb::mojom::LevelDBDatabase* database, |
38 const std::string& prefix, | 41 const std::string& prefix, |
39 size_t max_size, | 42 size_t max_size, |
40 base::TimeDelta default_commit_delay, | 43 base::TimeDelta default_commit_delay, |
41 int max_bytes_per_hour, | 44 int max_bytes_per_hour, |
42 int max_commits_per_hour, | 45 int max_commits_per_hour, |
43 const base::Closure& no_bindings_callback); | 46 const base::Closure& no_bindings_callback, |
| 47 const PrepareToCommitCallback& prepare_to_commit_callback); |
44 ~LevelDBWrapperImpl() override; | 48 ~LevelDBWrapperImpl() override; |
45 | 49 |
46 void Bind(mojom::LevelDBWrapperRequest request); | 50 void Bind(mojom::LevelDBWrapperRequest request); |
47 | 51 |
48 // Commence aggressive flushing. This should be called early during startup, | 52 // Commence aggressive flushing. This should be called early during startup, |
49 // before any localStorage writing. Currently scheduled writes will not be | 53 // before any localStorage writing. Currently scheduled writes will not be |
50 // rescheduled and will be flushed at the scheduled time after which | 54 // rescheduled and will be flushed at the scheduled time after which |
51 // aggressive flushing will commence. | 55 // aggressive flushing will commence. |
52 static void EnableAggressiveCommitDelay(); | 56 static void EnableAggressiveCommitDelay(); |
53 | 57 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 void CreateCommitBatchIfNeeded(); | 115 void CreateCommitBatchIfNeeded(); |
112 void StartCommitTimer(); | 116 void StartCommitTimer(); |
113 base::TimeDelta ComputeCommitDelay() const; | 117 base::TimeDelta ComputeCommitDelay() const; |
114 void CommitChanges(); | 118 void CommitChanges(); |
115 void OnCommitComplete(leveldb::mojom::DatabaseError error); | 119 void OnCommitComplete(leveldb::mojom::DatabaseError error); |
116 | 120 |
117 std::vector<uint8_t> prefix_; | 121 std::vector<uint8_t> prefix_; |
118 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; | 122 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; |
119 mojo::AssociatedInterfacePtrSet<mojom::LevelDBObserver> observers_; | 123 mojo::AssociatedInterfacePtrSet<mojom::LevelDBObserver> observers_; |
120 base::Closure no_bindings_callback_; | 124 base::Closure no_bindings_callback_; |
| 125 PrepareToCommitCallback prepare_to_commit_callback_; |
121 leveldb::mojom::LevelDBDatabase* database_; | 126 leveldb::mojom::LevelDBDatabase* database_; |
122 std::unique_ptr<ValueMap> map_; | 127 std::unique_ptr<ValueMap> map_; |
123 std::vector<base::Closure> on_load_complete_tasks_; | 128 std::vector<base::Closure> on_load_complete_tasks_; |
124 size_t bytes_used_; | 129 size_t bytes_used_; |
125 size_t max_size_; | 130 size_t max_size_; |
126 base::TimeTicks start_time_; | 131 base::TimeTicks start_time_; |
127 base::TimeDelta default_commit_delay_; | 132 base::TimeDelta default_commit_delay_; |
128 RateLimiter data_rate_limiter_; | 133 RateLimiter data_rate_limiter_; |
129 RateLimiter commit_rate_limiter_; | 134 RateLimiter commit_rate_limiter_; |
130 int commit_batches_in_flight_ = 0; | 135 int commit_batches_in_flight_ = 0; |
131 std::unique_ptr<CommitBatch> commit_batch_; | 136 std::unique_ptr<CommitBatch> commit_batch_; |
132 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; | 137 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; |
133 | 138 |
134 static bool s_aggressive_flushing_enabled_; | 139 static bool s_aggressive_flushing_enabled_; |
135 | 140 |
136 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); | 141 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); |
137 }; | 142 }; |
138 | 143 |
139 } // namespace content | 144 } // namespace content |
140 | 145 |
141 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 146 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
OLD | NEW |