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 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 if (!backing_.get()) | 244 if (!backing_.get()) |
244 return; | 245 return; |
245 | 246 |
246 bool success = task_runner_->PostShutdownBlockingTask( | 247 bool success = task_runner_->PostShutdownBlockingTask( |
247 FROM_HERE, | 248 FROM_HERE, |
248 DomStorageTaskRunner::COMMIT_SEQUENCE, | 249 DomStorageTaskRunner::COMMIT_SEQUENCE, |
249 base::Bind(&DomStorageArea::ShutdownInCommitSequence, this)); | 250 base::Bind(&DomStorageArea::ShutdownInCommitSequence, this)); |
250 DCHECK(success); | 251 DCHECK(success); |
251 } | 252 } |
252 | 253 |
| 254 // static |
| 255 void DomStorageArea::DisableCommitDelayForTesting() { |
| 256 commit_timer_seconds = 0; |
| 257 } |
| 258 |
253 void DomStorageArea::InitialImportIfNeeded() { | 259 void DomStorageArea::InitialImportIfNeeded() { |
254 if (is_initial_import_done_) | 260 if (is_initial_import_done_) |
255 return; | 261 return; |
256 | 262 |
257 DCHECK(backing_.get()); | 263 DCHECK(backing_.get()); |
258 | 264 |
259 ValuesMap initial_values; | 265 ValuesMap initial_values; |
260 backing_->ReadAllValues(&initial_values); | 266 backing_->ReadAllValues(&initial_values); |
261 map_->SwapValues(&initial_values); | 267 map_->SwapValues(&initial_values); |
262 is_initial_import_done_ = true; | 268 is_initial_import_done_ = true; |
263 } | 269 } |
264 | 270 |
265 DomStorageArea::CommitBatch* DomStorageArea::CreateCommitBatchIfNeeded() { | 271 DomStorageArea::CommitBatch* DomStorageArea::CreateCommitBatchIfNeeded() { |
266 DCHECK(!is_shutdown_); | 272 DCHECK(!is_shutdown_); |
267 if (!commit_batch_.get()) { | 273 if (!commit_batch_.get()) { |
268 commit_batch_.reset(new CommitBatch()); | 274 commit_batch_.reset(new CommitBatch()); |
269 | 275 |
270 // Start a timer to commit any changes that accrue in the batch, but only if | 276 // 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 | 277 // no commits are currently in flight. In that case the timer will be |
272 // started after the commits have happened. | 278 // started after the commits have happened. |
273 if (!commit_batches_in_flight_) { | 279 if (!commit_batches_in_flight_) { |
274 task_runner_->PostDelayedTask( | 280 task_runner_->PostDelayedTask( |
275 FROM_HERE, | 281 FROM_HERE, |
276 base::Bind(&DomStorageArea::OnCommitTimer, this), | 282 base::Bind(&DomStorageArea::OnCommitTimer, this), |
277 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | 283 base::TimeDelta::FromSeconds(commit_timer_seconds)); |
278 } | 284 } |
279 } | 285 } |
280 return commit_batch_.get(); | 286 return commit_batch_.get(); |
281 } | 287 } |
282 | 288 |
283 void DomStorageArea::OnCommitTimer() { | 289 void DomStorageArea::OnCommitTimer() { |
284 if (is_shutdown_) | 290 if (is_shutdown_) |
285 return; | 291 return; |
286 | 292 |
287 DCHECK(backing_.get()); | 293 DCHECK(backing_.get()); |
(...skipping 30 matching lines...) Expand all Loading... |
318 // We're back on the primary sequence in this method. | 324 // We're back on the primary sequence in this method. |
319 DCHECK(task_runner_->IsRunningOnPrimarySequence()); | 325 DCHECK(task_runner_->IsRunningOnPrimarySequence()); |
320 --commit_batches_in_flight_; | 326 --commit_batches_in_flight_; |
321 if (is_shutdown_) | 327 if (is_shutdown_) |
322 return; | 328 return; |
323 if (commit_batch_.get() && !commit_batches_in_flight_) { | 329 if (commit_batch_.get() && !commit_batches_in_flight_) { |
324 // More changes have accrued, restart the timer. | 330 // More changes have accrued, restart the timer. |
325 task_runner_->PostDelayedTask( | 331 task_runner_->PostDelayedTask( |
326 FROM_HERE, | 332 FROM_HERE, |
327 base::Bind(&DomStorageArea::OnCommitTimer, this), | 333 base::Bind(&DomStorageArea::OnCommitTimer, this), |
328 base::TimeDelta::FromSeconds(kCommitTimerSeconds)); | 334 base::TimeDelta::FromSeconds(commit_timer_seconds)); |
329 } | 335 } |
330 } | 336 } |
331 | 337 |
332 void DomStorageArea::ShutdownInCommitSequence() { | 338 void DomStorageArea::ShutdownInCommitSequence() { |
333 // This method executes on the commit sequence. | 339 // This method executes on the commit sequence. |
334 DCHECK(task_runner_->IsRunningOnCommitSequence()); | 340 DCHECK(task_runner_->IsRunningOnCommitSequence()); |
335 DCHECK(backing_.get()); | 341 DCHECK(backing_.get()); |
336 if (commit_batch_.get()) { | 342 if (commit_batch_.get()) { |
337 // Commit any changes that accrued prior to the timer firing. | 343 // Commit any changes that accrued prior to the timer firing. |
338 bool success = backing_->CommitChanges( | 344 bool success = backing_->CommitChanges( |
339 commit_batch_->clear_all_first, | 345 commit_batch_->clear_all_first, |
340 commit_batch_->changed_values); | 346 commit_batch_->changed_values); |
341 DCHECK(success); | 347 DCHECK(success); |
342 } | 348 } |
343 commit_batch_.reset(); | 349 commit_batch_.reset(); |
344 backing_.reset(); | 350 backing_.reset(); |
345 session_storage_backing_ = NULL; | 351 session_storage_backing_ = NULL; |
346 } | 352 } |
347 | 353 |
348 } // namespace dom_storage | 354 } // namespace dom_storage |
OLD | NEW |