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

Side by Side Diff: webkit/dom_storage/dom_storage_cached_area.cc

Issue 10383123: Switch to using the async DomStorage IPC messages and add a caching layer … (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 7 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 : namespace_id_(namespace_id), origin_(origin),
16 proxy_(proxy), weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
17 ignore_all_mutations_(false) {
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 obvsiously overbudget items to avoid
ericu 2012/05/22 23:23:35 Typo: obviously.
michaeln 2012/05/23 22:37:28 Done.
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;
ericu 2012/05/22 23:23:35 What if we call clear twice before the first OnCle
michaeln 2012/05/23 00:38:51 Reset() kills all pending callbacks by virtue of i
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& old_value,
92 const NullableString16& new_value) {
93 if (!map_ || ignore_all_mutations_)
94 return;
95
96 if (key.is_null()) {
97 // It's a clear event.
98 scoped_refptr<DomStorageMap> old = map_;
99 map_ = new DomStorageMap(dom_storage::kPerAreaQuota);
100
101 // We have to retain local additions which happened after this
102 // clear operation from another process.
103 std::map<string16, int>::iterator iter = ignore_key_mutations_.begin();
104 while (iter != ignore_key_mutations_.end()) {
105 NullableString16 value = old->GetItem(iter->first);
106 if (!value.is_null()) {
107 NullableString16 unused;
108 map_->SetItem(iter->first, value.string(), &unused);
109 }
110 ++iter;
111 }
112 return;
113 }
114
115 // We have to retain local changes.
116 if (ignore_key_mutations_.find(key.string()) != ignore_key_mutations_.end())
117 return;
118
119 if (new_value.is_null()) {
120 string16 unused;
121 map_->RemoveItem(key.string(), &unused);
122 } else {
123 // We turn off quota checking here to accomodate the over budget allowance
124 // that's provided in the browser process.
125 NullableString16 unused;
126 map_->set_quota(kint32max);
127 map_->SetItem(key.string(), new_value.string(), &unused);
128 map_->set_quota(dom_storage::kPerAreaQuota);
129 }
130 }
131
132 void DomStorageCachedArea::Prime(int connection_id) {
133 DCHECK(!map_);
134
135 // The LoadArea method is actually synchronous, but we have to
136 // wait for an asyncly delivered message to know when incoming
137 // mutation events should be applied. Our valuemap is plucked
138 // from ipc stream out of order, mutations in front if it need
139 // to be ignored.
140
141 // Ignore all mutations until OnLoadComplete time.
142 ignore_all_mutations_ = true;
143 ValuesMap values;
144 proxy_->LoadArea(
145 connection_id, &values,
146 base::Bind(&DomStorageCachedArea::OnLoadComplete,
147 weak_factory_.GetWeakPtr()));
148 map_ = new DomStorageMap(dom_storage::kPerAreaQuota);
149 map_->SwapValues(&values);
150 }
151
152 void DomStorageCachedArea::OnLoadComplete(bool success) {
153 DCHECK(success);
154 ignore_all_mutations_ = false;
ericu 2012/05/22 23:23:35 Here, too, what if we called clear before this cam
michaeln 2012/05/23 00:38:51 If we called clear before this came back... it wil
155 }
156
157 void DomStorageCachedArea::OnSetItemComplete(
158 const string16& key, bool success) {
159 if (!success) {
160 Reset();
161 return;
162 }
163 std::map<string16, int>::iterator found = ignore_key_mutations_.find(key);
164 DCHECK(found != ignore_key_mutations_.end());
165 if (--found->second == 0)
166 ignore_key_mutations_.erase(found);
167 }
168
169 void DomStorageCachedArea::OnRemoveItemComplete(
170 const string16& key, bool success) {
171 DCHECK(success);
172 std::map<string16, int>::iterator found = ignore_key_mutations_.find(key);
173 DCHECK(found != ignore_key_mutations_.end());
174 if (--found->second == 0)
175 ignore_key_mutations_.erase(found);
176 }
177
178 void DomStorageCachedArea::OnClearComplete(bool success) {
179 DCHECK(success);
180 ignore_all_mutations_ = false;
181 }
182
183 void DomStorageCachedArea::Reset() {
184 map_ = NULL;
185 weak_factory_.InvalidateWeakPtrs();
186 ignore_key_mutations_.clear();
187 ignore_all_mutations_ = false;
188 }
189
190 } // namespace dom_storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698