Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: content/renderer/dom_storage/local_storage_cached_area.cc

Issue 2611843004: Implement throttling behavior for LocalStorage mojo messages.
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 } 76 }
77 77
78 LocalStorageCachedArea::LocalStorageCachedArea( 78 LocalStorageCachedArea::LocalStorageCachedArea(
79 const url::Origin& origin, 79 const url::Origin& origin,
80 mojom::StoragePartitionService* storage_partition_service, 80 mojom::StoragePartitionService* storage_partition_service,
81 LocalStorageCachedAreas* cached_areas) 81 LocalStorageCachedAreas* cached_areas)
82 : origin_(origin), binding_(this), 82 : origin_(origin), binding_(this),
83 cached_areas_(cached_areas), weak_factory_(this) { 83 cached_areas_(cached_areas), weak_factory_(this) {
84 storage_partition_service->OpenLocalStorage(origin_, 84 storage_partition_service->OpenLocalStorage(origin_,
85 mojo::MakeRequest(&leveldb_)); 85 mojo::MakeRequest(&leveldb_));
86 leveldb_.set_process_async_during_sync(true);
86 mojom::LevelDBObserverAssociatedPtrInfo ptr_info; 87 mojom::LevelDBObserverAssociatedPtrInfo ptr_info;
87 binding_.Bind(&ptr_info, leveldb_.associated_group()); 88 binding_.Bind(&ptr_info, leveldb_.associated_group());
88 leveldb_->AddObserver(std::move(ptr_info)); 89 leveldb_->AddObserver(std::move(ptr_info));
89 } 90 }
90 91
91 LocalStorageCachedArea::~LocalStorageCachedArea() { 92 LocalStorageCachedArea::~LocalStorageCachedArea() {
92 cached_areas_->CacheAreaClosed(this); 93 cached_areas_->CacheAreaClosed(this);
93 } 94 }
94 95
95 unsigned LocalStorageCachedArea::GetLength() { 96 unsigned LocalStorageCachedArea::GetLength() {
(...skipping 25 matching lines...) Expand all
121 base::NullableString16 unused; 122 base::NullableString16 unused;
122 if (!map_->SetItem(key, value, &unused)) 123 if (!map_->SetItem(key, value, &unused))
123 return false; 124 return false;
124 125
125 // Ignore mutations to |key| until OnSetItemComplete. 126 // Ignore mutations to |key| until OnSetItemComplete.
126 ignore_key_mutations_[key]++; 127 ignore_key_mutations_[key]++;
127 leveldb_->Put(String16ToUint8Vector(key), String16ToUint8Vector(value), 128 leveldb_->Put(String16ToUint8Vector(key), String16ToUint8Vector(value),
128 PackSource(page_url, storage_area_id), 129 PackSource(page_url, storage_area_id),
129 base::Bind(&LocalStorageCachedArea::OnSetItemComplete, 130 base::Bind(&LocalStorageCachedArea::OnSetItemComplete,
130 weak_factory_.GetWeakPtr(), key)); 131 weak_factory_.GetWeakPtr(), key));
132 FlushIfNeeded();
133
131 return true; 134 return true;
132 } 135 }
133 136
134 void LocalStorageCachedArea::RemoveItem(const base::string16& key, 137 void LocalStorageCachedArea::RemoveItem(const base::string16& key,
135 const GURL& page_url, 138 const GURL& page_url,
136 const std::string& storage_area_id) { 139 const std::string& storage_area_id) {
137 EnsureLoaded(); 140 EnsureLoaded();
138 base::string16 unused; 141 base::string16 unused;
139 if (!map_->RemoveItem(key, &unused)) 142 if (!map_->RemoveItem(key, &unused))
140 return; 143 return;
141 144
142 // Ignore mutations to |key| until OnRemoveItemComplete. 145 // Ignore mutations to |key| until OnRemoveItemComplete.
143 ignore_key_mutations_[key]++; 146 ignore_key_mutations_[key]++;
144 leveldb_->Delete(String16ToUint8Vector(key), 147 leveldb_->Delete(String16ToUint8Vector(key),
145 PackSource(page_url, storage_area_id), 148 PackSource(page_url, storage_area_id),
146 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete, 149 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete,
147 weak_factory_.GetWeakPtr(), key)); 150 weak_factory_.GetWeakPtr(), key));
151 FlushIfNeeded();
148 } 152 }
149 153
150 void LocalStorageCachedArea::Clear(const GURL& page_url, 154 void LocalStorageCachedArea::Clear(const GURL& page_url,
151 const std::string& storage_area_id) { 155 const std::string& storage_area_id) {
152 // No need to prime the cache in this case. 156 // No need to prime the cache in this case.
153
154 Reset(); 157 Reset();
155 map_ = new DOMStorageMap(kPerStorageAreaQuota); 158 map_ = new DOMStorageMap(kPerStorageAreaQuota);
156 ignore_all_mutations_ = true; 159 ignore_all_mutations_ = true;
157 leveldb_->DeleteAll(PackSource(page_url, storage_area_id), 160 leveldb_->DeleteAll(PackSource(page_url, storage_area_id),
158 base::Bind(&LocalStorageCachedArea::OnClearComplete, 161 base::Bind(&LocalStorageCachedArea::OnClearComplete,
159 weak_factory_.GetWeakPtr())); 162 weak_factory_.GetWeakPtr()));
163 FlushIfNeeded();
160 } 164 }
161 165
162 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) { 166 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) {
163 areas_[area->id()] = area; 167 areas_[area->id()] = area;
164 } 168 }
165 169
166 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) { 170 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) {
167 areas_.erase(area->id()); 171 areas_.erase(area->id());
168 } 172 }
169 173
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 time_to_prime); 320 time_to_prime);
317 } else if (local_storage_size_kb < 1000) { 321 } else if (local_storage_size_kb < 1000) {
318 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor100KBTo1MB", 322 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor100KBTo1MB",
319 time_to_prime); 323 time_to_prime);
320 } else { 324 } else {
321 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor1MBTo5MB", 325 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor1MBTo5MB",
322 time_to_prime); 326 time_to_prime);
323 } 327 }
324 } 328 }
325 329
330 void LocalStorageCachedArea::FlushIfNeeded() {
331 if (leveldb_.pending_callback_count() > 1000) {
332 leveldb_->FlushPipe();
333 DCHECK_EQ(0u, leveldb_.pending_callback_count());
334 }
335 }
336
326 void LocalStorageCachedArea::OnSetItemComplete(const base::string16& key, 337 void LocalStorageCachedArea::OnSetItemComplete(const base::string16& key,
327 bool success) { 338 bool success) {
328 if (!success) { 339 if (!success) {
329 Reset(); 340 Reset();
330 return; 341 return;
331 } 342 }
332 343
333 auto found = ignore_key_mutations_.find(key); 344 auto found = ignore_key_mutations_.find(key);
334 DCHECK(found != ignore_key_mutations_.end()); 345 DCHECK(found != ignore_key_mutations_.end());
335 if (--found->second == 0) 346 if (--found->second == 0)
(...skipping 25 matching lines...) Expand all
361 } 372 }
362 373
363 void LocalStorageCachedArea::Reset() { 374 void LocalStorageCachedArea::Reset() {
364 map_ = NULL; 375 map_ = NULL;
365 ignore_key_mutations_.clear(); 376 ignore_key_mutations_.clear();
366 ignore_all_mutations_ = false; 377 ignore_all_mutations_ = false;
367 weak_factory_.InvalidateWeakPtrs(); 378 weak_factory_.InvalidateWeakPtrs();
368 } 379 }
369 380
370 } // namespace content 381 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/dom_storage/local_storage_cached_area.h ('k') | mojo/public/cpp/bindings/interface_endpoint_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698