Chromium Code Reviews| 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/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "content/common/dom_storage/dom_storage_map.h" | 14 #include "content/common/dom_storage/dom_storage_map.h" |
| 15 #include "content/common/storage_partition_service.mojom.h" | 15 #include "content/common/storage_partition_service.mojom.h" |
| 16 #include "content/renderer/dom_storage/local_storage_area.h" | 16 #include "content/renderer/dom_storage/local_storage_area.h" |
| 17 #include "content/renderer/dom_storage/local_storage_cached_areas.h" | 17 #include "content/renderer/dom_storage/local_storage_cached_areas.h" |
| 18 #include "mojo/public/cpp/bindings/strong_associated_binding.h" | |
| 18 #include "third_party/WebKit/public/platform/WebURL.h" | 19 #include "third_party/WebKit/public/platform/WebURL.h" |
| 19 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h" | 20 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h" |
| 20 | 21 |
| 22 namespace content { | |
| 23 | |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 base::string16 Uint8VectorToString16(const std::vector<uint8_t>& input) { | 26 base::string16 Uint8VectorToString16(const std::vector<uint8_t>& input) { |
| 24 return base::string16(reinterpret_cast<const base::char16*>(input.data()), | 27 return base::string16(reinterpret_cast<const base::char16*>(input.data()), |
| 25 input.size() / sizeof(base::char16)); | 28 input.size() / sizeof(base::char16)); |
| 26 } | 29 } |
| 27 | 30 |
| 28 std::vector<uint8_t> String16ToUint8Vector(const base::string16& input) { | 31 std::vector<uint8_t> String16ToUint8Vector(const base::string16& input) { |
| 29 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); | 32 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); |
| 30 return std::vector<uint8_t>(data, data + input.size() * sizeof(base::char16)); | 33 return std::vector<uint8_t>(data, data + input.size() * sizeof(base::char16)); |
| 31 } | 34 } |
| 32 | 35 |
| 36 class GetAllCallback : public mojom::LevelDBWrapperGetAllCallback { | |
| 37 public: | |
| 38 static mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo CreateAndBind( | |
| 39 mojo::AssociatedGroup* associated_group, | |
| 40 const base::Callback<void(bool)>& callback) { | |
| 41 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo ptr_info; | |
| 42 mojom::LevelDBWrapperGetAllCallbackAssociatedRequest request; | |
| 43 associated_group->CreateAssociatedInterface( | |
| 44 mojo::AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); | |
| 45 mojo::MakeStrongAssociatedBinding( | |
| 46 base::WrapUnique(new GetAllCallback(callback)), std::move(request)); | |
|
dcheng
2016/12/22 07:21:47
Nit: base::MakeUnique<GetAllCallback>(callback)
Marijn Kruisselbrink
2016/12/22 17:12:19
That would require making the constructor public (
| |
| 47 return ptr_info; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 explicit GetAllCallback(const base::Callback<void(bool)>& callback) | |
| 52 : m_callback(callback) {} | |
| 53 void Complete(bool success) override { m_callback.Run(success); } | |
| 54 | |
| 55 base::Callback<void(bool)> m_callback; | |
| 56 }; | |
| 57 | |
| 33 } // namespace | 58 } // namespace |
| 34 | 59 |
| 35 namespace content { | |
| 36 | |
| 37 // These methods are used to pack and unpack the page_url/storage_area_id into | 60 // These methods are used to pack and unpack the page_url/storage_area_id into |
| 38 // source strings to/from the browser. | 61 // source strings to/from the browser. |
| 39 std::string PackSource(const GURL& page_url, | 62 std::string PackSource(const GURL& page_url, |
| 40 const std::string& storage_area_id) { | 63 const std::string& storage_area_id) { |
| 41 return page_url.spec() + "\n" + storage_area_id; | 64 return page_url.spec() + "\n" + storage_area_id; |
| 42 } | 65 } |
| 43 | 66 |
| 44 void UnpackSource(const std::string& source, | 67 void UnpackSource(const std::string& source, |
| 45 GURL* page_url, | 68 GURL* page_url, |
| 46 std::string* storage_area_id) { | 69 std::string* storage_area_id) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 } | 234 } |
| 212 ++iter; | 235 ++iter; |
| 213 } | 236 } |
| 214 } | 237 } |
| 215 | 238 |
| 216 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( | 239 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( |
| 217 base::NullableString16(), base::NullableString16(), | 240 base::NullableString16(), base::NullableString16(), |
| 218 base::NullableString16(), origin_.GetURL(), page_url, originating_area); | 241 base::NullableString16(), origin_.GetURL(), page_url, originating_area); |
| 219 } | 242 } |
| 220 | 243 |
| 221 void LocalStorageCachedArea::GetAllComplete(const std::string& source) { | |
| 222 // Since the GetAll method is synchronous, we need this asynchronously | |
| 223 // delivered notification to avoid applying changes to the returned array | |
| 224 // that we already have. | |
| 225 if (source == get_all_request_id_) { | |
| 226 DCHECK(ignore_all_mutations_); | |
| 227 DCHECK(!get_all_request_id_.empty()); | |
| 228 ignore_all_mutations_ = false; | |
| 229 get_all_request_id_.clear(); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 void LocalStorageCachedArea::KeyAddedOrChanged( | 244 void LocalStorageCachedArea::KeyAddedOrChanged( |
| 234 const std::vector<uint8_t>& key, | 245 const std::vector<uint8_t>& key, |
| 235 const std::vector<uint8_t>& new_value, | 246 const std::vector<uint8_t>& new_value, |
| 236 const base::NullableString16& old_value, | 247 const base::NullableString16& old_value, |
| 237 const std::string& source) { | 248 const std::string& source) { |
| 238 GURL page_url; | 249 GURL page_url; |
| 239 std::string storage_area_id; | 250 std::string storage_area_id; |
| 240 UnpackSource(source, &page_url, &storage_area_id); | 251 UnpackSource(source, &page_url, &storage_area_id); |
| 241 | 252 |
| 242 base::string16 key_string = Uint8VectorToString16(key); | 253 base::string16 key_string = Uint8VectorToString16(key); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 265 key_string, old_value, new_value_string, origin_.GetURL(), page_url, | 276 key_string, old_value, new_value_string, origin_.GetURL(), page_url, |
| 266 originating_area); | 277 originating_area); |
| 267 } | 278 } |
| 268 | 279 |
| 269 void LocalStorageCachedArea::EnsureLoaded() { | 280 void LocalStorageCachedArea::EnsureLoaded() { |
| 270 if (map_) | 281 if (map_) |
| 271 return; | 282 return; |
| 272 | 283 |
| 273 base::TimeTicks before = base::TimeTicks::Now(); | 284 base::TimeTicks before = base::TimeTicks::Now(); |
| 274 ignore_all_mutations_ = true; | 285 ignore_all_mutations_ = true; |
| 275 get_all_request_id_ = base::Uint64ToString(base::RandUint64()); | |
| 276 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK; | 286 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK; |
| 277 std::vector<content::mojom::KeyValuePtr> data; | 287 std::vector<content::mojom::KeyValuePtr> data; |
| 278 leveldb_->GetAll(get_all_request_id_, &status, &data); | 288 leveldb_->GetAll(GetAllCallback::CreateAndBind( |
| 289 leveldb_.associated_group(), | |
| 290 base::Bind(&LocalStorageCachedArea::OnGetAllComplete, | |
| 291 weak_factory_.GetWeakPtr())), | |
| 292 &status, &data); | |
| 279 | 293 |
| 280 DOMStorageValuesMap values; | 294 DOMStorageValuesMap values; |
| 281 for (size_t i = 0; i < data.size(); ++i) { | 295 for (size_t i = 0; i < data.size(); ++i) { |
| 282 values[Uint8VectorToString16(data[i]->key)] = | 296 values[Uint8VectorToString16(data[i]->key)] = |
| 283 base::NullableString16(Uint8VectorToString16(data[i]->value), false); | 297 base::NullableString16(Uint8VectorToString16(data[i]->value), false); |
| 284 } | 298 } |
| 285 | 299 |
| 286 map_ = new DOMStorageMap(kPerStorageAreaQuota); | 300 map_ = new DOMStorageMap(kPerStorageAreaQuota); |
| 287 map_->SwapValues(&values); | 301 map_->SwapValues(&values); |
| 288 | 302 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 329 if (--found->second == 0) | 343 if (--found->second == 0) |
| 330 ignore_key_mutations_.erase(found); | 344 ignore_key_mutations_.erase(found); |
| 331 } | 345 } |
| 332 | 346 |
| 333 void LocalStorageCachedArea::OnClearComplete(bool success) { | 347 void LocalStorageCachedArea::OnClearComplete(bool success) { |
| 334 DCHECK(success); | 348 DCHECK(success); |
| 335 DCHECK(ignore_all_mutations_); | 349 DCHECK(ignore_all_mutations_); |
| 336 ignore_all_mutations_ = false; | 350 ignore_all_mutations_ = false; |
| 337 } | 351 } |
| 338 | 352 |
| 353 void LocalStorageCachedArea::OnGetAllComplete(bool success) { | |
| 354 // Since the GetAll method is synchronous, we need this asynchronously | |
| 355 // delivered notification to avoid applying changes to the returned array | |
| 356 // that we already have. | |
| 357 DCHECK(success); | |
| 358 DCHECK(ignore_all_mutations_); | |
| 359 ignore_all_mutations_ = false; | |
| 360 } | |
| 361 | |
| 339 void LocalStorageCachedArea::Reset() { | 362 void LocalStorageCachedArea::Reset() { |
| 340 map_ = NULL; | 363 map_ = NULL; |
| 341 ignore_key_mutations_.clear(); | 364 ignore_key_mutations_.clear(); |
| 342 ignore_all_mutations_ = false; | 365 ignore_all_mutations_ = false; |
| 343 weak_factory_.InvalidateWeakPtrs(); | 366 weak_factory_.InvalidateWeakPtrs(); |
| 344 } | 367 } |
| 345 | 368 |
| 346 } // namespace content | 369 } // namespace content |
| OLD | NEW |