File: chromoting_host_context.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (410 lines) | stat: -rw-r--r-- 17,639 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/chromoting_host_context.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/message_loop/message_pump_type.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "net/ssl/client_cert_store.h"
#include "remoting/base/auto_thread.h"
#include "remoting/base/certificate_helpers.h"
#include "remoting/base/url_request_context_getter.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/transitional_url_loader_factory_owner.h"

namespace remoting {

namespace {

#if BUILDFLAG(IS_CHROMEOS)
class ChromotingHostContextChromeOs : public ChromotingHostContext {
 public:
  using CreateClientCertStoreCallback =
      ChromotingHostContext::CreateClientCertStoreCallback;

  ChromotingHostContextChromeOs(
      scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
      scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
      scoped_refptr<AutoThreadTaskRunner> file_task_runner,
      scoped_refptr<AutoThreadTaskRunner> input_task_runner,
      scoped_refptr<AutoThreadTaskRunner> network_task_runner,
      scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
      scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner,
      scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory,
      CreateClientCertStoreCallback create_client_cert_store);

  ChromotingHostContextChromeOs(const ChromotingHostContextChromeOs&) = delete;
  ChromotingHostContextChromeOs& operator=(
      const ChromotingHostContextChromeOs&) = delete;

  ~ChromotingHostContextChromeOs() override;

  // remoting::ChromotingHostContext implementation.
  std::unique_ptr<ChromotingHostContext> Copy() override;
  std::unique_ptr<net::ClientCertStore> CreateClientCertStore() const override;
  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter()
      const override;
  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory() override;
  CreateClientCertStoreCallback create_client_cert_store_callback()
      const override;

 private:
  // |ui_shared_url_loader_factory_| is a SharedUrlLoaderFactory which is bound
  // to the ui_task_runner sequence and is used to create copies of the original
  // ChromotingHostContext instance.
  scoped_refptr<network::SharedURLLoaderFactory> ui_shared_url_loader_factory_;

  // |pending_factory_| is initialized from |ui_shared_url_loader_factory_| on
  // the UI thread which allows for binding |network_shared_url_loader_factory_|
  // to the network_task_runner sequence.
  std::unique_ptr<network::PendingSharedURLLoaderFactory> pending_factory_;

  // |network_shared_url_loader_factory_| is a SharedUrlLoaderFactory which is
  // bound to the network_task_runner sequence.
  scoped_refptr<network::SharedURLLoaderFactory>
      network_shared_url_loader_factory_;

