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