Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / host / chromoting_host_context.cc
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 #include "remoting/host/chromoting_host_context.h"
6
7 #include "content/public/browser/browser_thread.h"
8 #include "remoting/base/auto_thread.h"
9 #include "remoting/base/url_request_context_getter.h"
10
11 namespace remoting {
12
13 ChromotingHostContext::ChromotingHostContext(
14     scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
15     scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
16     scoped_refptr<AutoThreadTaskRunner> file_task_runner,
17     scoped_refptr<AutoThreadTaskRunner> input_task_runner,
18     scoped_refptr<AutoThreadTaskRunner> network_task_runner,
19     scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
20     scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner,
21     scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
22     : ui_task_runner_(ui_task_runner),
23       audio_task_runner_(audio_task_runner),
24       file_task_runner_(file_task_runner),
25       input_task_runner_(input_task_runner),
26       network_task_runner_(network_task_runner),
27       video_capture_task_runner_(video_capture_task_runner),
28       video_encode_task_runner_(video_encode_task_runner),
29       url_request_context_getter_(url_request_context_getter) {
30 }
31
32 ChromotingHostContext::~ChromotingHostContext() {
33 }
34
35 scoped_ptr<ChromotingHostContext> ChromotingHostContext::Copy() {
36   return make_scoped_ptr(new ChromotingHostContext(
37       ui_task_runner_, audio_task_runner_, file_task_runner_,
38       input_task_runner_, network_task_runner_, video_capture_task_runner_,
39       video_encode_task_runner_, url_request_context_getter_));
40 }
41
42 scoped_refptr<AutoThreadTaskRunner>
43 ChromotingHostContext::audio_task_runner() {
44   return audio_task_runner_;
45 }
46
47 scoped_refptr<AutoThreadTaskRunner>
48 ChromotingHostContext::file_task_runner() {
49   return file_task_runner_;
50 }
51
52 scoped_refptr<AutoThreadTaskRunner>
53 ChromotingHostContext::input_task_runner() {
54   return input_task_runner_;
55 }
56
57 scoped_refptr<AutoThreadTaskRunner>
58 ChromotingHostContext::network_task_runner() {
59   return network_task_runner_;
60 }
61
62 scoped_refptr<AutoThreadTaskRunner>
63 ChromotingHostContext::ui_task_runner() {
64   return ui_task_runner_;
65 }
66
67 scoped_refptr<AutoThreadTaskRunner>
68 ChromotingHostContext::video_capture_task_runner() {
69   return video_capture_task_runner_;
70 }
71
72 scoped_refptr<AutoThreadTaskRunner>
73 ChromotingHostContext::video_encode_task_runner() {
74   return video_encode_task_runner_;
75 }
76
77 scoped_refptr<net::URLRequestContextGetter>
78 ChromotingHostContext::url_request_context_getter() {
79   return url_request_context_getter_;
80 }
81
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