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" | 18 #include "webkit/dom_storage/session_storage_database.h" |
19 #include "webkit/dom_storage/session_storage_database_adapter.h" | 19 #include "webkit/dom_storage/session_storage_database_adapter.h" |
20 #include "webkit/fileapi/file_system_util.h" | 20 #include "webkit/fileapi/file_system_util.h" |
21 #include "webkit/glue/webkit_glue.h" | 21 #include "webkit/glue/webkit_glue.h" |
22 | 22 |
23 using webkit_database::DatabaseUtil; | 23 using webkit_database::DatabaseUtil; |
24 | 24 |
25 namespace dom_storage { | 25 namespace dom_storage { |
26 | 26 |
27 static const int kCommitTimerSeconds = 1; | 27 // Non-const for testing. |
| 28 static int commit_timer_seconds = 1; |
28 | 29 |
29 DomStorageArea::CommitBatch::CommitBatch() | 30 DomStorageArea::CommitBatch::CommitBatch() |
30 : clear_all_first(false) { | 31 : clear_all_first(false) { |
31 } | 32 } |
32 DomStorageArea::CommitBatch::~CommitBatch() {} | 33 DomStorageArea::CommitBatch::~CommitBatch() {} |
33 | 34 |
34 | 35 |
35 // static | 36 // static |
36 const FilePath::CharType DomStorageArea::kDatabaseFileExtension[] = | 37 const FilePath::CharType DomStorageArea::kDatabaseFileExtension[] = |
37 FILE_PATH_LITERAL(".localstorage"); | 38 FILE_PATH_LITERAL(".localstorage"); |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 if (!commit_batch_.get()) { | 268 if (!commit_batch_.get()) { |
268 commit_batch_.reset(new CommitBatch()); | 269 commit_batch_.reset(new CommitBatch()); |
269 | 270 |
270 // Start a timer to commit any changes that accrue in the batch, but only if | 271 // Start a timer to commit any changes that accrue in the batch, but only if |
271 // no commits are currently in flight. In that case the timer will be | 272 // no commits are currently in flight. In that case the timer will be |
272 // started after the commits have happened. | 273 // started after the commits have happened. |
273 if (!commit_batches_in_flight_) { | 274 if (!commit_batches_in_flight_) { |
274 task_runner_->PostDelayedTask( | 275 task_runner_->PostDelayedTask( |
275 FROM_HERE, | 276 FROM_HERE, |
276 base::Bind(&DomStorageArea::OnCommitTimer, this), | 277 base::Bind(&DomStorageArea::OnCommitTimer, this), |
277 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | 278 base::TimeDelta::FromSeconds(commit_timer_seconds)); |
278 } | 279 } |
279 } | 280 } |
280 return commit_batch_.get(); | 281 return commit_batch_.get(); |
281 } | 282 } |
282 | 283 |
283 void DomStorageArea::OnCommitTimer() { | 284 void DomStorageArea::OnCommitTimer() { |
284 if (is_shutdown_) | 285 if (is_shutdown_) |
285 return; | 286 return; |
286 | 287 |
287 DCHECK(backing_.get()); | 288 DCHECK(backing_.get()); |
(...skipping 30 matching lines...) Expand all Loading... |
318 // We're back on the primary sequence in this method. | 319 // We're back on the primary sequence in this method. |
319 DCHECK(task_runner_->IsRunningOnPrimarySequence()); | 320 DCHECK(task_runner_->IsRunningOnPrimarySequence()); |
320 --commit_batches_in_flight_; | 321 --commit_batches_in_flight_; |
321 if (is_shutdown_) | 322 if (is_shutdown_) |
322 return; | 323 return; |
323 if (commit_batch_.get() && !commit_batches_in_flight_) { | 324 if (commit_batch_.get() && !commit_batches_in_flight_) { |
324 // More changes have accrued, restart the timer. | 325 // More changes have accrued, restart the timer. |
325 task_runner_->PostDelayedTask( | 326 task_runner_->PostDelayedTask( |
326 FROM_HERE, | 327 FROM_HERE, |
327 base::Bind(&DomStorageArea::OnCommitTimer, this), | 328 base::Bind(&DomStorageArea::OnCommitTimer, this), |
328 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | 329 base::TimeDelta::FromSeconds(commit_timer_seconds)); |
329 } | 330 } |
330 } | 331 } |
331 | 332 |
332 void DomStorageArea::ShutdownInCommitSequence() { | 333 void DomStorageArea::ShutdownInCommitSequence() { |
333 // This method executes on the commit sequence. | 334 // This method executes on the commit sequence. |
334 DCHECK(task_runner_->IsRunningOnCommitSequence()); | 335 DCHECK(task_runner_->IsRunningOnCommitSequence()); |
335 DCHECK(backing_.get()); | 336 DCHECK(backing_.get()); |
336 if (commit_batch_.get()) { | 337 if (commit_batch_.get()) { |
337 // Commit any changes that accrued prior to the timer firing. | 338 // Commit any changes that accrued prior to the timer firing. |
338 bool success = backing_->CommitChanges( | 339 bool success = backing_->CommitChanges( |
339 commit_batch_->clear_all_first, | 340 commit_batch_->clear_all_first, |
340 commit_batch_->changed_values); | 341 commit_batch_->changed_values); |
341 DCHECK(success); | 342 DCHECK(success); |
342 } | 343 } |
343 commit_batch_.reset(); | 344 commit_batch_.reset(); |
344 backing_.reset(); | 345 backing_.reset(); |
345 session_storage_backing_ = NULL; | 346 session_storage_backing_ = NULL; |
346 } | 347 } |
347 | 348 |
| 349 // static |
| 350 void DomStorageArea::DisableCommitDelayForTesting() { |
| 351 commit_timer_seconds = 0; |
| 352 } |
| 353 |
348 } // namespace dom_storage | 354 } // namespace dom_storage |
OLD | NEW |