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.
4 
5 #ifndef REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_
6 #define REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "build/build_config.h"
13 
14 namespace base {
15 class SingleThreadTaskRunner;
16 }  // namespace base
17 
18 namespace net {
19 class URLRequestContextGetter;
20 }  // namespace net
21 
22 namespace network {
23 class SharedURLLoaderFactory;
24 class TransitionalURLLoaderFactoryOwner;
25 }  // namespace network
26 
27 namespace remoting {
28 
29 class AutoThreadTaskRunner;
30 
31 // A class that manages threads and running context for the chromoting host
32 // process.  This class is virtual only for testing purposes (see below).
33 class ChromotingHostContext {
34  public:
35   // Create threads and URLRequestContextGetter for use by a host.
36   // During shutdown the caller should tear-down the ChromotingHostContext and
37   // then continue to run until |ui_task_runner| is no longer referenced.
38   // nullptr is returned if any threads fail to start.
39   static std::unique_ptr<ChromotingHostContext> Create(
40       scoped_refptr<AutoThreadTaskRunner> ui_task_runner);
41 
42 #if defined(OS_CHROMEOS)
43   // Attaches task runners to the relevant browser threads for the chromoting
44   // host.  Must be called on the UI thread of the browser process.
45   // remoting::UrlRequestContextGetter returns ContainerURLRequestContext under
46   // the hood which spawns two new threads per instance.  Since
47   // ChromotingHostContext can be destroyed from any thread, as its owner
48   // (It2MeHost) is ref-counted, joining the created threads during shutdown
49   // violates the "Disallow IO" thread restrictions on some task runners (e.g.
50   // the IO Thread of the browser process).
51   // Instead, we re-use the |url_request_context_getter| in the browser process.
52   static std::unique_ptr<ChromotingHostContext> CreateForChromeOS(
53       scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
54       scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
55       scoped_refptr<base::SingleThreadTaskRunner> file_task_runner);
56 #endif  // defined(OS_CHROMEOS)
57 
58   ~ChromotingHostContext();
59 
60   std::unique_ptr<ChromotingHostContext> Copy();
61 
62   // Task runner for the thread that is used for the UI.
63   scoped_refptr<AutoThreadTaskRunner> ui_task_runner() const;
64 
65   // Task runner for the thread used for audio capture and encoding.
66   scoped_refptr<AutoThreadTaskRunner> audio_task_runner() const;
67 
68   // Task runner for the thread that is used for blocking file
69   // IO. This thread is used by the URLRequestContext to read proxy
70   // configuration and by NatConfig to read policy configs.
71   scoped_refptr<AutoThreadTaskRunner> file_task_runner() const;
72 
73   // Task runner for the thread that is used by the InputInjector.
74   //
75   // TODO(sergeyu): Do we need a separate thread for InputInjector?
76   // Can we use some other thread instead?
77   scoped_refptr<AutoThreadTaskRunner> input_task_runner() const;
78 
79   // Task runner for the thread used for network IO. This thread runs
80   // a libjingle message loop, and is the only thread on which
81   // libjingle code may be run.
82   scoped_refptr<AutoThreadTaskRunner> network_task_runner() const;
83 
84   // Task runner for the thread used by the ScreenRecorder to capture
85   // the screen.
86   scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner() const;
87 
88   // Task runner for the thread used to encode video streams.
89   scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner() const;
90 
91   scoped_refptr<net::URLRequestContextGetter> url_request_context_getter()
92       const;
93 
94   scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory();
95 
96  private:
97   ChromotingHostContext(
98       scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
99       scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
100       scoped_refptr<AutoThreadTaskRunner> file_task_runner,
101       scoped_refptr<AutoThreadTaskRunner> input_task_runner,
102       scoped_refptr<AutoThreadTaskRunner> network_task_runner,
103       scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
104       scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner,
105       scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
106 
107   // Caller-supplied UI thread. This is usually the application main thread.
108   scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
109 
110   // Thread for audio capture and encoding.
111   scoped_refptr<AutoThreadTaskRunner> audio_task_runner_;
112 
113   // Thread for I/O operations.
114   scoped_refptr<AutoThreadTaskRunner> file_task_runner_;
115 
116   // Thread for input injection.
117   scoped_refptr<AutoThreadTaskRunner> input_task_runner_;
118 
119   // Thread for network operations.
120   scoped_refptr<AutoThreadTaskRunner> network_task_runner_;
121 
122   // Thread for screen capture.
123   scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner_;
124 
125   // Thread for video encoding.
126   scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner_;
127 
128   // Serves URLRequestContexts that use the network and UI task runners.
129   scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
130 
131   // Makes a SharedURLLoaderFactory out of |url_request_context_getter_|
132   std::unique_ptr<network::TransitionalURLLoaderFactoryOwner>
133       url_loader_factory_owner_;
134 
135   DISALLOW_COPY_AND_ASSIGN(ChromotingHostContext);
136 };
137 
138 }  // namespace remoting
139 
140 #endif  // REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_
141