blob: d5f74405660d6d409560f95efb8fa3465f793abb [file] [log] [blame]
lambroslambroue0c2d0242016-03-23 01:21:201// Copyright 2016 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/client/chromoting_client_runtime.h"
6
7#include "base/bind.h"
Peter Kasting383640f52018-02-13 06:22:408#include "base/bind_helpers.h"
lambroslambroue0c2d0242016-03-23 01:21:209#include "base/logging.h"
nicholss825f7eb2017-03-17 18:48:4510#include "base/memory/singleton.h"
Alexander Timin4f9c35c2018-11-01 20:15:2011#include "base/message_loop/message_loop.h"
Gabriel Charetteaec08d42018-04-24 21:40:3112#include "base/message_loop/message_loop_current.h"
Gabriel Charette52fa3ae2019-04-15 21:44:3713#include "base/task/thread_pool/thread_pool.h"
Ran Ji3d6ec662018-07-09 21:18:3014#include "build/build_config.h"
Yuwei Huang4debbed72018-08-30 20:51:2515#include "mojo/core/embedder/embedder.h"
nicholss825f7eb2017-03-17 18:48:4516#include "remoting/base/chromium_url_request.h"
Yuwei Huang786d8872019-04-30 01:07:2317#include "remoting/base/oauth_token_getter_proxy.h"
nicholss825f7eb2017-03-17 18:48:4518#include "remoting/base/telemetry_log_writer.h"
lambroslambroue0c2d0242016-03-23 01:21:2019#include "remoting/base/url_request_context_getter.h"
Maks Orlovich8db7d0d62018-08-16 19:22:2720#include "services/network/public/cpp/shared_url_loader_factory.h"
21#include "services/network/transitional_url_loader_factory_owner.h"
lambroslambroue0c2d0242016-03-23 01:21:2022
lambroslambroue0c2d0242016-03-23 01:21:2023namespace remoting {
24
nicholss825f7eb2017-03-17 18:48:4525// static
26ChromotingClientRuntime* ChromotingClientRuntime::GetInstance() {
27 return base::Singleton<ChromotingClientRuntime>::get();
28}
29
30ChromotingClientRuntime::ChromotingClientRuntime() {
Gabriel Charette43fd3702019-05-29 16:36:5131 base::ThreadPoolInstance::CreateAndStartWithDefaultParams("Remoting");
nicholss825f7eb2017-03-17 18:48:4532
Gabriel Charetteaec08d42018-04-24 21:40:3133 DCHECK(!base::MessageLoopCurrent::Get());
Gabriel Charette12c0fc102018-03-30 02:03:2334
35 VLOG(1) << "Starting main message loop";
36 ui_loop_.reset(new base::MessageLoopForUI());
Ran Ji3d6ec662018-07-09 21:18:3037#if defined(OS_IOS)
38 // TODO(ranj): Attach on BindToCurrentThread().
Gabriel Charette12c0fc102018-03-30 02:03:2339 ui_loop_->Attach();
Ran Ji3d6ec662018-07-09 21:18:3040#endif
nicholss825f7eb2017-03-17 18:48:4541
42#if defined(DEBUG)
43 net::URLFetcher::SetIgnoreCertificateRequests(true);
44#endif // DEBUG
lambroslambroue0c2d0242016-03-23 01:21:2045
46 // |ui_loop_| runs on the main thread, so |ui_task_runner_| will run on the
47 // main thread. We can not kill the main thread when the message loop becomes
48 // idle so the callback function does nothing (as opposed to the typical
49 // base::MessageLoop::QuitClosure())
Peter Kasting341e1fb2018-02-24 00:03:0150 ui_task_runner_ =
51 new AutoThreadTaskRunner(ui_loop_->task_runner(), base::DoNothing());
Scott Nichols77a375a2017-09-19 20:49:0252 audio_task_runner_ = AutoThread::Create("native_audio", ui_task_runner_);
nicholss825f7eb2017-03-17 18:48:4553 display_task_runner_ = AutoThread::Create("native_disp", ui_task_runner_);
54 network_task_runner_ = AutoThread::CreateWithType(
55 "native_net", ui_task_runner_, base::MessageLoop::TYPE_IO);
Yuwei Huang4debbed72018-08-30 20:51:2556
57 mojo::core::Init();
lambroslambroue0c2d0242016-03-23 01:21:2058}
59
nicholss825f7eb2017-03-17 18:48:4560ChromotingClientRuntime::~ChromotingClientRuntime() {
61 if (delegate_) {
62 delegate_->RuntimeWillShutdown();
63 } else {
64 DLOG(ERROR) << "ClientRuntime Delegate is null.";
65 }
lambroslambroue0c2d0242016-03-23 01:21:2066
nicholss825f7eb2017-03-17 18:48:4567 // Block until tasks blocking shutdown have completed their execution.
Gabriel Charette43fd3702019-05-29 16:36:5168 base::ThreadPoolInstance::Get()->Shutdown();
nicholss825f7eb2017-03-17 18:48:4569
70 if (delegate_) {
71 delegate_->RuntimeDidShutdown();
72 }
73}
74
Yuwei Huang50afc832018-03-21 22:00:2675void ChromotingClientRuntime::Init(
nicholss825f7eb2017-03-17 18:48:4576 ChromotingClientRuntime::Delegate* delegate) {
Yuwei Huang50afc832018-03-21 22:00:2677 DCHECK(delegate);
78 DCHECK(!delegate_);
nicholss825f7eb2017-03-17 18:48:4579 delegate_ = delegate;
Yuwei Huanga1f0a5b2018-09-10 20:58:3480 url_requester_ = new URLRequestContextGetter(network_task_runner_);
Erik Jensen2c673d12019-05-31 00:00:1781 log_writer_ = std::make_unique<TelemetryLogWriter>(CreateOAuthTokenGetter());
Yuwei Huang4debbed72018-08-30 20:51:2582 network_task_runner()->PostTask(
83 FROM_HERE,
84 base::BindOnce(&ChromotingClientRuntime::InitializeOnNetworkThread,
85 base::Unretained(this)));
nicholss825f7eb2017-03-17 18:48:4586}
87
Yuwei Huang50afc832018-03-21 22:00:2688std::unique_ptr<OAuthTokenGetter>
89ChromotingClientRuntime::CreateOAuthTokenGetter() {
90 return std::make_unique<OAuthTokenGetterProxy>(
91 delegate_->oauth_token_getter(), ui_task_runner());
Yuwei Huang192209302017-11-09 00:06:0892}
lambroslambroue0c2d0242016-03-23 01:21:2093
Maks Orlovich8db7d0d62018-08-16 19:22:2794scoped_refptr<network::SharedURLLoaderFactory>
95ChromotingClientRuntime::url_loader_factory() {
Yuwei Huang4debbed72018-08-30 20:51:2596 DCHECK(network_task_runner()->BelongsToCurrentThread());
Maks Orlovich8db7d0d62018-08-16 19:22:2797 return url_loader_factory_owner_->GetURLLoaderFactory();
98}
99
Yuwei Huang4debbed72018-08-30 20:51:25100void ChromotingClientRuntime::InitializeOnNetworkThread() {
101 DCHECK(network_task_runner()->BelongsToCurrentThread());
Yuwei Huang4debbed72018-08-30 20:51:25102 url_loader_factory_owner_ =
103 std::make_unique<network::TransitionalURLLoaderFactoryOwner>(
104 url_requester_);
Yuwei Huang4debbed72018-08-30 20:51:25105}
106
lambroslambroue0c2d0242016-03-23 01:21:20107} // namespace remoting