Home | History | Annotate | Download | only in glue
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/bind.h"
      6 #include "base/message_loop/message_loop.h"
      7 #include "jingle/glue/task_pump.h"
      8 
      9 namespace jingle_glue {
     10 
     11 TaskPump::TaskPump()
     12     : weak_factory_(this),
     13       posted_wake_(false),
     14       stopped_(false) {}
     15 
     16 TaskPump::~TaskPump() {
     17   DCHECK(CalledOnValidThread());
     18 }
     19 
     20 void TaskPump::WakeTasks() {
     21   DCHECK(CalledOnValidThread());
     22   if (!stopped_ && !posted_wake_) {
     23     base::MessageLoop* current_message_loop = base::MessageLoop::current();
     24     CHECK(current_message_loop);
     25     // Do the requested wake up.
     26     current_message_loop->PostTask(
     27         FROM_HERE,
     28         base::Bind(&TaskPump::CheckAndRunTasks, weak_factory_.GetWeakPtr()));
     29     posted_wake_ = true;
     30   }
     31 }
     32 
     33 int64 TaskPump::CurrentTime() {
     34   DCHECK(CalledOnValidThread());
     35   // Only timeout tasks rely on this function.  Since we're not using
     36   // libjingle tasks for timeout, it's safe to return 0 here.
     37   return 0;
     38 }
     39 
     40 void TaskPump::Stop() {
     41   stopped_ = true;
     42 }
     43 
     44 void TaskPump::CheckAndRunTasks() {
     45   DCHECK(CalledOnValidThread());
     46   if (stopped_) {
     47     return;
     48   }
     49   posted_wake_ = false;
     50   // We shouldn't be using libjingle for timeout tasks, so we should
     51   // have no timeout tasks at all.
     52 
     53   // TODO(akalin): Add HasTimeoutTask() back in TaskRunner class and
     54   // uncomment this check.
     55   // DCHECK(!HasTimeoutTask())
     56   RunTasks();
     57 }
     58 
     59 }  // namespace jingle_glue
     60