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

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: Rebase, add changes for websocket.mojom 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/string16.h"
10 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.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 {
23
24 base::string16 Uint8VectorToString16(const std::vector<uint8_t>& input) {
25 if (input.empty())
26 return base::string16();
27
28 return base::string16(reinterpret_cast<const base::char16*>(&input.front()),
kinuko 2016/08/15 23:05:55 nit: use input.data() instead of &input.front() an
leonhsl(Using Gerrit) 2016/08/16 05:54:32 Done.
29 input.size() / sizeof(base::char16));
30 }
31
32 std::vector<uint8_t> String16ToUint8Vector(const base::string16& input) {
33 std::vector<uint8_t> result(input.size() * sizeof(base::char16));
34 if (!input.empty())
35 memcpy(&result.front(), input.c_str(), input.size() * sizeof(base::char16));
kinuko 2016/08/15 23:05:55 Hmm... could something like this work? const uint
leonhsl(Using Gerrit) 2016/08/16 05:54:32 Done and thanks! The code looks cleaner.
36 return result;
37 }
38
39 } // namespace
40
22 namespace content { 41 namespace content {
23 42
24 // These methods are used to pack and unpack the page_url/storage_area_id into 43 // These methods are used to pack and unpack the page_url/storage_area_id into
25 // source strings to/from the browser. 44 // source strings to/from the browser.
26 std::string PackSource(const GURL& page_url, 45 std::string PackSource(const GURL& page_url,
27 const std::string& storage_area_id) { 46 const std::string& storage_area_id) {
28 return page_url.spec() + "\n" + storage_area_id; 47 return page_url.spec() + "\n" + storage_area_id;
29 } 48 }
30 49
31 void UnpackSource(const mojo::String& source, 50 void UnpackSource(const std::string& source,
32 GURL* page_url, 51 GURL* page_url,
33 std::string* storage_area_id) { 52 std::string* storage_area_id) {
34 std::vector<std::string> result = base::SplitString( 53 std::vector<std::string> result = base::SplitString(
35 source.To<std::string>(), "\n", base::KEEP_WHITESPACE, 54 source, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
36 base::SPLIT_WANT_ALL);
37 DCHECK_EQ(result.size(), 2u); 55 DCHECK_EQ(result.size(), 2u);
38 *page_url = GURL(result[0]); 56 *page_url = GURL(result[0]);
39 *storage_area_id = result[1]; 57 *storage_area_id = result[1];
40 } 58 }
41 59
42 LocalStorageCachedArea::LocalStorageCachedArea( 60 LocalStorageCachedArea::LocalStorageCachedArea(
43 const url::Origin& origin, 61 const url::Origin& origin,
44 mojom::StoragePartitionService* storage_partition_service, 62 mojom::StoragePartitionService* storage_partition_service,
45 LocalStorageCachedAreas* cached_areas) 63 LocalStorageCachedAreas* cached_areas)
46 : origin_(origin), binding_(this), 64 : origin_(origin), binding_(this),
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 if (key.length() + value.length() > kPerStorageAreaQuota) 96 if (key.length() + value.length() > kPerStorageAreaQuota)
79 return false; 97 return false;
80 98
81 EnsureLoaded(); 99 EnsureLoaded();
82 base::NullableString16 unused; 100 base::NullableString16 unused;
83 if (!map_->SetItem(key, value, &unused)) 101 if (!map_->SetItem(key, value, &unused))
84 return false; 102 return false;
85 103
86 // Ignore mutations to |key| until OnSetItemComplete. 104 // Ignore mutations to |key| until OnSetItemComplete.
87 ignore_key_mutations_[key]++; 105 ignore_key_mutations_[key]++;
88 leveldb_->Put(mojo::Array<uint8_t>::From(key), 106 leveldb_->Put(String16ToUint8Vector(key), String16ToUint8Vector(value),
89 mojo::Array<uint8_t>::From(value),
90 PackSource(page_url, storage_area_id), 107 PackSource(page_url, storage_area_id),
91 base::Bind(&LocalStorageCachedArea::OnSetItemComplete, 108 base::Bind(&LocalStorageCachedArea::OnSetItemComplete,
92 weak_factory_.GetWeakPtr(), key)); 109 weak_factory_.GetWeakPtr(), key));
93 return true; 110 return true;
94 } 111 }
95 112
96 void LocalStorageCachedArea::RemoveItem(const base::string16& key, 113 void LocalStorageCachedArea::RemoveItem(const base::string16& key,
97 const GURL& page_url, 114 const GURL& page_url,
98 const std::string& storage_area_id) { 115 const std::string& storage_area_id) {
99 EnsureLoaded(); 116 EnsureLoaded();
100 base::string16 unused; 117 base::string16 unused;
101 if (!map_->RemoveItem(key, &unused)) 118 if (!map_->RemoveItem(key, &unused))
102 return; 119 return;
103 120
104 // Ignore mutations to |key| until OnRemoveItemComplete. 121 // Ignore mutations to |key| until OnRemoveItemComplete.
105 ignore_key_mutations_[key]++; 122 ignore_key_mutations_[key]++;
106 leveldb_->Delete(mojo::Array<uint8_t>::From(key), 123 leveldb_->Delete(String16ToUint8Vector(key),
107 PackSource(page_url, storage_area_id), 124 PackSource(page_url, storage_area_id),
108 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete, 125 base::Bind(&LocalStorageCachedArea::OnRemoveItemComplete,
109 weak_factory_.GetWeakPtr(), key)); 126 weak_factory_.GetWeakPtr(), key));
110 } 127 }
111 128
112 void LocalStorageCachedArea::Clear(const GURL& page_url, 129 void LocalStorageCachedArea::Clear(const GURL& page_url,
113 const std::string& storage_area_id) { 130 const std::string& storage_area_id) {
114 // No need to prime the cache in this case. 131 // No need to prime the cache in this case.
115 132
116 Reset(); 133 Reset();
117 map_ = new DOMStorageMap(kPerStorageAreaQuota); 134 map_ = new DOMStorageMap(kPerStorageAreaQuota);
118 ignore_all_mutations_ = true; 135 ignore_all_mutations_ = true;
119 leveldb_->DeleteAll(PackSource(page_url, storage_area_id), 136 leveldb_->DeleteAll(PackSource(page_url, storage_area_id),
120 base::Bind(&LocalStorageCachedArea::OnClearComplete, 137 base::Bind(&LocalStorageCachedArea::OnClearComplete,
121 weak_factory_.GetWeakPtr())); 138 weak_factory_.GetWeakPtr()));
122 } 139 }
123 140
124 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) { 141 void LocalStorageCachedArea::AreaCreated(LocalStorageArea* area) {
125 areas_[area->id()] = area; 142 areas_[area->id()] = area;
126 } 143 }
127 144
128 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) { 145 void LocalStorageCachedArea::AreaDestroyed(LocalStorageArea* area) {
129 areas_.erase(area->id()); 146 areas_.erase(area->id());
130 } 147 }
131 148
132 void LocalStorageCachedArea::KeyAdded(mojo::Array<uint8_t> key, 149 void LocalStorageCachedArea::KeyAdded(const std::vector<uint8_t>& key,
133 mojo::Array<uint8_t> value, 150 const std::vector<uint8_t>& value,
134 const mojo::String& source) { 151 const std::string& source) {
135 base::NullableString16 null_value; 152 base::NullableString16 null_value;
136 KeyAddedOrChanged(std::move(key), std::move(value), 153 KeyAddedOrChanged(key, value, null_value, source);
137 null_value, source);
138 } 154 }
139 155
140 void LocalStorageCachedArea::KeyChanged(mojo::Array<uint8_t> key, 156 void LocalStorageCachedArea::KeyChanged(const std::vector<uint8_t>& key,
141 mojo::Array<uint8_t> new_value, 157 const std::vector<uint8_t>& new_value,
142 mojo::Array<uint8_t> old_value, 158 const std::vector<uint8_t>& old_value,
143 const mojo::String& source) { 159 const std::string& source) {
144 base::NullableString16 old_value_str(old_value.To<base::string16>(), false); 160 base::NullableString16 old_value_str(Uint8VectorToString16(old_value), false);
145 KeyAddedOrChanged(std::move(key), std::move(new_value), 161 KeyAddedOrChanged(key, new_value, old_value_str, source);
146 old_value_str, source);
147 } 162 }
148 163
149 void LocalStorageCachedArea::KeyDeleted(mojo::Array<uint8_t> key, 164 void LocalStorageCachedArea::KeyDeleted(const std::vector<uint8_t>& key,
150 mojo::Array<uint8_t> old_value, 165 const std::vector<uint8_t>& old_value,
151 const mojo::String& source) { 166 const std::string& source) {
152 GURL page_url; 167 GURL page_url;
153 std::string storage_area_id; 168 std::string storage_area_id;
154 UnpackSource(source, &page_url, &storage_area_id); 169 UnpackSource(source, &page_url, &storage_area_id);
155 170
156 base::string16 key_string = key.To<base::string16>(); 171 base::string16 key_string = Uint8VectorToString16(key);
157 172
158 blink::WebStorageArea* originating_area = nullptr; 173 blink::WebStorageArea* originating_area = nullptr;
159 if (areas_.find(storage_area_id) != areas_.end()) { 174 if (areas_.find(storage_area_id) != areas_.end()) {
160 // The source storage area is in this process. 175 // The source storage area is in this process.
161 originating_area = areas_[storage_area_id]; 176 originating_area = areas_[storage_area_id];
162 } else if (map_ && !ignore_all_mutations_) { 177 } else if (map_ && !ignore_all_mutations_) {
163 // This was from another process or the storage area is gone. If the former, 178 // 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 179 // 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 180 // 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. 181 // because ignore_key_mutations_ won't be updated until the callback runs.
167 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) { 182 if (ignore_key_mutations_.find(key_string) != ignore_key_mutations_.end()) {
168 base::string16 unused; 183 base::string16 unused;
169 map_->RemoveItem(key_string, &unused); 184 map_->RemoveItem(key_string, &unused);
170 } 185 }
171 } 186 }
172 187
173 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( 188 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
174 key_string, old_value.To<base::string16>(), base::NullableString16(), 189 key_string, Uint8VectorToString16(old_value), base::NullableString16(),
175 GURL(origin_.Serialize()), page_url, originating_area); 190 GURL(origin_.Serialize()), page_url, originating_area);
176 } 191 }
177 192
178 void LocalStorageCachedArea::AllDeleted(const mojo::String& source) { 193 void LocalStorageCachedArea::AllDeleted(const std::string& source) {
179 GURL page_url; 194 GURL page_url;
180 std::string storage_area_id; 195 std::string storage_area_id;
181 UnpackSource(source, &page_url, &storage_area_id); 196 UnpackSource(source, &page_url, &storage_area_id);
182 197
183 blink::WebStorageArea* originating_area = nullptr; 198 blink::WebStorageArea* originating_area = nullptr;
184 if (areas_.find(storage_area_id) != areas_.end()) { 199 if (areas_.find(storage_area_id) != areas_.end()) {
185 // The source storage area is in this process. 200 // The source storage area is in this process.
186 originating_area = areas_[storage_area_id]; 201 originating_area = areas_[storage_area_id];
187 } else if (map_ && !ignore_all_mutations_) { 202 } else if (map_ && !ignore_all_mutations_) {
188 scoped_refptr<DOMStorageMap> old = map_; 203 scoped_refptr<DOMStorageMap> old = map_;
(...skipping 11 matching lines...) Expand all
200 ++iter; 215 ++iter;
201 } 216 }
202 } 217 }
203 218
204 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent( 219 blink::WebStorageEventDispatcher::dispatchLocalStorageEvent(
205 base::NullableString16(), base::NullableString16(), 220 base::NullableString16(), base::NullableString16(),
206 base::NullableString16(), GURL(origin_.Serialize()), page_url, 221 base::NullableString16(), GURL(origin_.Serialize()), page_url,
207 originating_area); 222 originating_area);
208 } 223 }
209 224
210 void LocalStorageCachedArea::GetAllComplete(const mojo::String& source) { 225 void LocalStorageCachedArea::GetAllComplete(const std::string& source) {
211 // Since the GetAll method is synchronous, we need this asynchronously 226 // Since the GetAll method is synchronous, we need this asynchronously
212 // delivered notification to avoid applying changes to the returned array 227 // delivered notification to avoid applying changes to the returned array
213 // that we already have. 228 // that we already have.
214 if (source.To<std::string>() == get_all_request_id_) { 229 if (source == get_all_request_id_) {
215 DCHECK(ignore_all_mutations_); 230 DCHECK(ignore_all_mutations_);
216 DCHECK(!get_all_request_id_.empty()); 231 DCHECK(!get_all_request_id_.empty());
217 ignore_all_mutations_ = false; 232 ignore_all_mutations_ = false;
218 get_all_request_id_.clear(); 233 get_all_request_id_.clear();
219 } 234 }
220 } 235 }
221 236
222 void LocalStorageCachedArea::KeyAddedOrChanged( 237 void LocalStorageCachedArea::KeyAddedOrChanged(
223 mojo::Array<uint8_t> key, 238 const std::vector<uint8_t>& key,
224 mojo::Array<uint8_t> new_value, 239 const std::vector<uint8_t>& new_value,
225 const base::NullableString16& old_value, 240 const base::NullableString16& old_value,
226 const mojo::String& source) { 241 const std::string& source) {
227 GURL page_url; 242 GURL page_url;
228 std::string storage_area_id; 243 std::string storage_area_id;
229 UnpackSource(source, &page_url, &storage_area_id); 244 UnpackSource(source, &page_url, &storage_area_id);
230 245
231 base::string16 key_string = key.To<base::string16>(); 246 base::string16 key_string = Uint8VectorToString16(key);
232 base::string16 new_value_string = new_value.To<base::string16>(); 247 base::string16 new_value_string = Uint8VectorToString16(new_value);
233 248
234 blink::WebStorageArea* originating_area = nullptr; 249 blink::WebStorageArea* originating_area = nullptr;
235 if (areas_.find(storage_area_id) != areas_.end()) { 250 if (areas_.find(storage_area_id) != areas_.end()) {
236 // The source storage area is in this process. 251 // The source storage area is in this process.
237 originating_area = areas_[storage_area_id]; 252 originating_area = areas_[storage_area_id];
238 } else if (map_ && !ignore_all_mutations_) { 253 } else if (map_ && !ignore_all_mutations_) {
239 // This was from another process or the storage area is gone. If the former, 254 // 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 255 // 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 256 // 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. 257 // because ignore_key_mutations_ won't be updated until the callback runs.
(...skipping 14 matching lines...) Expand all
257 } 272 }
258 273
259 void LocalStorageCachedArea::EnsureLoaded() { 274 void LocalStorageCachedArea::EnsureLoaded() {
260 if (map_) 275 if (map_)
261 return; 276 return;
262 277
263 base::TimeTicks before = base::TimeTicks::Now(); 278 base::TimeTicks before = base::TimeTicks::Now();
264 ignore_all_mutations_ = true; 279 ignore_all_mutations_ = true;
265 get_all_request_id_ = base::Uint64ToString(base::RandUint64()); 280 get_all_request_id_ = base::Uint64ToString(base::RandUint64());
266 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK; 281 leveldb::mojom::DatabaseError status = leveldb::mojom::DatabaseError::OK;
267 mojo::Array<content::mojom::KeyValuePtr> data; 282 std::vector<content::mojom::KeyValuePtr> data;
268 leveldb_->GetAll(get_all_request_id_, &status, &data); 283 leveldb_->GetAll(get_all_request_id_, &status, &data);
269 284
270 DOMStorageValuesMap values; 285 DOMStorageValuesMap values;
271 for (size_t i = 0; i < data.size(); ++i) { 286 for (size_t i = 0; i < data.size(); ++i) {
272 values[data[i]->key.To<base::string16>()] = 287 values[Uint8VectorToString16(data[i]->key)] =
273 base::NullableString16(data[i]->value.To<base::string16>(), false); 288 base::NullableString16(Uint8VectorToString16(data[i]->value), false);
274 } 289 }
275 290
276 map_ = new DOMStorageMap(kPerStorageAreaQuota); 291 map_ = new DOMStorageMap(kPerStorageAreaQuota);
277 map_->SwapValues(&values); 292 map_->SwapValues(&values);
278 293
279 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before; 294 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before;
280 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrime", time_to_prime); 295 UMA_HISTOGRAM_TIMES("LocalStorage.MojoTimeToPrime", time_to_prime);
281 296
282 size_t local_storage_size_kb = map_->bytes_used() / 1024; 297 size_t local_storage_size_kb = map_->bytes_used() / 1024;
283 // Track localStorage size, from 0-6MB. Note that the maximum size should be 298 // 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 } 342 }
328 343
329 void LocalStorageCachedArea::Reset() { 344 void LocalStorageCachedArea::Reset() {
330 map_ = NULL; 345 map_ = NULL;
331 ignore_key_mutations_.clear(); 346 ignore_key_mutations_.clear();
332 ignore_all_mutations_ = false; 347 ignore_all_mutations_ = false;
333 weak_factory_.InvalidateWeakPtrs(); 348 weak_factory_.InvalidateWeakPtrs();
334 } 349 }
335 350
336 } // namespace content 351 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/dom_storage/local_storage_cached_area.h ('k') | content/renderer/image_downloader/image_downloader_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698