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

Delta Between Two Patch Sets: libadblockplus-android/jni/JniFileSystem.cpp

Issue 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Left Patch Set: Created July 7, 2016, 10:22 a.m.
Right Patch Set: made helper methods static, fixed 'remove' for fs callback Created Dec. 13, 2016, 9:32 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject callbackObject)
24 {
25 try
26 {
27 return JniPtrToLong(new JniFileSystemCallback(env, callbackObject));
28 }
29 CATCH_THROW_AND_RETURN(env, 0)
30 }
31
32 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
33 {
34 delete JniLongToTypePtr<JniFileSystemCallback>(ptr);
35 }
36
37 JniFileSystemCallback::JniFileSystemCallback(JNIEnv* env, jobject callbackObject )
38 : JniCallbackBase(env, callbackObject),
39 AdblockPlus::FileSystem(),
40 statResultClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("FileSy stem$StatResult"))))
41 {
42 }
43
44 std::shared_ptr<std::istream> JniFileSystemCallback::Read(const std::string& pat h) const
45 {
46 JNIEnvAcquire env(GetJavaVM());
47
48 jmethodID method = env->GetMethodID(
49 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
50 "read",
51 "(Ljava/lang/String;)Ljava/lang/String;");
52
53 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
54 jstring jData = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *j Path);
55 CheckAndLogJavaException(*env);
56
57 if (!jData)
58 return NULL;
59
60 std::string cData = JniJavaToStdString(*env, jData);
61 std::shared_ptr<std::istream> cSharedStream(new std::istringstream(cData));
62 return cSharedStream;
63 }
64
65 void JniFileSystemCallback::Write(const std::string& path, std::shared_ptr<std:: istream> data)
66 {
67 JNIEnvAcquire env(GetJavaVM());
68
69 jmethodID method = env->GetMethodID(
70 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
71 "write",
72 "(Ljava/lang/String;Ljava/lang/String;)V");
73
74 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
75
76 // read all the data from the stream into buffer (no appropriate way to pass s treams over JNI)
77 std::string cData = JniStdStreamToStdString(data.get());
78 JniLocalReference<jstring> jData(*env, env->NewStringUTF(cData.c_str()));
79
80 env->CallVoidMethod(GetCallbackObject(), method, *jPath, *jData);
81 CheckAndLogJavaException(*env);
82 }
83
84 void JniFileSystemCallback::Move(const std::string& fromPath, const std::string& toPath)
85 {
86 JNIEnvAcquire env(GetJavaVM());
87
88 jmethodID method = env->GetMethodID(
89 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
90 "move",
91 "(Ljava/lang/String;Ljava/lang/String;)V");
92
93 JniLocalReference<jstring> jFromPath(*env, env->NewStringUTF(fromPath.c_str()) );
94 JniLocalReference<jstring> jToPath(*env, env->NewStringUTF(toPath.c_str()));
95
96 env->CallVoidMethod(GetCallbackObject(), method, *jFromPath, *jToPath);
97 CheckAndLogJavaException(*env);
98 }
99
100 void JniFileSystemCallback::Remove(const std::string& path)
101 {
102 JNIEnvAcquire env(GetJavaVM());
103
104 jmethodID method = env->GetMethodID(
105 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
106 "remove",
107 "(Ljava/lang/String;)V");
108
109 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
110
111 env->CallVoidMethod(GetCallbackObject(), method, *jPath);
112 CheckAndLogJavaException(*env);
113 }
114
115 AdblockPlus::FileSystem::StatResult JniFileSystemCallback::Stat(const std::strin g& path) const
116 {
117 JNIEnvAcquire env(GetJavaVM());
118
119 jmethodID method = env->GetMethodID(
120 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
121 "stat",
122 "(Ljava/lang/String;)" TYP("FileSystem$StatResult"));
123
124 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
125
126 jobject jStatResult = env->CallObjectMethod(GetCallbackObject(), method, *jPat h);
127 CheckAndLogJavaException(*env);
128
129 AdblockPlus::FileSystem::StatResult statResult;
130 method = env->GetMethodID(
131 *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
132 "exists",
133 "()Z");
134 statResult.exists = env->CallBooleanMethod(jStatResult, method) ? JNI_TRUE : J NI_FALSE;
135
136 method = env->GetMethodID(
137 *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
138 "isDirectory",
139 "()Z");
140 statResult.isDirectory = env->CallBooleanMethod(jStatResult, method) ? JNI_TRU E : JNI_FALSE;
141
142 method = env->GetMethodID(
143 *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
144 "isFile",
145 "()Z");
146 statResult.isFile = env->CallBooleanMethod(jStatResult, method) ? JNI_TRUE : J NI_FALSE;
147
148 method = env->GetMethodID(
149 *JniLocalReference<jclass>(*env, env->GetObjectClass(jStatResult)),
150 "getLastModified",
151 "()J");
152 statResult.lastModified = env->CallLongMethod(jStatResult, method);
153
154 return statResult;
155 }
156
157 std::string JniFileSystemCallback::Resolve(const std::string& path) const
158 {
159 JNIEnvAcquire env(GetJavaVM());
160
161 jmethodID method = env->GetMethodID(
162 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())),
163 "resolve",
164 "(Ljava/lang/String;)Ljava/lang/String;");
165
166 JniLocalReference<jstring> jPath(*env, env->NewStringUTF(path.c_str()));
167
168 jstring jRet = (jstring)env->CallObjectMethod(GetCallbackObject(), method, *jP ath);
169 CheckAndLogJavaException(*env);
170
171 return JniJavaToStdString(*env, jRet);
172 }
173
174 static JNINativeMethod methods[] =
175 {
176 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
177 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
178 };
179
180 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FileSystem _registerNatives(JNIEnv *env, jclass clazz)
181 {
182 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
183 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld