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"
8 #include "base/threading/thread_restrictions.h"
9 #include "remoting/base/auto_thread.h"
10 #include "remoting/base/url_request_context_getter.h"
16 void DisallowBlockingOperations() {
17 base::ThreadRestrictions::SetIOAllowed(false);
18 base::ThreadRestrictions::DisallowWaiting();
23 ChromotingHostContext::ChromotingHostContext(
24 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner
,
25 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner
,
26 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner
,
27 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner
,
28 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner
,
29 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner
,
30 scoped_refptr
<AutoThreadTaskRunner
> video_encode_task_runner
,
31 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
)
32 : ui_task_runner_(ui_task_runner
),
33 audio_task_runner_(audio_task_runner
),
34 file_task_runner_(file_task_runner
),
35 input_task_runner_(input_task_runner
),
36 network_task_runner_(network_task_runner
),
37 video_capture_task_runner_(video_capture_task_runner
),
38 video_encode_task_runner_(video_encode_task_runner
),
39 url_request_context_getter_(url_request_context_getter
) {
42 ChromotingHostContext::~ChromotingHostContext() {
45 scoped_ptr
<ChromotingHostContext
> ChromotingHostContext::Copy() {
46 return make_scoped_ptr(new ChromotingHostContext(
47 ui_task_runner_
, audio_task_runner_
, file_task_runner_
,
48 input_task_runner_
, network_task_runner_
, video_capture_task_runner_
,
49 video_encode_task_runner_
, url_request_context_getter_
));
52 scoped_refptr
<AutoThreadTaskRunner
> ChromotingHostContext::audio_task_runner()
54 return audio_task_runner_
;
57 scoped_refptr
<AutoThreadTaskRunner
> ChromotingHostContext::file_task_runner()
59 return file_task_runner_
;
62 scoped_refptr
<AutoThreadTaskRunner
> ChromotingHostContext::input_task_runner()
64 return input_task_runner_
;
67 scoped_refptr
<AutoThreadTaskRunner
> ChromotingHostContext::network_task_runner()
69 return network_task_runner_
;
72 scoped_refptr
<AutoThreadTaskRunner
> ChromotingHostContext::ui_task_runner()
74 return ui_task_runner_
;
77 scoped_refptr
<AutoThreadTaskRunner
>
78 ChromotingHostContext::video_capture_task_runner() const {
79 return video_capture_task_runner_
;
82 scoped_refptr
<AutoThreadTaskRunner
>
83 ChromotingHostContext::video_encode_task_runner() const {
84 return video_encode_task_runner_
;
87 scoped_refptr
<net::URLRequestContextGetter
>
88 ChromotingHostContext::url_request_context_getter() const {
89 return url_request_context_getter_
;
92 scoped_ptr
<ChromotingHostContext
> ChromotingHostContext::Create(
93 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner
) {
95 // On Windows the AudioCapturer requires COM, so we run a single-threaded
96 // apartment, which requires a UI thread.
97 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner
=
98 AutoThread::CreateWithLoopAndComInitTypes(
99 "ChromotingAudioThread", ui_task_runner
, base::MessageLoop::TYPE_UI
,
100 AutoThread::COM_INIT_STA
);
101 #else // !defined(OS_WIN)
102 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner
=
103 AutoThread::CreateWithType("ChromotingAudioThread", ui_task_runner
,
104 base::MessageLoop::TYPE_IO
);
105 #endif // !defined(OS_WIN)
106 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner
=
107 AutoThread::CreateWithType("ChromotingFileThread", ui_task_runner
,
108 base::MessageLoop::TYPE_IO
);
110 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner
=
111 AutoThread::CreateWithType("ChromotingNetworkThread", ui_task_runner
,
112 base::MessageLoop::TYPE_IO
);
113 network_task_runner
->PostTask(FROM_HERE
,
114 base::Bind(&DisallowBlockingOperations
));
116 return make_scoped_ptr(new ChromotingHostContext(
120 AutoThread::CreateWithType("ChromotingInputThread", ui_task_runner
,
121 base::MessageLoop::TYPE_IO
),
123 AutoThread::Create("ChromotingCaptureThread", ui_task_runner
),
124 AutoThread::Create("ChromotingEncodeThread", ui_task_runner
),
126 new URLRequestContextGetter(network_task_runner
, file_task_runner
))));
129 #if defined(OS_CHROMEOS)
132 scoped_ptr
<ChromotingHostContext
> ChromotingHostContext::CreateForChromeOS(
133 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
,
134 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
135 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
,
136 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner
) {
137 DCHECK(url_request_context_getter
.get());
140 // AutoThreadTaskRunner is a TaskRunner with the special property that it will
141 // continue to process tasks until no references remain, at least. The
142 // QuitClosure we usually pass does the simple thing of stopping the
143 // underlying TaskRunner. Since we are re-using browser's threads, we cannot
144 // stop them explicitly. Therefore, base::DoNothing is passed in as the quit
146 scoped_refptr
<AutoThreadTaskRunner
> io_auto_task_runner
=
147 new AutoThreadTaskRunner(io_task_runner
, base::Bind(&base::DoNothing
));
148 scoped_refptr
<AutoThreadTaskRunner
> file_auto_task_runner
=
149 new AutoThreadTaskRunner(file_task_runner
, base::Bind(&base::DoNothing
));
150 scoped_refptr
<AutoThreadTaskRunner
> ui_auto_task_runner
=
151 new AutoThreadTaskRunner(ui_task_runner
, base::Bind(&base::DoNothing
));
153 // Use browser's file thread as the joiner as it is the only browser-thread
154 // that allows blocking I/O, which is required by thread joining.
155 return make_scoped_ptr(new ChromotingHostContext(
157 AutoThread::Create("ChromotingAudioThread", file_auto_task_runner
),
158 file_auto_task_runner
,
159 ui_auto_task_runner
, // input_task_runner
160 io_auto_task_runner
, // network_task_runner
161 ui_auto_task_runner
, // video_capture_task_runner
162 AutoThread::Create("ChromotingEncodeThread", file_auto_task_runner
),
163 url_request_context_getter
));
165 #endif // defined(OS_CHROMEOS)
167 } // namespace remoting