io_realm_react_RealmReactModule.cpp
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#include <jni.h>
#include <android/log.h>
#include <android/asset_manager_jni.h>
#include "io_realm_react_RealmReactModule.h"
#include "rpc.hpp"
#include "platform.hpp"
#include "jni_utils.hpp"
#include "hack.hpp"
using namespace realm::rpc;
using namespace realm::jni_util;
static RPCServer *s_rpc_server;
extern bool realmContextInjected;
jclass ssl_helper_class;
namespace realm {
// set the AssetManager used to access bundled files within the APK
void set_asset_manager(AAssetManager* assetManager);
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*)
{
// Workaround for some known bugs in system calls on specific devices.
hack_init();
JNIEnv* env;
if (vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}
else {
JniUtils::initialize(vm, JNI_VERSION_1_6);
}
// We do lookup the class in this Thread, since FindClass sometimes fails
// when issued from the sync client thread
ssl_helper_class = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass("io/realm/react/util/SSLHelper")));
return JNI_VERSION_1_6;
}
JNIEXPORT void JNI_OnUnload(JavaVM* vm, void*)
{
JNIEnv* env;
if (vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) {
return;
}
else {
env->DeleteLocalRef(ssl_helper_class);
JniUtils::release();
}
}
JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_setDefaultRealmFileDirectory
(JNIEnv *env, jclass, jstring fileDir, jobject javaAssetManager)
{
__android_log_print(ANDROID_LOG_VERBOSE, "JSRealm", "setDefaultRealmFileDirectory");
// Get the assetManager in case we want to copy files from the APK (assets)
AAssetManager *assetManager = AAssetManager_fromJava(env, javaAssetManager);
if (assetManager == NULL) {
__android_log_print(ANDROID_LOG_ERROR, "JSRealm", "Error loading the AssetManager");
}
realm::set_asset_manager(assetManager);
// Setting the internal storage path for the application
const char* strFileDir = env->GetStringUTFChars(fileDir, NULL);
realm::set_default_realm_file_directory(strFileDir);
env->ReleaseStringUTFChars(fileDir, strFileDir);
__android_log_print(ANDROID_LOG_DEBUG, "JSRealm", "Absolute path: %s", realm::default_realm_file_directory().c_str());
}
JNIEXPORT jlong JNICALL Java_io_realm_react_RealmReactModule_setupChromeDebugModeRealmJsContext
(JNIEnv *, jclass)
{
__android_log_print(ANDROID_LOG_VERBOSE, "JSRealm", "setupChromeDebugModeRealmJsContext");
if (s_rpc_server) {
delete s_rpc_server;
}
s_rpc_server = new RPCServer();
return (jlong)s_rpc_server;
}
JNIEXPORT jstring JNICALL Java_io_realm_react_RealmReactModule_processChromeDebugCommand
(JNIEnv *env, jclass, jstring chrome_cmd, jstring chrome_args)
{
const char* cmd = env->GetStringUTFChars(chrome_cmd, NULL);
const char* args = env->GetStringUTFChars(chrome_args, NULL);
json response = s_rpc_server->perform_request(cmd, json::parse(args));
env->ReleaseStringUTFChars(chrome_cmd, cmd);
env->ReleaseStringUTFChars(chrome_args, args);
return env->NewStringUTF(response.dump().c_str());
}
JNIEXPORT jboolean JNICALL Java_io_realm_react_RealmReactModule_tryRunTask
(JNIEnv *env, jclass)
{
jboolean result = s_rpc_server->try_run_task();
return result;
}
JNIEXPORT jboolean JNICALL Java_io_realm_react_RealmReactModule_isContextInjected
(JNIEnv *env, jclass)
{
return realmContextInjected;
}
JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_clearContextInjectedFlag
(JNIEnv *env, jclass)
{
realmContextInjected = false;
}