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

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

Issue 1837883003: Some fleshing out the mojo based localstorage implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/rand_util.h"
9 #include "base/strings/string_split.h" 10 #include "base/strings/string_split.h"
10 #include "base/time/time.h" 11 #include "base/time/time.h"
11 #include "content/common/dom_storage/dom_storage_map.h" 12 #include "content/common/dom_storage/dom_storage_map.h"
12 #include "content/common/storage_partition_service.mojom.h" 13 #include "content/common/storage_partition_service.mojom.h"
13 #include "content/renderer/dom_storage/local_storage_area.h" 14 #include "content/renderer/dom_storage/local_storage_area.h"
14 #include "content/renderer/dom_storage/local_storage_cached_areas.h" 15 #include "content/renderer/dom_storage/local_storage_cached_areas.h"
15 #include "mojo/common/common_type_converters.h" 16 #include "mojo/common/common_type_converters.h"
16 #include "third_party/WebKit/public/platform/WebURL.h" 17 #include "third_party/WebKit/public/platform/WebURL.h"
17 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h" 18 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
18 #include "url/gurl.h" 19 #include "url/gurl.h"
(...skipping 15 matching lines...) Expand all
34 base::SPLIT_WANT_ALL); 35 base::SPLIT_WANT_ALL);
35 DCHECK_EQ(result.size(), 2u); 36 DCHECK_EQ(result.size(), 2u);
36 *page_url = GURL(result[0]); 37 *page_url = GURL(result[0]);
37 *storage_area_id = result[1]; 38 *storage_area_id = result[1];
38 } 39 }
39 40
40 LocalStorageCachedArea::LocalStorageCachedArea( 41 LocalStorageCachedArea::LocalStorageCachedArea(
41 const url::Origin& origin, 42 const url::Origin& origin,
42 mojom::StoragePartitionService* storage_partition_service, 43 mojom::StoragePartitionService* storage_partition_service,
43 LocalStorageCachedAreas* cached_areas) 44 LocalStorageCachedAreas* cached_areas)
44 : origin_(origin), binding_(this), cached_areas_(cached_areas) { 45 : origin_(origin), binding_(this),
46 cached_areas_(cached_areas), weak_factory_(this) {
45 storage_partition_service->OpenLocalStorage( 47 storage_partition_service->OpenLocalStorage(
46 origin_, mojo::GetProxy(&leveldb_)); 48 origin_, mojo::GetProxy(&leveldb_));
49 leveldb_->AddObserver(binding_.CreateInterfacePtrAndBind());
47 } 50 }
48 51
49 LocalStorageCachedArea::~LocalStorageCachedArea() { 52 LocalStorageCachedArea::~LocalStorageCachedArea() {
50 cached_areas_->CacheAreaClosed(this); 53 cached_areas_->CacheAreaClosed(this);
51 } 54 }
52 55
53 unsigned LocalStorageCachedArea::GetLength() { 56 unsigned LocalStorageCachedArea::GetLength() {
54 EnsureLoaded(); 57 EnsureLoaded();
55 return map_->Length(); 58 return map_->Length();
56 } 59 }
(...skipping 22 matching lines...) Expand all
79 base::NullableString16 unused; 82 base::NullableString16 unused;
80 if (!map_->SetItem(key, value, &unused)) 83 if (!map_->SetItem(key, value, &unused))
81 return false; 84 return false;
82 85
83 // Ignore mutations to |key| until OnSetItemComplete. 86 // Ignore mutations to |key| until OnSetItemComplete.
84 ignore_key_mutations_[key]++; 87 ignore_key_mutations_[key]++;
85 leveldb_->Put(mojo::Array<uint8_t>::From(key), 88 leveldb_->Put(mojo::Array<uint8_t>::From(key),
86 mojo::Array<uint8_t>::From(value), 89 mojo::Array<uint8_t>::From(value),
87 PackSource(page_url, storage_area_id), 90 PackSource(page_url, storage_area_id),
88 base::Bind(&LocalStorageCachedArea::OnSetItemComplete, 91 base::Bind(&LocalStorageCachedArea::OnSetItemComplete,
89 base::Unretained(this), key)); 92 weak_factory_.GetWeakPtr(), key));
90 return true; 93 return true;
91 } 94 }
92 95
93 void LocalStorageCachedArea::RemoveItem(const base::string16& key, 96 void LocalStorageCachedArea::RemoveItem(const base::string16& key,
94 const GURL& page_url, 97 const GURL& page_url,
95 const std::string& storage_area_id) { 98 const std::string& storage_area_id) {
96 EnsureLoaded(); 99 EnsureLoaded();
97 base::string16 unused; 100 base::string16 unused;
98 if (!map_->RemoveItem(key, &unused)) 101 if (!map_->RemoveItem(key, &unused))
99 return; 102 return;
100 103
101 // Ignore mutations to |key| until OnRemoveItemComplete. 104 // Ignore mutations to |key| until OnRemoveItemComplete.
102 ignore_key_mutations_[key]++; 105 ignore_key_mutations_[key]++;
103 leveldb_->Delete(mojo::Array<uint8_t>::From(key), 106 leveldb_->Delete(mojo::Array<uint8_t>::From(key),
104 PackSource(page_url, storage_area_id), 107 PackSource(page_url, storage_area_id),
105 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete, 108 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete,
106 base::Unretained(this), key)); 109 weak_factory_.GetWeakPtr(), key));
107 } 110 }
108 111
109 void LocalStorageCachedArea::Clear(const GURL& page_url, 112 void LocalStorageCachedArea::Clear(const GURL& page_url,
110 const std::string& storage_area_id) { 113 const std::string& storage_area_id) {
111 // No need to prime the cache in this case. 114 // No need to prime the cache in this case.
112 115
113 Reset(); 116 Reset();
114 map_ = new DOMStorageMap(kPerStorageAreaQuota); 117 map_ = new DOMStorageMap(kPerStorageAreaQuota);
115 118 ignore_all_mutations_ = true;
116 leveldb_->DeleteAll(binding_.CreateInterfacePtrAndBind(), 119 leveldb_->DeleteAll(PackSource(page_url, storage_area_id),
117 PackSource(page_url, storage_area_id),
118 base::Bind(&LocalStorageCachedArea::OnClearComplete, 120 base::Bind(&LocalStorageCachedArea::OnClearComplete,
119 base::Unretained(this))); 121 weak_factory_.GetWeakPtr()));
120 } 122 }
121 123
122 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) { 124 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) {
123 areas_[area->id()] = area; 125 areas_[area->id()] = area;
124 } 126 }
125 127
126 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) { 128 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) {
127 areas_.erase(area->id()); 129 areas_.erase(area->id());
128 } 130 }
129 131
132 void LocalStorageCachedArea::KeyAdded(mojo::Array<uint8_t> key,
133 mojo::Array<uint8_t> value,
134 const mojo::String& source) {
135 base::NullableString16 null_value;
136 KeyAddedOrChanged(std::move(key), std::move(value),
137 null_value, source);
138 }
139
130 void LocalStorageCachedArea::KeyChanged(mojo::Array<uint8_t> key, 140 void LocalStorageCachedArea::KeyChanged(mojo::Array<uint8_t> key,
131 mojo::Array<uint8_t> new_value, 141 mojo::Array<uint8_t> new_value,
132 mojo::Array<uint8_t> old_value, 142 mojo::Array<uint8_t> old_value,
133 const mojo::String& source) { 143 const mojo::String& source) {
134 GURL page_url; 144 base::NullableString16 old_value_str(old_value.To<base::string16>(), false);
135 std::string storage_area_id; 145 KeyAddedOrChanged(std::move(key), std::move(new_value),
136 UnpackSource(source, &page_url, &storage_area_id); 146 old_value_str, source);
137
138 base::string16 key_string = key.To<base::string16>();
139 base::string16 new_value_string = new_value.To<base::string16>();
140
141 blink::WebStorageArea* originating_area = nullptr;
142 if (areas_.find(storage_area_id) != areas_.end()) {
143 // The source storage area is in this process.
144 originating_area = areas_[storage_area_id];
145 } else {
146 // This was from another process or the storage area is gone. If the former,
147 // apply it to our cache if we haven't already changed it and are waiting
148 // for the confirmation callback. In the latter case, we won't do anything
149 // because ignore_key_mutations_ won't be updated until the callback runs.
150 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) {
151 // We turn off quota checking here to accomodate the over budget allowance
152 // that's provided in the browser process.
153 base::NullableString16 unused;
154 map_->set_quota(std::numeric_limits<int32_t>::max());
155 map_->SetItem(key_string, new_value_string, &unused);
156 map_->set_quota(kPerStorageAreaQuota);
157 }
158 }
159
160 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
161 key_string, old_value.To<base::string16>(), new_value_string,
162 GURL(origin_.Serialize()), page_url, originating_area);
163 } 147 }
164 148
165 void LocalStorageCachedArea::KeyDeleted(mojo::Array<uint8_t> key, 149 void LocalStorageCachedArea::KeyDeleted(mojo::Array<uint8_t> key,
166 mojo::Array<uint8_t> old_value, 150 mojo::Array<uint8_t> old_value,
167 const mojo::String& source) { 151 const mojo::String& source) {
168 GURL page_url; 152 GURL page_url;
169 std::string storage_area_id; 153 std::string storage_area_id;
170 UnpackSource(source, &page_url, &storage_area_id); 154 UnpackSource(source, &page_url, &storage_area_id);
171 155
172 base::string16 key_string = key.To<base::string16>(); 156 base::string16 key_string = key.To<base::string16>();
173 157
174 blink::WebStorageArea* originating_area = nullptr; 158 blink::WebStorageArea* originating_area = nullptr;
175 if (areas_.find(storage_area_id) != areas_.end()) { 159 if (areas_.find(storage_area_id) != areas_.end()) {
176 // The source storage area is in this process. 160 // The source storage area is in this process.
177 originating_area = areas_[storage_area_id]; 161 originating_area = areas_[storage_area_id];
178 } else { 162 } else if (map_.get() && !ignore_all_mutations_) {
179 // This was from another process or the storage area is gone. If the former, 163 // This was from another process or the storage area is gone. If the former,
180 // remove it from our cache if we haven't already changed it and are waiting 164 // remove it from our cache if we haven't already changed it and are waiting
181 // for the confirmation callback. In the latter case, we won't do anything 165 // for the confirmation callback. In the latter case, we won't do anything
182 // because ignore_key_mutations_ won't be updated until the callback runs. 166 // because ignore_key_mutations_ won't be updated until the callback runs.
183 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) { 167 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) {
184 base::string16 unused; 168 base::string16 unused;
185 map_->RemoveItem(key_string, &unused); 169 map_->RemoveItem(key_string, &unused);
186 } 170 }
187 } 171 }
188 172
189 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( 173 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
190 key_string, old_value.To<base::string16>(), base::NullableString16(), 174 key_string, old_value.To<base::string16>(), base::NullableString16(),
191 GURL(origin_.Serialize()), page_url, originating_area); 175 GURL(origin_.Serialize()), page_url, originating_area);
192 } 176 }
193 177
194 void LocalStorageCachedArea::AllDeleted(const mojo::String& source) { 178 void LocalStorageCachedArea::AllDeleted(const mojo::String& source) {
195 GURL page_url; 179 GURL page_url;
196 std::string storage_area_id; 180 std::string storage_area_id;
197 UnpackSource(source, &page_url, &storage_area_id); 181 UnpackSource(source, &page_url, &storage_area_id);
198 182
199 blink::WebStorageArea* originating_area = nullptr; 183 blink::WebStorageArea* originating_area = nullptr;
200 if (areas_.find(storage_area_id) != areas_.end()) { 184 if (areas_.find(storage_area_id) != areas_.end()) {
201 // The source storage area is in this process. 185 // The source storage area is in this process.
202 originating_area = areas_[storage_area_id]; 186 originating_area = areas_[storage_area_id];
203 } else { 187 } else if (map_.get() && !ignore_all_mutations_) {
204 scoped_refptr<DOMStorageMap> old = map_; 188 scoped_refptr<DOMStorageMap> old = map_;
205 map_ = new DOMStorageMap(kPerStorageAreaQuota); 189 map_ = new DOMStorageMap(kPerStorageAreaQuota);
206 190
207 // We have to retain local additions which happened after this clear 191 // We have to retain local additions which happened after this clear
208 // operation from another process. 192 // operation from another process.
209 auto iter = ignore_key_mutations_.begin(); 193 auto iter = ignore_key_mutations_.begin();
210 while (iter != ignore_key_mutations_.end()) { 194 while (iter != ignore_key_mutations_.end()) {
211 base::NullableString16 value = old->GetItem(iter->first); 195 base::NullableString16 value = old->GetItem(iter->first);
212 if (!value.is_null()) { 196 if (!value.is_null()) {
213 base::NullableString16 unused; 197 base::NullableString16 unused;
214 map_->SetItem(iter->first, value.string(), &unused); 198 map_->SetItem(iter->first, value.string(), &unused);
215 } 199 }
216 ++iter; 200 ++iter;
217 } 201 }
218 } 202 }
219 203
220 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( 204 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
221 base::NullableString16(), base::NullableString16(), 205 base::NullableString16(), base::NullableString16(),
222 base::NullableString16(), GURL(origin_.Serialize()), page_url, 206 base::NullableString16(), GURL(origin_.Serialize()), page_url,
223 originating_area); 207 originating_area);
224 } 208 }
225 209
210 void LocalStorageCachedArea::GetAllComplete(uint64_t request_id) {
jam 2016/03/30 17:15:16 this is subtle, per your offline explanation, so c
michaeln 2016/03/30 21:57:01 Done, added a comment in the this to the mojom fil
211 if (request_id == get_all_request_id_) {
212 DCHECK(ignore_all_mutations_);
213 ignore_all_mutations_ = false;
214 get_all_request_id_ = 0;
215 }
216 }
217
218 void LocalStorageCachedArea::KeyAddedOrChanged(
219 mojo::Array<uint8_t> key,
220 mojo::Array<uint8_t> new_value,
221 base::NullableString16& old_value,
222 const mojo::String& source) {
223 GURL page_url;
224 std::string storage_area_id;
225 UnpackSource(source, &page_url, &storage_area_id);
226
227 base::string16 key_string = key.To<base::string16>();
228 base::string16 new_value_string = new_value.To<base::string16>();
229
230 blink::WebStorageArea* originating_area = nullptr;
231 if (areas_.find(storage_area_id) != areas_.end()) {
232 // The source storage area is in this process.
233 originating_area = areas_[storage_area_id];
234 } else if (map_.get() && !ignore_all_mutations_) {
235 // This was from another process or the storage area is gone. If the former,
236 // apply it to our cache if we haven't already changed it and are waiting
237 // for the confirmation callback. In the latter case, we won't do anything
238 // because ignore_key_mutations_ won't be updated until the callback runs.
239 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) {
240 // We turn off quota checking here to accomodate the over budget allowance
241 // that's provided in the browser process.
242 base::NullableString16 unused;
243 map_->set_quota(std::numeric_limits<int32_t>::max());
244 map_->SetItem(key_string, new_value_string, &unused);
245 map_->set_quota(kPerStorageAreaQuota);
246 }
247 }
248
249 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
250 key_string, old_value, new_value_string,
251 GURL(origin_.Serialize()), page_url, originating_area);
252
253 }
254
226 void LocalStorageCachedArea::EnsureLoaded() { 255 void LocalStorageCachedArea::EnsureLoaded() {
227 if (map_) 256 if (map_.get())
228 return; 257 return;
229 258
230 base::TimeTicks before = base::TimeTicks::Now(); 259 base::TimeTicks before = base::TimeTicks::Now();
260 ignore_all_mutations_ = true;
261 do {
262 get_all_request_id_ = base::RandUint64();
263 } while(!get_all_request_id_);
231 leveldb::DatabaseError status = leveldb::DatabaseError::OK; 264 leveldb::DatabaseError status = leveldb::DatabaseError::OK;
232 mojo::Array<content::mojom::KeyValuePtr> data; 265 mojo::Array<content::mojom::KeyValuePtr> data;
233 leveldb_->GetAll(binding_.CreateInterfacePtrAndBind(), &status, &data); 266 leveldb_->GetAll(get_all_request_id_, &status, &data);
234 267
235 DOMStorageValuesMap values; 268 DOMStorageValuesMap values;
236 for (size_t i = 0; i < data.size(); ++i) { 269 for (size_t i = 0; i < data.size(); ++i) {
237 values[data[i]->key.To<base::string16>()] = 270 values[data[i]->key.To<base::string16>()] =
238 base::NullableString16(data[i]->value.To<base::string16>(), false); 271 base::NullableString16(data[i]->value.To<base::string16>(), false);
239 } 272 }
240 273
241 map_ = new DOMStorageMap(kPerStorageAreaQuota); 274 map_ = new DOMStorageMap(kPerStorageAreaQuota);
242 map_->SwapValues(&values); 275 map_->SwapValues(&values);
243 276
(...skipping 13 matching lines...) Expand all
257 } else if (local_storage_size_kb < 1000) { 290 } else if (local_storage_size_kb < 1000) {
258 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor100KBTo1MB", 291 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor100KBTo1MB",
259 time_to_prime); 292 time_to_prime);
260 } else { 293 } else {
261 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor1MBTo5MB", 294 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrimeFor1MBTo5MB",
262 time_to_prime); 295 time_to_prime);
263 } 296 }
264 } 297 }
265 298
266 void LocalStorageCachedArea::OnSetItemComplete(const base::string16& key, 299 void LocalStorageCachedArea::OnSetItemComplete(const base::string16& key,
267 leveldb::DatabaseError result) { 300 bool success) {
268 if (result != leveldb::DatabaseError::OK) { 301 if (!success) {
269 Reset(); 302 Reset();
270 return; 303 return;
271 } 304 }
272 305
273 auto found = ignore_key_mutations_.find(key); 306 auto found = ignore_key_mutations_.find(key);
274 DCHECK(found != ignore_key_mutations_.end()); 307 DCHECK(found != ignore_key_mutations_.end());
275 if (--found->second == 0) 308 if (--found->second == 0)
276 ignore_key_mutations_.erase(found); 309 ignore_key_mutations_.erase(found);
277 } 310 }
278 311
279 void LocalStorageCachedArea::OnRemoveItemComplete( 312 void LocalStorageCachedArea::OnRemoveItemComplete(
280 const base::string16& key, leveldb::DatabaseError result) { 313 const base::string16& key, bool success) {
281 DCHECK_EQ(result, leveldb::DatabaseError::OK); 314 DCHECK(success);
282 auto found = ignore_key_mutations_.find(key); 315 auto found = ignore_key_mutations_.find(key);
283 DCHECK(found != ignore_key_mutations_.end()); 316 DCHECK(found != ignore_key_mutations_.end());
284 if (--found->second == 0) 317 if (--found->second == 0)
285 ignore_key_mutations_.erase(found); 318 ignore_key_mutations_.erase(found);
286 } 319 }
287 320
288 void LocalStorageCachedArea::OnClearComplete(leveldb::DatabaseError result) { 321 void LocalStorageCachedArea::OnClearComplete(bool success) {
289 DCHECK_EQ(result, leveldb::DatabaseError::OK); 322 DCHECK(success);
323 DCHECK(ignore_all_mutations_);
324 ignore_all_mutations_ = false;
290 } 325 }
291 326
292 void LocalStorageCachedArea::Reset() { 327 void LocalStorageCachedArea::Reset() {
293 binding_.Close();
294 map_ = NULL; 328 map_ = NULL;
295 ignore_key_mutations_.clear(); 329 ignore_key_mutations_.clear();
330 ignore_all_mutations_ = false;
331 weak_factory_.InvalidateWeakPtrs();
296 } 332 }
297 333
298 } // namespace content 334 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698