  CreateClientCertStoreCallback create_client_cert_store_;
};

ChromotingHostContextChromeOs::ChromotingHostContextChromeOs(
    scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
    scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
    scoped_refptr<AutoThreadTaskRunner> file_task_runner,
    scoped_refptr<AutoThreadTaskRunner> input_task_runner,
    scoped_refptr<AutoThreadTaskRunner> network_task_runner,
    scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
    scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner,
    scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory,
    CreateClientCertStoreCallback create_client_cert_store)
    : ChromotingHostContext(ui_task_runner,
                            audio_task_runner,
                            file_task_runner,
                            input_task_runner,
                            network_task_runner,
                            video_capture_task_runner,
                            video_encode_task_runner),
      ui_shared_url_loader_factory_(shared_url_loader_factory),
      pending_factory_(ui_shared_url_loader_factory_->Clone()),
      create_client_cert_store_(create_client_cert_store) {}

ChromotingHostContextChromeOs::~ChromotingHostContextChromeOs() {
  // |ui_shared_url_loader_factory_| should always be valid however
  // |network_shared_url_loader_factory_| may not be if it was never accessed.
  ui_task_runner()->ReleaseSoon(FROM_HERE,
                                std::move(ui_shared_url_loader_factory_));
  if (network_shared_url_loader_factory_) {
    network_task_runner()->ReleaseSoon(
        FROM_HERE, std::move(network_shared_url_loader_factory_));
  }
}

std::unique_ptr<ChromotingHostContext> ChromotingHostContextChromeOs::Copy() {
  DCHECK(ui_task_runner()->BelongsToCurrentThread());
  return std::make_unique<ChromotingHostContextChromeOs>(
      ui_task_runner(), audio_task_runner(), file_task_runner(),
      input_task_runner(), network_task_runner(), video_capture_task_runner(),
      video_encode_task_runner(), ui_shared_url_loader_factory_,
      create_client_cert_store_);
}

std::unique_ptr<net::ClientCertStore>
ChromotingHostContextChromeOs::CreateClientCertStore() const {
  DCHECK(network_task_runner()->BelongsToCurrentThread());
  return create_client_cert_store_.Run();
}

scoped_refptr<net::URLRequestContextGetter>
ChromotingHostContextChromeOs::url_request_context_getter() const {
  NOTREACHED();
}

scoped_refptr<network::SharedURLLoaderFactory>
ChromotingHostContextChromeOs::url_loader_factory() {
  DCHECK(network_task_runner()->BelongsToCurrentThread());
  if (!network_shared_url_loader_factory_) {
    network_shared_url_loader_factory_ =
        network::SharedURLLoaderFactory::Create(std::move(pending_factory_));
  }
  return network_shared_url_loader_factory_;
}

ChromotingHostContext::CreateClientCertStoreCallback
ChromotingHostContextChromeOs::create_client_cert_store_callback() const {
  return create_client_cert_store_;
}

#else  // !BUILDFLAG(IS_CHROMEOS)
void DisallowBlockingOperations() {
  base::DisallowBlocking();
  // TODO(crbug.com/41360128): Re-enable after the underlying issue is fixed.
  // base::DisallowBaseSyncPrimitives();
}

class ChromotingHostContextDesktop : public ChromotingHostContext {
 public:
  ChromotingHostContextDesktop(
      scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
      scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
      scoped_refptr<AutoThreadTaskRunner> file_task_runner,
      scoped_refptr<AutoThreadTaskRunner> input_task_runner,
      scoped_refptr<AutoThreadTaskRunner> network_task_runner,
      scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
      scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner,
      scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);

  ChromotingHostContextDesktop(const ChromotingHostContextDesktop&) = delete;
  ChromotingHostContextDesktop& operator=(const ChromotingHostContextDesktop&) =
      delete;

  ~ChromotingHostContextDesktop() override;

  // remoting::ChromotingHostContext implementation.
  std::unique_ptr<ChromotingHostContext> Copy() override;
  std::unique_ptr<net::ClientCertStore> CreateClientCertStore() const override;
  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter()
      const override;
  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory() override;
  CreateClientCertStoreCallback create_client_cert_store_callback()
      const override;

 private:
  // Serves URLRequestContexts that use the network and UI task runners.
  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;

