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 "webkit/dom_storage/dom_storage_area.h" | 5 #include "webkit/dom_storage/dom_storage_area.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "base/tracked_objects.h" | 10 #include "base/tracked_objects.h" |
11 #include "webkit/dom_storage/dom_storage_map.h" | 11 #include "webkit/dom_storage/dom_storage_map.h" |
12 #include "webkit/dom_storage/dom_storage_namespace.h" | 12 #include "webkit/dom_storage/dom_storage_namespace.h" |
13 #include "webkit/dom_storage/dom_storage_task_runner.h" | 13 #include "webkit/dom_storage/dom_storage_task_runner.h" |
14 #include "webkit/dom_storage/dom_storage_types.h" | 14 #include "webkit/dom_storage/dom_storage_types.h" |
15 #include "webkit/fileapi/file_system_util.h" | 15 #include "webkit/fileapi/file_system_util.h" |
16 | 16 |
17 namespace dom_storage { | 17 namespace dom_storage { |
18 | 18 |
19 static const int kCommitTimerSeconds = 1; | |
20 | |
21 struct DomStorageArea::CommitBatch { | |
22 bool clear_all_first; | |
23 ValuesMap changed_values; | |
24 CommitBatch() : clear_all_first(false) {} | |
25 }; | |
26 | |
19 // static | 27 // static |
20 const FilePath::CharType DomStorageArea::kDatabaseFileExtension[] = | 28 const FilePath::CharType DomStorageArea::kDatabaseFileExtension[] = |
21 FILE_PATH_LITERAL(".localstorage"); | 29 FILE_PATH_LITERAL(".localstorage"); |
22 | 30 |
23 // static | 31 // static |
24 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { | 32 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { |
25 std::string filename = fileapi::GetOriginIdentifierFromURL(origin); | 33 std::string filename = fileapi::GetOriginIdentifierFromURL(origin); |
26 // There is no FilePath.AppendExtension() method, so start with just the | 34 // There is no FilePath.AppendExtension() method, so start with just the |
27 // extension as the filename, and then InsertBeforeExtension the desired | 35 // extension as the filename, and then InsertBeforeExtension the desired |
28 // name. | 36 // name. |
29 return FilePath().Append(kDatabaseFileExtension). | 37 return FilePath().Append(kDatabaseFileExtension). |
30 InsertBeforeExtensionASCII(filename); | 38 InsertBeforeExtensionASCII(filename); |
31 } | 39 } |
32 | 40 |
33 DomStorageArea::DomStorageArea( | 41 DomStorageArea::DomStorageArea( |
34 int64 namespace_id, const GURL& origin, | 42 int64 namespace_id, const GURL& origin, |
35 const FilePath& directory, DomStorageTaskRunner* task_runner) | 43 const FilePath& directory, DomStorageTaskRunner* task_runner) |
36 : namespace_id_(namespace_id), origin_(origin), | 44 : namespace_id_(namespace_id), origin_(origin), |
37 directory_(directory), | 45 directory_(directory), |
38 task_runner_(task_runner), | 46 task_runner_(task_runner), |
39 map_(new DomStorageMap(kPerAreaQuota)), | 47 map_(new DomStorageMap(kPerAreaQuota)), |
40 backing_(NULL), | 48 is_initial_import_done_(true), |
41 initial_import_done_(false), | 49 is_shutdown_(false) { |
42 clear_all_next_commit_(false), | |
43 commit_in_flight_(false) { | |
44 | |
45 if (namespace_id == kLocalStorageNamespaceId && !directory.empty()) { | 50 if (namespace_id == kLocalStorageNamespaceId && !directory.empty()) { |
46 FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_)); | 51 FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_)); |
47 backing_.reset(new DomStorageDatabase(path)); | 52 backing_.reset(new DomStorageDatabase(path)); |
48 } else { | 53 is_initial_import_done_ = false; |
49 // Not a local storage area or no directory specified for backing | |
50 // database, (i.e. it's an incognito profile). | |
51 initial_import_done_ = true; | |
52 } | 54 } |
53 } | 55 } |
54 | 56 |
55 DomStorageArea::~DomStorageArea() { | 57 DomStorageArea::~DomStorageArea() { |
56 if (clear_all_next_commit_ || !changed_values_.empty()) { | |
57 // Still some data left that was not committed to disk, try now. | |
58 // We do this regardless of whether we think a commit is in flight | |
59 // as there is no guarantee that that commit will actually get | |
60 // processed. For example the task_runner_'s message loop could | |
61 // unexpectedly quit before the delayed task is fired and leave the | |
62 // commit_in_flight_ flag set. But there's no way for us to determine | |
63 // that has happened so force a commit now. | |
64 | |
65 CommitChanges(); | |
66 | |
67 // TODO(benm): It's possible that the commit failed, and in | |
68 // that case we're going to lose data. Integrate with UMA | |
69 // to gather stats about how often this actually happens, | |
70 // so that we can figure out a contingency plan. | |
71 } | |
72 } | 58 } |
73 | 59 |
74 unsigned DomStorageArea::Length() { | 60 unsigned DomStorageArea::Length() { |
61 if (is_shutdown_) | |
62 return 0; | |
75 InitialImportIfNeeded(); | 63 InitialImportIfNeeded(); |
76 return map_->Length(); | 64 return map_->Length(); |
77 } | 65 } |
78 | 66 |
79 NullableString16 DomStorageArea::Key(unsigned index) { | 67 NullableString16 DomStorageArea::Key(unsigned index) { |
68 if (is_shutdown_) | |
69 return NullableString16(true); | |
80 InitialImportIfNeeded(); | 70 InitialImportIfNeeded(); |
81 return map_->Key(index); | 71 return map_->Key(index); |
82 } | 72 } |
83 | 73 |
84 NullableString16 DomStorageArea::GetItem(const string16& key) { | 74 NullableString16 DomStorageArea::GetItem(const string16& key) { |
75 if (is_shutdown_) | |
76 return NullableString16(true); | |
85 InitialImportIfNeeded(); | 77 InitialImportIfNeeded(); |
86 return map_->GetItem(key); | 78 return map_->GetItem(key); |
87 } | 79 } |
88 | 80 |
89 bool DomStorageArea::SetItem(const string16& key, | 81 bool DomStorageArea::SetItem(const string16& key, |
90 const string16& value, | 82 const string16& value, |
91 NullableString16* old_value) { | 83 NullableString16* old_value) { |
84 if (is_shutdown_) | |
85 return false; | |
92 InitialImportIfNeeded(); | 86 InitialImportIfNeeded(); |
93 | |
94 if (!map_->HasOneRef()) | 87 if (!map_->HasOneRef()) |
95 map_ = map_->DeepCopy(); | 88 map_ = map_->DeepCopy(); |
96 bool success = map_->SetItem(key, value, old_value); | 89 bool success = map_->SetItem(key, value, old_value); |
97 if (success && backing_.get()) { | 90 if (success && backing_.get()) { |
98 changed_values_[key] = NullableString16(value, false); | 91 CommitBatch* commit_batch = CreateCommitBatchIfNeeded(); |
99 ScheduleCommitChanges(); | 92 commit_batch->changed_values[key] = NullableString16(value, false); |
100 } | 93 } |
101 return success; | 94 return success; |
102 } | 95 } |
103 | 96 |
104 bool DomStorageArea::RemoveItem(const string16& key, string16* old_value) { | 97 bool DomStorageArea::RemoveItem(const string16& key, string16* old_value) { |
98 if (is_shutdown_) | |
99 return false; | |
105 InitialImportIfNeeded(); | 100 InitialImportIfNeeded(); |
106 if (!map_->HasOneRef()) | 101 if (!map_->HasOneRef()) |
107 map_ = map_->DeepCopy(); | 102 map_ = map_->DeepCopy(); |
108 bool success = map_->RemoveItem(key, old_value); | 103 bool success = map_->RemoveItem(key, old_value); |
109 if (success && backing_.get()) { | 104 if (success && backing_.get()) { |
110 changed_values_[key] = NullableString16(true); | 105 CommitBatch* commit_batch = CreateCommitBatchIfNeeded(); |
111 ScheduleCommitChanges(); | 106 commit_batch->changed_values[key] = NullableString16(true); |
112 } | 107 } |
113 return success; | 108 return success; |
114 } | 109 } |
115 | 110 |
116 bool DomStorageArea::Clear() { | 111 bool DomStorageArea::Clear() { |
112 if (is_shutdown_) | |
113 return false; | |
117 InitialImportIfNeeded(); | 114 InitialImportIfNeeded(); |
118 if (map_->Length() == 0) | 115 if (map_->Length() == 0) |
119 return false; | 116 return false; |
120 | 117 |
121 map_ = new DomStorageMap(kPerAreaQuota); | 118 map_ = new DomStorageMap(kPerAreaQuota); |
122 | 119 |
123 if (backing_.get()) { | 120 if (backing_.get()) { |
124 changed_values_.clear(); | 121 CommitBatch* commit_batch = CreateCommitBatchIfNeeded(); |
125 clear_all_next_commit_ = true; | 122 commit_batch->clear_all_first = true; |
126 ScheduleCommitChanges(); | 123 commit_batch->changed_values.clear(); |
127 } | 124 } |
128 | 125 |
129 return true; | 126 return true; |
130 } | 127 } |
131 | 128 |
132 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { | 129 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { |
130 DCHECK(!is_shutdown_); | |
133 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 131 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
134 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 132 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
135 // SessionNamespaces aren't backed by files on disk. | 133 // SessionNamespaces aren't backed by files on disk. |
136 DCHECK(!backing_.get()); | 134 DCHECK(!backing_.get()); |
137 | 135 |
138 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, | 136 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, |
139 FilePath(), task_runner_); | 137 FilePath(), task_runner_); |
140 copy->map_ = map_; | 138 copy->map_ = map_; |
141 return copy; | 139 return copy; |
142 } | 140 } |
143 | 141 |
142 void DomStorageArea::Shutdown() { | |
143 DCHECK(!is_shutdown_); | |
144 is_shutdown_ = true; | |
145 map_ = NULL; | |
146 if (!backing_.get()) | |
147 return; | |
148 | |
149 bool success = task_runner_->PostShutdownBlockingTask( | |
150 FROM_HERE, | |
151 DomStorageTaskRunner::COMMIT_SEQUENCE, | |
152 base::Bind(&DomStorageArea::ShutdownInCommitSequence, this)); | |
153 DCHECK(success); | |
154 } | |
155 | |
144 void DomStorageArea::InitialImportIfNeeded() { | 156 void DomStorageArea::InitialImportIfNeeded() { |
145 if (initial_import_done_) | 157 if (is_initial_import_done_) |
146 return; | 158 return; |
147 | 159 |
148 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | 160 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
149 DCHECK(backing_.get()); | 161 DCHECK(backing_.get()); |
150 | 162 |
151 ValuesMap initial_values; | 163 ValuesMap initial_values; |
152 backing_->ReadAllValues(&initial_values); | 164 backing_->ReadAllValues(&initial_values); |
153 map_->SwapValues(&initial_values); | 165 map_->SwapValues(&initial_values); |
154 initial_import_done_ = true; | 166 is_initial_import_done_ = true; |
155 } | 167 } |
156 | 168 |
157 void DomStorageArea::ScheduleCommitChanges() { | 169 DomStorageArea::CommitBatch* DomStorageArea::CreateCommitBatchIfNeeded() { |
170 DCHECK(!is_shutdown_); | |
171 if (!commit_batch_.get()) { | |
172 commit_batch_.reset(new CommitBatch()); | |
173 | |
174 // Start a timer to commit any changes that accrue in the batch, | |
175 // but only if a commit is not currently in flight. In that case | |
176 // the timer will be started after the current commit has happened. | |
177 if (!in_flight_commit_batch_.get()) { | |
178 task_runner_->PostDelayedTask( | |
179 FROM_HERE, | |
180 base::Bind(&DomStorageArea::OnCommitTimer, this), | |
181 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | |
182 } | |
183 } | |
184 return commit_batch_.get(); | |
185 } | |
186 | |
187 void DomStorageArea::OnCommitTimer() { | |
158 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | 188 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
159 DCHECK(backing_.get()); | 189 DCHECK(backing_.get()); |
160 DCHECK(clear_all_next_commit_ || !changed_values_.empty()); | 190 DCHECK(commit_batch_.get()); |
161 DCHECK(task_runner_.get()); | 191 DCHECK(!in_flight_commit_batch_.get()); |
162 | 192 if (is_shutdown_) |
163 if (commit_in_flight_) | |
164 return; | 193 return; |
165 | 194 |
166 commit_in_flight_ = task_runner_->PostDelayedTask( | 195 // This method executes on the 'read' sequence, we schedule |
benm (inactive)
2012/03/20 13:39:16
s/read/primary
| |
167 FROM_HERE, base::Bind(&DomStorageArea::CommitChanges, this), | 196 // a task for immediate execution on the 'write' sequence. |
benm (inactive)
2012/03/20 13:39:16
s/write/commit
| |
168 base::TimeDelta::FromSeconds(1)); | 197 in_flight_commit_batch_ = commit_batch_.Pass(); |
169 DCHECK(commit_in_flight_); | 198 bool success = task_runner_->PostShutdownBlockingTask( |
199 FROM_HERE, | |
200 DomStorageTaskRunner::COMMIT_SEQUENCE, | |
201 base::Bind(&DomStorageArea::CommitChanges, this)); | |
202 DCHECK(success); | |
170 } | 203 } |
171 | 204 |
172 void DomStorageArea::CommitChanges() { | 205 void DomStorageArea::CommitChanges() { |
206 // This method executes on the 'commit' sequence. | |
207 DCHECK(in_flight_commit_batch_.get()); | |
208 bool success = backing_->CommitChanges( | |
209 in_flight_commit_batch_->clear_all_first, | |
210 in_flight_commit_batch_->changed_values); | |
211 DCHECK(success); // TODO(michaeln): what if it fails? | |
212 task_runner_->PostTask( | |
213 FROM_HERE, | |
214 base::Bind(&DomStorageArea::OnCommitComplete, this)); | |
215 } | |
216 | |
217 void DomStorageArea::OnCommitComplete() { | |
benm (inactive)
2012/03/20 13:39:16
mention we're back on the primary sequence?
| |
218 if (is_shutdown_) | |
219 return; | |
220 in_flight_commit_batch_.reset(); | |
221 if (commit_batch_.get()) { | |
222 // More changes have accrued, restart the timer. | |
223 task_runner_->PostDelayedTask( | |
224 FROM_HERE, | |
225 base::Bind(&DomStorageArea::OnCommitTimer, this), | |
226 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | |
227 } | |
228 } | |
229 | |
230 void DomStorageArea::ShutdownInCommitSequence() { | |
231 // This method executes on the 'commit' sequence. | |
173 DCHECK(backing_.get()); | 232 DCHECK(backing_.get()); |
174 if (backing_->CommitChanges(clear_all_next_commit_, changed_values_)) { | 233 if (commit_batch_.get()) { |
175 clear_all_next_commit_ = false; | 234 // Commit any changes that accrued prior to the timer firing. |
176 changed_values_.clear(); | 235 bool success = backing_->CommitChanges( |
236 commit_batch_->clear_all_first, | |
237 commit_batch_->changed_values); | |
238 DCHECK(success); | |
177 } | 239 } |
178 commit_in_flight_ = false; | 240 commit_batch_.reset(); |
241 in_flight_commit_batch_.reset(); | |
242 backing_.reset(); | |
179 } | 243 } |
180 | 244 |
181 } // namespace dom_storage | 245 } // namespace dom_storage |
OLD | NEW |