OLD | NEW |
---|---|
(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/jni_runtime_delegate.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 remoting { | |
29 | |
30 bool RegisterJniRuntimeDelegate(JNIEnv* env) { | |
31 return remoting::RegisterNativesImpl(env); | |
32 } | |
33 | |
34 // Implementation of stubs defined in JniInterface_jni.h. These are the entry | |
35 // points for JNI calls from Java into C++. | |
36 | |
37 static void LoadNative(JNIEnv* env, const JavaParamRef<jclass>& clazz) { | |
38 base::CommandLine::Init(0, nullptr); | |
39 | |
40 // Create the singleton now so that the Chromoting threads will be set up. | |
41 ChromotingClientRuntime* runtime = | |
42 remoting::ChromotingClientRuntime::GetInstance(); | |
43 JniRuntimeDelegate* delegate = remoting::JniRuntimeDelegate::GetInstance(); | |
44 runtime->SetDelegate(delegate); | |
45 } | |
46 | |
47 static void HandleAuthTokenOnNetworkThread(const std::string& token) { | |
48 ChromotingClientRuntime* runtime = | |
49 remoting::ChromotingClientRuntime::GetInstance(); | |
50 DCHECK(runtime->network_task_runner()->BelongsToCurrentThread()); | |
51 runtime->GetLogWriter()->SetAuthToken(token); | |
52 } | |
53 | |
54 static void OnAuthTokenFetched(JNIEnv* env, | |
55 const JavaParamRef<jclass>& clazz, | |
56 const JavaParamRef<jstring>& token) { | |
57 ChromotingClientRuntime* runtime = | |
58 remoting::ChromotingClientRuntime::GetInstance(); | |
59 runtime->network_task_runner()->PostTask( | |
60 FROM_HERE, base::Bind(&HandleAuthTokenOnNetworkThread, | |
61 ConvertJavaStringToUTF8(env, token))); | |
62 } | |
63 | |
64 // JniRuntimeDelegate implementation. | |
65 | |
66 // static | |
67 JniRuntimeDelegate* JniRuntimeDelegate::GetInstance() { | |
68 return base::Singleton<JniRuntimeDelegate>::get(); | |
69 } | |
70 | |
71 JniRuntimeDelegate::JniRuntimeDelegate() { | |
72 runtime_ = ChromotingClientRuntime::GetInstance(); | |
73 } | |
74 | |
75 JniRuntimeDelegate::~JniRuntimeDelegate() { | |
76 runtime_ = nullptr; | |
77 } | |
78 | |
79 void JniRuntimeDelegate::RuntimeWillShutdown() { | |
80 DCHECK(runtime_->ui_task_runner()->BelongsToCurrentThread()); | |
81 | |
82 base::WaitableEvent done_event( | |
83 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
84 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
85 runtime_->network_task_runner()->PostTask( | |
86 FROM_HERE, base::Bind(&JniRuntimeDelegate::DetachFromVmAndSignal, | |
87 base::Unretained(this), &done_event)); | |
88 done_event.Wait(); | |
89 runtime_->display_task_runner()->PostTask( | |
90 FROM_HERE, base::Bind(&JniRuntimeDelegate::DetachFromVmAndSignal, | |
91 base::Unretained(this), &done_event)); | |
92 done_event.Wait(); | |
93 | |
94 // Block until tasks blocking shutdown have completed their execution. | |
95 base::TaskScheduler::GetInstance()->Shutdown(); | |
Yuwei
2017/03/10 20:18:53
I feel it's weird to create the TaskScheduler in t
nicholss
2017/03/10 20:53:11
Maybe I need to add a RuntimeDidShutdown method fo
| |
96 | |
97 base::android::LibraryLoaderExitHook(); | |
98 base::android::DetachFromVM(); | |
99 } | |
100 | |
101 void JniRuntimeDelegate::RequestAuthTokenForLogger() { | |
102 if (!runtime_->ui_task_runner()->BelongsToCurrentThread()) { | |
103 runtime_->ui_task_runner()->PostTask( | |
104 FROM_HERE, base::Bind(&JniRuntimeDelegate::RequestAuthTokenForLogger, | |
105 base::Unretained(this))); | |
106 return; | |
107 } | |
108 JNIEnv* env = base::android::AttachCurrentThread(); | |
109 | |
110 // TODO(nicholss): I do not like this method name, change it soon. | |
111 Java_JniInterface_fetchAuthToken(env); | |
112 } | |
113 | |
114 void JniRuntimeDelegate::DetachFromVmAndSignal(base::WaitableEvent* waiter) { | |
115 base::android::DetachFromVM(); | |
116 waiter->Signal(); | |
117 } | |
118 | |
119 } // namespace remoting | |
OLD | NEW |