[email protected] | f47f3bb59 | 2012-03-06 02:40:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4d37c874 | 2010-07-20 00:34:57 | [diff] [blame] | 5 | #ifndef REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_ |
| 6 | #define REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_ |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 7 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | 3c8cfbe7 | 2012-07-03 00:24:15 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | df338b7d | 2012-09-05 19:58:36 | [diff] [blame] | 13 | |
sergeyu | f87c35de | 2015-02-18 02:19:07 | [diff] [blame] | 14 | namespace base { |
| 15 | class SingleThreadTaskRunner; |
| 16 | } // namespace base |
| 17 | |
[email protected] | 95178556 | 2012-05-01 00:40:50 | [diff] [blame] | 18 | namespace net { |
[email protected] | e6ed017 | 2012-04-17 19:11:48 | [diff] [blame] | 19 | class URLRequestContextGetter; |
[email protected] | 95178556 | 2012-05-01 00:40:50 | [diff] [blame] | 20 | } // namespace net |
| 21 | |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 22 | namespace network { |
| 23 | class SharedURLLoaderFactory; |
| 24 | class TransitionalURLLoaderFactoryOwner; |
| 25 | } // namespace network |
| 26 | |
[email protected] | 95178556 | 2012-05-01 00:40:50 | [diff] [blame] | 27 | namespace remoting { |
[email protected] | b592165 | 2012-10-02 21:26:39 | [diff] [blame] | 28 | |
[email protected] | ef948e6 | 2012-09-04 22:27:43 | [diff] [blame] | 29 | class AutoThreadTaskRunner; |
[email protected] | e6ed017 | 2012-04-17 19:11:48 | [diff] [blame] | 30 | |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 31 | // A class that manages threads and running context for the chromoting host |
[email protected] | 1a6cdf4 | 2011-05-18 07:41:01 | [diff] [blame] | 32 | // process. This class is virtual only for testing purposes (see below). |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 33 | class ChromotingHostContext { |
| 34 | public: |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 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. |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 38 | // nullptr is returned if any threads fail to start. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 39 | static std::unique_ptr<ChromotingHostContext> Create( |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 40 | scoped_refptr<AutoThreadTaskRunner> ui_task_runner); |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 41 | |
kelvinp | 561074cf | 2014-10-30 21:46:16 | [diff] [blame] | 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. |
wjmaclean | 7f36a61b | 2015-08-12 07:31:29 | [diff] [blame] | 45 | // remoting::UrlRequestContextGetter returns ContainerURLRequestContext under |
kelvinp | 561074cf | 2014-10-30 21:46:16 | [diff] [blame] | 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. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 52 | static std::unique_ptr<ChromotingHostContext> CreateForChromeOS( |
sergeyu | f87c35de | 2015-02-18 02:19:07 | [diff] [blame] | 53 | scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 54 | scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
Scott Violet | 9778dcd | 2019-06-20 21:59:37 | [diff] [blame] | 55 | scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
kelvinp | 561074cf | 2014-10-30 21:46:16 | [diff] [blame] | 56 | #endif // defined(OS_CHROMEOS) |
| 57 | |
| 58 | ~ChromotingHostContext(); |
| 59 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 60 | std::unique_ptr<ChromotingHostContext> Copy(); |
kelvinp | 561074cf | 2014-10-30 21:46:16 | [diff] [blame] | 61 | |
| 62 | // Task runner for the thread that is used for the UI. |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 63 | scoped_refptr<AutoThreadTaskRunner> ui_task_runner() const; |
kelvinp | 561074cf | 2014-10-30 21:46:16 | [diff] [blame] | 64 | |
[email protected] | ef948e6 | 2012-09-04 22:27:43 | [diff] [blame] | 65 | // Task runner for the thread used for audio capture and encoding. |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 66 | scoped_refptr<AutoThreadTaskRunner> audio_task_runner() const; |
[email protected] | ef948e6 | 2012-09-04 22:27:43 | [diff] [blame] | 67 | |
[email protected] | 3c8cfbe7 | 2012-07-03 00:24:15 | [diff] [blame] | 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. |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 71 | scoped_refptr<AutoThreadTaskRunner> file_task_runner() const; |
[email protected] | 3c8cfbe7 | 2012-07-03 00:24:15 | [diff] [blame] | 72 | |
[email protected] | b0b72f11 | 2013-03-24 03:42:42 | [diff] [blame] | 73 | // Task runner for the thread that is used by the InputInjector. |
[email protected] | b592165 | 2012-10-02 21:26:39 | [diff] [blame] | 74 | // |
[email protected] | b0b72f11 | 2013-03-24 03:42:42 | [diff] [blame] | 75 | // TODO(sergeyu): Do we need a separate thread for InputInjector? |
[email protected] | b592165 | 2012-10-02 21:26:39 | [diff] [blame] | 76 | // Can we use some other thread instead? |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 77 | scoped_refptr<AutoThreadTaskRunner> input_task_runner() const; |
[email protected] | b592165 | 2012-10-02 21:26:39 | [diff] [blame] | 78 | |
[email protected] | ef948e6 | 2012-09-04 22:27:43 | [diff] [blame] | 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. |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 82 | scoped_refptr<AutoThreadTaskRunner> network_task_runner() const; |
[email protected] | ef948e6 | 2012-09-04 22:27:43 | [diff] [blame] | 83 | |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 84 | // Task runner for the thread used by the ScreenRecorder to capture |
| 85 | // the screen. |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 86 | scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner() const; |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 87 | |
| 88 | // Task runner for the thread used to encode video streams. |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 89 | scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner() const; |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 90 | |
lukasza | 806efecb | 2015-01-12 19:01:07 | [diff] [blame] | 91 | scoped_refptr<net::URLRequestContextGetter> url_request_context_getter() |
| 92 | const; |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 93 | |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 94 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory(); |
| 95 | |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 96 | private: |
kelvinp | 561074cf | 2014-10-30 21:46:16 | [diff] [blame] | 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, |
Scott Violet | 9778dcd | 2019-06-20 21:59:37 | [diff] [blame] | 105 | scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); |
kelvinp | 561074cf | 2014-10-30 21:46:16 | [diff] [blame] | 106 | |
| 107 | // Caller-supplied UI thread. This is usually the application main thread. |
| 108 | scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 109 | |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 110 | // Thread for audio capture and encoding. |
| 111 | scoped_refptr<AutoThreadTaskRunner> audio_task_runner_; |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 112 | |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 113 | // Thread for I/O operations. |
| 114 | scoped_refptr<AutoThreadTaskRunner> file_task_runner_; |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 115 | |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 116 | // Thread for input injection. |
| 117 | scoped_refptr<AutoThreadTaskRunner> input_task_runner_; |
[email protected] | ef948e6 | 2012-09-04 22:27:43 | [diff] [blame] | 118 | |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 119 | // Thread for network operations. |
| 120 | scoped_refptr<AutoThreadTaskRunner> network_task_runner_; |
[email protected] | cec4d7a | 2012-04-17 05:26:29 | [diff] [blame] | 121 | |
[email protected] | 32debfb | 2012-12-05 23:40:51 | [diff] [blame] | 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. |
[email protected] | 95178556 | 2012-05-01 00:40:50 | [diff] [blame] | 129 | scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
[email protected] | e6ed017 | 2012-04-17 19:11:48 | [diff] [blame] | 130 | |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 131 | // Makes a SharedURLLoaderFactory out of |url_request_context_getter_| |
| 132 | std::unique_ptr<network::TransitionalURLLoaderFactoryOwner> |
| 133 | url_loader_factory_owner_; |
| 134 | |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(ChromotingHostContext); |
| 136 | }; |
| 137 | |
| 138 | } // namespace remoting |
| 139 | |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 140 | #endif // REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_ |