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

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

Issue 2201763002: Switch on use_new_wrapper_types mode for content/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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/rand_util.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h" 11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
13 #include "content/common/dom_storage/dom_storage_map.h" 14 #include "content/common/dom_storage/dom_storage_map.h"
14 #include "content/common/storage_partition_service.mojom.h" 15 #include "content/common/storage_partition_service.mojom.h"
15 #include "content/renderer/dom_storage/local_storage_area.h" 16 #include "content/renderer/dom_storage/local_storage_area.h"
16 #include "content/renderer/dom_storage/local_storage_cached_areas.h" 17 #include "content/renderer/dom_storage/local_storage_cached_areas.h"
17 #include "mojo/common/common_type_converters.h"
18 #include "third_party/WebKit/public/platform/WebURL.h" 18 #include "third_party/WebKit/public/platform/WebURL.h"
19 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h" 19 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
20 #include "url/gurl.h" 20 #include "url/gurl.h"
21 21
22 namespace content { 22 namespace content {
23 23
24 // These methods are used to pack and unpack the page_url/storage_area_id into 24 // These methods are used to pack and unpack the page_url/storage_area_id into
25 // source strings to/from the browser. 25 // source strings to/from the browser.
26 std::string PackSource(const GURL& page_url, 26 std::string PackSource(const GURL& page_url,
27 const std::string& storage_area_id) { 27 const std::string& storage_area_id) {
28 return page_url.spec() + "\n" + storage_area_id; 28 return page_url.spec() + "\n" + storage_area_id;
29 } 29 }
30 30
31 void UnpackSource(const mojo::String& source, 31 void UnpackSource(const std::string& source,
32 GURL* page_url, 32 GURL* page_url,
33 std::string* storage_area_id) { 33 std::string* storage_area_id) {
34 std::vector<std::string> result = base::SplitString( 34 std::vector<std::string> result = base::SplitString(
35 source.To<std::string>(), "\n", base::KEEP_WHITESPACE, 35 source, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
36 base::SPLIT_WANT_ALL);
37 DCHECK_EQ(result.size(), 2u); 36 DCHECK_EQ(result.size(), 2u);
38 *page_url = GURL(result[0]); 37 *page_url = GURL(result[0]);
39 *storage_area_id = result[1]; 38 *storage_area_id = result[1];
40 } 39 }
41 40
42 LocalStorageCachedArea::LocalStorageCachedArea( 41 LocalStorageCachedArea::LocalStorageCachedArea(
43 const url::Origin& origin, 42 const url::Origin& origin,
44 mojom::StoragePartitionService* storage_partition_service, 43 mojom::StoragePartitionService* storage_partition_service,
45 LocalStorageCachedAreas* cached_areas) 44 LocalStorageCachedAreas* cached_areas)
46 : origin_(origin), binding_(this), 45 : origin_(origin), binding_(this),
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 if (key.length() + value.length() > kPerStorageAreaQuota) 77 if (key.length() + value.length() > kPerStorageAreaQuota)
79 return false; 78 return false;
80 79
81 EnsureLoaded(); 80 EnsureLoaded();
82 base::NullableString16 unused; 81 base::NullableString16 unused;
83 if (!map_->SetItem(key, value, &unused)) 82 if (!map_->SetItem(key, value, &unused))
84 return false; 83 return false;
85 84
86 // Ignore mutations to |key| until OnSetItemComplete. 85 // Ignore mutations to |key| until OnSetItemComplete.
87 ignore_key_mutations_[key]++; 86 ignore_key_mutations_[key]++;
88 leveldb_->Put(mojo::Array<uint8_t>::From(key), 87 leveldb_->Put(base::String16ToUint8Vector(key),
89 mojo::Array<uint8_t>::From(value), 88 base::String16ToUint8Vector(value),
90 PackSource(page_url, storage_area_id), 89 PackSource(page_url, storage_area_id),
91 base::Bind(&LocalStorageCachedArea::OnSetItemComplete, 90 base::Bind(&LocalStorageCachedArea::OnSetItemComplete,
92 weak_factory_.GetWeakPtr(), key)); 91 weak_factory_.GetWeakPtr(), key));
93 return true; 92 return true;
94 } 93 }
95 94
96 void LocalStorageCachedArea::RemoveItem(const base::string16& key, 95 void LocalStorageCachedArea::RemoveItem(const base::string16& key,
97 const GURL& page_url, 96 const GURL& page_url,
98 const std::string& storage_area_id) { 97 const std::string& storage_area_id) {
99 EnsureLoaded(); 98 EnsureLoaded();
100 base::string16 unused; 99 base::string16 unused;
101 if (!map_->RemoveItem(key, &unused)) 100 if (!map_->RemoveItem(key, &unused))
102 return; 101 return;
103 102
104 // Ignore mutations to |key| until OnRemoveItemComplete. 103 // Ignore mutations to |key| until OnRemoveItemComplete.
105 ignore_key_mutations_[key]++; 104 ignore_key_mutations_[key]++;
106 leveldb_->Delete(mojo::Array<uint8_t>::From(key), 105 leveldb_->Delete(base::String16ToUint8Vector(key),
107 PackSource(page_url, storage_area_id), 106 PackSource(page_url, storage_area_id),
108 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete, 107 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete,
109 weak_factory_.GetWeakPtr(), key)); 108 weak_factory_.GetWeakPtr(), key));
110 } 109 }
111 110
112 void LocalStorageCachedArea::Clear(const GURL& page_url, 111 void LocalStorageCachedArea::Clear(const GURL& page_url,
113 const std::string& storage_area_id) { 112 const std::string& storage_area_id) {
114 // No need to prime the cache in this case. 113 // No need to prime the cache in this case.
115 114
116 Reset(); 115 Reset();
117 map_ = new DOMStorageMap(kPerStorageAreaQuota); 116 map_ = new DOMStorageMap(kPerStorageAreaQuota);
118 ignore_all_mutations_ = true; 117 ignore_all_mutations_ = true;
119 leveldb_->DeleteAll(PackSource(page_url, storage_area_id), 118 leveldb_->DeleteAll(PackSource(page_url, storage_area_id),
120 base::Bind(&LocalStorageCachedArea::OnClearComplete, 119 base::Bind(&LocalStorageCachedArea::OnClearComplete,
121 weak_factory_.GetWeakPtr())); 120 weak_factory_.GetWeakPtr()));
122 } 121 }
123 122
124 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) { 123 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) {
125 areas_[area->id()] = area; 124 areas_[area->id()] = area;
126 } 125 }
127 126
128 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) { 127 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) {
129 areas_.erase(area->id()); 128 areas_.erase(area->id());
130 } 129 }
131 130
132 void LocalStorageCachedArea::KeyAdded(mojo::Array<uint8_t> key, 131 void LocalStorageCachedArea::KeyAdded(const std::vector<uint8_t>& key,
133 mojo::Array<uint8_t> value, 132 const std::vector<uint8_t>& value,
134 const mojo::String& source) { 133 const std::string& source) {
135 base::NullableString16 null_value; 134 base::NullableString16 null_value;
136 KeyAddedOrChanged(std::move(key), std::move(value), 135 KeyAddedOrChanged(key, value, null_value, source);
137 null_value, source);
138 } 136 }
139 137
140 void LocalStorageCachedArea::KeyChanged(mojo::Array<uint8_t> key, 138 void LocalStorageCachedArea::KeyChanged(const std::vector<uint8_t>& key,
141 mojo::Array<uint8_t> new_value, 139 const std::vector<uint8_t>& new_value,
142 mojo::Array<uint8_t> old_value, 140 const std::vector<uint8_t>& old_value,
143 const mojo::String& source) { 141 const std::string& source) {
144 base::NullableString16 old_value_str(old_value.To<base::string16>(), false); 142 base::NullableString16 old_value_str(base::Uint8VectorToString16(old_value),
145 KeyAddedOrChanged(std::move(key), std::move(new_value), 143 false);
146 old_value_str, source); 144 KeyAddedOrChanged(key, new_value, old_value_str, source);
147 } 145 }
148 146
149 void LocalStorageCachedArea::KeyDeleted(mojo::Array<uint8_t> key, 147 void LocalStorageCachedArea::KeyDeleted(const std::vector<uint8_t>& key,
150 mojo::Array<uint8_t> old_value, 148 const std::vector<uint8_t>& old_value,
151 const mojo::String& source) { 149 const std::string& source) {
152 GURL page_url; 150 GURL page_url;
153 std::string storage_area_id; 151 std::string storage_area_id;
154 UnpackSource(source, &page_url, &storage_area_id); 152 UnpackSource(source, &page_url, &storage_area_id);
155 153
156 base::string16 key_string = key.To<base::string16>(); 154 base::string16 key_string = base::Uint8VectorToString16(key);
157 155
158 blink::WebStorageArea* originating_area = nullptr; 156 blink::WebStorageArea* originating_area = nullptr;
159 if (areas_.find(storage_area_id) != areas_.end()) { 157 if (areas_.find(storage_area_id) != areas_.end()) {
160 // The source storage area is in this process. 158 // The source storage area is in this process.
161 originating_area = areas_[storage_area_id]; 159 originating_area = areas_[storage_area_id];
162 } else if (map_ && !ignore_all_mutations_) { 160 } else if (map_ && !ignore_all_mutations_) {
163 // This was from another process or the storage area is gone. If the former, 161 // This was from another process or the storage area is gone. If the former,
164 // remove it from our cache if we haven't already changed it and are waiting 162 // remove it from our cache if we haven't already changed it and are waiting
165 // for the confirmation callback. In the latter case, we won't do anything 163 // for the confirmation callback. In the latter case, we won't do anything
166 // because ignore_key_mutations_ won't be updated until the callback runs. 164 // because ignore_key_mutations_ won't be updated until the callback runs.
167 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) { 165 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) {
168 base::string16 unused; 166 base::string16 unused;
169 map_->RemoveItem(key_string, &unused); 167 map_->RemoveItem(key_string, &unused);
170 } 168 }
171 } 169 }
172 170
173 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( 171 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
174 key_string, old_value.To<base::string16>(), base::NullableString16(), 172 key_string, base::Uint8VectorToString16(old_value),
175 GURL(origin_.Serialize()), page_url, originating_area); 173 base::NullableString16(), GURL(origin_.Serialize()), page_url,
174 originating_area);
176 } 175 }
177 176
178 void LocalStorageCachedArea::AllDeleted(const mojo::String& source) { 177 void LocalStorageCachedArea::AllDeleted(const std::string& source) {
179 GURL page_url; 178 GURL page_url;
180 std::string storage_area_id; 179 std::string storage_area_id;
181 UnpackSource(source, &page_url, &storage_area_id); 180 UnpackSource(source, &page_url, &storage_area_id);
182 181
183 blink::WebStorageArea* originating_area = nullptr; 182 blink::WebStorageArea* originating_area = nullptr;
184 if (areas_.find(storage_area_id) != areas_.end()) { 183 if (areas_.find(storage_area_id) != areas_.end()) {
185 // The source storage area is in this process. 184 // The source storage area is in this process.
186 originating_area = areas_[storage_area_id]; 185 originating_area = areas_[storage_area_id];
187 } else if (map_ && !ignore_all_mutations_) { 186 } else if (map_ && !ignore_all_mutations_) {
188 scoped_refptr<DOMStorageMap> old = map_; 187 scoped_refptr<DOMStorageMap> old = map_;
(...skipping 11 matching lines...) Expand all
200 ++iter; 199 ++iter;
201 } 200 }
202 } 201 }
203 202
204 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( 203 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
205 base::NullableString16(), base::NullableString16(), 204 base::NullableString16(), base::NullableString16(),
206 base::NullableString16(), GURL(origin_.Serialize()), page_url, 205 base::NullableString16(), GURL(origin_.Serialize()), page_url,
207 originating_area); 206 originating_area);
208 } 207 }
209 208
210 void LocalStorageCachedArea::GetAllComplete(const mojo::String& source) { 209 void LocalStorageCachedArea::GetAllComplete(const std::string& source) {
211 // Since the GetAll method is synchronous, we need this asynchronously 210 // Since the GetAll method is synchronous, we need this asynchronously
212 // delivered notification to avoid applying changes to the returned array 211 // delivered notification to avoid applying changes to the returned array
213 // that we already have. 212 // that we already have.
214 if (source.To<std::string>() == get_all_request_id_) { 213 if (source == get_all_request_id_) {
215 DCHECK(ignore_all_mutations_); 214 DCHECK(ignore_all_mutations_);
216 DCHECK(!get_all_request_id_.empty()); 215 DCHECK(!get_all_request_id_.empty());
217 ignore_all_mutations_ = false; 216 ignore_all_mutations_ = false;
218 get_all_request_id_.clear(); 217 get_all_request_id_.clear();
219 } 218 }
220 } 219 }
221 220
222 void LocalStorageCachedArea::KeyAddedOrChanged( 221 void LocalStorageCachedArea::KeyAddedOrChanged(
223 mojo::Array<uint8_t> key, 222 const std::vector<uint8_t>& key,
224 mojo::Array<uint8_t> new_value, 223 const std::vector<uint8_t>& new_value,
225 const base::NullableString16& old_value, 224 const base::NullableString16& old_value,
226 const mojo::String& source) { 225 const std::string& source) {
227 GURL page_url; 226 GURL page_url;
228 std::string storage_area_id; 227 std::string storage_area_id;
229 UnpackSource(source, &page_url, &storage_area_id); 228 UnpackSource(source, &page_url, &storage_area_id);
230 229
231 base::string16 key_string = key.To<base::string16>(); 230 base::string16 key_string = base::Uint8VectorToString16(key);
232 base::string16 new_value_string = new_value.To<base::string16>(); 231 base::string16 new_value_string = base::Uint8VectorToString16(new_value);
233 232
234 blink::WebStorageArea* originating_area = nullptr; 233 blink::WebStorageArea* originating_area = nullptr;
235 if (areas_.find(storage_area_id) != areas_.end()) { 234 if (areas_.find(storage_area_id) != areas_.end()) {
236 // The source storage area is in this process. 235 // The source storage area is in this process.
237 originating_area = areas_[storage_area_id]; 236 originating_area = areas_[storage_area_id];
238 } else if (map_ && !ignore_all_mutations_) { 237 } else if (map_ && !ignore_all_mutations_) {
239 // This was from another process or the storage area is gone. If the former, 238 // This was from another process or the storage area is gone. If the former,
240 // apply it to our cache if we haven't already changed it and are waiting 239 // apply it to our cache if we haven't already changed it and are waiting
241 // for the confirmation callback. In the latter case, we won't do anything 240 // for the confirmation callback. In the latter case, we won't do anything
242 // because ignore_key_mutations_ won't be updated until the callback runs. 241 // because ignore_key_mutations_ won't be updated until the callback runs.
(...skipping 14 matching lines...) Expand all
257 } 256 }
258 257
259 void LocalStorageCachedArea::EnsureLoaded() { 258 void LocalStorageCachedArea::EnsureLoaded() {
260 if (map_) 259 if (map_)
261 return; 260 return;
262 261
263 base::TimeTicks before = base::TimeTicks::Now(); 262 base::TimeTicks before = base::TimeTicks::Now();
264 ignore_all_mutations_ = true; 263 ignore_all_mutations_ = true;
265 get_all_request_id_ = base::Uint64ToString(base::RandUint64()); 264 get_all_request_id_ = base::Uint64ToString(base::RandUint64());
266 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK; 265 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK;
267 mojo::Array<content::mojom::KeyValuePtr> data; 266 std::vector<content::mojom::KeyValuePtr> data;
268 leveldb_->GetAll(get_all_request_id_, &status, &data); 267 leveldb_->GetAll(get_all_request_id_, &status, &data);
269 268
270 DOMStorageValuesMap values; 269 DOMStorageValuesMap values;
271 for (size_t i = 0; i < data.size(); ++i) { 270 for (size_t i = 0; i < data.size(); ++i) {
272 values[data[i]->key.To<base::string16>()] = 271 values[base::Uint8VectorToString16(data[i]->key)] = base::NullableString16(
273 base::NullableString16(data[i]->value.To<base::string16>(), false); 272 base::Uint8VectorToString16(data[i]->value), false);
274 } 273 }
275 274
276 map_ = new DOMStorageMap(kPerStorageAreaQuota); 275 map_ = new DOMStorageMap(kPerStorageAreaQuota);
277 map_->SwapValues(&values); 276 map_->SwapValues(&values);
278 277
279 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before; 278 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before;
280 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrime", time_to_prime); 279 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrime", time_to_prime);
281 280
282 size_t local_storage_size_kb = map_->bytes_used() / 1024; 281 size_t local_storage_size_kb = map_->bytes_used() / 1024;
283 // Track localStorage size, from 0-6MB. Note that the maximum size should be 282 // Track localStorage size, from 0-6MB. Note that the maximum size should be
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 } 326 }
328 327
329 void LocalStorageCachedArea::Reset() { 328 void LocalStorageCachedArea::Reset() {
330 map_ = NULL; 329 map_ = NULL;
331 ignore_key_mutations_.clear(); 330 ignore_key_mutations_.clear();
332 ignore_all_mutations_ = false; 331 ignore_all_mutations_ = false;
333 weak_factory_.InvalidateWeakPtrs(); 332 weak_factory_.InvalidateWeakPtrs();
334 } 333 }
335 334
336 } // namespace content 335 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698