OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/dom_storage/dom_storage_cached_area.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "webkit/dom_storage/dom_storage_map.h" |
| 9 #include "webkit/dom_storage/dom_storage_proxy.h" |
| 10 |
| 11 namespace dom_storage { |
| 12 |
| 13 DomStorageCachedArea::DomStorageCachedArea( |
| 14 int64 namespace_id, const GURL& origin, DomStorageProxy* proxy) |
| 15 : ignore_all_mutations_(false), |
| 16 namespace_id_(namespace_id), origin_(origin), |
| 17 proxy_(proxy), weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 18 } |
| 19 |
| 20 DomStorageCachedArea::~DomStorageCachedArea() { |
| 21 } |
| 22 |
| 23 unsigned DomStorageCachedArea::GetLength(int connection_id) { |
| 24 PrimeIfNeeded(connection_id); |
| 25 return map_->Length(); |
| 26 } |
| 27 |
| 28 NullableString16 DomStorageCachedArea::GetKey( |
| 29 int connection_id, unsigned index) { |
| 30 PrimeIfNeeded(connection_id); |
| 31 return map_->Key(index); |
| 32 } |
| 33 |
| 34 NullableString16 DomStorageCachedArea::GetItem( |
| 35 int connection_id, const string16& key) { |
| 36 PrimeIfNeeded(connection_id); |
| 37 return map_->GetItem(key); |
| 38 } |
| 39 |
| 40 bool DomStorageCachedArea::SetItem( |
| 41 int connection_id, const string16& key, |
| 42 const string16& value, const GURL& page_url) { |
| 43 // A quick check to reject obviously overbudget items to avoid |
| 44 // the priming the cache. |
| 45 if (key.length() + value.length() > dom_storage::kPerAreaQuota) |
| 46 return false; |
| 47 |
| 48 PrimeIfNeeded(connection_id); |
| 49 NullableString16 unused; |
| 50 if (!map_->SetItem(key, value, &unused)) |
| 51 return false; |
| 52 |
| 53 // Ignore mutations to 'key' until OnSetItemComplete. |
| 54 ignore_key_mutations_[key]++; |
| 55 proxy_->SetItem( |
| 56 connection_id, key, value, page_url, |
| 57 base::Bind(&DomStorageCachedArea::OnSetItemComplete, |
| 58 weak_factory_.GetWeakPtr(), key)); |
| 59 return true; |
| 60 } |
| 61 |
| 62 void DomStorageCachedArea::RemoveItem( |
| 63 int connection_id, const string16& key, const GURL& page_url) { |
| 64 PrimeIfNeeded(connection_id); |
| 65 string16 unused; |
| 66 if (!map_->RemoveItem(key, &unused)) |
| 67 return; |
| 68 |
| 69 // Ignore mutations to 'key' until OnRemoveItemComplete. |
| 70 ignore_key_mutations_[key]++; |
| 71 proxy_->RemoveItem( |
| 72 connection_id, key, page_url, |
| 73 base::Bind(&DomStorageCachedArea::OnRemoveItemComplete, |
| 74 weak_factory_.GetWeakPtr(), key)); |
| 75 } |
| 76 |
| 77 void DomStorageCachedArea::Clear(int connection_id, const GURL& page_url) { |
| 78 // No need to prime the cache in this case. |
| 79 Reset(); |
| 80 map_ = new DomStorageMap(dom_storage::kPerAreaQuota); |
| 81 |
| 82 // Ignore all mutations until OnClearComplete time. |
| 83 ignore_all_mutations_ = true; |
| 84 proxy_->ClearArea( |
| 85 connection_id, page_url, |
| 86 base::Bind(&DomStorageCachedArea::OnClearComplete, |
| 87 weak_factory_.GetWeakPtr())); |
| 88 } |
| 89 |
| 90 void DomStorageCachedArea::ApplyMutation( |
| 91 const NullableString16& key, const NullableString16& new_value) { |
| 92 if (!map_ || ignore_all_mutations_) |
| 93 return; |
| 94 |
| 95 if (key.is_null()) { |
| 96 // It's a clear event. |
| 97 scoped_refptr<DomStorageMap> old = map_; |
| 98 map_ = new DomStorageMap(dom_storage::kPerAreaQuota); |
| 99 |
| 100 // We have to retain local additions which happened after this |
| 101 // clear operation from another process. |
| 102 std::map<string16, int>::iterator iter = ignore_key_mutations_.begin(); |
| 103 while (iter != ignore_key_mutations_.end()) { |
| 104 NullableString16 value = old->GetItem(iter->first); |
| 105 if (!value.is_null()) { |
| 106 NullableString16 unused; |
| 107 map_->SetItem(iter->first, value.string(), &unused); |
| 108 } |
| 109 ++iter; |
| 110 } |
| 111 return; |
| 112 } |
| 113 |
| 114 // We have to retain local changes. |
| 115 if (should_ignore_key_mutation(key.string())) |
| 116 return; |
| 117 |
| 118 if (new_value.is_null()) { |
| 119 // It's a remove item event. |
| 120 string16 unused; |
| 121 map_->RemoveItem(key.string(), &unused); |
| 122 return; |
| 123 } |
| 124 |
| 125 // It's a set item event. |
| 126 // We turn off quota checking here to accomodate the over budget |
| 127 // allowance that's provided in the browser process. |
| 128 NullableString16 unused; |
| 129 map_->set_quota(kint32max); |
| 130 map_->SetItem(key.string(), new_value.string(), &unused); |
| 131 map_->set_quota(dom_storage::kPerAreaQuota); |
| 132 } |
| 133 |
| 134 void DomStorageCachedArea::Prime(int connection_id) { |
| 135 DCHECK(!map_); |
| 136 |
| 137 // The LoadArea method is actually synchronous, but we have to |
| 138 // wait for an asyncly delivered message to know when incoming |
| 139 // mutation events should be applied. Our valuemap is plucked |
| 140 // from ipc stream out of order, mutations in front if it need |
| 141 // to be ignored. |
| 142 |
| 143 // Ignore all mutations until OnLoadComplete time. |
| 144 ignore_all_mutations_ = true; |
| 145 ValuesMap values; |
| 146 proxy_->LoadArea( |
| 147 connection_id, &values, |
| 148 base::Bind(&DomStorageCachedArea::OnLoadComplete, |
| 149 weak_factory_.GetWeakPtr())); |
| 150 map_ = new DomStorageMap(dom_storage::kPerAreaQuota); |
| 151 map_->SwapValues(&values); |
| 152 } |
| 153 |
| 154 void DomStorageCachedArea::Reset() { |
| 155 map_ = NULL; |
| 156 weak_factory_.InvalidateWeakPtrs(); |
| 157 ignore_key_mutations_.clear(); |
| 158 ignore_all_mutations_ = false; |
| 159 } |
| 160 |
| 161 void DomStorageCachedArea::OnLoadComplete(bool success) { |
| 162 DCHECK(success); |
| 163 DCHECK(ignore_all_mutations_); |
| 164 ignore_all_mutations_ = false; |
| 165 } |
| 166 |
| 167 void DomStorageCachedArea::OnSetItemComplete( |
| 168 const string16& key, bool success) { |
| 169 if (!success) { |
| 170 Reset(); |
| 171 return; |
| 172 } |
| 173 std::map<string16, int>::iterator found = ignore_key_mutations_.find(key); |
| 174 DCHECK(found != ignore_key_mutations_.end()); |
| 175 if (--found->second == 0) |
| 176 ignore_key_mutations_.erase(found); |
| 177 } |
| 178 |
| 179 void DomStorageCachedArea::OnRemoveItemComplete( |
| 180 const string16& key, bool success) { |
| 181 DCHECK(success); |
| 182 std::map<string16, int>::iterator found = ignore_key_mutations_.find(key); |
| 183 DCHECK(found != ignore_key_mutations_.end()); |
| 184 if (--found->second == 0) |
| 185 ignore_key_mutations_.erase(found); |
| 186 } |
| 187 |
| 188 void DomStorageCachedArea::OnClearComplete(bool success) { |
| 189 DCHECK(success); |
| 190 DCHECK(ignore_all_mutations_); |
| 191 ignore_all_mutations_ = false; |
| 192 } |
| 193 |
| 194 } // namespace dom_storage |
OLD | NEW |