blob: c877697f62c492e27512a3d87663642e0fd4d6a8 [file] [log] [blame]
nicholss825f7eb2017-03-17 18:48:451// 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
23using base::android::ConvertJavaStringToUTF8;
24using base::android::ConvertUTF8ToJavaString;
25using base::android::JavaParamRef;
26using base::android::ToJavaByteArray;
27
28namespace remoting {
29
nicholss825f7eb2017-03-17 18:48:4530// Implementation of stubs defined in JniInterface_jni.h. These are the entry
31// points for JNI calls from Java into C++.
32
33static 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
43static 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
50static 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
63JniRuntimeDelegate* JniRuntimeDelegate::GetInstance() {
64 return base::Singleton<JniRuntimeDelegate>::get();
65}
66
67JniRuntimeDelegate::JniRuntimeDelegate() {
68 runtime_ = ChromotingClientRuntime::GetInstance();
69}
70
71JniRuntimeDelegate::~JniRuntimeDelegate() {
72 runtime_ = nullptr;
73}
74
75void 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
91void JniRuntimeDelegate::RuntimeDidShutdown() {
92 base::android::LibraryLoaderExitHook();
93 base::android::DetachFromVM();
94}
95
96void 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
109void JniRuntimeDelegate::DetachFromVmAndSignal(base::WaitableEvent* waiter) {
110 base::android::DetachFromVM();
111 waiter->Signal();
112}
113
114} // namespace remoting