| 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/optional.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "content/common/leveldb_wrapper.mojom.h" | 17 #include "content/common/leveldb_wrapper.mojom.h" |
| 17 #include "mojo/public/cpp/bindings/binding_set.h" | 18 #include "mojo/public/cpp/bindings/binding_set.h" |
| 18 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | 19 #include "mojo/public/cpp/bindings/interface_ptr_set.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 // This is a wrapper around a leveldb::mojom::LevelDBDatabase. Multiple | 23 // This is a wrapper around a leveldb::mojom::LevelDBDatabase. Multiple |
| 23 // interface | 24 // interface |
| 24 // 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 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 45 void Bind(mojom::LevelDBWrapperRequest request); | 46 void Bind(mojom::LevelDBWrapperRequest request); |
| 46 void AddObserver(mojom::LevelDBObserverPtr observer); | 47 void AddObserver(mojom::LevelDBObserverPtr observer); |
| 47 | 48 |
| 48 // Commence aggressive flushing. This should be called early during startup, | 49 // Commence aggressive flushing. This should be called early during startup, |
| 49 // before any localStorage writing. Currently scheduled writes will not be | 50 // before any localStorage writing. Currently scheduled writes will not be |
| 50 // rescheduled and will be flushed at the scheduled time after which | 51 // rescheduled and will be flushed at the scheduled time after which |
| 51 // aggressive flushing will commence. | 52 // aggressive flushing will commence. |
| 52 static void EnableAggressiveCommitDelay(); | 53 static void EnableAggressiveCommitDelay(); |
| 53 | 54 |
| 54 private: | 55 private: |
| 55 using ValueMap = std::map<mojo::Array<uint8_t>, mojo::Array<uint8_t>>; | 56 using ValueMap = std::map<std::vector<uint8_t>, std::vector<uint8_t>>; |
| 57 using ChangedValueMap = |
| 58 std::map<std::vector<uint8_t>, base::Optional<std::vector<uint8_t>>>; |
| 56 | 59 |
| 57 // Used to rate limit commits. | 60 // Used to rate limit commits. |
| 58 class RateLimiter { | 61 class RateLimiter { |
| 59 public: | 62 public: |
| 60 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); | 63 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); |
| 61 | 64 |
| 62 void add_samples(size_t samples) { samples_ += samples; } | 65 void add_samples(size_t samples) { samples_ += samples; } |
| 63 | 66 |
| 64 // Computes the total time needed to process the total samples seen | 67 // Computes the total time needed to process the total samples seen |
| 65 // at the desired rate. | 68 // at the desired rate. |
| 66 base::TimeDelta ComputeTimeNeeded() const; | 69 base::TimeDelta ComputeTimeNeeded() const; |
| 67 | 70 |
| 68 // Given the elapsed time since the start of the rate limiting session, | 71 // Given the elapsed time since the start of the rate limiting session, |
| 69 // computes the delay needed to mimic having processed the total samples | 72 // computes the delay needed to mimic having processed the total samples |
| 70 // seen at the desired rate. | 73 // seen at the desired rate. |
| 71 base::TimeDelta ComputeDelayNeeded( | 74 base::TimeDelta ComputeDelayNeeded( |
| 72 const base::TimeDelta elapsed_time) const; | 75 const base::TimeDelta elapsed_time) const; |
| 73 | 76 |
| 74 private: | 77 private: |
| 75 float rate_; | 78 float rate_; |
| 76 float samples_; | 79 float samples_; |
| 77 base::TimeDelta time_quantum_; | 80 base::TimeDelta time_quantum_; |
| 78 }; | 81 }; |
| 79 | 82 |
| 80 struct CommitBatch { | 83 struct CommitBatch { |
| 81 bool clear_all_first; | 84 bool clear_all_first; |
| 82 ValueMap changed_values; | 85 ChangedValueMap changed_values; |
| 83 | 86 |
| 84 CommitBatch(); | 87 CommitBatch(); |
| 85 ~CommitBatch(); | 88 ~CommitBatch(); |
| 86 size_t GetDataSize() const; | 89 size_t GetDataSize() const; |
| 87 }; | 90 }; |
| 88 | 91 |
| 89 // LevelDBWrapperImpl: | 92 // LevelDBWrapperImpl: |
| 90 void Put(mojo::Array<uint8_t> key, | 93 void Put(const std::vector<uint8_t>& key, |
| 91 mojo::Array<uint8_t> value, | 94 const std::vector<uint8_t>& value, |
| 92 const mojo::String& source, | 95 const std::string& source, |
| 93 const PutCallback& callback) override; | 96 const PutCallback& callback) override; |
| 94 void Delete(mojo::Array<uint8_t> key, | 97 void Delete(const std::vector<uint8_t>& key, |
| 95 const mojo::String& source, | 98 const std::string& source, |
| 96 const DeleteCallback& callback) override; | 99 const DeleteCallback& callback) override; |
| 97 void DeleteAll(const mojo::String& source, | 100 void DeleteAll(const std::string& source, |
| 98 const DeleteAllCallback& callback) override; | 101 const DeleteAllCallback& callback) override; |
| 99 void Get(mojo::Array<uint8_t> key, const GetCallback& callback) override; | 102 void Get(const std::vector<uint8_t>& key, |
| 100 void GetAll(const mojo::String& source, | 103 const GetCallback& callback) override; |
| 104 void GetAll(const std::string& source, |
| 101 const GetAllCallback& callback) override; | 105 const GetAllCallback& callback) override; |
| 102 | 106 |
| 103 void OnConnectionError(); | 107 void OnConnectionError(); |
| 104 void LoadMap(const base::Closure& completion_callback); | 108 void LoadMap(const base::Closure& completion_callback); |
| 105 void OnLoadComplete(leveldb::mojom::DatabaseError status, | 109 void OnLoadComplete(leveldb::mojom::DatabaseError status, |
| 106 mojo::Array<leveldb::mojom::KeyValuePtr> data); | 110 mojo::Array<leveldb::mojom::KeyValuePtr> data); |
| 107 void CreateCommitBatchIfNeeded(); | 111 void CreateCommitBatchIfNeeded(); |
| 108 void StartCommitTimer(); | 112 void StartCommitTimer(); |
| 109 base::TimeDelta ComputeCommitDelay() const; | 113 base::TimeDelta ComputeCommitDelay() const; |
| 110 void CommitChanges(); | 114 void CommitChanges(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 128 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; | 132 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; |
| 129 | 133 |
| 130 static bool s_aggressive_flushing_enabled_; | 134 static bool s_aggressive_flushing_enabled_; |
| 131 | 135 |
| 132 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); | 136 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); |
| 133 }; | 137 }; |
| 134 | 138 |
| 135 } // namespace content | 139 } // namespace content |
| 136 | 140 |
| 137 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 141 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
| OLD | NEW |