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 DomStorageArea::CommitBatch::CommitBatch() |
| 22 : clear_all_first(false) { |
| 23 } |
| 24 DomStorageArea::CommitBatch::~CommitBatch() {} |
| 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) { |
133 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 130 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
134 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 131 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
135 // SessionNamespaces aren't backed by files on disk. | 132 DCHECK(!backing_.get()); // SessionNamespaces aren't stored on disk. |
136 DCHECK(!backing_.get()); | |
137 | 133 |
138 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, | 134 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, |
139 FilePath(), task_runner_); | 135 FilePath(), task_runner_); |
140 copy->map_ = map_; | 136 copy->map_ = map_; |
| 137 copy->is_shutdown_ = is_shutdown_; |
141 return copy; | 138 return copy; |
142 } | 139 } |
143 | 140 |
| 141 void DomStorageArea::Shutdown() { |
| 142 DCHECK(!is_shutdown_); |
| 143 is_shutdown_ = true; |
| 144 map_ = NULL; |
| 145 if (!backing_.get()) |
| 146 return; |
| 147 |
| 148 bool success = task_runner_->PostShutdownBlockingTask( |
| 149 FROM_HERE, |
| 150 DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 151 base::Bind(&DomStorageArea::ShutdownInCommitSequence, this)); |
| 152 DCHECK(success); |
| 153 } |
| 154 |
144 void DomStorageArea::InitialImportIfNeeded() { | 155 void DomStorageArea::InitialImportIfNeeded() { |
145 if (initial_import_done_) | 156 if (is_initial_import_done_) |
146 return; | 157 return; |
147 | 158 |
148 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | 159 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
149 DCHECK(backing_.get()); | 160 DCHECK(backing_.get()); |
150 | 161 |
151 ValuesMap initial_values; | 162 ValuesMap initial_values; |
152 backing_->ReadAllValues(&initial_values); | 163 backing_->ReadAllValues(&initial_values); |
153 map_->SwapValues(&initial_values); | 164 map_->SwapValues(&initial_values); |
154 initial_import_done_ = true; | 165 is_initial_import_done_ = true; |
155 } | 166 } |
156 | 167 |
157 void DomStorageArea::ScheduleCommitChanges() { | 168 DomStorageArea::CommitBatch* DomStorageArea::CreateCommitBatchIfNeeded() { |
| 169 DCHECK(!is_shutdown_); |
| 170 if (!commit_batch_.get()) { |
| 171 commit_batch_.reset(new CommitBatch()); |
| 172 |
| 173 // Start a timer to commit any changes that accrue in the batch, |
| 174 // but only if a commit is not currently in flight. In that case |
| 175 // the timer will be started after the current commit has happened. |
| 176 if (!in_flight_commit_batch_.get()) { |
| 177 task_runner_->PostDelayedTask( |
| 178 FROM_HERE, |
| 179 base::Bind(&DomStorageArea::OnCommitTimer, this), |
| 180 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); |
| 181 } |
| 182 } |
| 183 return commit_batch_.get(); |
| 184 } |
| 185 |
| 186 void DomStorageArea::OnCommitTimer() { |
158 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | 187 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
159 DCHECK(backing_.get()); | 188 if (is_shutdown_) |
160 DCHECK(clear_all_next_commit_ || !changed_values_.empty()); | |
161 DCHECK(task_runner_.get()); | |
162 | |
163 if (commit_in_flight_) | |
164 return; | 189 return; |
165 | 190 |
166 commit_in_flight_ = task_runner_->PostDelayedTask( | 191 DCHECK(backing_.get()); |
167 FROM_HERE, base::Bind(&DomStorageArea::CommitChanges, this), | 192 DCHECK(commit_batch_.get()); |
168 base::TimeDelta::FromSeconds(1)); | 193 DCHECK(!in_flight_commit_batch_.get()); |
169 DCHECK(commit_in_flight_); | 194 |
| 195 // This method executes on the primary sequence, we schedule |
| 196 // a task for immediate execution on the commit sequence. |
| 197 in_flight_commit_batch_ = commit_batch_.Pass(); |
| 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() { |
| 218 // We're back on the primary sequence in this method. |
| 219 if (is_shutdown_) |
| 220 return; |
| 221 in_flight_commit_batch_.reset(); |
| 222 if (commit_batch_.get()) { |
| 223 // More changes have accrued, restart the timer. |
| 224 task_runner_->PostDelayedTask( |
| 225 FROM_HERE, |
| 226 base::Bind(&DomStorageArea::OnCommitTimer, this), |
| 227 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); |
| 228 } |
| 229 } |
| 230 |
| 231 void DomStorageArea::ShutdownInCommitSequence() { |
| 232 // This method executes on the commit sequence. |
173 DCHECK(backing_.get()); | 233 DCHECK(backing_.get()); |
174 if (backing_->CommitChanges(clear_all_next_commit_, changed_values_)) { | 234 if (commit_batch_.get()) { |
175 clear_all_next_commit_ = false; | 235 // Commit any changes that accrued prior to the timer firing. |
176 changed_values_.clear(); | 236 bool success = backing_->CommitChanges( |
| 237 commit_batch_->clear_all_first, |
| 238 commit_batch_->changed_values); |
| 239 DCHECK(success); |
177 } | 240 } |
178 commit_in_flight_ = false; | 241 commit_batch_.reset(); |
| 242 in_flight_commit_batch_.reset(); |
| 243 backing_.reset(); |
179 } | 244 } |
180 | 245 |
181 } // namespace dom_storage | 246 } // namespace dom_storage |
OLD | NEW |