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/renderer/dom_storage/local_storage_cached_area.h" | 5 #include "content/renderer/dom_storage/local_storage_cached_area.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 } | 30 } |
31 | 31 |
32 std::vector<uint8_t> String16ToUint8Vector(const base::string16& input) { | 32 std::vector<uint8_t> String16ToUint8Vector(const base::string16& input) { |
33 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); | 33 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); |
34 return std::vector<uint8_t>(data, data + input.size() * sizeof(base::char16)); | 34 return std::vector<uint8_t>(data, data + input.size() * sizeof(base::char16)); |
35 } | 35 } |
36 | 36 |
37 class GetAllCallback : public mojom::LevelDBWrapperGetAllCallback { | 37 class GetAllCallback : public mojom::LevelDBWrapperGetAllCallback { |
38 public: | 38 public: |
39 static mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo CreateAndBind( | 39 static mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo CreateAndBind( |
40 mojo::AssociatedGroup* associated_group, | |
41 const base::Callback<void(bool)>& callback) { | 40 const base::Callback<void(bool)>& callback) { |
42 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo ptr_info; | 41 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo ptr_info; |
43 mojom::LevelDBWrapperGetAllCallbackAssociatedRequest request; | 42 auto request = mojo::MakeRequest(&ptr_info); |
44 associated_group->CreateAssociatedInterface( | |
45 mojo::AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); | |
46 mojo::MakeStrongAssociatedBinding( | 43 mojo::MakeStrongAssociatedBinding( |
47 base::WrapUnique(new GetAllCallback(callback)), std::move(request)); | 44 base::WrapUnique(new GetAllCallback(callback)), std::move(request)); |
48 return ptr_info; | 45 return ptr_info; |
49 } | 46 } |
50 | 47 |
51 private: | 48 private: |
52 explicit GetAllCallback(const base::Callback<void(bool)>& callback) | 49 explicit GetAllCallback(const base::Callback<void(bool)>& callback) |
53 : m_callback(callback) {} | 50 : m_callback(callback) {} |
54 void Complete(bool success) override { m_callback.Run(success); } | 51 void Complete(bool success) override { m_callback.Run(success); } |
55 | 52 |
(...skipping 21 matching lines...) Expand all Loading... |
77 | 74 |
78 LocalStorageCachedArea::LocalStorageCachedArea( | 75 LocalStorageCachedArea::LocalStorageCachedArea( |
79 const url::Origin& origin, | 76 const url::Origin& origin, |
80 mojom::StoragePartitionService* storage_partition_service, | 77 mojom::StoragePartitionService* storage_partition_service, |
81 LocalStorageCachedAreas* cached_areas) | 78 LocalStorageCachedAreas* cached_areas) |
82 : origin_(origin), binding_(this), | 79 : origin_(origin), binding_(this), |
83 cached_areas_(cached_areas), weak_factory_(this) { | 80 cached_areas_(cached_areas), weak_factory_(this) { |
84 storage_partition_service->OpenLocalStorage(origin_, | 81 storage_partition_service->OpenLocalStorage(origin_, |
85 mojo::MakeRequest(&leveldb_)); | 82 mojo::MakeRequest(&leveldb_)); |
86 mojom::LevelDBObserverAssociatedPtrInfo ptr_info; | 83 mojom::LevelDBObserverAssociatedPtrInfo ptr_info; |
87 binding_.Bind(&ptr_info, leveldb_.associated_group()); | 84 binding_.Bind(&ptr_info); |
88 leveldb_->AddObserver(std::move(ptr_info)); | 85 leveldb_->AddObserver(std::move(ptr_info)); |
89 } | 86 } |
90 | 87 |
91 LocalStorageCachedArea::~LocalStorageCachedArea() { | 88 LocalStorageCachedArea::~LocalStorageCachedArea() { |
92 cached_areas_->CacheAreaClosed(this); | 89 cached_areas_->CacheAreaClosed(this); |
93 } | 90 } |
94 | 91 |
95 unsigned LocalStorageCachedArea::GetLength() { | 92 unsigned LocalStorageCachedArea::GetLength() { |
96 EnsureLoaded(); | 93 EnsureLoaded(); |
97 return map_->Length(); | 94 return map_->Length(); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 | 280 |
284 void LocalStorageCachedArea::EnsureLoaded() { | 281 void LocalStorageCachedArea::EnsureLoaded() { |
285 if (map_) | 282 if (map_) |
286 return; | 283 return; |
287 | 284 |
288 base::TimeTicks before = base::TimeTicks::Now(); | 285 base::TimeTicks before = base::TimeTicks::Now(); |
289 ignore_all_mutations_ = true; | 286 ignore_all_mutations_ = true; |
290 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK; | 287 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK; |
291 std::vector<content::mojom::KeyValuePtr> data; | 288 std::vector<content::mojom::KeyValuePtr> data; |
292 leveldb_->GetAll(GetAllCallback::CreateAndBind( | 289 leveldb_->GetAll(GetAllCallback::CreateAndBind( |
293 leveldb_.associated_group(), | |
294 base::Bind(&LocalStorageCachedArea::OnGetAllComplete, | 290 base::Bind(&LocalStorageCachedArea::OnGetAllComplete, |
295 weak_factory_.GetWeakPtr())), | 291 weak_factory_.GetWeakPtr())), |
296 &status, &data); | 292 &status, &data); |
297 | 293 |
298 DOMStorageValuesMap values; | 294 DOMStorageValuesMap values; |
299 for (size_t i = 0; i < data.size(); ++i) { | 295 for (size_t i = 0; i < data.size(); ++i) { |
300 values[Uint8VectorToString16(data[i]->key)] = | 296 values[Uint8VectorToString16(data[i]->key)] = |
301 base::NullableString16(Uint8VectorToString16(data[i]->value), false); | 297 base::NullableString16(Uint8VectorToString16(data[i]->value), false); |
302 } | 298 } |
303 | 299 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 } | 360 } |
365 | 361 |
366 void LocalStorageCachedArea::Reset() { | 362 void LocalStorageCachedArea::Reset() { |
367 map_ = NULL; | 363 map_ = NULL; |
368 ignore_key_mutations_.clear(); | 364 ignore_key_mutations_.clear(); |
369 ignore_all_mutations_ = false; | 365 ignore_all_mutations_ = false; |
370 weak_factory_.InvalidateWeakPtrs(); | 366 weak_factory_.InvalidateWeakPtrs(); |
371 } | 367 } |
372 | 368 |
373 } // namespace content | 369 } // namespace content |
OLD | NEW |