summaryrefslogtreecommitdiffstats
path: root/webkit/dom_storage/dom_storage_task_runner.cc
blob: 9935ad7be9d0530e25eedf06e7c1ad46ea5eef8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/dom_storage/dom_storage_task_runner.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop_proxy.h"
#include "base/tracked_objects.h"

namespace dom_storage {

// DomStorageTaskRunner

bool DomStorageTaskRunner::RunsTasksOnCurrentThread() const {
  return IsRunningOnSequence(PRIMARY_SEQUENCE);
}

// DomStorageWorkerPoolTaskRunner

DomStorageWorkerPoolTaskRunner::DomStorageWorkerPoolTaskRunner(
    base::SequencedWorkerPool* sequenced_worker_pool,
    base::SequencedWorkerPool::SequenceToken primary_sequence_token,
    base::SequencedWorkerPool::SequenceToken commit_sequence_token,
    base::MessageLoopProxy* delayed_task_loop)
    : message_loop_(delayed_task_loop),
      sequenced_worker_pool_(sequenced_worker_pool),
      primary_sequence_token_(primary_sequence_token),
      commit_sequence_token_(commit_sequence_token) {
}

DomStorageWorkerPoolTaskRunner::~DomStorageWorkerPoolTaskRunner() {
}

bool DomStorageWorkerPoolTaskRunner::PostDelayedTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task,
    base::TimeDelta delay) {
  // Note base::TaskRunner implements PostTask in terms of PostDelayedTask
  // with a delay of zero, we detect that usage and avoid the unecessary
  // trip thru the message loop.
  if (delay == base::TimeDelta()) {
    return sequenced_worker_pool_->PostSequencedWorkerTaskWithShutdownBehavior(
        primary_sequence_token_, from_here, task,
        base::SequencedWorkerPool::BLOCK_SHUTDOWN);
  }
  // Post a task to call this->PostTask() after the delay.
  return message_loop_->PostDelayedTask(
      FROM_HERE,
      base::Bind(base::IgnoreResult(&DomStorageWorkerPoolTaskRunner::PostTask),
                 this, from_here, task),
      delay);
}

bool DomStorageWorkerPoolTaskRunner::PostShutdownBlockingTask(
    const tracked_objects::Location& from_here,
    SequenceID sequence_id,
    const base::Closure& task) {
  return sequenced_worker_pool_->PostSequencedWorkerTaskWithShutdownBehavior(
      IDtoToken(sequence_id), from_here, task,
      base::SequencedWorkerPool::BLOCK_SHUTDOWN);
}

bool DomStorageWorkerPoolTaskRunner::IsRunningOnSequence(
    SequenceID sequence_id) const {
  return sequenced_worker_pool_->IsRunningSequenceOnCurrentThread(
      IDtoToken(sequence_id));
}

base::SequencedWorkerPool::SequenceToken
DomStorageWorkerPoolTaskRunner::IDtoToken(SequenceID id) const {
  if (id == PRIMARY_SEQUENCE)
    return primary_sequence_token_;
  DCHECK_EQ(COMMIT_SEQUENCE, id);
  return commit_sequence_token_;
}

// MockDomStorageTaskRunner

MockDomStorageTaskRunner::MockDomStorageTaskRunner(
    base::MessageLoopProxy* message_loop)
    : message_loop_(message_loop) {
}

MockDomStorageTaskRunner::~MockDomStorageTaskRunner() {
}

bool MockDomStorageTaskRunner::PostDelayedTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task,
    base::TimeDelta delay) {
  return message_loop_->PostTask(from_here, task);
}

bool MockDomStorageTaskRunner::PostShutdownBlockingTask(
    const tracked_objects::Location& from_here,
    SequenceID sequence_id,
    const base::Closure& task) {
  return message_loop_->PostTask(from_here, task);
}

bool MockDomStorageTaskRunner::IsRunningOnSequence(SequenceID) const {
  return message_loop_->RunsTasksOnCurrentThread();
}

}  // namespace dom_storage