| 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/common/dom_storage/dom_storage_map.h" |
| 11 #include "content/renderer/dom_storage/dom_storage_proxy.h" | 11 #include "content/renderer/dom_storage/dom_storage_proxy.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 namespace { | |
| 16 | |
| 17 static const int kMaxLogGetMessagesToSend = 16 * 1024; | |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 DOMStorageCachedArea::DOMStorageCachedArea(int64 namespace_id, | 15 DOMStorageCachedArea::DOMStorageCachedArea(int64 namespace_id, |
| 22 const GURL& origin, | 16 const GURL& origin, |
| 23 DOMStorageProxy* proxy) | 17 DOMStorageProxy* proxy) |
| 24 : ignore_all_mutations_(false), | 18 : ignore_all_mutations_(false), |
| 25 namespace_id_(namespace_id), | 19 namespace_id_(namespace_id), |
| 26 origin_(origin), | 20 origin_(origin), |
| 27 proxy_(proxy), | 21 proxy_(proxy), |
| 28 remaining_log_get_messages_(0), | |
| 29 weak_factory_(this) {} | 22 weak_factory_(this) {} |
| 30 | 23 |
| 31 DOMStorageCachedArea::~DOMStorageCachedArea() {} | 24 DOMStorageCachedArea::~DOMStorageCachedArea() {} |
| 32 | 25 |
| 33 unsigned DOMStorageCachedArea::GetLength(int connection_id) { | 26 unsigned DOMStorageCachedArea::GetLength(int connection_id) { |
| 34 PrimeIfNeeded(connection_id); | 27 PrimeIfNeeded(connection_id); |
| 35 return map_->Length(); | 28 return map_->Length(); |
| 36 } | 29 } |
| 37 | 30 |
| 38 base::NullableString16 DOMStorageCachedArea::GetKey(int connection_id, | 31 base::NullableString16 DOMStorageCachedArea::GetKey(int connection_id, |
| 39 unsigned index) { | 32 unsigned index) { |
| 40 PrimeIfNeeded(connection_id); | 33 PrimeIfNeeded(connection_id); |
| 41 return map_->Key(index); | 34 return map_->Key(index); |
| 42 } | 35 } |
| 43 | 36 |
| 44 base::NullableString16 DOMStorageCachedArea::GetItem( | 37 base::NullableString16 DOMStorageCachedArea::GetItem( |
| 45 int connection_id, | 38 int connection_id, |
| 46 const base::string16& key) { | 39 const base::string16& key) { |
| 47 PrimeIfNeeded(connection_id); | 40 PrimeIfNeeded(connection_id); |
| 48 base::NullableString16 result = map_->GetItem(key); | 41 return map_->GetItem(key); |
| 49 if (remaining_log_get_messages_ > 0) { | |
| 50 remaining_log_get_messages_--; | |
| 51 proxy_->LogGetItem(connection_id, key, result); | |
| 52 } | |
| 53 return result; | |
| 54 } | 42 } |
| 55 | 43 |
| 56 bool DOMStorageCachedArea::SetItem(int connection_id, | 44 bool DOMStorageCachedArea::SetItem(int connection_id, |
| 57 const base::string16& key, | 45 const base::string16& key, |
| 58 const base::string16& value, | 46 const base::string16& value, |
| 59 const GURL& page_url) { | 47 const GURL& page_url) { |
| 60 // A quick check to reject obviously overbudget items to avoid | 48 // A quick check to reject obviously overbudget items to avoid |
| 61 // the priming the cache. | 49 // the priming the cache. |
| 62 if (key.length() + value.length() > kPerStorageAreaQuota) | 50 if (key.length() + value.length() > kPerStorageAreaQuota) |
| 63 return false; | 51 return false; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 148 |
| 161 // The LoadArea method is actually synchronous, but we have to | 149 // The LoadArea method is actually synchronous, but we have to |
| 162 // wait for an asyncly delivered message to know when incoming | 150 // wait for an asyncly delivered message to know when incoming |
| 163 // mutation events should be applied. Our valuemap is plucked | 151 // mutation events should be applied. Our valuemap is plucked |
| 164 // 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 |
| 165 // to be ignored. | 153 // to be ignored. |
| 166 | 154 |
| 167 // Ignore all mutations until OnLoadComplete time. | 155 // Ignore all mutations until OnLoadComplete time. |
| 168 ignore_all_mutations_ = true; | 156 ignore_all_mutations_ = true; |
| 169 DOMStorageValuesMap values; | 157 DOMStorageValuesMap values; |
| 170 bool send_log_get_messages = false; | |
| 171 base::TimeTicks before = base::TimeTicks::Now(); | 158 base::TimeTicks before = base::TimeTicks::Now(); |
| 172 proxy_->LoadArea(connection_id, | 159 proxy_->LoadArea(connection_id, |
| 173 &values, | 160 &values, |
| 174 &send_log_get_messages, | |
| 175 base::Bind(&DOMStorageCachedArea::OnLoadComplete, | 161 base::Bind(&DOMStorageCachedArea::OnLoadComplete, |
| 176 weak_factory_.GetWeakPtr())); | 162 weak_factory_.GetWeakPtr())); |
| 177 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before; | 163 base::TimeDelta time_to_prime = base::TimeTicks::Now() - before; |
| 178 // Keeping this histogram named the same (without the ForRenderer suffix) | 164 // Keeping this histogram named the same (without the ForRenderer suffix) |
| 179 // to maintain histogram continuity. | 165 // to maintain histogram continuity. |
| 180 UMA_HISTOGRAM_TIMES("LocalStorage.TimeToPrimeLocalStorage", | 166 UMA_HISTOGRAM_TIMES("LocalStorage.TimeToPrimeLocalStorage", |
| 181 time_to_prime); | 167 time_to_prime); |
| 182 map_ = new DOMStorageMap(kPerStorageAreaQuota); | 168 map_ = new DOMStorageMap(kPerStorageAreaQuota); |
| 183 map_->SwapValues(&values); | 169 map_->SwapValues(&values); |
| 184 if (send_log_get_messages) | |
| 185 remaining_log_get_messages_ = kMaxLogGetMessagesToSend; | |
| 186 | 170 |
| 187 size_t local_storage_size_kb = map_->bytes_used() / 1024; | 171 size_t local_storage_size_kb = map_->bytes_used() / 1024; |
| 188 // 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 |
| 189 // 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 |
| 190 // above what we see in practice, since histograms can't change. | 174 // above what we see in practice, since histograms can't change. |
| 191 UMA_HISTOGRAM_CUSTOM_COUNTS("LocalStorage.RendererLocalStorageSizeInKB", | 175 UMA_HISTOGRAM_CUSTOM_COUNTS("LocalStorage.RendererLocalStorageSizeInKB", |
| 192 local_storage_size_kb, | 176 local_storage_size_kb, |
| 193 0, 6 * 1024, 50); | 177 0, 6 * 1024, 50); |
| 194 if (local_storage_size_kb < 100) { | 178 if (local_storage_size_kb < 100) { |
| 195 UMA_HISTOGRAM_TIMES( | 179 UMA_HISTOGRAM_TIMES( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 ignore_key_mutations_.erase(found); | 226 ignore_key_mutations_.erase(found); |
| 243 } | 227 } |
| 244 | 228 |
| 245 void DOMStorageCachedArea::OnClearComplete(bool success) { | 229 void DOMStorageCachedArea::OnClearComplete(bool success) { |
| 246 DCHECK(success); | 230 DCHECK(success); |
| 247 DCHECK(ignore_all_mutations_); | 231 DCHECK(ignore_all_mutations_); |
| 248 ignore_all_mutations_ = false; | 232 ignore_all_mutations_ = false; |
| 249 } | 233 } |
| 250 | 234 |
| 251 } // namespace content | 235 } // namespace content |
| OLD | NEW |