Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: libadblockplus-android/jni/JniFileSystem.cpp

Issue 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: patchset 2 + patchset 3 + applied Eyeo's codestyle Created Aug. 8, 2016, 8:09 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « libadblockplus-android/jni/JniCallbacks.h ('k') | libadblockplus-android/jni/JniJsEngine.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: libadblockplus-android/jni/JniFileSystem.cpp
diff --git a/libadblockplus-android/jni/JniFileSystem.cpp b/libadblockplus-android/jni/JniFileSystem.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d32457d25e750443fcca4cf6dc2808949b2f81fb
--- /dev/null
+++ b/libadblockplus-android/jni/JniFileSystem.cpp
@@ -0,0 +1,183 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "JniCallbacks.h"
+#include "AdblockPlus/FileSystem.h"
+#include "Utils.h"
+#include <sstream>
+
+static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject callbackObject)
+{
+ try
+ {
+ return JniPtrToLong(new JniFileSystemCallback(env, callbackObject));
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ delete JniLongToTypePtr<JniFileSystemCallback>(ptr);
+}
+
+JniFileSystemCallback::JniFileSystemCallback(JNIEnv* env, jobject callbackObject)
+ : JniCallbackBase(env, callbackObject),
+ AdblockPlus::FileSystem(),
+ statResultClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("FileSystem$StatResult"))))
+{
+}
+
+std::shared_ptr<std::istream> JniFileSystemCallback::Read(const std::string& path) const
+{
+ JNIEnvAcquire env(GetJavaVM());
+
+ jmethodID method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
+ "read",
+ "(Ljava/lang/String;)Ljava/lang/String;");
+
+ JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
+ jstring jData = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *jPath);
+ CheckAndLogJavaException(*env);
+
+ if (!jData)
+ return NULL;
+
+ std::string cData = JniJavaToStdString(*env, jData);
+ std::shared_ptr<std::istream> cSharedStream(new std::istringstream(cData));
+ return cSharedStream;
+}
+
+void JniFileSystemCallback::Write(const std::string& path, std::shared_ptr<std::istream> data)
+{
+ JNIEnvAcquire env(GetJavaVM());
+
+ jmethodID method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
+ "write",
+ "(Ljava/lang/String;Ljava/lang/String;)V");
+
+ JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
+
+ // read all the data from the stream into buffer (no appropriate way to pass streams over JNI)
+ std::string cData = JniStdStreamToStdString(data.get());
+ JniLocalReference<jstring> jData(*env, env->NewStringUTF(cData.c_str()));
+
+ env->CallVoidMethod(GetCallbackObject(), method, *jPath, *jData);
+ CheckAndLogJavaException(*env);
+}
+
+void JniFileSystemCallback::Move(const std::string& fromPath, const std::string& toPath)
+{
+ JNIEnvAcquire env(GetJavaVM());
+
+ jmethodID method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
+ "move",
+ "(Ljava/lang/String;Ljava/lang/String;)V");
+
+ JniLocalReference<jstring> jFromPath(*env, env->NewStringUTF(fromPath.c_str()));
+ JniLocalReference<jstring> jToPath(*env, env->NewStringUTF(toPath.c_str()));
+
+ env->CallVoidMethod(GetCallbackObject(), method, *jFromPath, *jToPath);
+ CheckAndLogJavaException(*env);
+}
+
+void JniFileSystemCallback::Remove(const std::string& path)
+{
+ JNIEnvAcquire env(GetJavaVM());
+
+ jmethodID method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
+ "move",
+ "(Ljava/lang/String;Ljava/lang/String;)V");
+
+ JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
+
+ env->CallVoidMethod(GetCallbackObject(), method, *jPath);
+ CheckAndLogJavaException(*env);
+}
+
+AdblockPlus::FileSystem::StatResult JniFileSystemCallback::Stat(const std::string& path) const
+{
+ JNIEnvAcquire env(GetJavaVM());
+
+ jmethodID method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
+ "stat",
+ "(Ljava/lang/String;)" TYP("FileSystem$StatResult"));
+
+ JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
+
+ jobject jStatResult = env->CallObjectMethod(GetCallbackObject(), method, *jPath);
+ CheckAndLogJavaException(*env);
+
+ AdblockPlus::FileSystem::StatResult statResult;
+ method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
+ "exists",
+ "()Z");
+ statResult.exists = env->CallBooleanMethod(jStatResult, method) ? JNI_TRUE : JNI_FALSE;
+
+ method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
+ "isDirectory",
+ "()Z");
+ statResult.isDirectory = env->CallBooleanMethod(jStatResult, method) ? JNI_TRUE : JNI_FALSE;
+
+ method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
+ "isFile",
+ "()Z");
+ statResult.isFile = env->CallBooleanMethod(jStatResult, method) ? JNI_TRUE : JNI_FALSE;
+
+ method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
+ "getLastModified",
+ "()J");
+ statResult.lastModified = env->CallLongMethod(jStatResult, method);
+
+ return statResult;
+}
+
+std::string JniFileSystemCallback::Resolve(const std::string& path) const
+{
+ JNIEnvAcquire env(GetJavaVM());
+
+ jmethodID method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
+ "resolve",
+ "(Ljava/lang/String;)Ljava/lang/String;");
+
+ JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
+
+ jstring jRet = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *jPath);
+ CheckAndLogJavaException(*env);
+
+ return JniJavaToStdString(*env, jRet);
+}
+
+static JNINativeMethod methods[] =
+{
+ { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
+ { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
+};
+
+extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FileSystem_registerNatives(JNIEnv *env, jclass clazz)
+{
+ env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
+}
« no previous file with comments | « libadblockplus-android/jni/JniCallbacks.h ('k') | libadblockplus-android/jni/JniJsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld