Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: remoting/client/chromoting_client_runtime.cc

Issue 2745583008: Refactoring out the chromoting jni runtime class in favor of chromoting client runtime. (Closed)
Patch Set: Update a comment found in self review. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #endif // OS_ANDROID
46 #if defined(OS_IOS)
47 base::MessageLoopForUI::current()->Attach();
48 #endif // OS_IOS
49 } else {
50 ui_loop_.reset(base::MessageLoopForUI::current());
Yuwei 2017/03/10 20:18:53 I personally think the UI loop should be an implem
nicholss 2017/03/10 20:53:11 If I move it out then every type of client needs t
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_) {
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), 81 }
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 82
54 ChromotingClientRuntime::~ChromotingClientRuntime() {} 83 void ChromotingClientRuntime::SetDelegate(
84 ChromotingClientRuntime::Delegate* delegate) {
85 delegate_ = delegate;
86 delegate_->RequestAuthTokenForLogger();
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 TelemetryLogWriter* ChromotingClientRuntime::GetLogWriter() {
113 DCHECK(network_task_runner()->BelongsToCurrentThread());
114 return log_writer_.get();
115 }
116
55 117
56 } // namespace remoting 118 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698