| 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 12 #include "webkit/database/database_util.h" | 12 #include "webkit/database/database_util.h" |
| 13 #include "webkit/dom_storage/dom_storage_map.h" | 13 #include "webkit/dom_storage/dom_storage_map.h" |
| 14 #include "webkit/dom_storage/dom_storage_namespace.h" | 14 #include "webkit/dom_storage/dom_storage_namespace.h" |
| 15 #include "webkit/dom_storage/dom_storage_task_runner.h" | 15 #include "webkit/dom_storage/dom_storage_task_runner.h" |
| 16 #include "webkit/dom_storage/dom_storage_types.h" | 16 #include "webkit/dom_storage/dom_storage_types.h" |
| 17 #include "webkit/dom_storage/local_storage_database_adapter.h" | 17 #include "webkit/dom_storage/local_storage_database_adapter.h" |
| 18 #include "webkit/dom_storage/session_storage_database.h" |
| 19 #include "webkit/dom_storage/session_storage_database_adapter.h" |
| 18 #include "webkit/fileapi/file_system_util.h" | 20 #include "webkit/fileapi/file_system_util.h" |
| 19 #include "webkit/glue/webkit_glue.h" | 21 #include "webkit/glue/webkit_glue.h" |
| 20 | 22 |
| 21 using webkit_database::DatabaseUtil; | 23 using webkit_database::DatabaseUtil; |
| 22 | 24 |
| 23 namespace dom_storage { | 25 namespace dom_storage { |
| 24 | 26 |
| 25 static const int kCommitTimerSeconds = 1; | 27 static const int kCommitTimerSeconds = 1; |
| 26 | 28 |
| 27 DomStorageArea::CommitBatch::CommitBatch() | 29 DomStorageArea::CommitBatch::CommitBatch() |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_)); | 67 FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_)); |
| 66 backing_.reset(new LocalStorageDatabaseAdapter(path)); | 68 backing_.reset(new LocalStorageDatabaseAdapter(path)); |
| 67 is_initial_import_done_ = false; | 69 is_initial_import_done_ = false; |
| 68 } | 70 } |
| 69 } | 71 } |
| 70 | 72 |
| 71 DomStorageArea::DomStorageArea( | 73 DomStorageArea::DomStorageArea( |
| 72 int64 namespace_id, | 74 int64 namespace_id, |
| 73 const std::string& persistent_namespace_id, | 75 const std::string& persistent_namespace_id, |
| 74 const GURL& origin, | 76 const GURL& origin, |
| 77 SessionStorageDatabase* session_storage_backing, |
| 75 DomStorageTaskRunner* task_runner) | 78 DomStorageTaskRunner* task_runner) |
| 76 : namespace_id_(namespace_id), | 79 : namespace_id_(namespace_id), |
| 77 persistent_namespace_id_(persistent_namespace_id), | 80 persistent_namespace_id_(persistent_namespace_id), |
| 78 origin_(origin), | 81 origin_(origin), |
| 79 task_runner_(task_runner), | 82 task_runner_(task_runner), |
| 80 map_(new DomStorageMap(kPerAreaQuota + kPerAreaOverQuotaAllowance)), | 83 map_(new DomStorageMap(kPerAreaQuota + kPerAreaOverQuotaAllowance)), |
| 84 session_storage_backing_(session_storage_backing), |
| 81 is_initial_import_done_(true), | 85 is_initial_import_done_(true), |
| 82 is_shutdown_(false), | 86 is_shutdown_(false), |
| 83 commit_batches_in_flight_(0) { | 87 commit_batches_in_flight_(0) { |
| 84 DCHECK(namespace_id != kLocalStorageNamespaceId); | 88 DCHECK(namespace_id != kLocalStorageNamespaceId); |
| 89 if (session_storage_backing) { |
| 90 backing_.reset(new SessionStorageDatabaseAdapter( |
| 91 session_storage_backing, persistent_namespace_id, origin)); |
| 92 is_initial_import_done_ = false; |
| 93 } |
| 85 } | 94 } |
| 86 | 95 |
| 87 DomStorageArea::~DomStorageArea() { | 96 DomStorageArea::~DomStorageArea() { |
| 88 } | 97 } |
| 89 | 98 |
| 90 void DomStorageArea::ExtractValues(ValuesMap* map) { | 99 void DomStorageArea::ExtractValues(ValuesMap* map) { |
| 91 if (is_shutdown_) | 100 if (is_shutdown_) |
| 92 return; | 101 return; |
| 93 InitialImportIfNeeded(); | 102 InitialImportIfNeeded(); |
| 94 map_->ExtractValues(map); | 103 map_->ExtractValues(map); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 170 } |
| 162 | 171 |
| 163 return true; | 172 return true; |
| 164 } | 173 } |
| 165 | 174 |
| 166 DomStorageArea* DomStorageArea::ShallowCopy( | 175 DomStorageArea* DomStorageArea::ShallowCopy( |
| 167 int64 destination_namespace_id, | 176 int64 destination_namespace_id, |
| 168 const std::string& destination_persistent_namespace_id) { | 177 const std::string& destination_persistent_namespace_id) { |
| 169 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 178 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
| 170 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 179 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
| 171 DCHECK(!backing_.get()); // SessionNamespaces aren't stored on disk. | |
| 172 | 180 |
| 173 DomStorageArea* copy = new DomStorageArea( | 181 DomStorageArea* copy = new DomStorageArea( |
| 174 destination_namespace_id, destination_persistent_namespace_id, origin_, | 182 destination_namespace_id, destination_persistent_namespace_id, origin_, |
| 175 task_runner_); | 183 session_storage_backing_, task_runner_); |
| 176 copy->map_ = map_; | 184 copy->map_ = map_; |
| 177 copy->is_shutdown_ = is_shutdown_; | 185 copy->is_shutdown_ = is_shutdown_; |
| 186 copy->is_initial_import_done_ = true; |
| 187 |
| 188 // All the uncommitted changes to this area need to happen before the actual |
| 189 // shallow copy is made (scheduled by the upper layer). Another OnCommitTimer |
| 190 // call might be in the event queue at this point, but it's handled gracefully |
| 191 // when it fires. |
| 192 if (commit_batch_.get()) |
| 193 OnCommitTimer(); |
| 178 return copy; | 194 return copy; |
| 179 } | 195 } |
| 180 | 196 |
| 181 bool DomStorageArea::HasUncommittedChanges() const { | 197 bool DomStorageArea::HasUncommittedChanges() const { |
| 182 DCHECK(!is_shutdown_); | 198 DCHECK(!is_shutdown_); |
| 183 return commit_batch_.get() || commit_batches_in_flight_; | 199 return commit_batch_.get() || commit_batches_in_flight_; |
| 184 } | 200 } |
| 185 | 201 |
| 186 void DomStorageArea::DeleteOrigin() { | 202 void DomStorageArea::DeleteOrigin() { |
| 187 DCHECK(!is_shutdown_); | 203 DCHECK(!is_shutdown_); |
| 204 // This function shouldn't be called for sessionStorage. |
| 205 DCHECK(session_storage_backing_.get()); |
| 188 if (HasUncommittedChanges()) { | 206 if (HasUncommittedChanges()) { |
| 189 // TODO(michaeln): This logically deletes the data immediately, | 207 // TODO(michaeln): This logically deletes the data immediately, |
| 190 // and in a matter of a second, deletes the rows from the backing | 208 // and in a matter of a second, deletes the rows from the backing |
| 191 // database file, but the file itself will linger until shutdown | 209 // database file, but the file itself will linger until shutdown |
| 192 // or purge time. Ideally, this should delete the file more | 210 // or purge time. Ideally, this should delete the file more |
| 193 // quickly. | 211 // quickly. |
| 194 Clear(); | 212 Clear(); |
| 195 return; | 213 return; |
| 196 } | 214 } |
| 197 map_ = new DomStorageMap(kPerAreaQuota + kPerAreaOverQuotaAllowance); | 215 map_ = new DomStorageMap(kPerAreaQuota + kPerAreaOverQuotaAllowance); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 FROM_HERE, | 247 FROM_HERE, |
| 230 DomStorageTaskRunner::COMMIT_SEQUENCE, | 248 DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 231 base::Bind(&DomStorageArea::ShutdownInCommitSequence, this)); | 249 base::Bind(&DomStorageArea::ShutdownInCommitSequence, this)); |
| 232 DCHECK(success); | 250 DCHECK(success); |
| 233 } | 251 } |
| 234 | 252 |
| 235 void DomStorageArea::InitialImportIfNeeded() { | 253 void DomStorageArea::InitialImportIfNeeded() { |
| 236 if (is_initial_import_done_) | 254 if (is_initial_import_done_) |
| 237 return; | 255 return; |
| 238 | 256 |
| 239 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | |
| 240 DCHECK(backing_.get()); | 257 DCHECK(backing_.get()); |
| 241 | 258 |
| 242 ValuesMap initial_values; | 259 ValuesMap initial_values; |
| 243 backing_->ReadAllValues(&initial_values); | 260 backing_->ReadAllValues(&initial_values); |
| 244 map_->SwapValues(&initial_values); | 261 map_->SwapValues(&initial_values); |
| 245 is_initial_import_done_ = true; | 262 is_initial_import_done_ = true; |
| 246 } | 263 } |
| 247 | 264 |
| 248 DomStorageArea::CommitBatch* DomStorageArea::CreateCommitBatchIfNeeded() { | 265 DomStorageArea::CommitBatch* DomStorageArea::CreateCommitBatchIfNeeded() { |
| 249 DCHECK(!is_shutdown_); | 266 DCHECK(!is_shutdown_); |
| 250 if (!commit_batch_.get()) { | 267 if (!commit_batch_.get()) { |
| 251 commit_batch_.reset(new CommitBatch()); | 268 commit_batch_.reset(new CommitBatch()); |
| 252 | 269 |
| 253 // Start a timer to commit any changes that accrue in the batch, but only if | 270 // Start a timer to commit any changes that accrue in the batch, but only if |
| 254 // no commits are currently in flight. In that case the timer will be | 271 // no commits are currently in flight. In that case the timer will be |
| 255 // started after the commits have happened. | 272 // started after the commits have happened. |
| 256 if (!commit_batches_in_flight_) { | 273 if (!commit_batches_in_flight_) { |
| 257 task_runner_->PostDelayedTask( | 274 task_runner_->PostDelayedTask( |
| 258 FROM_HERE, | 275 FROM_HERE, |
| 259 base::Bind(&DomStorageArea::OnCommitTimer, this), | 276 base::Bind(&DomStorageArea::OnCommitTimer, this), |
| 260 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | 277 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); |
| 261 } | 278 } |
| 262 } | 279 } |
| 263 return commit_batch_.get(); | 280 return commit_batch_.get(); |
| 264 } | 281 } |
| 265 | 282 |
| 266 void DomStorageArea::OnCommitTimer() { | 283 void DomStorageArea::OnCommitTimer() { |
| 267 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | |
| 268 if (is_shutdown_) | 284 if (is_shutdown_) |
| 269 return; | 285 return; |
| 270 | 286 |
| 271 DCHECK(backing_.get()); | 287 DCHECK(backing_.get()); |
| 272 DCHECK(commit_batch_.get()); | 288 |
| 273 DCHECK(!commit_batches_in_flight_); | 289 // It's possible that there is nothing to commit, since a shallow copy occured |
| 290 // before the timer fired. |
| 291 if (!commit_batch_.get()) |
| 292 return; |
| 274 | 293 |
| 275 // This method executes on the primary sequence, we schedule | 294 // This method executes on the primary sequence, we schedule |
| 276 // a task for immediate execution on the commit sequence. | 295 // a task for immediate execution on the commit sequence. |
| 277 DCHECK(task_runner_->IsRunningOnPrimarySequence()); | 296 DCHECK(task_runner_->IsRunningOnPrimarySequence()); |
| 278 bool success = task_runner_->PostShutdownBlockingTask( | 297 bool success = task_runner_->PostShutdownBlockingTask( |
| 279 FROM_HERE, | 298 FROM_HERE, |
| 280 DomStorageTaskRunner::COMMIT_SEQUENCE, | 299 DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 281 base::Bind(&DomStorageArea::CommitChanges, this, | 300 base::Bind(&DomStorageArea::CommitChanges, this, |
| 282 base::Owned(commit_batch_.release()))); | 301 base::Owned(commit_batch_.release()))); |
| 283 ++commit_batches_in_flight_; | 302 ++commit_batches_in_flight_; |
| 284 DCHECK(success); | 303 DCHECK(success); |
| 285 } | 304 } |
| 286 | 305 |
| 287 void DomStorageArea::CommitChanges(const CommitBatch* commit_batch) { | 306 void DomStorageArea::CommitChanges(const CommitBatch* commit_batch) { |
| 288 // This method executes on the commit sequence. | 307 // This method executes on the commit sequence. |
| 289 DCHECK(task_runner_->IsRunningOnCommitSequence()); | 308 DCHECK(task_runner_->IsRunningOnCommitSequence()); |
| 290 bool success = backing_->CommitChanges(commit_batch->clear_all_first, | 309 bool success = backing_->CommitChanges(commit_batch->clear_all_first, |
| 291 commit_batch->changed_values); | 310 commit_batch->changed_values); |
| 292 DCHECK(success); // TODO(michaeln): what if it fails? | 311 DCHECK(success); // TODO(michaeln): what if it fails? |
| 293 task_runner_->PostTask( | 312 task_runner_->PostTask( |
| 294 FROM_HERE, | 313 FROM_HERE, |
| 295 base::Bind(&DomStorageArea::OnCommitComplete, this)); | 314 base::Bind(&DomStorageArea::OnCommitComplete, this)); |
| 296 } | 315 } |
| 297 | 316 |
| 298 void DomStorageArea::OnCommitComplete() { | 317 void DomStorageArea::OnCommitComplete() { |
| 299 // We're back on the primary sequence in this method. | 318 // We're back on the primary sequence in this method. |
| 300 DCHECK(task_runner_->IsRunningOnPrimarySequence()); | 319 DCHECK(task_runner_->IsRunningOnPrimarySequence()); |
| 320 --commit_batches_in_flight_; |
| 301 if (is_shutdown_) | 321 if (is_shutdown_) |
| 302 return; | 322 return; |
| 303 --commit_batches_in_flight_; | |
| 304 if (commit_batch_.get() && !commit_batches_in_flight_) { | 323 if (commit_batch_.get() && !commit_batches_in_flight_) { |
| 305 // More changes have accrued, restart the timer. | 324 // More changes have accrued, restart the timer. |
| 306 task_runner_->PostDelayedTask( | 325 task_runner_->PostDelayedTask( |
| 307 FROM_HERE, | 326 FROM_HERE, |
| 308 base::Bind(&DomStorageArea::OnCommitTimer, this), | 327 base::Bind(&DomStorageArea::OnCommitTimer, this), |
| 309 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | 328 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); |
| 310 } | 329 } |
| 311 } | 330 } |
| 312 | 331 |
| 313 void DomStorageArea::ShutdownInCommitSequence() { | 332 void DomStorageArea::ShutdownInCommitSequence() { |
| 314 // This method executes on the commit sequence. | 333 // This method executes on the commit sequence. |
| 315 DCHECK(task_runner_->IsRunningOnCommitSequence()); | 334 DCHECK(task_runner_->IsRunningOnCommitSequence()); |
| 316 DCHECK(backing_.get()); | 335 DCHECK(backing_.get()); |
| 317 if (commit_batch_.get()) { | 336 if (commit_batch_.get()) { |
| 318 // Commit any changes that accrued prior to the timer firing. | 337 // Commit any changes that accrued prior to the timer firing. |
| 319 bool success = backing_->CommitChanges( | 338 bool success = backing_->CommitChanges( |
| 320 commit_batch_->clear_all_first, | 339 commit_batch_->clear_all_first, |
| 321 commit_batch_->changed_values); | 340 commit_batch_->changed_values); |
| 322 DCHECK(success); | 341 DCHECK(success); |
| 323 } | 342 } |
| 324 commit_batch_.reset(); | 343 commit_batch_.reset(); |
| 325 backing_.reset(); | 344 backing_.reset(); |
| 345 session_storage_backing_ = NULL; |
| 326 } | 346 } |
| 327 | 347 |
| 328 } // namespace dom_storage | 348 } // namespace dom_storage |
| OLD | NEW |