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

Side by Side Diff: remoting/client/jni/chromoting_jni_runtime.cc

Issue 2745583008: Refactoring out the chromoting jni runtime class in favor of chromoting client runtime. (Closed)
Patch Set: Missed out on a () 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
(Empty)
1 // Copyright 2013 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/jni/chromoting_jni_runtime.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/library_loader/library_loader_hooks.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/command_line.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/memory/singleton.h"
15 #include "base/stl_util.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/task_scheduler/task_scheduler.h"
18 #include "jni/JniInterface_jni.h"
19 #include "remoting/base/chromium_url_request.h"
20 #include "remoting/base/url_request_context_getter.h"
21 #include "remoting/client/jni/jni_touch_event_data.h"
22
23 using base::android::ConvertJavaStringToUTF8;
24 using base::android::ConvertUTF8ToJavaString;
25 using base::android::JavaParamRef;
26 using base::android::ToJavaByteArray;
27
28 namespace {
29
30 const char kTelemetryBaseUrl[] = "https://remoting-pa.googleapis.com/v1/events";
31
32 } // namespace
33
34 namespace remoting {
35
36 bool RegisterChromotingJniRuntime(JNIEnv* env) {
37 return remoting::RegisterNativesImpl(env);
38 }
39
40 // Implementation of stubs defined in JniInterface_jni.h. These are the entry
41 // points for JNI calls from Java into C++.
42
43 static void LoadNative(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
44 base::CommandLine::Init(0, nullptr);
45
46 // TODO(sergeyu): Consider adding separate pools for different task classes.
47 const int kMaxBackgroundThreads = 5;
48 base::TaskScheduler::CreateAndSetSimpleTaskScheduler(kMaxBackgroundThreads);
49
50 // Create the singleton now so that the Chromoting threads will be set up.
51 remoting::ChromotingJniRuntime::GetInstance();
52 }
53
54 static void HandleAuthTokenOnNetworkThread(const std::string& token) {
55 ChromotingJniRuntime* runtime = remoting::ChromotingJniRuntime::GetInstance();
56 DCHECK(runtime->network_task_runner()->BelongsToCurrentThread());
57 runtime->GetLogWriter()->SetAuthToken(token);
58 }
59
60 static void OnAuthTokenFetched(JNIEnv* env,
61 const JavaParamRef<jclass>& clazz,
62 const JavaParamRef<jstring>& token) {
63 ChromotingJniRuntime* runtime = remoting::ChromotingJniRuntime::GetInstance();
64 runtime->network_task_runner()->PostTask(
65 FROM_HERE, base::Bind(&HandleAuthTokenOnNetworkThread,
66 ConvertJavaStringToUTF8(env, token)));
67 }
68
69 // ChromotingJniRuntime implementation.
70
71 // static
72 ChromotingJniRuntime* ChromotingJniRuntime::GetInstance() {
73 return base::Singleton<ChromotingJniRuntime>::get();
74 }
75
76 ChromotingJniRuntime::ChromotingJniRuntime() {
77 // Grab or create the threads.
78 // TODO(nicholss): We could runtime this as a constructor argument when jni
79 // runtime is not no longer a singleton.
80
81 if (!base::MessageLoop::current()) {
82 VLOG(1) << "Starting main message loop";
83 // On Android, the UI thread is managed by Java, so we need to attach and
84 // start a special type of message loop to allow Chromium code to run tasks.
85 ui_loop_.reset(new base::MessageLoopForUI());
86 ui_loop_->Start();
87 } else {
88 VLOG(1) << "Using existing main message loop";
89 ui_loop_.reset(base::MessageLoopForUI::current());
90 }
91
92 // Pass the main ui loop already attached to be used for creating threads.
93 runtime_ = ChromotingClientRuntime::Create(ui_loop_.get());
94 network_task_runner()->PostTask(
95 FROM_HERE, base::Bind(&ChromotingJniRuntime::StartLoggerOnNetworkThread,
96 base::Unretained(this)));
97 }
98
99 ChromotingJniRuntime::~ChromotingJniRuntime() {
100 // The singleton should only ever be destroyed on the main thread.
101 DCHECK(ui_task_runner()->BelongsToCurrentThread());
102
103 base::WaitableEvent done_event(
104 base::WaitableEvent::ResetPolicy::AUTOMATIC,
105 base::WaitableEvent::InitialState::NOT_SIGNALED);
106 network_task_runner()->PostTask(
107 FROM_HERE, base::Bind(&ChromotingJniRuntime::DetachFromVmAndSignal,
108 base::Unretained(this), &done_event));
109 done_event.Wait();
110 display_task_runner()->PostTask(
111 FROM_HERE, base::Bind(&ChromotingJniRuntime::DetachFromVmAndSignal,
112 base::Unretained(this), &done_event));
113 done_event.Wait();
114
115 // Block until tasks blocking shutdown have completed their execution.
116 base::TaskScheduler::GetInstance()->Shutdown();
117
118 base::android::LibraryLoaderExitHook();
119 base::android::DetachFromVM();
120 }
121
122 TelemetryLogWriter* ChromotingJniRuntime::GetLogWriter() {
123 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
124 return log_writer_.get();
125 }
126
127 void ChromotingJniRuntime::FetchAuthToken() {
128 if (!ui_task_runner()->BelongsToCurrentThread()) {
129 ui_task_runner()->PostTask(
130 FROM_HERE, base::Bind(&ChromotingJniRuntime::FetchAuthToken,
131 base::Unretained(this)));
132 return;
133 }
134 JNIEnv* env = base::android::AttachCurrentThread();
135
136 Java_JniInterface_fetchAuthToken(env);
137 }
138
139 void ChromotingJniRuntime::DetachFromVmAndSignal(base::WaitableEvent* waiter) {
140 base::android::DetachFromVM();
141 waiter->Signal();
142 }
143
144 void ChromotingJniRuntime::StartLoggerOnNetworkThread() {
145 DCHECK(network_task_runner()->BelongsToCurrentThread());
146 log_writer_.reset(new TelemetryLogWriter(
147 kTelemetryBaseUrl,
148 base::MakeUnique<ChromiumUrlRequestFactory>(runtime_->url_requester())));
149 log_writer_->SetAuthClosure(
150 base::Bind(&ChromotingJniRuntime::FetchAuthToken,
151 base::Unretained(this)));
152 }
153
154 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698