summaryrefslogtreecommitdiffstats
path: root/jingle/glue/task_pump.cc
blob: f2c769d34b5e2f4804569e5ef71dd39ed007eb35 (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
// 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 <stdint.h>

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "jingle/glue/task_pump.h"

namespace jingle_glue {

TaskPump::TaskPump()
    : posted_wake_(false),
      stopped_(false),
      weak_factory_(this) {
}

TaskPump::~TaskPump() {
  DCHECK(CalledOnValidThread());
}

void TaskPump::WakeTasks() {
  DCHECK(CalledOnValidThread());
  if (!stopped_ && !posted_wake_) {
    base::MessageLoop* current_message_loop = base::MessageLoop::current();
    CHECK(current_message_loop);
    // Do the requested wake up.
    current_message_loop->PostTask(
        FROM_HERE,
        base::Bind(&TaskPump::CheckAndRunTasks, weak_factory_.GetWeakPtr()));
    posted_wake_ = true;
  }
}

int64_t TaskPump::CurrentTime() {
  DCHECK(CalledOnValidThread());
  // Only timeout tasks rely on this function.  Since we're not using
  // libjingle tasks for timeout, it's safe to return 0 here.
  return 0;
}

void TaskPump::Stop() {
  stopped_ = true;
}

void TaskPump::CheckAndRunTasks() {
  DCHECK(CalledOnValidThread());
  if (stopped_) {
    return;
  }
  posted_wake_ = false;
  // We shouldn't be using libjingle for timeout tasks, so we should
  // have no timeout tasks at all.

  // TODO(akalin): Add HasTimeoutTask() back in TaskRunner class and
  // uncomment this check.
  // DCHECK(!HasTimeoutTask())
  RunTasks();
}

}  // namespace jingle_glue