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 #ifndef REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_
6 #define REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
13 class URLRequestContextGetter
;
18 class AutoThreadTaskRunner
;
20 // A class that manages threads and running context for the chromoting host
21 // process. This class is virtual only for testing purposes (see below).
22 class ChromotingHostContext
{
24 // Create threads and URLRequestContextGetter for use by a host.
25 // During shutdown the caller should tear-down the ChromotingHostContext and
26 // then continue to run until |ui_task_runner| is no longer referenced.
27 // nullptr is returned if any threads fail to start.
28 static scoped_ptr
<ChromotingHostContext
> Create(
29 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner
);
31 #if defined(OS_CHROMEOS)
32 // Attaches task runners to the relevant browser threads for the chromoting
33 // host. Must be called on the UI thread of the browser process.
34 // remoting::UrlRequestContextGetter returns BasicURLRequestContext under
35 // the hood which spawns two new threads per instance. Since
36 // ChromotingHostContext can be destroyed from any thread, as its owner
37 // (It2MeHost) is ref-counted, joining the created threads during shutdown
38 // violates the "Disallow IO" thread restrictions on some task runners (e.g.
39 // the IO Thread of the browser process).
40 // Instead, we re-use the |url_request_context_getter| in the browser process.
41 static scoped_ptr
<ChromotingHostContext
> CreateForChromeOS(
42 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
);
43 #endif // defined(OS_CHROMEOS)
45 ~ChromotingHostContext();
47 scoped_ptr
<ChromotingHostContext
> Copy();
49 // Task runner for the thread that is used for the UI.
50 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner() const;
52 // Task runner for the thread used for audio capture and encoding.
53 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner() const;
55 // Task runner for the thread that is used for blocking file
56 // IO. This thread is used by the URLRequestContext to read proxy
57 // configuration and by NatConfig to read policy configs.
58 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner() const;
60 // Task runner for the thread that is used by the InputInjector.
62 // TODO(sergeyu): Do we need a separate thread for InputInjector?
63 // Can we use some other thread instead?
64 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner() const;
66 // Task runner for the thread used for network IO. This thread runs
67 // a libjingle message loop, and is the only thread on which
68 // libjingle code may be run.
69 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner() const;
71 // Task runner for the thread used by the ScreenRecorder to capture
73 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner() const;
75 // Task runner for the thread used to encode video streams.
76 scoped_refptr
<AutoThreadTaskRunner
> video_encode_task_runner() const;
78 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter()
82 ChromotingHostContext(
83 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner
,
84 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner
,
85 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner
,
86 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner
,
87 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner
,
88 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner
,
89 scoped_refptr
<AutoThreadTaskRunner
> video_encode_task_runner
,
90 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
);
92 // Caller-supplied UI thread. This is usually the application main thread.
93 scoped_refptr
<AutoThreadTaskRunner
> ui_task_runner_
;
95 // Thread for audio capture and encoding.
96 scoped_refptr
<AutoThreadTaskRunner
> audio_task_runner_
;
98 // Thread for I/O operations.
99 scoped_refptr
<AutoThreadTaskRunner
> file_task_runner_
;
101 // Thread for input injection.
102 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner_
;
104 // Thread for network operations.
105 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner_
;
107 // Thread for screen capture.
108 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner_
;
110 // Thread for video encoding.
111 scoped_refptr
<AutoThreadTaskRunner
> video_encode_task_runner_
;
113 // Serves URLRequestContexts that use the network and UI task runners.
114 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
116 DISALLOW_COPY_AND_ASSIGN(ChromotingHostContext
);
119 } // namespace remoting
121 #endif // REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_