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 "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "content/common/leveldb_wrapper.mojom.h" | 10 #include "content/common/leveldb_wrapper.mojom.h" |
11 #include "mojo/public/cpp/bindings/binding_set.h" | 11 #include "mojo/public/cpp/bindings/binding_set.h" |
12 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | 12 #include "mojo/public/cpp/bindings/interface_ptr_set.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 // This is a wrapper around a leveldb::LevelDBDatabase. It adds a couple of | 16 // This is a wrapper around a leveldb::LevelDBDatabase. It adds a couple of |
17 // features: | 17 // features: |
18 // 1) Adds the given prefix, if any, to all keys. This allows the sharing of one | 18 // 1) Adds the given prefix, if any, to all keys. This allows the sharing of one |
19 // database across many, possibly untrusted, consumers and ensuring that they | 19 // database across many, possibly untrusted, consumers and ensuring that they |
20 // can't access each other's values. | 20 // can't access each other's values. |
21 // 2) Enforces a max_size constraint. | 21 // 2) Enforces a max_size constraint. |
22 // 3) Informs an observer when the given prefix' values are modified. | 22 // 3) Informs an observer when the given prefix' values are modified. |
23 // 4) Throttles requests to avoid overwhelming the disk. | 23 // 4) Throttles requests to avoid overwhelming the disk. |
24 class LevelDBWrapperImpl : public mojom::LevelDBWrapper { | 24 class LevelDBWrapperImpl : public mojom::LevelDBWrapper { |
25 public: | 25 public: |
26 // |no_bindings_callback| will be called when this object has no more | 26 // |no_bindings_callback| will be called when this object has no more |
27 // bindings. | 27 // bindings. |
28 LevelDBWrapperImpl(leveldb::LevelDBDatabase* database, | 28 LevelDBWrapperImpl(const std::string& prefix, |
29 const std::string& prefix, | |
30 size_t max_size, | 29 size_t max_size, |
31 const base::Closure& no_bindings_callback); | 30 const base::Closure& no_bindings_callback); |
32 ~LevelDBWrapperImpl() override; | 31 ~LevelDBWrapperImpl() override; |
33 | 32 |
34 void Bind(mojom::LevelDBWrapperRequest request); | 33 void Bind(mojom::LevelDBWrapperRequest request); |
35 void AddObserver(mojom::LevelDBObserverPtr observer); | 34 void AddObserver(mojom::LevelDBObserverPtr observer); |
36 | 35 |
37 private: | 36 private: |
38 // LevelDBWrapperImpl: | 37 // LevelDBWrapperImpl: |
39 void Put(mojo::Array<uint8_t> key, | 38 void Put(mojo::Array<uint8_t> key, |
40 mojo::Array<uint8_t> value, | 39 mojo::Array<uint8_t> value, |
41 const mojo::String& source, | 40 const mojo::String& source, |
42 const PutCallback& callback) override; | 41 const PutCallback& callback) override; |
43 void Delete(mojo::Array<uint8_t> key, | 42 void Delete(mojo::Array<uint8_t> key, |
44 const mojo::String& source, | 43 const mojo::String& source, |
45 const DeleteCallback& callback) override; | 44 const DeleteCallback& callback) override; |
46 void DeleteAll(const mojo::String& source, | 45 void DeleteAll(const mojo::String& source, |
47 const DeleteAllCallback& callback) override; | 46 const DeleteAllCallback& callback) override; |
48 void Get(mojo::Array<uint8_t> key, const GetCallback& callback) override; | 47 void Get(mojo::Array<uint8_t> key, const GetCallback& callback) override; |
49 void GetAll(const mojo::String& source, | 48 void GetAll(const mojo::String& source, |
50 const GetAllCallback& callback) override; | 49 const GetAllCallback& callback) override; |
51 | 50 |
52 void OnConnectionError(); | 51 void OnConnectionError(); |
53 | 52 |
54 std::string prefix_; | 53 std::string prefix_; |
55 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; | 54 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; |
56 mojo::InterfacePtrSet<mojom::LevelDBObserver> observers_; | 55 mojo::InterfacePtrSet<mojom::LevelDBObserver> observers_; |
57 base::Closure no_bindings_callback_; | 56 base::Closure no_bindings_callback_; |
58 leveldb::LevelDBDatabase* database_; | |
59 std::map<mojo::Array<uint8_t>, mojo::Array<uint8_t>> map_; | 57 std::map<mojo::Array<uint8_t>, mojo::Array<uint8_t>> map_; |
60 size_t bytes_used_; | 58 size_t bytes_used_; |
61 size_t max_size_; | 59 size_t max_size_; |
62 | 60 |
63 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); | 61 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); |
64 }; | 62 }; |
65 | 63 |
66 } // namespace content | 64 } // namespace content |
67 | 65 |
68 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 66 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
OLD | NEW |