| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/chromoting_host_context.h" | 5 #include "remoting/host/chromoting_host_context.h" |
| 6 | 6 |
| 7 #include "content/public/browser/browser_thread.h" | 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 8 #include "remoting/base/auto_thread.h" | 10 #include "remoting/base/auto_thread.h" |
| 9 #include "remoting/base/url_request_context_getter.h" | 11 #include "remoting/base/url_request_context_getter.h" |
| 10 | 12 |
| 11 namespace remoting { | 13 namespace remoting { |
| 12 | 14 |
| 13 ChromotingHostContext::ChromotingHostContext( | 15 ChromotingHostContext::ChromotingHostContext( |
| 14 scoped_refptr<AutoThreadTaskRunner> ui_task_runner, | 16 AutoThreadTaskRunner* ui_task_runner) |
| 15 scoped_refptr<AutoThreadTaskRunner> audio_task_runner, | 17 : ui_task_runner_(ui_task_runner) { |
| 16 scoped_refptr<AutoThreadTaskRunner> file_task_runner, | 18 #if defined(OS_WIN) |
| 17 scoped_refptr<AutoThreadTaskRunner> input_task_runner, | 19 // On Windows the AudioCapturer requires COM, so we run a single-threaded |
| 18 scoped_refptr<AutoThreadTaskRunner> network_task_runner, | 20 // apartment, which requires a UI thread. |
| 19 scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner, | 21 audio_task_runner_ = |
| 20 scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner, | 22 AutoThread::CreateWithLoopAndComInitTypes("ChromotingAudioThread", |
| 21 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) | 23 ui_task_runner_, |
| 22 : ui_task_runner_(ui_task_runner), | 24 base::MessageLoop::TYPE_UI, |
| 23 audio_task_runner_(audio_task_runner), | 25 AutoThread::COM_INIT_STA); |
| 24 file_task_runner_(file_task_runner), | 26 #else // !defined(OS_WIN) |
| 25 input_task_runner_(input_task_runner), | 27 audio_task_runner_ = AutoThread::CreateWithType( |
| 26 network_task_runner_(network_task_runner), | 28 "ChromotingAudioThread", ui_task_runner_, base::MessageLoop::TYPE_IO); |
| 27 video_capture_task_runner_(video_capture_task_runner), | 29 #endif // !defined(OS_WIN) |
| 28 video_encode_task_runner_(video_encode_task_runner), | 30 |
| 29 url_request_context_getter_(url_request_context_getter) { | 31 file_task_runner_ = AutoThread::CreateWithType( |
| 32 "ChromotingFileThread", ui_task_runner_, base::MessageLoop::TYPE_IO); |
| 33 input_task_runner_ = AutoThread::CreateWithType( |
| 34 "ChromotingInputThread", ui_task_runner_, base::MessageLoop::TYPE_IO); |
| 35 network_task_runner_ = AutoThread::CreateWithType( |
| 36 "ChromotingNetworkThread", ui_task_runner_, base::MessageLoop::TYPE_IO); |
| 37 video_capture_task_runner_ = |
| 38 AutoThread::Create("ChromotingCaptureThread", ui_task_runner_); |
| 39 video_encode_task_runner_ = AutoThread::Create( |
| 40 "ChromotingEncodeThread", ui_task_runner_); |
| 41 |
| 42 url_request_context_getter_ = new URLRequestContextGetter( |
| 43 network_task_runner_, file_task_runner_); |
| 30 } | 44 } |
| 31 | 45 |
| 32 ChromotingHostContext::~ChromotingHostContext() { | 46 ChromotingHostContext::~ChromotingHostContext() { |
| 33 } | 47 } |
| 34 | 48 |
| 35 scoped_ptr<ChromotingHostContext> ChromotingHostContext::Copy() { | 49 scoped_ptr<ChromotingHostContext> ChromotingHostContext::Create( |
| 36 return make_scoped_ptr(new ChromotingHostContext( | 50 scoped_refptr<AutoThreadTaskRunner> ui_task_runner) { |
| 37 ui_task_runner_, audio_task_runner_, file_task_runner_, | 51 DCHECK(ui_task_runner->BelongsToCurrentThread()); |
| 38 input_task_runner_, network_task_runner_, video_capture_task_runner_, | 52 |
| 39 video_encode_task_runner_, url_request_context_getter_)); | 53 scoped_ptr<ChromotingHostContext> context( |
| 54 new ChromotingHostContext(ui_task_runner.get())); |
| 55 if (!context->audio_task_runner_.get() || !context->file_task_runner_.get() || |
| 56 !context->input_task_runner_.get() || |
| 57 !context->network_task_runner_.get() || |
| 58 !context->video_capture_task_runner_.get() || |
| 59 !context->video_encode_task_runner_.get() || |
| 60 !context->url_request_context_getter_.get()) { |
| 61 context.reset(); |
| 62 } |
| 63 |
| 64 return context.Pass(); |
| 40 } | 65 } |
| 41 | 66 |
| 42 scoped_refptr<AutoThreadTaskRunner> | 67 scoped_refptr<AutoThreadTaskRunner> |
| 43 ChromotingHostContext::audio_task_runner() { | 68 ChromotingHostContext::audio_task_runner() { |
| 44 return audio_task_runner_; | 69 return audio_task_runner_; |
| 45 } | 70 } |
| 46 | 71 |
| 47 scoped_refptr<AutoThreadTaskRunner> | 72 scoped_refptr<AutoThreadTaskRunner> |
| 48 ChromotingHostContext::file_task_runner() { | 73 ChromotingHostContext::file_task_runner() { |
| 49 return file_task_runner_; | 74 return file_task_runner_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 scoped_refptr<AutoThreadTaskRunner> | 97 scoped_refptr<AutoThreadTaskRunner> |
| 73 ChromotingHostContext::video_encode_task_runner() { | 98 ChromotingHostContext::video_encode_task_runner() { |
| 74 return video_encode_task_runner_; | 99 return video_encode_task_runner_; |
| 75 } | 100 } |
| 76 | 101 |
| 77 scoped_refptr<net::URLRequestContextGetter> | 102 scoped_refptr<net::URLRequestContextGetter> |
| 78 ChromotingHostContext::url_request_context_getter() { | 103 ChromotingHostContext::url_request_context_getter() { |
| 79 return url_request_context_getter_; | 104 return url_request_context_getter_; |
| 80 } | 105 } |
| 81 | 106 |
| 82 scoped_ptr<ChromotingHostContext> ChromotingHostContext::Create( | |
| 83 scoped_refptr<AutoThreadTaskRunner> ui_task_runner) { | |
| 84 #if defined(OS_WIN) | |
| 85 // On Windows the AudioCapturer requires COM, so we run a single-threaded | |
| 86 // apartment, which requires a UI thread. | |
| 87 scoped_refptr<AutoThreadTaskRunner> audio_task_runner = | |
| 88 AutoThread::CreateWithLoopAndComInitTypes( | |
| 89 "ChromotingAudioThread", ui_task_runner, base::MessageLoop::TYPE_UI, | |
| 90 AutoThread::COM_INIT_STA); | |
| 91 #else // !defined(OS_WIN) | |
| 92 scoped_refptr<AutoThreadTaskRunner> audio_task_runner = | |
| 93 AutoThread::CreateWithType("ChromotingAudioThread", ui_task_runner, | |
| 94 base::MessageLoop::TYPE_IO); | |
| 95 #endif // !defined(OS_WIN) | |
| 96 scoped_refptr<AutoThreadTaskRunner> file_task_runner = | |
| 97 AutoThread::CreateWithType("ChromotingFileThread", ui_task_runner, | |
| 98 base::MessageLoop::TYPE_IO); | |
| 99 scoped_refptr<AutoThreadTaskRunner> network_task_runner = | |
| 100 AutoThread::CreateWithType("ChromotingNetworkThread", ui_task_runner, | |
| 101 base::MessageLoop::TYPE_IO); | |
| 102 | |
| 103 return make_scoped_ptr(new ChromotingHostContext( | |
| 104 ui_task_runner, | |
| 105 audio_task_runner, | |
| 106 file_task_runner, | |
| 107 AutoThread::CreateWithType("ChromotingInputThread", ui_task_runner, | |
| 108 base::MessageLoop::TYPE_IO), | |
| 109 network_task_runner, | |
| 110 AutoThread::Create("ChromotingCaptureThread", ui_task_runner), | |
| 111 AutoThread::Create("ChromotingEncodeThread", ui_task_runner), | |
| 112 make_scoped_refptr( | |
| 113 new URLRequestContextGetter(network_task_runner, file_task_runner)))); | |
| 114 } | |
| 115 | |
| 116 #if defined(OS_CHROMEOS) | |
| 117 namespace { | |
| 118 // Retrieves the task_runner from the browser thread with |id|. | |
| 119 scoped_refptr<AutoThreadTaskRunner> WrapBrowserThread( | |
| 120 content::BrowserThread::ID id) { | |
| 121 // AutoThreadTaskRunner is a TaskRunner with the special property that it will | |
| 122 // continue to process tasks until no references remain, at least. The | |
| 123 // QuitClosure we usually pass does the simple thing of stopping the | |
| 124 // underlying TaskRunner. Since we are re-using the ui_task_runner of the | |
| 125 // browser thread, we cannot stop it explicitly. Therefore, base::DoNothing | |
| 126 // is passed in as the quit closure. | |
| 127 // TODO(kelvinp): Fix this (See crbug.com/428187). | |
| 128 return new AutoThreadTaskRunner( | |
| 129 content::BrowserThread::GetMessageLoopProxyForThread(id).get(), | |
| 130 base::Bind(&base::DoNothing)); | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 // static | |
| 136 scoped_ptr<ChromotingHostContext> ChromotingHostContext::CreateForChromeOS( | |
| 137 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { | |
| 138 DCHECK(url_request_context_getter.get()); | |
| 139 | |
| 140 // Use BrowserThread::FILE as the joiner as it is the only browser-thread | |
| 141 // that allows blocking I/O, which is required by thread joining. | |
| 142 // TODO(kelvinp): Fix AutoThread so that it can be joinable on task runners | |
| 143 // that disallow I/O (crbug.com/428466). | |
| 144 scoped_refptr<AutoThreadTaskRunner> file_task_runner = | |
| 145 WrapBrowserThread(content::BrowserThread::FILE); | |
| 146 | |
| 147 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = | |
| 148 WrapBrowserThread(content::BrowserThread::UI); | |
| 149 | |
| 150 return make_scoped_ptr(new ChromotingHostContext( | |
| 151 ui_task_runner, | |
| 152 AutoThread::CreateWithType("ChromotingAudioThread", file_task_runner, | |
| 153 base::MessageLoop::TYPE_IO), | |
| 154 file_task_runner, | |
| 155 AutoThread::CreateWithType("ChromotingInputThread", file_task_runner, | |
| 156 base::MessageLoop::TYPE_IO), | |
| 157 WrapBrowserThread(content::BrowserThread::IO), // network_task_runner | |
| 158 ui_task_runner, // video_capture_task_runner | |
| 159 AutoThread::CreateWithType("ChromotingEncodeThread", file_task_runner, | |
| 160 base::MessageLoop::TYPE_IO), | |
| 161 url_request_context_getter)); | |
| 162 } | |
| 163 #endif // defined(OS_CHROMEOS) | |
| 164 | |
| 165 } // namespace remoting | 107 } // namespace remoting |
| OLD | NEW |