lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 1 | // 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" |
| 8 | #include "base/logging.h" |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 9 | #include "base/memory/ptr_util.h" |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 10 | #include "base/memory/singleton.h" |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 12 | #include "base/task_scheduler/task_scheduler.h" |
| 13 | #include "remoting/base/chromium_url_request.h" |
| 14 | #include "remoting/base/telemetry_log_writer.h" |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 15 | #include "remoting/base/url_request_context_getter.h" |
| 16 | |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | const char kTelemetryBaseUrl[] = "https://remoting-pa.googleapis.com/v1/events"; |
| 20 | |
| 21 | } // namespace |
| 22 | |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 23 | namespace remoting { |
| 24 | |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 25 | // static |
| 26 | ChromotingClientRuntime* ChromotingClientRuntime::GetInstance() { |
| 27 | return base::Singleton<ChromotingClientRuntime>::get(); |
| 28 | } |
| 29 | |
| 30 | ChromotingClientRuntime::ChromotingClientRuntime() { |
fdoray | f264f1a | 2017-04-26 21:54:58 | [diff] [blame] | 31 | base::TaskScheduler::CreateAndStartWithDefaultParams("Remoting"); |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 32 | |
| 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 |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 50 | |
| 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()) |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 55 | ui_task_runner_ = new AutoThreadTaskRunner(ui_loop_->task_runner(), |
| 56 | base::Bind(&base::DoNothing)); |
Scott Nichols | 77a375a | 2017-09-19 20:49:02 | [diff] [blame^] | 57 | audio_task_runner_ = AutoThread::Create("native_audio", ui_task_runner_); |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 58 | 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_); |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 65 | |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 66 | CreateLogWriter(); |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 67 | } |
| 68 | |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 69 | ChromotingClientRuntime::~ChromotingClientRuntime() { |
| 70 | if (delegate_) { |
| 71 | delegate_->RuntimeWillShutdown(); |
| 72 | } else { |
| 73 | DLOG(ERROR) << "ClientRuntime Delegate is null."; |
| 74 | } |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 75 | |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 76 | // Block until tasks blocking shutdown have completed their execution. |
| 77 | base::TaskScheduler::GetInstance()->Shutdown(); |
| 78 | |
| 79 | if (delegate_) { |
| 80 | delegate_->RuntimeDidShutdown(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void ChromotingClientRuntime::SetDelegate( |
| 85 | ChromotingClientRuntime::Delegate* delegate) { |
| 86 | delegate_ = delegate; |
nicholss | 825f7eb | 2017-03-17 18:48:45 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void 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, |
| 98 | base::MakeUnique<ChromiumUrlRequestFactory>(url_requester()))); |
| 99 | log_writer_->SetAuthClosure( |
| 100 | base::Bind(&ChromotingClientRuntime::RequestAuthTokenForLogger, |
| 101 | base::Unretained(this))); |
| 102 | } |
| 103 | |
| 104 | void ChromotingClientRuntime::RequestAuthTokenForLogger() { |
| 105 | if (delegate_) { |
| 106 | delegate_->RequestAuthTokenForLogger(); |
| 107 | } else { |
| 108 | DLOG(ERROR) << "ClientRuntime Delegate is null."; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | ChromotingEventLogWriter* ChromotingClientRuntime::log_writer() { |
| 113 | DCHECK(network_task_runner()->BelongsToCurrentThread()); |
| 114 | return log_writer_.get(); |
| 115 | } |
| 116 | |
lambroslambrou | e0c2d024 | 2016-03-23 01:21:20 | [diff] [blame] | 117 | |
| 118 | } // namespace remoting |