blob: 3a66c6222e3dbf4bb4560fb91c08eff01a827dbb [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"
lambroslambroue0c2d0242016-03-23 01:21:2011#include "base/message_loop/message_loop.h"
nicholss825f7eb2017-03-17 18:48:4512#include "base/task_scheduler/task_scheduler.h"
13#include "remoting/base/chromium_url_request.h"
14#include "remoting/base/telemetry_log_writer.h"
lambroslambroue0c2d0242016-03-23 01:21:2015#include "remoting/base/url_request_context_getter.h"
16
nicholss825f7eb2017-03-17 18:48:4517namespace {
18
19const char kTelemetryBaseUrl[] = "https://remoting-pa.googleapis.com/v1/events";
20
21} // namespace
22
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() {
fdorayf264f1a2017-04-26 21:54:5831 base::TaskScheduler::CreateAndStartWithDefaultParams("Remoting");
nicholss825f7eb2017-03-17 18:48:4532
33 if (!base::MessageLoop::current()) {
34 VLOG(1) << "Starting main message loop";
35 ui_loop_.reset(new base::MessageLoopForUI());
36#if defined(OS_ANDROID)
37 // On Android, the UI thread is managed by Java, so we need to attach and
38 // start a special type of message loop to allow Chromium code to run tasks.
39 ui_loop_->Start();
40#elif defined(OS_IOS)
41 base::MessageLoopForUI::current()->Attach();
42#endif // OS_ANDROID, OS_IOS
43 } else {
44 ui_loop_.reset(base::MessageLoopForUI::current());
45 }
46
47#if defined(DEBUG)
48 net::URLFetcher::SetIgnoreCertificateRequests(true);
49#endif // DEBUG
lambroslambroue0c2d0242016-03-23 01:21:2050
51 // |ui_loop_| runs on the main thread, so |ui_task_runner_| will run on the
52 // main thread. We can not kill the main thread when the message loop becomes
53 // idle so the callback function does nothing (as opposed to the typical
54 // base::MessageLoop::QuitClosure())
nicholss825f7eb2017-03-17 18:48:4555 ui_task_runner_ = new AutoThreadTaskRunner(ui_loop_->task_runner(),
56 base::Bind(&base::DoNothing));
Scott Nichols77a375a2017-09-19 20:49:0257 audio_task_runner_ = AutoThread::Create("native_audio", ui_task_runner_);
nicholss825f7eb2017-03-17 18:48:4558 display_task_runner_ = AutoThread::Create("native_disp", ui_task_runner_);
59 network_task_runner_ = AutoThread::CreateWithType(
60 "native_net", ui_task_runner_, base::MessageLoop::TYPE_IO);
61 file_task_runner_ = AutoThread::CreateWithType("native_file", ui_task_runner_,
62 base::MessageLoop::TYPE_IO);
63 url_requester_ =
64 new URLRequestContextGetter(network_task_runner_, file_task_runner_);
lambroslambroue0c2d0242016-03-23 01:21:2065
nicholss825f7eb2017-03-17 18:48:4566 CreateLogWriter();
lambroslambroue0c2d0242016-03-23 01:21:2067}
68
nicholss825f7eb2017-03-17 18:48:4569ChromotingClientRuntime::~ChromotingClientRuntime() {
70 if (delegate_) {
71 delegate_->RuntimeWillShutdown();
72 } else {
73 DLOG(ERROR) << "ClientRuntime Delegate is null.";
74 }
lambroslambroue0c2d0242016-03-23 01:21:2075
nicholss825f7eb2017-03-17 18:48:4576 // Block until tasks blocking shutdown have completed their execution.
77 base::TaskScheduler::GetInstance()->Shutdown();
78
79 if (delegate_) {
80 delegate_->RuntimeDidShutdown();
81 }
82}
83
84void ChromotingClientRuntime::SetDelegate(
85 ChromotingClientRuntime::Delegate* delegate) {
86 delegate_ = delegate;
nicholss825f7eb2017-03-17 18:48:4587}
88
89void ChromotingClientRuntime::CreateLogWriter() {
90 if (!network_task_runner()->BelongsToCurrentThread()) {
91 network_task_runner()->PostTask(
92 FROM_HERE, base::Bind(&ChromotingClientRuntime::CreateLogWriter,
93 base::Unretained(this)));
94 return;
95 }
96 log_writer_.reset(new TelemetryLogWriter(
97 kTelemetryBaseUrl,
Jinho Bang138fde32018-01-18 23:13:4298 std::make_unique<ChromiumUrlRequestFactory>(url_requester())));
nicholss825f7eb2017-03-17 18:48:4599 log_writer_->SetAuthClosure(
100 base::Bind(&ChromotingClientRuntime::RequestAuthTokenForLogger,
101 base::Unretained(this)));
102}
103
104void ChromotingClientRuntime::RequestAuthTokenForLogger() {
105 if (delegate_) {
106 delegate_->RequestAuthTokenForLogger();
107 } else {
108 DLOG(ERROR) << "ClientRuntime Delegate is null.";
109 }
110}
111
112ChromotingEventLogWriter* ChromotingClientRuntime::log_writer() {
113 DCHECK(network_task_runner()->BelongsToCurrentThread());
114 return log_writer_.get();
115}
116
Yuwei Huang192209302017-11-09 00:06:08117OAuthTokenGetter* ChromotingClientRuntime::token_getter() {
118 return delegate_->token_getter();
119}
lambroslambroue0c2d0242016-03-23 01:21:20120
121} // namespace remoting