chromoting_host_context.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include "remoting/host/chromoting_host_context.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "base/message_loop/message_pump_type.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "base/threading/thread_restrictions.h"
  11. #include "build/build_config.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "remoting/base/auto_thread.h"
  14. #include "remoting/base/url_request_context_getter.h"
  15. #include "services/network/public/cpp/shared_url_loader_factory.h"
  16. #include "services/network/transitional_url_loader_factory_owner.h"
  17. namespace remoting {
  18. namespace {
  19. void DisallowBlockingOperations() {
  20. base::DisallowBlocking();
  21. // TODO(crbug.com/793486): Re-enable after the underlying issue is fixed.
  22. // base::DisallowBaseSyncPrimitives();
  23. }
  24. } // namespace
  25. ChromotingHostContext::ChromotingHostContext(
  26. scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
  27. scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
  28. scoped_refptr<AutoThreadTaskRunner> file_task_runner,
  29. scoped_refptr<AutoThreadTaskRunner> input_task_runner,
  30. scoped_refptr<AutoThreadTaskRunner> network_task_runner,
  31. scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
  32. scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner,
  33. scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
  34. : ui_task_runner_(ui_task_runner),
  35. audio_task_runner_(audio_task_runner),
  36. file_task_runner_(file_task_runner),
  37. input_task_runner_(input_task_runner),
  38. network_task_runner_(network_task_runner),
  39. video_capture_task_runner_(video_capture_task_runner),
  40. video_encode_task_runner_(video_encode_task_runner),
  41. url_request_context_getter_(url_request_context_getter) {}
  42. ChromotingHostContext::~ChromotingHostContext() {
  43. if (url_loader_factory_owner_)
  44. network_task_runner_->DeleteSoon(FROM_HERE,
  45. url_loader_factory_owner_.release());
  46. }
  47. std::unique_ptr<ChromotingHostContext> ChromotingHostContext::Copy() {
  48. return base::WrapUnique(new ChromotingHostContext(
  49. ui_task_runner_, audio_task_runner_, file_task_runner_,
  50. input_task_runner_, network_task_runner_, video_capture_task_runner_,
  51. video_encode_task_runner_, url_request_context_getter_));
  52. }
  53. scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::audio_task_runner()
  54. const {
  55. return audio_task_runner_;
  56. }
  57. scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::file_task_runner()
  58. const {
  59. return file_task_runner_;
  60. }
  61. scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::input_task_runner()
  62. const {
  63. return input_task_runner_;
  64. }
  65. scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::network_task_runner()
  66. const {
  67. return network_task_runner_;
  68. }
  69. scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::ui_task_runner()
  70. const {
  71. return ui_task_runner_;
  72. }
  73. scoped_refptr<AutoThreadTaskRunner>
  74. ChromotingHostContext::video_capture_task_runner() const {
  75. return video_capture_task_runner_;
  76. }
  77. scoped_refptr<AutoThreadTaskRunner>
  78. ChromotingHostContext::video_encode_task_runner() const {
  79. return video_encode_task_runner_;
  80. }
  81. scoped_refptr<net::URLRequestContextGetter>
  82. ChromotingHostContext::url_request_context_getter() const {
  83. return url_request_context_getter_;
  84. }
  85. scoped_refptr<network::SharedURLLoaderFactory>
  86. ChromotingHostContext::url_loader_factory() {
  87. DCHECK(network_task_runner_->RunsTasksInCurrentSequence());
  88. if (!url_loader_factory_owner_) {
  89. url_loader_factory_owner_ =
  90. std::make_unique<network::TransitionalURLLoaderFactoryOwner>(
  91. url_request_context_getter_);
  92. }
  93. return url_loader_factory_owner_->GetURLLoaderFactory();
  94. }
  95. policy::ManagementService* ChromotingHostContext::management_service() {
  96. return policy::PlatformManagementService::GetInstance();
  97. }
  98. std::unique_ptr<ChromotingHostContext> ChromotingHostContext::Create(
  99. scoped_refptr<AutoThreadTaskRunner> ui_task_runner) {
  100. #if BUILDFLAG(IS_WIN)
  101. // On Windows the AudioCapturer requires COM, so we run a single-threaded
  102. // apartment, which requires a UI thread.
  103. scoped_refptr<AutoThreadTaskRunner> audio_task_runner =
  104. AutoThread::CreateWithLoopAndComInitTypes(
  105. "ChromotingAudioThread", ui_task_runner, base::MessagePumpType::UI,
  106. AutoThread::COM_INIT_STA);
  107. #else // !BUILDFLAG(IS_WIN)
  108. scoped_refptr<AutoThreadTaskRunner> audio_task_runner =
  109. AutoThread::CreateWithType("ChromotingAudioThread", ui_task_runner,
  110. base::MessagePumpType::IO);
  111. #endif // !BUILDFLAG(IS_WIN)
  112. scoped_refptr<AutoThreadTaskRunner> file_task_runner =
  113. AutoThread::CreateWithType("ChromotingFileThread", ui_task_runner,
  114. base::MessagePumpType::IO);
  115. scoped_refptr<AutoThreadTaskRunner> network_task_runner =
  116. AutoThread::CreateWithType("ChromotingNetworkThread", ui_task_runner,
  117. base::MessagePumpType::IO);
  118. network_task_runner->PostTask(FROM_HERE,
  119. base::BindOnce(&DisallowBlockingOperations));
  120. // InputInjectorX11 requires an X11EventSource, which can only be created
  121. // on a UI thread.
  122. scoped_refptr<AutoThreadTaskRunner> input_task_runner =
  123. AutoThread::CreateWithType("ChromotingInputThread", ui_task_runner,
  124. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  125. base::MessagePumpType::UI);
  126. #else
  127. base::MessagePumpType::IO);
  128. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  129. return base::WrapUnique(new ChromotingHostContext(
  130. ui_task_runner, audio_task_runner, file_task_runner, input_task_runner,
  131. network_task_runner,
  132. #if BUILDFLAG(IS_APPLE)
  133. // Mac requires a UI thread for the capturer.
  134. AutoThread::CreateWithType("ChromotingCaptureThread", ui_task_runner,
  135. base::MessagePumpType::UI),
  136. #else
  137. AutoThread::Create("ChromotingCaptureThread", ui_task_runner),
  138. #endif
  139. AutoThread::Create("ChromotingEncodeThread", ui_task_runner),
  140. base::MakeRefCounted<URLRequestContextGetter>(network_task_runner)));
  141. }
  142. #if BUILDFLAG(IS_CHROMEOS_ASH)
  143. // static
  144. std::unique_ptr<ChromotingHostContext> ChromotingHostContext::CreateForChromeOS(
  145. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
  146. scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
  147. scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) {
  148. // AutoThreadTaskRunner is a TaskRunner with the special property that it will
  149. // continue to process tasks until no references remain, at least. The
  150. // QuitClosure we usually pass does the simple thing of stopping the
  151. // underlying TaskRunner. Since we are re-using browser's threads, we cannot
  152. // stop them explicitly. Therefore, base::DoNothing is passed in as the quit
  153. // closure.
  154. scoped_refptr<AutoThreadTaskRunner> io_auto_task_runner =
  155. new AutoThreadTaskRunner(io_task_runner, base::DoNothing());
  156. scoped_refptr<AutoThreadTaskRunner> file_auto_task_runner =
  157. new AutoThreadTaskRunner(file_task_runner, base::DoNothing());
  158. scoped_refptr<AutoThreadTaskRunner> ui_auto_task_runner =
  159. new AutoThreadTaskRunner(ui_task_runner, base::DoNothing());
  160. // Use browser's file thread as the joiner as it is the only browser-thread
  161. // that allows blocking I/O, which is required by thread joining.
  162. return base::WrapUnique(new ChromotingHostContext(
  163. ui_auto_task_runner,
  164. AutoThread::Create("ChromotingAudioThread", file_auto_task_runner),
  165. file_auto_task_runner,
  166. ui_auto_task_runner, // input_task_runner
  167. io_auto_task_runner, // network_task_runner
  168. ui_auto_task_runner, // video_capture_task_runner
  169. AutoThread::Create("ChromotingEncodeThread", file_auto_task_runner),
  170. base::MakeRefCounted<URLRequestContextGetter>(io_auto_task_runner)));
  171. }
  172. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  173. } // namespace remoting