summaryrefslogtreecommitdiffstats
path: root/remoting/host/chromoting_host_context.cc
blob: c3699ca7f8d5d45d5b9cadd247917e608969dab2 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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 "remoting/host/chromoting_host_context.h"

#include <string>

#include "base/bind.h"
#include "base/threading/thread.h"
#include "remoting/base/auto_thread_task_runner.h"
#include "remoting/host/url_request_context.h"

namespace remoting {

ChromotingHostContext::ChromotingHostContext(
    scoped_refptr<AutoThreadTaskRunner> ui_task_runner)
    : audio_thread_("ChromotingAudioThread"),
      capture_thread_("ChromotingCaptureThread"),
      encode_thread_("ChromotingEncodeThread"),
      file_thread_("ChromotingFileIOThread"),
      input_thread_("ChromotingInputThread"),
      network_thread_("ChromotingNetworkThread"),
      ui_task_runner_(ui_task_runner) {
}

ChromotingHostContext::~ChromotingHostContext() {
}

void ChromotingHostContext::ReleaseTaskRunners() {
  url_request_context_getter_ = NULL;
  audio_task_runner_ = NULL;
  capture_task_runner_ = NULL;
  encode_task_runner_ = NULL;
  file_task_runner_ = NULL;
  input_task_runner_ = NULL;
  network_task_runner_ = NULL;
  ui_task_runner_ = NULL;
}

bool ChromotingHostContext::Start() {
  // Start all the threads.
  base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0);
  bool started = capture_thread_.Start() && encode_thread_.Start() &&
      audio_thread_.StartWithOptions(io_thread_options) &&
      file_thread_.StartWithOptions(io_thread_options) &&
      input_thread_.StartWithOptions(io_thread_options) &&
      network_thread_.StartWithOptions(io_thread_options);
  if (!started)
    return false;

  // Wrap worker threads with |AutoThreadTaskRunner| and have them reference
  // the main thread via |ui_task_runner_|, to ensure that it remain active to
  // Stop() them when no references remain.
  audio_task_runner_ =
      new AutoThreadTaskRunner(audio_thread_.message_loop_proxy(),
                               ui_task_runner_);
  capture_task_runner_ =
      new AutoThreadTaskRunner(capture_thread_.message_loop_proxy(),
                               ui_task_runner_);
  encode_task_runner_ =
      new AutoThreadTaskRunner(encode_thread_.message_loop_proxy(),
                               ui_task_runner_);
  file_task_runner_ =
      new AutoThreadTaskRunner(file_thread_.message_loop_proxy(),
                               ui_task_runner_);
  input_task_runner_ =
      new AutoThreadTaskRunner(input_thread_.message_loop_proxy(),
                               ui_task_runner_);
  network_task_runner_ =
      new AutoThreadTaskRunner(network_thread_.message_loop_proxy(),
                               ui_task_runner_);

  url_request_context_getter_ = new URLRequestContextGetter(
      ui_task_runner(), network_task_runner());
  return true;
}

base::SingleThreadTaskRunner* ChromotingHostContext::audio_task_runner() {
  return audio_task_runner_;
}

base::SingleThreadTaskRunner* ChromotingHostContext::capture_task_runner() {
  return capture_task_runner_;
}

base::SingleThreadTaskRunner* ChromotingHostContext::encode_task_runner() {
  return encode_task_runner_;
}

base::SingleThreadTaskRunner* ChromotingHostContext::file_task_runner() {
  return file_task_runner_;
}

base::SingleThreadTaskRunner* ChromotingHostContext::input_task_runner() {
  return input_task_runner_;
}

base::SingleThreadTaskRunner* ChromotingHostContext::network_task_runner() {
  return network_task_runner_;
}

base::SingleThreadTaskRunner* ChromotingHostContext::ui_task_runner() {
  return ui_task_runner_;
}

const scoped_refptr<net::URLRequestContextGetter>&
ChromotingHostContext::url_request_context_getter() {
  DCHECK(url_request_context_getter_.get());
  return url_request_context_getter_;
}

}  // namespace remoting