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. | |
Yuwei
2017/03/14 19:40:16
Please be aware of this CL:
https://codereview.ch
nicholss
2017/03/14 22:56:44
better land this before them!
| |
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 #endif // OS_ANDROID | |
46 #if defined(OS_IOS) | |
Sergey Ulanov
2017/03/14 19:09:37
#elif
nicholss
2017/03/14 22:56:44
Done.
| |
47 base::MessageLoopForUI::current()->Attach(); | |
48 #endif // OS_IOS | |
49 } else { | |
50 ui_loop_.reset(base::MessageLoopForUI::current()); | |
51 } | |
52 | |
53 #if defined(DEBUG) | |
54 net::URLFetcher::SetIgnoreCertificateRequests(true); | |
55 #endif // DEBUG | |
18 | 56 |
19 // |ui_loop_| runs on the main thread, so |ui_task_runner_| will run on the | 57 // |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 | 58 // 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 | 59 // idle so the callback function does nothing (as opposed to the typical |
22 // base::MessageLoop::QuitClosure()) | 60 // base::MessageLoop::QuitClosure()) |
23 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = new AutoThreadTaskRunner( | 61 ui_task_runner_ = new AutoThreadTaskRunner(ui_loop_->task_runner(), |
24 ui_loop->task_runner(), base::Bind(&base::DoNothing)); | 62 base::Bind(&base::DoNothing)); |
25 | 63 |
26 scoped_refptr<AutoThreadTaskRunner> display_task_runner = | 64 display_task_runner_ = AutoThread::Create("native_disp", ui_task_runner_); |
27 AutoThread::Create("native_disp", ui_task_runner); | 65 network_task_runner_ = AutoThread::CreateWithType( |
28 scoped_refptr<AutoThreadTaskRunner> network_task_runner = | 66 "native_net", ui_task_runner_, base::MessageLoop::TYPE_IO); |
29 AutoThread::CreateWithType("native_net", ui_task_runner, | 67 file_task_runner_ = AutoThread::CreateWithType("native_file", ui_task_runner_, |
30 base::MessageLoop::TYPE_IO); | 68 base::MessageLoop::TYPE_IO); |
31 scoped_refptr<AutoThreadTaskRunner> file_task_runner = | 69 url_requester_ = |
32 AutoThread::CreateWithType("native_file", ui_task_runner, | 70 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 | 71 |
37 return base::WrapUnique(new ChromotingClientRuntime( | 72 CreateLogWriter(); |
38 ui_task_runner, display_task_runner, network_task_runner, | |
39 file_task_runner, url_requester)); | |
40 } | 73 } |
41 | 74 |
42 ChromotingClientRuntime::ChromotingClientRuntime( | 75 ChromotingClientRuntime::~ChromotingClientRuntime() { |
43 scoped_refptr<AutoThreadTaskRunner> ui_task_runner, | 76 if (delegate_) { |
Yuwei
2017/03/14 19:40:16
Do we need to support the case when delegate_ is n
nicholss
2017/03/14 22:56:44
delegate is checked twice because it is accessed t
| |
44 scoped_refptr<AutoThreadTaskRunner> display_task_runner, | 77 delegate_->RuntimeWillShutdown(); |
45 scoped_refptr<AutoThreadTaskRunner> network_task_runner, | 78 } else { |
46 scoped_refptr<AutoThreadTaskRunner> file_task_runner, | 79 DLOG(ERROR) << "ClientRuntime Delegate is null."; |
47 scoped_refptr<net::URLRequestContextGetter> url_requester) | 80 } |
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 | 81 |
54 ChromotingClientRuntime::~ChromotingClientRuntime() {} | 82 // Block until tasks blocking shutdown have completed their execution. |
83 base::TaskScheduler::GetInstance()->Shutdown(); | |
84 | |
85 if (delegate_) { | |
86 delegate_->RuntimeDidShutdown(); | |
Sergey Ulanov
2017/03/14 19:09:37
Should we also stop all background here before thi
nicholss
2017/03/14 22:56:44
Not sure, if you tell me how we do that we can try
Sergey Ulanov
2017/03/16 07:23:50
Ah, all other threads are are AutoThreads, and the
| |
87 } else { | |
88 DLOG(ERROR) << "ClientRuntime Delegate is null."; | |
Sergey Ulanov
2017/03/14 19:09:37
this message will be logged multiple times in this
nicholss
2017/03/14 22:56:44
Acknowledged.
| |
89 } | |
90 } | |
91 | |
92 void ChromotingClientRuntime::SetDelegate( | |
93 ChromotingClientRuntime::Delegate* delegate) { | |
94 delegate_ = delegate; | |
95 delegate_->RequestAuthTokenForLogger(); | |
96 } | |
97 | |
98 void ChromotingClientRuntime::CreateLogWriter() { | |
99 if (!network_task_runner()->BelongsToCurrentThread()) { | |
100 network_task_runner()->PostTask( | |
101 FROM_HERE, base::Bind(&ChromotingClientRuntime::CreateLogWriter, | |
102 base::Unretained(this))); | |
103 return; | |
104 } | |
105 log_writer_.reset(new TelemetryLogWriter( | |
106 kTelemetryBaseUrl, | |
107 base::MakeUnique<ChromiumUrlRequestFactory>(url_requester()))); | |
108 log_writer_->SetAuthClosure( | |
109 base::Bind(&ChromotingClientRuntime::RequestAuthTokenForLogger, | |
110 base::Unretained(this))); | |
111 } | |
112 | |
113 void ChromotingClientRuntime::RequestAuthTokenForLogger() { | |
114 if (delegate_) { | |
115 delegate_->RequestAuthTokenForLogger(); | |
116 } else { | |
117 DLOG(ERROR) << "ClientRuntime Delegate is null."; | |
118 } | |
119 } | |
120 | |
121 TelemetryLogWriter* ChromotingClientRuntime::GetLogWriter() { | |
122 DCHECK(network_task_runner()->BelongsToCurrentThread()); | |
123 return log_writer_.get(); | |
124 } | |
125 | |
55 | 126 |
56 } // namespace remoting | 127 } // namespace remoting |
OLD | NEW |