summaryrefslogtreecommitdiffstats
path: root/remoting/jingle_glue/jingle_thread.cc
blob: ee02a5cc579574159538a12bb900dec8091943fa (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
// Copyright (c) 2010 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 "remoting/jingle_glue/jingle_thread.h"

#include "base/logging.h"
#include "base/message_loop.h"
#include "talk/base/ssladapter.h"

namespace remoting {

const int kRunTasksMessageId = 1;

TaskPump::TaskPump() {
}

void TaskPump::WakeTasks() {
  talk_base::Thread::Current()->Post(this);
}

int64 TaskPump::CurrentTime() {
  return static_cast<int64>(talk_base::Time());
}

void TaskPump::OnMessage(talk_base::Message* pmsg) {
  RunTasks();
}

JingleThread::JingleThread()
    : task_pump_(NULL),
      started_event_(true, false),
      message_loop_(NULL) { }

JingleThread::~JingleThread() { }

void JingleThread::Start() {
  Thread::Start();
  started_event_.Wait();
}

void JingleThread::Run() {
  LOG(INFO) << "Started Jingle thread.";

  MessageLoopForIO message_loop;
  message_loop_ = &message_loop;

  TaskPump task_pump;
  task_pump_ = &task_pump;

  // Signal after we've initialized |message_loop_| and |task_pump_|.
  started_event_.Signal();

  Post(this, kRunTasksMessageId);

  Thread::Run();

  message_loop.RunAllPending();

  task_pump_ = NULL;
  message_loop_ = NULL;

  LOG(INFO) << "Jingle thread finished.";
}

// This method is called every 20ms to process  tasks from |message_loop_|
// on this thread.
// TODO(sergeyu): Remove it when JingleThread moved to Chromium's base::Thread.
void JingleThread::PumpAuxiliaryLoops() {
  MessageLoop::current()->RunAllPending();

  // Schedule next execution 20ms from now.
  PostDelayed(20, this, kRunTasksMessageId);
}

void JingleThread::OnMessage(talk_base::Message* msg) {
  PumpAuxiliaryLoops();
}

}  // namespace remoting