OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include "JniCallbacks.h" |
| 19 #include "AdblockPlus/FileSystem.h" |
| 20 #include "Utils.h" |
| 21 #include <sstream> |
| 22 |
| 23 // precached in JNI_OnLoad and released in JNI_OnUnload |
| 24 JniGlobalReference<jclass>* statResultClass; |
| 25 jmethodID existsMethod; |
| 26 jmethodID isDirectoryMethod; |
| 27 jmethodID isFileMethod; |
| 28 jmethodID getLastModifiedMethod; |
| 29 |
| 30 void JniFileSystem_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved) |
| 31 { |
| 32 statResultClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("File
System$StatResult"))); |
| 33 existsMethod = env->GetMethodID(statResultClass->Get(), "exists", "()Z"); |
| 34 isDirectoryMethod = env->GetMethodID(statResultClass->Get(), "isDirectory", "(
)Z"); |
| 35 isFileMethod = env->GetMethodID(statResultClass->Get(), "isFile", "()Z"); |
| 36 getLastModifiedMethod = env->GetMethodID(statResultClass->Get(), "getLastModif
ied", "()J"); |
| 37 } |
| 38 |
| 39 void JniFileSystem_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved) |
| 40 { |
| 41 if (statResultClass) |
| 42 { |
| 43 delete statResultClass; |
| 44 statResultClass = NULL; |
| 45 } |
| 46 } |
| 47 |
| 48 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject callbackObject) |
| 49 { |
| 50 try |
| 51 { |
| 52 return JniPtrToLong(new AdblockPlus::FileSystemPtr(new JniFileSystemCallback
(env, callbackObject))); |
| 53 } |
| 54 CATCH_THROW_AND_RETURN(env, 0) |
| 55 } |
| 56 |
| 57 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
| 58 { |
| 59 delete JniLongToTypePtr<AdblockPlus::FileSystemPtr>(ptr); |
| 60 } |
| 61 |
| 62 JniFileSystemCallback::JniFileSystemCallback(JNIEnv* env, jobject callbackObject
) |
| 63 : JniCallbackBase(env, callbackObject) |
| 64 { |
| 65 } |
| 66 |
| 67 std::shared_ptr<std::istream> JniFileSystemCallback::Read(const std::string& pat
h) const |
| 68 { |
| 69 JNIEnvAcquire env(GetJavaVM()); |
| 70 |
| 71 jmethodID method = env->GetMethodID( |
| 72 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())), |
| 73 "read", |
| 74 "(Ljava/lang/String;)Ljava/lang/String;"); |
| 75 |
| 76 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str())); |
| 77 jstring jData = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *j
Path); |
| 78 CheckAndLogJavaException(*env); |
| 79 |
| 80 if (!jData) |
| 81 return NULL; |
| 82 |
| 83 std::string cData = JniJavaToStdString(*env, jData); |
| 84 std::shared_ptr<std::istream> cSharedStream(new std::istringstream(cData)); |
| 85 return cSharedStream; |
| 86 } |
| 87 |
| 88 void JniFileSystemCallback::Write(const std::string& path, std::shared_ptr<std::
istream> data) |
| 89 { |
| 90 JNIEnvAcquire env(GetJavaVM()); |
| 91 |
| 92 jmethodID method = env->GetMethodID( |
| 93 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())), |
| 94 "write", |
| 95 "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 96 |
| 97 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str())); |
| 98 |
| 99 // read all the data from the stream into buffer (no appropriate way to pass s
treams over JNI) |
| 100 std::string cData = AdblockPlus::Utils::Slurp(*data.get()); |
| 101 JniLocalReference<jstring> jData(*env, env->NewStringUTF(cData.c_str())); |
| 102 |
| 103 env->CallVoidMethod(GetCallbackObject(), method, *jPath, *jData); |
| 104 CheckAndLogJavaException(*env); |
| 105 } |
| 106 |
| 107 void JniFileSystemCallback::Move(const std::string& fromPath, const std::string&
toPath) |
| 108 { |
| 109 JNIEnvAcquire env(GetJavaVM()); |
| 110 |
| 111 jmethodID method = env->GetMethodID( |
| 112 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())), |
| 113 "move", |
| 114 "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 115 |
| 116 JniLocalReference<jstring> jFromPath(*env, env->NewStringUTF(fromPath.c_str())
); |
| 117 JniLocalReference<jstring> jToPath(*env, env->NewStringUTF(toPath.c_str())); |
| 118 |
| 119 env->CallVoidMethod(GetCallbackObject(), method, *jFromPath, *jToPath); |
| 120 CheckAndLogJavaException(*env); |
| 121 } |
| 122 |
| 123 void JniFileSystemCallback::Remove(const std::string& path) |
| 124 { |
| 125 JNIEnvAcquire env(GetJavaVM()); |
| 126 |
| 127 jmethodID method = env->GetMethodID( |
| 128 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())), |
| 129 "remove", |
| 130 "(Ljava/lang/String;)V"); |
| 131 |
| 132 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str())); |
| 133 |
| 134 env->CallVoidMethod(GetCallbackObject(), method, *jPath); |
| 135 CheckAndLogJavaException(*env); |
| 136 } |
| 137 |
| 138 AdblockPlus::FileSystem::StatResult JniFileSystemCallback::Stat(const std::strin
g& path) const |
| 139 { |
| 140 JNIEnvAcquire env(GetJavaVM()); |
| 141 |
| 142 jmethodID method = env->GetMethodID( |
| 143 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())), |
| 144 "stat", |
| 145 "(Ljava/lang/String;)" TYP("FileSystem$StatResult")); |
| 146 |
| 147 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str())); |
| 148 |
| 149 jobject jStatResult = env->CallObjectMethod(GetCallbackObject(), method, *jPat
h); |
| 150 CheckAndLogJavaException(*env); |
| 151 |
| 152 AdblockPlus::FileSystem::StatResult statResult; |
| 153 |
| 154 statResult.exists = env->CallBooleanMethod(jStatResult, existsMethod) ? JNI_TR
UE : JNI_FALSE; |
| 155 CheckAndLogJavaException(*env); |
| 156 |
| 157 statResult.isDirectory = env->CallBooleanMethod(jStatResult, isDirectoryMethod
) ? JNI_TRUE : JNI_FALSE; |
| 158 CheckAndLogJavaException(*env); |
| 159 |
| 160 statResult.isFile = env->CallBooleanMethod(jStatResult, isFileMethod) ? JNI_TR
UE : JNI_FALSE; |
| 161 CheckAndLogJavaException(*env); |
| 162 |
| 163 statResult.lastModified = env->CallLongMethod(jStatResult, getLastModifiedMeth
od); |
| 164 CheckAndLogJavaException(*env); |
| 165 |
| 166 return statResult; |
| 167 } |
| 168 |
| 169 std::string JniFileSystemCallback::Resolve(const std::string& path) const |
| 170 { |
| 171 JNIEnvAcquire env(GetJavaVM()); |
| 172 |
| 173 jmethodID method = env->GetMethodID( |
| 174 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())), |
| 175 "resolve", |
| 176 "(Ljava/lang/String;)Ljava/lang/String;"); |
| 177 |
| 178 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str())); |
| 179 |
| 180 jstring jRet = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *jP
ath); |
| 181 CheckAndLogJavaException(*env); |
| 182 |
| 183 return JniJavaToStdString(*env, jRet); |
| 184 } |
| 185 |
| 186 static JNINativeMethod methods[] = |
| 187 { |
| 188 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, |
| 189 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
| 190 }; |
| 191 |
| 192 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FileSystem
_registerNatives(JNIEnv *env, jclass clazz) |
| 193 { |
| 194 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
| 195 } |
OLD | NEW |