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

Side by Side Diff: libadblockplus-android/jni/JniFileSystem.cpp

Issue 29424615: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: using method from c++ utils Created April 28, 2017, 10:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 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<JniFileSystemCallback>(ptr);
60 }
61
62 JniFileSystemCallback::JniFileSystemCallback(JNIEnv* env, jobject callbackObject )
63 : JniCallbackBase(env, callbackObject),
64 AdblockPlus::FileSystem()
sergei 2017/05/22 12:09:07 The call of AdblockPlus::FileSystem() is not requi
anton 2017/05/22 13:04:37 Acknowledged.
65 {
66 }
67
68 std::shared_ptr<std::istream> JniFileSystemCallback::Read(const std::string& pat h) const
69 {
70 JNIEnvAcquire env(GetJavaVM());
71
72 jmethodID method = env->GetMethodID(
73 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
74 "read",
75 "(Ljava/lang/String;)Ljava/lang/String;");
76
77 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
78 jstring jData = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *j Path);
79 CheckAndLogJavaException(*env);
80
81 if (!jData)
82 return NULL;
83
84 std::string cData = JniJavaToStdString(*env, jData);
85 std::shared_ptr<std::istream> cSharedStream(new std::istringstream(cData));
86 return cSharedStream;
87 }
88
89 void JniFileSystemCallback::Write(const std::string& path, std::shared_ptr<std:: istream> data)
90 {
91 JNIEnvAcquire env(GetJavaVM());
92
93 jmethodID method = env->GetMethodID(
94 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
95 "write",
96 "(Ljava/lang/String;Ljava/lang/String;)V");
97
98 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
99
100 // read all the data from the stream into buffer (no appropriate way to pass s treams over JNI)
101 std::string cData = AdblockPlus::Utils::Slurp(*data.get());
102 JniLocalReference<jstring> jData(*env, env->NewStringUTF(cData.c_str()));
103
104 env->CallVoidMethod(GetCallbackObject(), method, *jPath, *jData);
105 CheckAndLogJavaException(*env);
106 }
107
108 void JniFileSystemCallback::Move(const std::string& fromPath, const std::string& toPath)
109 {
110 JNIEnvAcquire env(GetJavaVM());
111
112 jmethodID method = env->GetMethodID(
113 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
114 "move",
115 "(Ljava/lang/String;Ljava/lang/String;)V");
116
117 JniLocalReference<jstring> jFromPath(*env, env->NewStringUTF(fromPath.c_str()) );
118 JniLocalReference<jstring> jToPath(*env, env->NewStringUTF(toPath.c_str()));
119
120 env->CallVoidMethod(GetCallbackObject(), method, *jFromPath, *jToPath);
121 CheckAndLogJavaException(*env);
122 }
123
124 void JniFileSystemCallback::Remove(const std::string& path)
125 {
126 JNIEnvAcquire env(GetJavaVM());
127
128 jmethodID method = env->GetMethodID(
129 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
130 "remove",
131 "(Ljava/lang/String;)V");
132
133 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
134
135 env->CallVoidMethod(GetCallbackObject(), method, *jPath);
136 CheckAndLogJavaException(*env);
137 }
138
139 AdblockPlus::FileSystem::StatResult JniFileSystemCallback::Stat(const std::strin g& path) const
140 {
141 JNIEnvAcquire env(GetJavaVM());
142
143 jmethodID method = env->GetMethodID(
144 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
145 "stat",
146 "(Ljava/lang/String;)" TYP("FileSystem$StatResult"));
147
148 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
149
150 jobject jStatResult = env->CallObjectMethod(GetCallbackObject(), method, *jPat h);
151 CheckAndLogJavaException(*env);
152
153 AdblockPlus::FileSystem::StatResult statResult;
154
155 statResult.exists = env->CallBooleanMethod(jStatResult, existsMethod) ? JNI_TR UE : JNI_FALSE;
156 CheckAndLogJavaException(*env);
157
158 statResult.isDirectory = env->CallBooleanMethod(jStatResult, isDirectoryMethod ) ? JNI_TRUE : JNI_FALSE;
159 CheckAndLogJavaException(*env);
160
161 statResult.isFile = env->CallBooleanMethod(jStatResult, isFileMethod) ? JNI_TR UE : JNI_FALSE;
162 CheckAndLogJavaException(*env);
163
164 statResult.lastModified = env->CallLongMethod(jStatResult, getLastModifiedMeth od);
165 CheckAndLogJavaException(*env);
166
167 return statResult;
168 }
169
170 std::string JniFileSystemCallback::Resolve(const std::string& path) const
171 {
172 JNIEnvAcquire env(GetJavaVM());
173
174 jmethodID method = env->GetMethodID(
175 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
176 "resolve",
177 "(Ljava/lang/String;)Ljava/lang/String;");
178
179 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
180
181 jstring jRet = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *jP ath);
182 CheckAndLogJavaException(*env);
183
184 return JniJavaToStdString(*env, jRet);
185 }
186
187 static JNINativeMethod methods[] =
188 {
189 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
190 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
191 };
192
193 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FileSystem _registerNatives(JNIEnv *env, jclass clazz)
194 {
195 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld