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.
5 #include "remoting/host/chromoting_host_context.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "remoting/base/auto_thread.h"
9 #include "remoting/base/url_request_context_getter.h"
13 ChromotingHostContext::ChromotingHostContext(
14 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner
,
15 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner
,
16 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner
,
17 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner
,
18 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner
,
19 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner
,
20 scoped_refptr
<AutoThreadTaskRunner
> video_encode_task_runner
,
21 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
)
22 : ui_task_runner_(ui_task_runner
),
23 audio_task_runner_(audio_task_runner
),
24 file_task_runner_(file_task_runner
),
25 input_task_runner_(input_task_runner
),
26 network_task_runner_(network_task_runner
),
27 video_capture_task_runner_(video_capture_task_runner
),
28 video_encode_task_runner_(video_encode_task_runner
),
29 url_request_context_getter_(url_request_context_getter
) {
32 ChromotingHostContext::~ChromotingHostContext() {
35 scoped_ptr
<ChromotingHostContext
> ChromotingHostContext::Copy() {
36 return make_scoped_ptr(new ChromotingHostContext(
37 ui_task_runner_
, audio_task_runner_
, file_task_runner_
,
38 input_task_runner_
, network_task_runner_
, video_capture_task_runner_
,
39 video_encode_task_runner_
, url_request_context_getter_
));
42 scoped_refptr
<AutoThreadTaskRunner
>
43 ChromotingHostContext::audio_task_runner() {
44 return audio_task_runner_
;
47 scoped_refptr
<AutoThreadTaskRunner
>
48 ChromotingHostContext::file_task_runner() {
49 return file_task_runner_
;
52 scoped_refptr
<AutoThreadTaskRunner
>
53 ChromotingHostContext::input_task_runner() {
54 return input_task_runner_
;
57 scoped_refptr
<AutoThreadTaskRunner
>
58 ChromotingHostContext::network_task_runner() {
59 return network_task_runner_
;
62 scoped_refptr
<AutoThreadTaskRunner
>
63 ChromotingHostContext::ui_task_runner() {
64 return ui_task_runner_
;
67 scoped_refptr
<AutoThreadTaskRunner
>
68 ChromotingHostContext::video_capture_task_runner() {
69 return video_capture_task_runner_
;
72 scoped_refptr
<AutoThreadTaskRunner
>
73 ChromotingHostContext::video_encode_task_runner() {
74 return video_encode_task_runner_
;
77 scoped_refptr
<net::URLRequestContextGetter
>
78 ChromotingHostContext::url_request_context_getter() {
79 return url_request_context_getter_
;
82 scoped_ptr
<ChromotingHostContext
> ChromotingHostContext::Create(
83 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner
) {
85 // On Windows the AudioCapturer requires COM, so we run a single-threaded
86 // apartment, which requires a UI thread.
87 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner
=
88 AutoThread::CreateWithLoopAndComInitTypes(
89 "ChromotingAudioThread", ui_task_runner
, base::MessageLoop::TYPE_UI
,
90 AutoThread::COM_INIT_STA
);
91 #else // !defined(OS_WIN)
92 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner
=
93 AutoThread::CreateWithType("ChromotingAudioThread", ui_task_runner
,
94 base::MessageLoop::TYPE_IO
);
95 #endif // !defined(OS_WIN)
96 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner
=
97 AutoThread::CreateWithType("ChromotingFileThread", ui_task_runner
,
98 base::MessageLoop::TYPE_IO
);
99 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner
=
100 AutoThread::CreateWithType("ChromotingNetworkThread", ui_task_runner
,
101 base::MessageLoop::TYPE_IO
);
103 return make_scoped_ptr(new ChromotingHostContext(
107 AutoThread::CreateWithType("ChromotingInputThread", ui_task_runner
,
108 base::MessageLoop::TYPE_IO
),
110 AutoThread::Create("ChromotingCaptureThread", ui_task_runner
),
111 AutoThread::Create("ChromotingEncodeThread", ui_task_runner
),
113 new URLRequestContextGetter(network_task_runner
, file_task_runner
))));
116 #if defined(OS_CHROMEOS)
118 // Retrieves the task_runner from the browser thread with |id|.
119 scoped_refptr
<AutoThreadTaskRunner
> WrapBrowserThread(
120 content::BrowserThread::ID id
) {
121 // AutoThreadTaskRunner is a TaskRunner with the special property that it will
122 // continue to process tasks until no references remain, at least. The
123 // QuitClosure we usually pass does the simple thing of stopping the
124 // underlying TaskRunner. Since we are re-using the ui_task_runner of the
125 // browser thread, we cannot stop it explicitly. Therefore, base::DoNothing
126 // is passed in as the quit closure.
127 // TODO(kelvinp): Fix this (See crbug.com/428187).
128 return new AutoThreadTaskRunner(
129 content::BrowserThread::GetMessageLoopProxyForThread(id
).get(),
130 base::Bind(&base::DoNothing
));
136 scoped_ptr
<ChromotingHostContext
> ChromotingHostContext::CreateForChromeOS(
137 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
) {
138 DCHECK(url_request_context_getter
.get());
140 // Use BrowserThread::FILE as the joiner as it is the only browser-thread
141 // that allows blocking I/O, which is required by thread joining.
142 // TODO(kelvinp): Fix AutoThread so that it can be joinable on task runners
143 // that disallow I/O (crbug.com/428466).
144 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner
=
145 WrapBrowserThread(content::BrowserThread::FILE);
147 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner
=
148 WrapBrowserThread(content::BrowserThread::UI
);
150 return make_scoped_ptr(new ChromotingHostContext(
152 AutoThread::CreateWithType("ChromotingAudioThread", file_task_runner
,
153 base::MessageLoop::TYPE_IO
),
155 AutoThread::CreateWithType("ChromotingInputThread", file_task_runner
,
156 base::MessageLoop::TYPE_IO
),
157 WrapBrowserThread(content::BrowserThread::IO
), // network_task_runner
158 ui_task_runner
, // video_capture_task_runner
159 AutoThread::CreateWithType("ChromotingEncodeThread", file_task_runner
,
160 base::MessageLoop::TYPE_IO
),
161 url_request_context_getter
));
163 #endif // defined(OS_CHROMEOS)
165 } // namespace remoting