| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/dom_storage/dom_storage_task_runner.h" | 5 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
| 12 | 14 |
| 13 namespace content { | 15 namespace content { |
| 14 | 16 |
| 15 // DOMStorageWorkerPoolTaskRunner | 17 // DOMStorageWorkerPoolTaskRunner |
| 16 | 18 |
| 17 DOMStorageWorkerPoolTaskRunner::DOMStorageWorkerPoolTaskRunner( | 19 DOMStorageWorkerPoolTaskRunner::DOMStorageWorkerPoolTaskRunner( |
| 18 base::SequencedWorkerPool* sequenced_worker_pool, | 20 scoped_refptr<base::SequencedTaskRunner> primary_sequence, |
| 19 base::SequencedWorkerPool::SequenceToken primary_sequence_token, | 21 scoped_refptr<base::SequencedTaskRunner> commit_sequence) |
| 20 base::SequencedWorkerPool::SequenceToken commit_sequence_token, | 22 : primary_sequence_(std::move(primary_sequence)), |
| 21 base::SingleThreadTaskRunner* delayed_task_task_runner) | 23 commit_sequence_(std::move(commit_sequence)) {} |
| 22 : task_runner_(delayed_task_task_runner), | |
| 23 sequenced_worker_pool_(sequenced_worker_pool), | |
| 24 primary_sequence_token_(primary_sequence_token), | |
| 25 commit_sequence_token_(commit_sequence_token) { | |
| 26 primary_sequence_checker_.DetachFromSequence(); | |
| 27 commit_sequence_checker_.DetachFromSequence(); | |
| 28 } | |
| 29 | 24 |
| 30 DOMStorageWorkerPoolTaskRunner::~DOMStorageWorkerPoolTaskRunner() { | 25 DOMStorageWorkerPoolTaskRunner::~DOMStorageWorkerPoolTaskRunner() = default; |
| 31 } | |
| 32 | 26 |
| 33 bool DOMStorageWorkerPoolTaskRunner::RunsTasksOnCurrentThread() const { | 27 bool DOMStorageWorkerPoolTaskRunner::RunsTasksOnCurrentThread() const { |
| 34 // It is valid for an implementation to always return true. | 28 // It is valid for an implementation to always return true. |
| 35 return true; | 29 return true; |
| 36 } | 30 } |
| 37 | 31 |
| 38 bool DOMStorageWorkerPoolTaskRunner::PostDelayedTask( | 32 bool DOMStorageWorkerPoolTaskRunner::PostDelayedTask( |
| 39 const tracked_objects::Location& from_here, | 33 const tracked_objects::Location& from_here, |
| 40 const base::Closure& task, | 34 const base::Closure& task, |
| 41 base::TimeDelta delay) { | 35 base::TimeDelta delay) { |
| 42 // Note base::TaskRunner implements PostTask in terms of PostDelayedTask | 36 return primary_sequence_->PostDelayedTask(from_here, task, delay); |
| 43 // with a delay of zero, we detect that usage and avoid the unecessary | |
| 44 // trip thru the message loop. | |
| 45 if (delay.is_zero()) { | |
| 46 return sequenced_worker_pool_->PostSequencedWorkerTaskWithShutdownBehavior( | |
| 47 primary_sequence_token_, from_here, task, | |
| 48 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | |
| 49 } | |
| 50 // Post a task to call this->PostTask() after the delay. | |
| 51 return task_runner_->PostDelayedTask( | |
| 52 FROM_HERE, | |
| 53 base::Bind(base::IgnoreResult(&DOMStorageWorkerPoolTaskRunner::PostTask), | |
| 54 this, from_here, task), | |
| 55 delay); | |
| 56 } | 37 } |
| 57 | 38 |
| 58 bool DOMStorageWorkerPoolTaskRunner::PostShutdownBlockingTask( | 39 bool DOMStorageWorkerPoolTaskRunner::PostShutdownBlockingTask( |
| 59 const tracked_objects::Location& from_here, | 40 const tracked_objects::Location& from_here, |
| 60 SequenceID sequence_id, | 41 SequenceID sequence_id, |
| 61 const base::Closure& task) { | 42 const base::Closure& task) { |
| 62 return sequenced_worker_pool_->PostSequencedWorkerTaskWithShutdownBehavior( | 43 return GetSequencedTaskRunner(sequence_id)->PostTask(from_here, task); |
| 63 IDtoToken(sequence_id), from_here, task, | |
| 64 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | |
| 65 } | 44 } |
| 66 | 45 |
| 67 void DOMStorageWorkerPoolTaskRunner::AssertIsRunningOnPrimarySequence() const { | 46 void DOMStorageWorkerPoolTaskRunner::AssertIsRunningOnPrimarySequence() const { |
| 68 DCHECK(primary_sequence_checker_.CalledOnValidSequence()); | 47 DCHECK(primary_sequence_->RunsTasksOnCurrentThread()); |
| 69 } | 48 } |
| 70 | 49 |
| 71 void DOMStorageWorkerPoolTaskRunner::AssertIsRunningOnCommitSequence() const { | 50 void DOMStorageWorkerPoolTaskRunner::AssertIsRunningOnCommitSequence() const { |
| 72 DCHECK(commit_sequence_checker_.CalledOnValidSequence()); | 51 DCHECK(commit_sequence_->RunsTasksOnCurrentThread()); |
| 73 } | 52 } |
| 74 | 53 |
| 75 scoped_refptr<base::SequencedTaskRunner> | 54 scoped_refptr<base::SequencedTaskRunner> |
| 76 DOMStorageWorkerPoolTaskRunner::GetSequencedTaskRunner(SequenceID sequence_id) { | 55 DOMStorageWorkerPoolTaskRunner::GetSequencedTaskRunner(SequenceID sequence_id) { |
| 77 return sequenced_worker_pool_->GetSequencedTaskRunner(IDtoToken(sequence_id)); | 56 if (sequence_id == PRIMARY_SEQUENCE) |
| 78 } | 57 return primary_sequence_; |
| 79 | 58 DCHECK_EQ(COMMIT_SEQUENCE, sequence_id); |
| 80 base::SequencedWorkerPool::SequenceToken | 59 return commit_sequence_; |
| 81 DOMStorageWorkerPoolTaskRunner::IDtoToken(SequenceID id) const { | |
| 82 if (id == PRIMARY_SEQUENCE) | |
| 83 return primary_sequence_token_; | |
| 84 DCHECK_EQ(COMMIT_SEQUENCE, id); | |
| 85 return commit_sequence_token_; | |
| 86 } | 60 } |
| 87 | 61 |
| 88 // MockDOMStorageTaskRunner | 62 // MockDOMStorageTaskRunner |
| 89 | 63 |
| 90 MockDOMStorageTaskRunner::MockDOMStorageTaskRunner( | 64 MockDOMStorageTaskRunner::MockDOMStorageTaskRunner( |
| 91 base::SingleThreadTaskRunner* task_runner) | 65 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 92 : task_runner_(task_runner) { | 66 : task_runner_(std::move(task_runner)) {} |
| 93 } | |
| 94 | 67 |
| 95 MockDOMStorageTaskRunner::~MockDOMStorageTaskRunner() { | 68 MockDOMStorageTaskRunner::~MockDOMStorageTaskRunner() = default; |
| 96 } | |
| 97 | 69 |
| 98 bool MockDOMStorageTaskRunner::RunsTasksOnCurrentThread() const { | 70 bool MockDOMStorageTaskRunner::RunsTasksOnCurrentThread() const { |
| 99 return task_runner_->RunsTasksOnCurrentThread(); | 71 return task_runner_->RunsTasksOnCurrentThread(); |
| 100 } | 72 } |
| 101 | 73 |
| 102 bool MockDOMStorageTaskRunner::PostDelayedTask( | 74 bool MockDOMStorageTaskRunner::PostDelayedTask( |
| 103 const tracked_objects::Location& from_here, | 75 const tracked_objects::Location& from_here, |
| 104 const base::Closure& task, | 76 const base::Closure& task, |
| 105 base::TimeDelta delay) { | 77 base::TimeDelta delay) { |
| 106 return task_runner_->PostTask(from_here, task); | 78 return task_runner_->PostTask(from_here, task); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 120 void MockDOMStorageTaskRunner::AssertIsRunningOnCommitSequence() const { | 92 void MockDOMStorageTaskRunner::AssertIsRunningOnCommitSequence() const { |
| 121 DCHECK(RunsTasksOnCurrentThread()); | 93 DCHECK(RunsTasksOnCurrentThread()); |
| 122 } | 94 } |
| 123 | 95 |
| 124 scoped_refptr<base::SequencedTaskRunner> | 96 scoped_refptr<base::SequencedTaskRunner> |
| 125 MockDOMStorageTaskRunner::GetSequencedTaskRunner(SequenceID sequence_id) { | 97 MockDOMStorageTaskRunner::GetSequencedTaskRunner(SequenceID sequence_id) { |
| 126 return task_runner_; | 98 return task_runner_; |
| 127 } | 99 } |
| 128 | 100 |
| 129 } // namespace content | 101 } // namespace content |
| OLD | NEW |