diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-24 18:08:46 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-24 18:08:46 +0000 |
commit | 40eb07fda546a23dcbd07e9e8a68d1d1d9c2ef9a (patch) | |
tree | 3a598ff7a461d42ba7cf6a3907ae3ad32bafbd12 /jingle/glue | |
parent | 66e2c28c1f940c38f54fc042b625c11d63375b48 (diff) | |
download | chromium_src-40eb07fda546a23dcbd07e9e8a68d1d1d9c2ef9a.zip chromium_src-40eb07fda546a23dcbd07e9e8a68d1d1d9c2ef9a.tar.gz chromium_src-40eb07fda546a23dcbd07e9e8a68d1d1d9c2ef9a.tar.bz2 |
Move TaskPump class from jingle/notifier/base to jingle/glue
BUG=137140
Review URL: https://chromiumcodereview.appspot.com/10809066
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148142 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'jingle/glue')
-rw-r--r-- | jingle/glue/mock_task.cc | 13 | ||||
-rw-r--r-- | jingle/glue/mock_task.h | 26 | ||||
-rw-r--r-- | jingle/glue/task_pump.cc | 59 | ||||
-rw-r--r-- | jingle/glue/task_pump.h | 42 | ||||
-rw-r--r-- | jingle/glue/task_pump_unittest.cc | 50 |
5 files changed, 190 insertions, 0 deletions
diff --git a/jingle/glue/mock_task.cc b/jingle/glue/mock_task.cc new file mode 100644 index 0000000..8894fbe --- /dev/null +++ b/jingle/glue/mock_task.cc @@ -0,0 +1,13 @@ +// 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 "jingle/glue/mock_task.h" + +namespace jingle_glue { + +MockTask::MockTask(TaskParent* parent) : talk_base::Task(parent) {} + +MockTask::~MockTask() {} + +} // namespace jingle_glue diff --git a/jingle/glue/mock_task.h b/jingle/glue/mock_task.h new file mode 100644 index 0000000..7fdaddf --- /dev/null +++ b/jingle/glue/mock_task.h @@ -0,0 +1,26 @@ +// 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. +// +// A mock of talk_base::Task. + +#ifndef JINGLE_GLUE_MOCK_TASK_H_ +#define JINGLE_GLUE_MOCK_TASK_H_ + +#include "testing/gmock/include/gmock/gmock.h" +#include "third_party/libjingle/source/talk/base/task.h" + +namespace jingle_glue { + +class MockTask : public talk_base::Task { + public: + MockTask(TaskParent* parent); + + virtual ~MockTask(); + + MOCK_METHOD0(ProcessStart, int()); +}; + +} // namespace jingle_glue + +#endif // JINGLE_GLUE_MOCK_TASK_H_ diff --git a/jingle/glue/task_pump.cc b/jingle/glue/task_pump.cc new file mode 100644 index 0000000..e2c305b --- /dev/null +++ b/jingle/glue/task_pump.cc @@ -0,0 +1,59 @@ +// 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 "base/bind.h" +#include "base/message_loop.h" +#include "jingle/glue/task_pump.h" + +namespace jingle_glue { + +TaskPump::TaskPump() + : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), + posted_wake_(false), + stopped_(false) {} + +TaskPump::~TaskPump() { + DCHECK(CalledOnValidThread()); +} + +void TaskPump::WakeTasks() { + DCHECK(CalledOnValidThread()); + if (!stopped_ && !posted_wake_) { + MessageLoop* current_message_loop = 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 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 diff --git a/jingle/glue/task_pump.h b/jingle/glue/task_pump.h new file mode 100644 index 0000000..17ce689 --- /dev/null +++ b/jingle/glue/task_pump.h @@ -0,0 +1,42 @@ +// 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. + +#ifndef JINGLE_GLUE_TASK_PUMP_H_ +#define JINGLE_GLUE_TASK_PUMP_H_ + +#include "base/compiler_specific.h" +#include "base/memory/weak_ptr.h" +#include "base/threading/non_thread_safe.h" +#include "third_party/libjingle/source/talk/base/taskrunner.h" + +namespace jingle_glue { + +// talk_base::TaskRunner implementation that works on chromium threads. +class TaskPump : public talk_base::TaskRunner, public base::NonThreadSafe { + public: + TaskPump(); + + virtual ~TaskPump(); + + // talk_base::TaskRunner implementation. + virtual void WakeTasks() OVERRIDE; + virtual int64 CurrentTime() OVERRIDE; + + // No tasks will be processed after this is called, even if + // WakeTasks() is called. + void Stop(); + + private: + void CheckAndRunTasks(); + + base::WeakPtrFactory<TaskPump> weak_factory_; + bool posted_wake_; + bool stopped_; + + DISALLOW_COPY_AND_ASSIGN(TaskPump); +}; + +} // namespace jingle_glue + +#endif // JINGLE_GLUE_TASK_PUMP_H_ diff --git a/jingle/glue/task_pump_unittest.cc b/jingle/glue/task_pump_unittest.cc new file mode 100644 index 0000000..416ce4d --- /dev/null +++ b/jingle/glue/task_pump_unittest.cc @@ -0,0 +1,50 @@ +// 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 "jingle/glue/task_pump.h" + +#include "base/message_loop.h" +#include "jingle/glue/mock_task.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace jingle_glue { + +namespace { + +using ::testing::Return; + +class TaskPumpTest : public testing::Test { + private: + MessageLoop message_loop_; +}; + +TEST_F(TaskPumpTest, Basic) { + TaskPump task_pump; + MockTask* task = new MockTask(&task_pump); + // We have to do this since the state enum is protected in + // talk_base::Task. + const int TASK_STATE_DONE = 2; + EXPECT_CALL(*task, ProcessStart()).WillOnce(Return(TASK_STATE_DONE)); + task->Start(); + + MessageLoop::current()->RunAllPending(); +} + +TEST_F(TaskPumpTest, Stop) { + TaskPump task_pump; + MockTask* task = new MockTask(&task_pump); + // We have to do this since the state enum is protected in + // talk_base::Task. + const int TASK_STATE_ERROR = 3; + ON_CALL(*task, ProcessStart()).WillByDefault(Return(TASK_STATE_ERROR)); + EXPECT_CALL(*task, ProcessStart()).Times(0); + task->Start(); + + task_pump.Stop(); + MessageLoop::current()->RunAllPending(); +} + +} // namespace + +} // namespace jingle_glue |