OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/dom_storage_cached_area.h" | 5 #include "content/renderer/dom_storage/dom_storage_cached_area.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "content/common/dom_storage/dom_storage_map.h" |
10 #include "content/renderer/dom_storage/dom_storage_proxy.h" | 11 #include "content/renderer/dom_storage/dom_storage_proxy.h" |
11 #include "webkit/common/dom_storage/dom_storage_map.h" | |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 DomStorageCachedArea::DomStorageCachedArea(int64 namespace_id, | 15 DomStorageCachedArea::DomStorageCachedArea(int64 namespace_id, |
16 const GURL& origin, | 16 const GURL& origin, |
17 DomStorageProxy* proxy) | 17 DomStorageProxy* proxy) |
18 : ignore_all_mutations_(false), | 18 : ignore_all_mutations_(false), |
19 namespace_id_(namespace_id), | 19 namespace_id_(namespace_id), |
20 origin_(origin), | 20 origin_(origin), |
21 proxy_(proxy), | 21 proxy_(proxy), |
(...skipping 18 matching lines...) Expand all Loading... |
40 PrimeIfNeeded(connection_id); | 40 PrimeIfNeeded(connection_id); |
41 return map_->GetItem(key); | 41 return map_->GetItem(key); |
42 } | 42 } |
43 | 43 |
44 bool DomStorageCachedArea::SetItem(int connection_id, | 44 bool DomStorageCachedArea::SetItem(int connection_id, |
45 const base::string16& key, | 45 const base::string16& key, |
46 const base::string16& value, | 46 const base::string16& value, |
47 const GURL& page_url) { | 47 const GURL& page_url) { |
48 // A quick check to reject obviously overbudget items to avoid | 48 // A quick check to reject obviously overbudget items to avoid |
49 // the priming the cache. | 49 // the priming the cache. |
50 if (key.length() + value.length() > dom_storage::kPerAreaQuota) | 50 if (key.length() + value.length() > kPerStorageAreaQuota) |
51 return false; | 51 return false; |
52 | 52 |
53 PrimeIfNeeded(connection_id); | 53 PrimeIfNeeded(connection_id); |
54 base::NullableString16 unused; | 54 base::NullableString16 unused; |
55 if (!map_->SetItem(key, value, &unused)) | 55 if (!map_->SetItem(key, value, &unused)) |
56 return false; | 56 return false; |
57 | 57 |
58 // Ignore mutations to 'key' until OnSetItemComplete. | 58 // Ignore mutations to 'key' until OnSetItemComplete. |
59 ignore_key_mutations_[key]++; | 59 ignore_key_mutations_[key]++; |
60 proxy_->SetItem( | 60 proxy_->SetItem( |
(...skipping 15 matching lines...) Expand all Loading... |
76 ignore_key_mutations_[key]++; | 76 ignore_key_mutations_[key]++; |
77 proxy_->RemoveItem( | 77 proxy_->RemoveItem( |
78 connection_id, key, page_url, | 78 connection_id, key, page_url, |
79 base::Bind(&DomStorageCachedArea::OnRemoveItemComplete, | 79 base::Bind(&DomStorageCachedArea::OnRemoveItemComplete, |
80 weak_factory_.GetWeakPtr(), key)); | 80 weak_factory_.GetWeakPtr(), key)); |
81 } | 81 } |
82 | 82 |
83 void DomStorageCachedArea::Clear(int connection_id, const GURL& page_url) { | 83 void DomStorageCachedArea::Clear(int connection_id, const GURL& page_url) { |
84 // No need to prime the cache in this case. | 84 // No need to prime the cache in this case. |
85 Reset(); | 85 Reset(); |
86 map_ = new dom_storage::DomStorageMap(dom_storage::kPerAreaQuota); | 86 map_ = new DOMStorageMap(kPerStorageAreaQuota); |
87 | 87 |
88 // Ignore all mutations until OnClearComplete time. | 88 // Ignore all mutations until OnClearComplete time. |
89 ignore_all_mutations_ = true; | 89 ignore_all_mutations_ = true; |
90 proxy_->ClearArea(connection_id, | 90 proxy_->ClearArea(connection_id, |
91 page_url, | 91 page_url, |
92 base::Bind(&DomStorageCachedArea::OnClearComplete, | 92 base::Bind(&DomStorageCachedArea::OnClearComplete, |
93 weak_factory_.GetWeakPtr())); | 93 weak_factory_.GetWeakPtr())); |
94 } | 94 } |
95 | 95 |
96 void DomStorageCachedArea::ApplyMutation( | 96 void DomStorageCachedArea::ApplyMutation( |
97 const base::NullableString16& key, | 97 const base::NullableString16& key, |
98 const base::NullableString16& new_value) { | 98 const base::NullableString16& new_value) { |
99 if (!map_.get() || ignore_all_mutations_) | 99 if (!map_.get() || ignore_all_mutations_) |
100 return; | 100 return; |
101 | 101 |
102 if (key.is_null()) { | 102 if (key.is_null()) { |
103 // It's a clear event. | 103 // It's a clear event. |
104 scoped_refptr<dom_storage::DomStorageMap> old = map_; | 104 scoped_refptr<DOMStorageMap> old = map_; |
105 map_ = new dom_storage::DomStorageMap(dom_storage::kPerAreaQuota); | 105 map_ = new DOMStorageMap(kPerStorageAreaQuota); |
106 | 106 |
107 // We have to retain local additions which happened after this | 107 // We have to retain local additions which happened after this |
108 // clear operation from another process. | 108 // clear operation from another process. |
109 std::map<base::string16, int>::iterator iter = | 109 std::map<base::string16, int>::iterator iter = |
110 ignore_key_mutations_.begin(); | 110 ignore_key_mutations_.begin(); |
111 while (iter != ignore_key_mutations_.end()) { | 111 while (iter != ignore_key_mutations_.end()) { |
112 base::NullableString16 value = old->GetItem(iter->first); | 112 base::NullableString16 value = old->GetItem(iter->first); |
113 if (!value.is_null()) { | 113 if (!value.is_null()) { |
114 base::NullableString16 unused; | 114 base::NullableString16 unused; |
115 map_->SetItem(iter->first, value.string(), &unused); | 115 map_->SetItem(iter->first, value.string(), &unused); |
(...skipping 13 matching lines...) Expand all Loading... |
129 map_->RemoveItem(key.string(), &unused); | 129 map_->RemoveItem(key.string(), &unused); |
130 return; | 130 return; |
131 } | 131 } |
132 | 132 |
133 // It's a set item event. | 133 // It's a set item event. |
134 // We turn off quota checking here to accomodate the over budget | 134 // We turn off quota checking here to accomodate the over budget |
135 // allowance that's provided in the browser process. | 135 // allowance that's provided in the browser process. |
136 base::NullableString16 unused; | 136 base::NullableString16 unused; |
137 map_->set_quota(kint32max); | 137 map_->set_quota(kint32max); |
138 map_->SetItem(key.string(), new_value.string(), &unused); | 138 map_->SetItem(key.string(), new_value.string(), &unused); |
139 map_->set_quota(dom_storage::kPerAreaQuota); | 139 map_->set_quota(kPerStorageAreaQuota); |
140 } | 140 } |
141 | 141 |
142 size_t DomStorageCachedArea::MemoryBytesUsedByCache() const { | 142 size_t DomStorageCachedArea::MemoryBytesUsedByCache() const { |
143 return map_.get() ? map_->bytes_used() : 0; | 143 return map_.get() ? map_->bytes_used() : 0; |
144 } | 144 } |
145 | 145 |
146 void DomStorageCachedArea::Prime(int connection_id) { | 146 void DomStorageCachedArea::Prime(int connection_id) { |
147 DCHECK(!map_.get()); | 147 DCHECK(!map_.get()); |
148 | 148 |
149 // The LoadArea method is actually synchronous, but we have to | 149 // The LoadArea method is actually synchronous, but we have to |
150 // wait for an asyncly delivered message to know when incoming | 150 // wait for an asyncly delivered message to know when incoming |
151 // mutation events should be applied. Our valuemap is plucked | 151 // mutation events should be applied. Our valuemap is plucked |
152 // from ipc stream out of order, mutations in front if it need | 152 // from ipc stream out of order, mutations in front if it need |
153 // to be ignored. | 153 // to be ignored. |
154 | 154 |
155 // Ignore all mutations until OnLoadComplete time. | 155 // Ignore all mutations until OnLoadComplete time. |
156 ignore_all_mutations_ = true; | 156 ignore_all_mutations_ = true; |
157 dom_storage::ValuesMap values; | 157 DOMStorageValuesMap values; |
158 base::TimeTicks before = base::TimeTicks::Now(); | 158 base::TimeTicks before = base::TimeTicks::Now(); |
159 proxy_->LoadArea(connection_id, | 159 proxy_->LoadArea(connection_id, |
160 &values, | 160 &values, |
161 base::Bind(&DomStorageCachedArea::OnLoadComplete, | 161 base::Bind(&DomStorageCachedArea::OnLoadComplete, |
162 weak_factory_.GetWeakPtr())); | 162 weak_factory_.GetWeakPtr())); |
163 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before; | 163 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before; |
164 // Keeping this histogram named the same (without the ForRenderer suffix) | 164 // Keeping this histogram named the same (without the ForRenderer suffix) |
165 // to maintain histogram continuity. | 165 // to maintain histogram continuity. |
166 UMA_HISTOGRAM_TIMES("LocalStorage.TimeToPrimeLocalStorage", | 166 UMA_HISTOGRAM_TIMES("LocalStorage.TimeToPrimeLocalStorage", |
167 time_to_prime); | 167 time_to_prime); |
168 map_ = new dom_storage::DomStorageMap(dom_storage::kPerAreaQuota); | 168 map_ = new DOMStorageMap(kPerStorageAreaQuota); |
169 map_->SwapValues(&values); | 169 map_->SwapValues(&values); |
170 | 170 |
171 size_t local_storage_size_kb = map_->bytes_used() / 1024; | 171 size_t local_storage_size_kb = map_->bytes_used() / 1024; |
172 // Track localStorage size, from 0-6MB. Note that the maximum size should be | 172 // Track localStorage size, from 0-6MB. Note that the maximum size should be |
173 // 5MB, but we add some slop since we want to make sure the max size is always | 173 // 5MB, but we add some slop since we want to make sure the max size is always |
174 // above what we see in practice, since histograms can't change. | 174 // above what we see in practice, since histograms can't change. |
175 UMA_HISTOGRAM_CUSTOM_COUNTS("LocalStorage.RendererLocalStorageSizeInKB", | 175 UMA_HISTOGRAM_CUSTOM_COUNTS("LocalStorage.RendererLocalStorageSizeInKB", |
176 local_storage_size_kb, | 176 local_storage_size_kb, |
177 0, 6 * 1024, 50); | 177 0, 6 * 1024, 50); |
178 if (local_storage_size_kb < 100) { | 178 if (local_storage_size_kb < 100) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 ignore_key_mutations_.erase(found); | 226 ignore_key_mutations_.erase(found); |
227 } | 227 } |
228 | 228 |
229 void DomStorageCachedArea::OnClearComplete(bool success) { | 229 void DomStorageCachedArea::OnClearComplete(bool success) { |
230 DCHECK(success); | 230 DCHECK(success); |
231 DCHECK(ignore_all_mutations_); | 231 DCHECK(ignore_all_mutations_); |
232 ignore_all_mutations_ = false; | 232 ignore_all_mutations_ = false; |
233 } | 233 } |
234 | 234 |
235 } // namespace content | 235 } // namespace content |
OLD | NEW |