  // Makes a SharedURLLoaderFactory out of |url_request_context_getter_|
  std::unique_ptr<network::TransitionalURLLoaderFactoryOwner>
      url_loader_factory_owner_;
};

ChromotingHostContextDesktop::ChromotingHostContextDesktop(
    scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
    scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
    scoped_refptr<AutoThreadTaskRunner> file_task_runner,
    scoped_refptr<AutoThreadTaskRunner> input_task_runner,
    scoped_refptr<AutoThreadTaskRunner> network_task_runner,
    scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
    scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner,
    scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
    : ChromotingHostContext(ui_task_runner,
                            audio_task_runner,
                            file_task_runner,
                            input_task_runner,
                            network_task_runner,
                            video_capture_task_runner,
                            video_encode_task_runner),
      url_request_context_getter_(url_request_context_getter) {}

ChromotingHostContextDesktop::~ChromotingHostContextDesktop() {
  if (url_loader_factory_owner_) {
    network_task_runner()->DeleteSoon(FROM_HERE,
                                      url_loader_factory_owner_.release());
  }
}

std::unique_ptr<ChromotingHostContext> ChromotingHostContextDesktop::Copy() {
  return std::make_unique<ChromotingHostContextDesktop>(
      ui_task_runner(), audio_task_runner(), file_task_runner(),
      input_task_runner(), network_task_runner(), video_capture_task_runner(),
      video_encode_task_runner(), url_request_context_getter_);
}

std::unique_ptr<net::ClientCertStore>
ChromotingHostContextDesktop::CreateClientCertStore() const {
  DCHECK(network_task_runner()->BelongsToCurrentThread());
  return CreateClientCertStoreInstance();
}

scoped_refptr<net::URLRequestContextGetter>
ChromotingHostContextDesktop::url_request_context_getter() const {
  return url_request_context_getter_;
}

scoped_refptr<network::SharedURLLoaderFactory>
ChromotingHostContextDesktop::url_loader_factory() {
  DCHECK(network_task_runner()->BelongsToCurrentThread());
  if (!url_loader_factory_owner_) {
    url_loader_factory_owner_ =
        std::make_unique<network::TransitionalURLLoaderFactoryOwner>(
            url_request_context_getter_, /* is_trusted= */ true);
  }
  return url_loader_factory_owner_->GetURLLoaderFactory();
}

ChromotingHostContext::CreateClientCertStoreCallback
ChromotingHostContextDesktop::create_client_cert_store_callback() const {
  return base::BindRepeating(&CreateClientCertStoreInstance);
}

#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace

ChromotingHostContext::ChromotingHostContext(
    scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
    scoped_refptr<AutoThreadTaskRunner> audio_task_runner,
    scoped_refptr<AutoThreadTaskRunner> file_task_runner,
    scoped_refptr<AutoThreadTaskRunner> input_task_runner,
    scoped_refptr<AutoThreadTaskRunner> network_task_runner,
    scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner,
    scoped_refptr<AutoThreadTaskRunner> video_encode_task_runner)
    : ui_task_runner_(ui_task_runner),
      audio_task_runner_(audio_task_runner),
      file_task_runner_(file_task_runner),
      input_task_runner_(input_task_runner),
      network_task_runner_(network_task_runner),
      video_capture_task_runner_(video_capture_task_runner),
      video_encode_task_runner_(video_encode_task_runner) {}

ChromotingHostContext::~ChromotingHostContext() = default;

scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::audio_task_runner()
    const {
  return audio_task_runner_;
}

scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::file_task_runner()
    const {
  return file_task_runner_;
}

scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::input_task_runner()
    const {
  return input_task_runner_;
}

scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::network_task_runner()
    const {
  return network_task_runner_;
}

scoped_refptr<AutoThreadTaskRunner> ChromotingHostContext::ui_task_runner()
    const {
  return ui_task_runner_;
}

scoped_refptr<AutoThreadTaskRunner>
ChromotingHostContext::video_capture_task_runner() const {
  return video_capture_task_runner_;
}

scoped_refptr<AutoThreadTaskRunner>
ChromotingHostContext::video_encode_task_runner() const {
  return video_encode_task_runner_;
}

policy::ManagementService* ChromotingHostContext::management_service() {
  return policy::PlatformManagementService::GetInstance();
}

#if !BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<ChromotingHostContext> ChromotingHostContext::Create(
    scoped_refptr<AutoThreadTaskRunner> ui_task_runner) {
#if BUILDFLAG(IS_WIN)
  // On Windows the AudioCapturer requires COM, so we run a single-threaded
  // apartment, which requires a UI thread.
  scoped_refptr<AutoThreadTaskRunner> audio_task_runner =
      AutoThread::CreateWithLoopAndComInitTypes(
          "ChromotingAudioThread", ui_task_runner, base::MessagePumpType::UI,
          AutoThread::COM_INIT_STA);
#else   // !BUILDFLAG(IS_WIN)
  scoped_refptr<AutoThreadTaskRunner> audio_task_runner =
      AutoThread::CreateWithType("ChromotingAudioThread", ui_task_runner,
                                 base::MessagePumpType::IO);
#endif  // !BUILDFLAG(IS_WIN)
  scoped_refptr<AutoThreadTaskRunner> file_task_runner =
      AutoThread::CreateWithType("ChromotingFileThread", ui_task_runner,
                                 base::MessagePumpType::IO);

  scoped_refptr<AutoThreadTaskRunner> network_task_runner =
      AutoThread::CreateWithType("ChromotingNetworkThread", ui_task_runner,
                                 base::MessagePumpType::IO);
  network_task_runner->PostTask(FROM_HERE,
                                base::BindOnce(&DisallowBlockingOperations));

  // InputInjectorX11 requires an X11EventSource, which can only be created
  // on a UI thread.
  scoped_refptr<AutoThreadTaskRunner> input_task_runner =
      AutoThread::CreateWithType("ChromotingInputThread", ui_task_runner,
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
                                 base::MessagePumpType::UI);
#else
                                 base::MessagePumpType::IO);
#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

