OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/client/chromoting_client_runtime.h" | 5 #include "remoting/client/chromoting_client_runtime.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/singleton.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/task_scheduler/task_scheduler.h" |
| 13 #include "remoting/base/chromium_url_request.h" |
| 14 #include "remoting/base/telemetry_log_writer.h" |
11 #include "remoting/base/url_request_context_getter.h" | 15 #include "remoting/base/url_request_context_getter.h" |
12 | 16 |
| 17 namespace { |
| 18 |
| 19 const char kTelemetryBaseUrl[] = "https://remoting-pa.googleapis.com/v1/events"; |
| 20 |
| 21 } // namespace |
| 22 |
13 namespace remoting { | 23 namespace remoting { |
14 | 24 |
15 std::unique_ptr<ChromotingClientRuntime> ChromotingClientRuntime::Create( | 25 // static |
16 base::MessageLoopForUI* ui_loop) { | 26 ChromotingClientRuntime* ChromotingClientRuntime::GetInstance() { |
17 DCHECK(ui_loop); | 27 return base::Singleton<ChromotingClientRuntime>::get(); |
| 28 } |
| 29 |
| 30 ChromotingClientRuntime::ChromotingClientRuntime() { |
| 31 // TODO(sergeyu): Consider adding separate pools for different task classes. |
| 32 const int kMaxBackgroundThreads = 5; |
| 33 if (!base::TaskScheduler::GetInstance()) { |
| 34 // Make sure TaskScheduler is initialized. |
| 35 base::TaskScheduler::CreateAndSetSimpleTaskScheduler(kMaxBackgroundThreads); |
| 36 } |
| 37 |
| 38 if (!base::MessageLoop::current()) { |
| 39 VLOG(1) << "Starting main message loop"; |
| 40 ui_loop_.reset(new base::MessageLoopForUI()); |
| 41 #if defined(OS_ANDROID) |
| 42 // On Android, the UI thread is managed by Java, so we need to attach and |
| 43 // start a special type of message loop to allow Chromium code to run tasks. |
| 44 ui_loop_->Start(); |
| 45 #elif defined(OS_IOS) |
| 46 base::MessageLoopForUI::current()->Attach(); |
| 47 #endif // OS_ANDROID, OS_IOS |
| 48 } else { |
| 49 ui_loop_.reset(base::MessageLoopForUI::current()); |
| 50 } |
| 51 |
| 52 #if defined(DEBUG) |
| 53 net::URLFetcher::SetIgnoreCertificateRequests(true); |
| 54 #endif // DEBUG |
18 | 55 |
19 // |ui_loop_| runs on the main thread, so |ui_task_runner_| will run on the | 56 // |ui_loop_| runs on the main thread, so |ui_task_runner_| will run on the |
20 // main thread. We can not kill the main thread when the message loop becomes | 57 // main thread. We can not kill the main thread when the message loop becomes |
21 // idle so the callback function does nothing (as opposed to the typical | 58 // idle so the callback function does nothing (as opposed to the typical |
22 // base::MessageLoop::QuitClosure()) | 59 // base::MessageLoop::QuitClosure()) |
23 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = new AutoThreadTaskRunner( | 60 ui_task_runner_ = new AutoThreadTaskRunner(ui_loop_->task_runner(), |
24 ui_loop->task_runner(), base::Bind(&base::DoNothing)); | 61 base::Bind(&base::DoNothing)); |
25 | 62 |
26 scoped_refptr<AutoThreadTaskRunner> display_task_runner = | 63 display_task_runner_ = AutoThread::Create("native_disp", ui_task_runner_); |
27 AutoThread::Create("native_disp", ui_task_runner); | 64 network_task_runner_ = AutoThread::CreateWithType( |
28 scoped_refptr<AutoThreadTaskRunner> network_task_runner = | 65 "native_net", ui_task_runner_, base::MessageLoop::TYPE_IO); |
29 AutoThread::CreateWithType("native_net", ui_task_runner, | 66 file_task_runner_ = AutoThread::CreateWithType("native_file", ui_task_runner_, |
30 base::MessageLoop::TYPE_IO); | 67 base::MessageLoop::TYPE_IO); |
31 scoped_refptr<AutoThreadTaskRunner> file_task_runner = | 68 url_requester_ = |
32 AutoThread::CreateWithType("native_file", ui_task_runner, | 69 new URLRequestContextGetter(network_task_runner_, file_task_runner_); |
33 base::MessageLoop::TYPE_IO); | |
34 scoped_refptr<net::URLRequestContextGetter> url_requester = | |
35 new URLRequestContextGetter(network_task_runner, file_task_runner); | |
36 | 70 |
37 return base::WrapUnique(new ChromotingClientRuntime( | 71 CreateLogWriter(); |
38 ui_task_runner, display_task_runner, network_task_runner, | |
39 file_task_runner, url_requester)); | |
40 } | 72 } |
41 | 73 |
42 ChromotingClientRuntime::ChromotingClientRuntime( | 74 ChromotingClientRuntime::~ChromotingClientRuntime() { |
43 scoped_refptr<AutoThreadTaskRunner> ui_task_runner, | 75 if (delegate_) { |
44 scoped_refptr<AutoThreadTaskRunner> display_task_runner, | 76 delegate_->RuntimeWillShutdown(); |
45 scoped_refptr<AutoThreadTaskRunner> network_task_runner, | 77 } else { |
46 scoped_refptr<AutoThreadTaskRunner> file_task_runner, | 78 DLOG(ERROR) << "ClientRuntime Delegate is null."; |
47 scoped_refptr<net::URLRequestContextGetter> url_requester) | 79 } |
48 : ui_task_runner_(ui_task_runner), | |
49 display_task_runner_(display_task_runner), | |
50 network_task_runner_(network_task_runner), | |
51 file_task_runner_(file_task_runner), | |
52 url_requester_(url_requester) {} | |
53 | 80 |
54 ChromotingClientRuntime::~ChromotingClientRuntime() {} | 81 // Block until tasks blocking shutdown have completed their execution. |
| 82 base::TaskScheduler::GetInstance()->Shutdown(); |
| 83 |
| 84 if (delegate_) { |
| 85 delegate_->RuntimeDidShutdown(); |
| 86 } |
| 87 } |
| 88 |
| 89 void ChromotingClientRuntime::SetDelegate( |
| 90 ChromotingClientRuntime::Delegate* delegate) { |
| 91 delegate_ = delegate; |
| 92 delegate_->RequestAuthTokenForLogger(); |
| 93 } |
| 94 |
| 95 void ChromotingClientRuntime::CreateLogWriter() { |
| 96 if (!network_task_runner()->BelongsToCurrentThread()) { |
| 97 network_task_runner()->PostTask( |
| 98 FROM_HERE, base::Bind(&ChromotingClientRuntime::CreateLogWriter, |
| 99 base::Unretained(this))); |
| 100 return; |
| 101 } |
| 102 log_writer_.reset(new TelemetryLogWriter( |
| 103 kTelemetryBaseUrl, |
| 104 base::MakeUnique<ChromiumUrlRequestFactory>(url_requester()))); |
| 105 log_writer_->SetAuthClosure( |
| 106 base::Bind(&ChromotingClientRuntime::RequestAuthTokenForLogger, |
| 107 base::Unretained(this))); |
| 108 } |
| 109 |
| 110 void ChromotingClientRuntime::RequestAuthTokenForLogger() { |
| 111 if (delegate_) { |
| 112 delegate_->RequestAuthTokenForLogger(); |
| 113 } else { |
| 114 DLOG(ERROR) << "ClientRuntime Delegate is null."; |
| 115 } |
| 116 } |
| 117 |
| 118 ChromotingEventLogWriter* ChromotingClientRuntime::log_writer() { |
| 119 DCHECK(network_task_runner()->BelongsToCurrentThread()); |
| 120 return log_writer_.get(); |
| 121 } |
| 122 |
55 | 123 |
56 } // namespace remoting | 124 } // namespace remoting |
OLD | NEW |