Home | History | Annotate | Download | only in dom_storage
      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
      3 // found in the LICENSE file.
      4 
      5 #include "content/browser/dom_storage/dom_storage_task_runner.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/message_loop/message_loop_proxy.h"
     10 #include "base/tracked_objects.h"
     11 
     12 namespace content {
     13 
     14 // DOMStorageTaskRunner
     15 
     16 bool DOMStorageTaskRunner::RunsTasksOnCurrentThread() const {
     17   return IsRunningOnSequence(PRIMARY_SEQUENCE);
     18 }
     19 
     20 // DOMStorageWorkerPoolTaskRunner
     21 
     22 DOMStorageWorkerPoolTaskRunner::DOMStorageWorkerPoolTaskRunner(
     23     base::SequencedWorkerPool* sequenced_worker_pool,
     24     base::SequencedWorkerPool::SequenceToken primary_sequence_token,
     25     base::SequencedWorkerPool::SequenceToken commit_sequence_token,
     26     base::MessageLoopProxy* delayed_task_loop)
     27     : message_loop_(delayed_task_loop),
     28       sequenced_worker_pool_(sequenced_worker_pool),
     29       primary_sequence_token_(primary_sequence_token),
     30       commit_sequence_token_(commit_sequence_token) {
     31 }
     32 
     33 DOMStorageWorkerPoolTaskRunner::~DOMStorageWorkerPoolTaskRunner() {
     34 }
     35 
     36 bool DOMStorageWorkerPoolTaskRunner::PostDelayedTask(
     37     const tracked_objects::Location& from_here,
     38     const base::Closure& task,
     39     base::TimeDelta delay) {
     40   // Note base::TaskRunner implements PostTask in terms of PostDelayedTask
     41   // with a delay of zero, we detect that usage and avoid the unecessary
     42   // trip thru the message loop.
     43   if (delay == base::TimeDelta()) {
     44     return sequenced_worker_pool_->PostSequencedWorkerTaskWithShutdownBehavior(
     45         primary_sequence_token_, from_here, task,
     46         base::SequencedWorkerPool::BLOCK_SHUTDOWN);
     47   }
     48   // Post a task to call this->PostTask() after the delay.
     49   return message_loop_->PostDelayedTask(
     50       FROM_HERE,
     51       base::Bind(base::IgnoreResult(&DOMStorageWorkerPoolTaskRunner::PostTask),
     52                  this, from_here, task),
     53       delay);
     54 }
     55 
     56 bool DOMStorageWorkerPoolTaskRunner::PostShutdownBlockingTask(
     57     const tracked_objects::Location& from_here,
     58     SequenceID sequence_id,
     59     const base::Closure& task) {
     60   return sequenced_worker_pool_->PostSequencedWorkerTaskWithShutdownBehavior(
     61       IDtoToken(sequence_id), from_here, task,
     62       base::SequencedWorkerPool::BLOCK_SHUTDOWN);
     63 }
     64 
     65 bool DOMStorageWorkerPoolTaskRunner::IsRunningOnSequence(
     66     SequenceID sequence_id) const {
     67   return sequenced_worker_pool_->IsRunningSequenceOnCurrentThread(
     68       IDtoToken(sequence_id));
     69 }
     70 
     71 base::SequencedWorkerPool::SequenceToken
     72 DOMStorageWorkerPoolTaskRunner::IDtoToken(SequenceID id) const {
     73   if (id == PRIMARY_SEQUENCE)
     74     return primary_sequence_token_;
     75   DCHECK_EQ(COMMIT_SEQUENCE, id);
     76   return commit_sequence_token_;
     77 }
     78 
     79 // MockDOMStorageTaskRunner
     80 
     81 MockDOMStorageTaskRunner::MockDOMStorageTaskRunner(
     82     base::MessageLoopProxy* message_loop)
     83     : message_loop_(message_loop) {
     84 }
     85 
     86 MockDOMStorageTaskRunner::~MockDOMStorageTaskRunner() {
     87 }
     88 
     89 bool MockDOMStorageTaskRunner::PostDelayedTask(
     90     const tracked_objects::Location& from_here,
     91     const base::Closure& task,
     92     base::TimeDelta delay) {
     93   return message_loop_->PostTask(from_here, task);
     94 }
     95 
     96 bool MockDOMStorageTaskRunner::PostShutdownBlockingTask(
     97     const tracked_objects::Location& from_here,
     98     SequenceID sequence_id,
     99     const base::Closure& task) {
    100   return message_loop_->PostTask(from_here, task);
    101 }
    102 
    103 bool MockDOMStorageTaskRunner::IsRunningOnSequence(SequenceID) const {
    104   return message_loop_->RunsTasksOnCurrentThread();
    105 }
    106 
    107 }  // namespace content
    108