  return std::make_unique<ChromotingHostContextDesktop>(
      ui_task_runner, audio_task_runner, file_task_runner, input_task_runner,
      network_task_runner,
#if BUILDFLAG(IS_APPLE)
      // Mac requires a UI thread for the capturer.
      AutoThread::CreateWithType("ChromotingCaptureThread", ui_task_runner,
                                 base::MessagePumpType::UI),
#else   // !BUILDFLAG(IS_APPLE)
      AutoThread::Create("ChromotingCaptureThread", ui_task_runner),
#endif  // !BUILDFLAG(IS_APPLE)
      AutoThread::Create("ChromotingEncodeThread", ui_task_runner),
      base::MakeRefCounted<URLRequestContextGetter>(network_task_runner));
}
#else   // BUILDFLAG(IS_CHROMEOS)

// static
std::unique_ptr<ChromotingHostContext> ChromotingHostContext::CreateForChromeOS(
    scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
    scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
    scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
    scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory,
    CreateClientCertStoreCallback create_client_cert_store) {
  // AutoThreadTaskRunner is a TaskRunner with the special property that it will
  // continue to process tasks until no references remain. We usually provide a
  // QuitClosure which is run when the AutoThreadTaskRunner instance is
  // destroyed, however on ChromeOS we are running on threads provided by the
  // browser (meaning we don't own them or their lifetime) so we should not be
  // stopping them when a remote session terminates.
  // Providing any sort of callback (even base::DoNothing) will cause a crash if
  // ash-chrome is shutting down when the AutoThreadTaskRunner is being
  // destroyed. A real-world example is starting a CRD session and then signing
  // out, see b/260395047 for more details.
  scoped_refptr<AutoThreadTaskRunner> io_auto_task_runner =
      new AutoThreadTaskRunner(io_task_runner);
  scoped_refptr<AutoThreadTaskRunner> file_auto_task_runner =
      new AutoThreadTaskRunner(file_task_runner);
  scoped_refptr<AutoThreadTaskRunner> ui_auto_task_runner =
      new AutoThreadTaskRunner(ui_task_runner);

  // Use browser's file thread as the joiner as it is the only browser-thread
  // that allows blocking I/O, which is required by thread joining.
  return std::make_unique<ChromotingHostContextChromeOs>(
      ui_auto_task_runner,
      AutoThread::Create("ChromotingAudioThread", file_auto_task_runner),
      file_auto_task_runner,
      ui_auto_task_runner,  // input_task_runner
      io_auto_task_runner,  // network_task_runner
      ui_auto_task_runner,  // video_capture_task_runner
      AutoThread::Create("ChromotingEncodeThread", file_auto_task_runner),
      shared_url_loader_factory, create_client_cert_store);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

// static
std::unique_ptr<ChromotingHostContext> ChromotingHostContext::CreateForTesting(
    scoped_refptr<AutoThreadTaskRunner> ui_task_runner,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
#if BUILDFLAG(IS_CHROMEOS)
  return ChromotingHostContext::CreateForChromeOS(
      ui_task_runner, ui_task_runner, ui_task_runner, url_loader_factory,
      base::BindRepeating(&CreateClientCertStoreInstance));
#else
  return ChromotingHostContext::Create(ui_task_runner);
#endif
}

}  // namespace remoting