| 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 = | 35 using ValueMap = std::map<std::vector<uint8_t>, std::vector<uint8_t>>; |
| 36 base::Callback<std::vector<leveldb::mojom::BatchedOperationPtr>( | 36 using ValueMapCallback = base::OnceCallback<void(std::unique_ptr<ValueMap>)>; |
| 37 const LevelDBWrapperImpl&)>; | 37 |
| 38 class CONTENT_EXPORT Delegate { |
| 39 public: |
| 40 virtual void OnNoBindings() = 0; |
| 41 virtual std::vector<leveldb::mojom::BatchedOperationPtr> |
| 42 PrepareToCommit() = 0; |
| 43 virtual void DidCommit(leveldb::mojom::DatabaseError error) = 0; |
| 44 // Called during loading if no data was found. Needs to call |callback|. |
| 45 virtual void MigrateData(ValueMapCallback callback); |
| 46 }; |
| 38 | 47 |
| 39 // |no_bindings_callback| will be called when this object has no more | 48 // |no_bindings_callback| will be called when this object has no more |
| 40 // bindings and all pending modifications have been processed. | 49 // bindings and all pending modifications have been processed. |
| 41 LevelDBWrapperImpl(leveldb::mojom::LevelDBDatabase* database, | 50 LevelDBWrapperImpl(leveldb::mojom::LevelDBDatabase* database, |
| 42 const std::string& prefix, | 51 const std::string& prefix, |
| 43 size_t max_size, | 52 size_t max_size, |
| 44 base::TimeDelta default_commit_delay, | 53 base::TimeDelta default_commit_delay, |
| 45 int max_bytes_per_hour, | 54 int max_bytes_per_hour, |
| 46 int max_commits_per_hour, | 55 int max_commits_per_hour, |
| 47 const base::Closure& no_bindings_callback, | 56 Delegate* delegate); |
| 48 const PrepareToCommitCallback& prepare_to_commit_callback); | |
| 49 ~LevelDBWrapperImpl() override; | 57 ~LevelDBWrapperImpl() override; |
| 50 | 58 |
| 51 void Bind(mojom::LevelDBWrapperRequest request); | 59 void Bind(mojom::LevelDBWrapperRequest request); |
| 52 | 60 |
| 61 bool empty() const { return bytes_used_ == 0; } |
| 53 size_t bytes_used() const { return bytes_used_; } | 62 size_t bytes_used() const { return bytes_used_; } |
| 54 | 63 |
| 55 // Commence aggressive flushing. This should be called early during startup, | 64 // Commence aggressive flushing. This should be called early during startup, |
| 56 // before any localStorage writing. Currently scheduled writes will not be | 65 // before any localStorage writing. Currently scheduled writes will not be |
| 57 // rescheduled and will be flushed at the scheduled time after which | 66 // rescheduled and will be flushed at the scheduled time after which |
| 58 // aggressive flushing will commence. | 67 // aggressive flushing will commence. |
| 59 static void EnableAggressiveCommitDelay(); | 68 static void EnableAggressiveCommitDelay(); |
| 60 | 69 |
| 61 // Commits any uncommitted data to the database as soon as possible. This | 70 // Commits any uncommitted data to the database as soon as possible. This |
| 62 // usually means data will be committed immediately, but if we're currently | 71 // usually means data will be committed immediately, but if we're currently |
| (...skipping 12 matching lines...) Expand all Loading... |
| 75 const DeleteCallback& callback) override; | 84 const DeleteCallback& callback) override; |
| 76 void DeleteAll(const std::string& source, | 85 void DeleteAll(const std::string& source, |
| 77 const DeleteAllCallback& callback) override; | 86 const DeleteAllCallback& callback) override; |
| 78 void Get(const std::vector<uint8_t>& key, | 87 void Get(const std::vector<uint8_t>& key, |
| 79 const GetCallback& callback) override; | 88 const GetCallback& callback) override; |
| 80 void GetAll( | 89 void GetAll( |
| 81 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo complete_callback, | 90 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo complete_callback, |
| 82 const GetAllCallback& callback) override; | 91 const GetAllCallback& callback) override; |
| 83 | 92 |
| 84 private: | 93 private: |
| 85 using ValueMap = std::map<std::vector<uint8_t>, std::vector<uint8_t>>; | |
| 86 | |
| 87 // Used to rate limit commits. | 94 // Used to rate limit commits. |
| 88 class RateLimiter { | 95 class RateLimiter { |
| 89 public: | 96 public: |
| 90 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); | 97 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); |
| 91 | 98 |
| 92 void add_samples(size_t samples) { samples_ += samples; } | 99 void add_samples(size_t samples) { samples_ += samples; } |
| 93 | 100 |
| 94 // Computes the total time needed to process the total samples seen | 101 // Computes the total time needed to process the total samples seen |
| 95 // at the desired rate. | 102 // at the desired rate. |
| 96 base::TimeDelta ComputeTimeNeeded() const; | 103 base::TimeDelta ComputeTimeNeeded() const; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 110 struct CommitBatch { | 117 struct CommitBatch { |
| 111 bool clear_all_first; | 118 bool clear_all_first; |
| 112 std::set<std::vector<uint8_t>> changed_keys; | 119 std::set<std::vector<uint8_t>> changed_keys; |
| 113 | 120 |
| 114 CommitBatch(); | 121 CommitBatch(); |
| 115 ~CommitBatch(); | 122 ~CommitBatch(); |
| 116 }; | 123 }; |
| 117 | 124 |
| 118 void OnConnectionError(); | 125 void OnConnectionError(); |
| 119 void LoadMap(const base::Closure& completion_callback); | 126 void LoadMap(const base::Closure& completion_callback); |
| 120 void OnLoadComplete(leveldb::mojom::DatabaseError status, | 127 void OnMapLoaded(leveldb::mojom::DatabaseError status, |
| 121 std::vector<leveldb::mojom::KeyValuePtr> data); | 128 std::vector<leveldb::mojom::KeyValuePtr> data); |
| 129 void OnGotMigrationData(std::unique_ptr<ValueMap> data); |
| 130 void OnLoadComplete(); |
| 122 void CreateCommitBatchIfNeeded(); | 131 void CreateCommitBatchIfNeeded(); |
| 123 void StartCommitTimer(); | 132 void StartCommitTimer(); |
| 124 base::TimeDelta ComputeCommitDelay() const; | 133 base::TimeDelta ComputeCommitDelay() const; |
| 125 void CommitChanges(); | 134 void CommitChanges(); |
| 126 void OnCommitComplete(leveldb::mojom::DatabaseError error); | 135 void OnCommitComplete(leveldb::mojom::DatabaseError error); |
| 127 | 136 |
| 128 std::vector<uint8_t> prefix_; | 137 std::vector<uint8_t> prefix_; |
| 129 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; | 138 mojo::BindingSet<mojom::LevelDBWrapper> bindings_; |
| 130 mojo::AssociatedInterfacePtrSet<mojom::LevelDBObserver> observers_; | 139 mojo::AssociatedInterfacePtrSet<mojom::LevelDBObserver> observers_; |
| 131 base::Closure no_bindings_callback_; | 140 Delegate* delegate_; |
| 132 PrepareToCommitCallback prepare_to_commit_callback_; | |
| 133 leveldb::mojom::LevelDBDatabase* database_; | 141 leveldb::mojom::LevelDBDatabase* database_; |
| 134 std::unique_ptr<ValueMap> map_; | 142 std::unique_ptr<ValueMap> map_; |
| 135 std::vector<base::Closure> on_load_complete_tasks_; | 143 std::vector<base::Closure> on_load_complete_tasks_; |
| 136 size_t bytes_used_; | 144 size_t bytes_used_; |
| 137 size_t max_size_; | 145 size_t max_size_; |
| 138 base::TimeTicks start_time_; | 146 base::TimeTicks start_time_; |
| 139 base::TimeDelta default_commit_delay_; | 147 base::TimeDelta default_commit_delay_; |
| 140 RateLimiter data_rate_limiter_; | 148 RateLimiter data_rate_limiter_; |
| 141 RateLimiter commit_rate_limiter_; | 149 RateLimiter commit_rate_limiter_; |
| 142 int commit_batches_in_flight_ = 0; | 150 int commit_batches_in_flight_ = 0; |
| 143 std::unique_ptr<CommitBatch> commit_batch_; | 151 std::unique_ptr<CommitBatch> commit_batch_; |
| 144 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; | 152 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; |
| 145 | 153 |
| 146 static bool s_aggressive_flushing_enabled_; | 154 static bool s_aggressive_flushing_enabled_; |
| 147 | 155 |
| 148 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); | 156 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); |
| 149 }; | 157 }; |
| 150 | 158 |
| 151 } // namespace content | 159 } // namespace content |
| 152 | 160 |
| 153 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 161 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
| OLD | NEW |