| 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 #include "content/browser/leveldb_wrapper_impl.h" | 5 #include "content/browser/leveldb_wrapper_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "mojo/common/common_type_converters.h" | 10 #include "mojo/common/common_type_converters.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 81 |
| 82 void LevelDBWrapperImpl::EnableAggressiveCommitDelay() { | 82 void LevelDBWrapperImpl::EnableAggressiveCommitDelay() { |
| 83 s_aggressive_flushing_enabled_ = true; | 83 s_aggressive_flushing_enabled_ = true; |
| 84 } | 84 } |
| 85 | 85 |
| 86 void LevelDBWrapperImpl::Put(mojo::Array<uint8_t> key, | 86 void LevelDBWrapperImpl::Put(mojo::Array<uint8_t> key, |
| 87 mojo::Array<uint8_t> value, | 87 mojo::Array<uint8_t> value, |
| 88 const mojo::String& source, | 88 const mojo::String& source, |
| 89 const PutCallback& callback) { | 89 const PutCallback& callback) { |
| 90 if (!map_) { | 90 if (!map_) { |
| 91 on_load_complete_tasks_.push_back( | 91 LoadMap( |
| 92 base::Bind(&LevelDBWrapperImpl::Put, base::Unretained(this), | 92 base::Bind(&LevelDBWrapperImpl::Put, base::Unretained(this), |
| 93 base::Passed(&key), base::Passed(&value), source, callback)); | 93 base::Passed(&key), base::Passed(&value), source, callback)); |
| 94 LoadMap(); | |
| 95 return; | 94 return; |
| 96 } | 95 } |
| 97 | 96 |
| 98 bool has_old_item = false; | 97 bool has_old_item = false; |
| 99 mojo::Array<uint8_t> old_value; | 98 mojo::Array<uint8_t> old_value; |
| 100 size_t old_item_size = 0; | 99 size_t old_item_size = 0; |
| 101 auto found = map_->find(key); | 100 auto found = map_->find(key); |
| 102 if (found != map_->end()) { | 101 if (found != map_->end()) { |
| 103 if (found->second.Equals(value)) { | 102 if (found->second.Equals(value)) { |
| 104 callback.Run(true); // Key already has this value. | 103 callback.Run(true); // Key already has this value. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 key.Clone(), value.Clone(), old_value.Clone(), source); | 138 key.Clone(), value.Clone(), old_value.Clone(), source); |
| 140 }); | 139 }); |
| 141 } | 140 } |
| 142 callback.Run(true); | 141 callback.Run(true); |
| 143 } | 142 } |
| 144 | 143 |
| 145 void LevelDBWrapperImpl::Delete(mojo::Array<uint8_t> key, | 144 void LevelDBWrapperImpl::Delete(mojo::Array<uint8_t> key, |
| 146 const mojo::String& source, | 145 const mojo::String& source, |
| 147 const DeleteCallback& callback) { | 146 const DeleteCallback& callback) { |
| 148 if (!map_) { | 147 if (!map_) { |
| 149 on_load_complete_tasks_.push_back( | 148 LoadMap( |
| 150 base::Bind(&LevelDBWrapperImpl::Delete, base::Unretained(this), | 149 base::Bind(&LevelDBWrapperImpl::Delete, base::Unretained(this), |
| 151 base::Passed(&key), source, callback)); | 150 base::Passed(&key), source, callback)); |
| 152 LoadMap(); | |
| 153 return; | 151 return; |
| 154 } | 152 } |
| 155 | 153 |
| 156 auto found = map_->find(key); | 154 auto found = map_->find(key); |
| 157 if (found == map_->end()) { | 155 if (found == map_->end()) { |
| 158 callback.Run(true); | 156 callback.Run(true); |
| 159 return; | 157 return; |
| 160 } | 158 } |
| 161 | 159 |
| 162 if (database_) { | 160 if (database_) { |
| 163 CreateCommitBatchIfNeeded(); | 161 CreateCommitBatchIfNeeded(); |
| 164 commit_batch_->changed_values[key.Clone()] = mojo::Array<uint8_t>(nullptr); | 162 commit_batch_->changed_values[key.Clone()] = mojo::Array<uint8_t>(nullptr); |
| 165 } | 163 } |
| 166 | 164 |
| 167 mojo::Array<uint8_t> old_value = std::move(found->second); | 165 mojo::Array<uint8_t> old_value = std::move(found->second); |
| 168 map_->erase(found); | 166 map_->erase(found); |
| 169 bytes_used_ -= key.size() + old_value.size(); | 167 bytes_used_ -= key.size() + old_value.size(); |
| 170 observers_.ForAllPtrs( | 168 observers_.ForAllPtrs( |
| 171 [&key, &source, &old_value](mojom::LevelDBObserver* observer) { | 169 [&key, &source, &old_value](mojom::LevelDBObserver* observer) { |
| 172 observer->KeyDeleted( | 170 observer->KeyDeleted( |
| 173 key.Clone(), old_value.Clone(), source); | 171 key.Clone(), old_value.Clone(), source); |
| 174 }); | 172 }); |
| 175 callback.Run(true); | 173 callback.Run(true); |
| 176 } | 174 } |
| 177 | 175 |
| 178 void LevelDBWrapperImpl::DeleteAll(const mojo::String& source, | 176 void LevelDBWrapperImpl::DeleteAll(const mojo::String& source, |
| 179 const DeleteAllCallback& callback) { | 177 const DeleteAllCallback& callback) { |
| 180 if (!map_) { | 178 if (!map_) { |
| 181 on_load_complete_tasks_.push_back( | 179 LoadMap( |
| 182 base::Bind(&LevelDBWrapperImpl::DeleteAll, base::Unretained(this), | 180 base::Bind(&LevelDBWrapperImpl::DeleteAll, base::Unretained(this), |
| 183 source, callback)); | 181 source, callback)); |
| 184 LoadMap(); | |
| 185 return; | 182 return; |
| 186 } | 183 } |
| 187 | 184 |
| 188 if (database_ && !map_->empty()) { | 185 if (database_ && !map_->empty()) { |
| 189 CreateCommitBatchIfNeeded(); | 186 CreateCommitBatchIfNeeded(); |
| 190 commit_batch_->clear_all_first = true; | 187 commit_batch_->clear_all_first = true; |
| 191 commit_batch_->changed_values.clear(); | 188 commit_batch_->changed_values.clear(); |
| 192 } | 189 } |
| 193 map_->clear(); | 190 map_->clear(); |
| 194 bytes_used_ = 0; | 191 bytes_used_ = 0; |
| 195 observers_.ForAllPtrs( | 192 observers_.ForAllPtrs( |
| 196 [&source](mojom::LevelDBObserver* observer) { | 193 [&source](mojom::LevelDBObserver* observer) { |
| 197 observer->AllDeleted(source); | 194 observer->AllDeleted(source); |
| 198 }); | 195 }); |
| 199 callback.Run(true); | 196 callback.Run(true); |
| 200 } | 197 } |
| 201 | 198 |
| 202 void LevelDBWrapperImpl::Get(mojo::Array<uint8_t> key, | 199 void LevelDBWrapperImpl::Get(mojo::Array<uint8_t> key, |
| 203 const GetCallback& callback) { | 200 const GetCallback& callback) { |
| 204 if (!map_) { | 201 if (!map_) { |
| 205 on_load_complete_tasks_.push_back( | 202 LoadMap( |
| 206 base::Bind(&LevelDBWrapperImpl::Get, base::Unretained(this), | 203 base::Bind(&LevelDBWrapperImpl::Get, base::Unretained(this), |
| 207 base::Passed(&key), callback)); | 204 base::Passed(&key), callback)); |
| 208 LoadMap(); | |
| 209 return; | 205 return; |
| 210 } | 206 } |
| 211 | 207 |
| 212 auto found = map_->find(key); | 208 auto found = map_->find(key); |
| 213 if (found == map_->end()) { | 209 if (found == map_->end()) { |
| 214 callback.Run(false, mojo::Array<uint8_t>()); | 210 callback.Run(false, mojo::Array<uint8_t>()); |
| 215 return; | 211 return; |
| 216 } | 212 } |
| 217 callback.Run(true, found->second.Clone()); | 213 callback.Run(true, found->second.Clone()); |
| 218 } | 214 } |
| 219 | 215 |
| 220 void LevelDBWrapperImpl::GetAll(const mojo::String& source, | 216 void LevelDBWrapperImpl::GetAll(const mojo::String& source, |
| 221 const GetAllCallback& callback) { | 217 const GetAllCallback& callback) { |
| 222 if (!map_) { | 218 if (!map_) { |
| 223 on_load_complete_tasks_.push_back( | 219 LoadMap( |
| 224 base::Bind(&LevelDBWrapperImpl::GetAll, base::Unretained(this), | 220 base::Bind(&LevelDBWrapperImpl::GetAll, base::Unretained(this), |
| 225 source, callback)); | 221 source, callback)); |
| 226 LoadMap(); | |
| 227 return; | 222 return; |
| 228 } | 223 } |
| 229 | 224 |
| 230 mojo::Array<mojom::KeyValuePtr> all(map_->size()); | 225 mojo::Array<mojom::KeyValuePtr> all(map_->size()); |
| 231 for (const auto& it : (*map_)) { | 226 for (const auto& it : (*map_)) { |
| 232 mojom::KeyValuePtr kv = mojom::KeyValue::New(); | 227 mojom::KeyValuePtr kv = mojom::KeyValue::New(); |
| 233 kv->key = it.first.Clone(); | 228 kv->key = it.first.Clone(); |
| 234 kv->value = it.second.Clone(); | 229 kv->value = it.second.Clone(); |
| 235 all.push_back(std::move(kv)); | 230 all.push_back(std::move(kv)); |
| 236 } | 231 } |
| 237 callback.Run(leveldb::DatabaseError::OK, std::move(all)); | 232 callback.Run(leveldb::DatabaseError::OK, std::move(all)); |
| 238 observers_.ForAllPtrs( | 233 observers_.ForAllPtrs( |
| 239 [source](mojom::LevelDBObserver* observer) { | 234 [source](mojom::LevelDBObserver* observer) { |
| 240 observer->GetAllComplete(source); | 235 observer->GetAllComplete(source); |
| 241 }); | 236 }); |
| 242 } | 237 } |
| 243 | 238 |
| 244 void LevelDBWrapperImpl::OnConnectionError() { | 239 void LevelDBWrapperImpl::OnConnectionError() { |
| 245 if (!bindings_.empty()) | 240 if (!bindings_.empty()) |
| 246 return; | 241 return; |
| 247 no_bindings_callback_.Run(); | 242 no_bindings_callback_.Run(); |
| 248 } | 243 } |
| 249 | 244 |
| 250 void LevelDBWrapperImpl::LoadMap() { | 245 void LevelDBWrapperImpl::LoadMap(const base::Closure& completion_callback) { |
| 246 DCHECK(!map_); |
| 247 on_load_complete_tasks_.push_back(completion_callback); |
| 248 if (on_load_complete_tasks_.size() > 1) |
| 249 return; |
| 250 |
| 251 // TODO(michaeln): Import from sqlite localstorage db. | 251 // TODO(michaeln): Import from sqlite localstorage db. |
| 252 DCHECK(!map_); | |
| 253 database_->GetPrefixed(mojo::Array<uint8_t>::From(prefix_), | 252 database_->GetPrefixed(mojo::Array<uint8_t>::From(prefix_), |
| 254 base::Bind(&LevelDBWrapperImpl::OnLoadComplete, | 253 base::Bind(&LevelDBWrapperImpl::OnLoadComplete, |
| 255 weak_ptr_factory_.GetWeakPtr())); | 254 weak_ptr_factory_.GetWeakPtr())); |
| 256 } | 255 } |
| 257 | 256 |
| 258 void LevelDBWrapperImpl::OnLoadComplete( | 257 void LevelDBWrapperImpl::OnLoadComplete( |
| 259 leveldb::DatabaseError status, | 258 leveldb::DatabaseError status, |
| 260 mojo::Array<leveldb::KeyValuePtr> data) { | 259 mojo::Array<leveldb::KeyValuePtr> data) { |
| 261 DCHECK(!map_); | 260 DCHECK(!map_); |
| 262 map_.reset(new ValueMap); | 261 map_.reset(new ValueMap); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 weak_ptr_factory_.GetWeakPtr())); | 352 weak_ptr_factory_.GetWeakPtr())); |
| 354 } | 353 } |
| 355 | 354 |
| 356 void LevelDBWrapperImpl::OnCommitComplete(leveldb::DatabaseError error) { | 355 void LevelDBWrapperImpl::OnCommitComplete(leveldb::DatabaseError error) { |
| 357 // TODO(michaeln): What if it fails, uma here or in the DB class? | 356 // TODO(michaeln): What if it fails, uma here or in the DB class? |
| 358 --commit_batches_in_flight_; | 357 --commit_batches_in_flight_; |
| 359 StartCommitTimer(); | 358 StartCommitTimer(); |
| 360 } | 359 } |
| 361 | 360 |
| 362 } // namespace content | 361 } // namespace content |
| OLD | NEW |