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