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

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

Issue 29678581: Issue 6000 - Rename "libadblockplus-android" (Closed)
Patch Set: Created Jan. 24, 2018, 6:53 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-present 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 <AdblockPlus.h>
19 #include "Utils.h"
20 #include "JniCallbacks.h"
21 #include "JniPlatform.h"
22
23 static void TransformAppInfo(JNIEnv* env, jobject jAppInfo, AdblockPlus::AppInfo & appInfo)
24 {
25 jclass clazz = env->GetObjectClass(jAppInfo);
26
27 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application");
28 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat ionVersion");
29 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale");
30 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name");
31 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version");
32
33 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme ntBuild");
34 }
35
36 static AdblockPlus::Platform& GetPlatformRef(jlong ptr)
37 {
38 return *JniLongToTypePtr<JniPlatform>(ptr)->platform;
39 }
40
41 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz,
42 jobject logSystem, jobject webRequest, jstring jBas ePath)
43 {
44 try
45 {
46 JniPlatform* jniPlatform = new JniPlatform();
47 AdblockPlus::DefaultPlatformBuilder platformBuilder;
48 jniPlatform->scheduler = platformBuilder.GetDefaultAsyncExecutor();
49 if (logSystem)
50 {
51 platformBuilder.logSystem.reset(new JniLogSystemCallback(env, logSystem));
52 }
53 if (webRequest)
54 {
55 platformBuilder.CreateDefaultWebRequest(AdblockPlus::WebRequestSyncPtr(new JniWebRequest(env, webRequest)));
56 }
57 if (jBasePath)
58 {
59 platformBuilder.CreateDefaultFileSystem(JniJavaToStdString(env, jBasePath) );
60 }
61 jniPlatform->platform = platformBuilder.CreatePlatform();
62 return JniPtrToLong(jniPlatform);
63 }
64 CATCH_THROW_AND_RETURN(env, 0)
65 }
66
67 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
68 {
69 delete JniLongToTypePtr<JniPlatform>(ptr);
70 }
71
72 static void JNICALL JniSetUpJsEngine(JNIEnv* env, jclass clazz,
73 jlong ptr, jobject jAppInfo, jlong v8Isolat eProviderPtr)
74 {
75 try
76 {
77 AdblockPlus::AppInfo appInfo;
78 TransformAppInfo(env, jAppInfo, appInfo);
79 std::unique_ptr<AdblockPlus::IV8IsolateProvider> isolateProvider;
80 if (v8IsolateProviderPtr)
81 {
82 isolateProvider.reset(JniLongToTypePtr<AdblockPlus::IV8IsolateProvider>(v8 IsolateProviderPtr));
83 }
84
85 GetPlatformRef(ptr).SetUpJsEngine(appInfo, std::move(isolateProvider));
86 }
87 CATCH_AND_THROW(env)
88 }
89
90 static long JNICALL JniGetJsEnginePtr(JNIEnv* env, jclass clazz, jlong ptr)
91 {
92 try
93 {
94 return JniPtrToLong(&GetPlatformRef(ptr).GetJsEngine());
95 }
96 CATCH_THROW_AND_RETURN(env, 0)
97 }
98
99 static void JNICALL JniSetUpFilterEngine(JNIEnv* env, jclass clazz, jlong ptr, j object jIsSubscriptionDownloadAllowedCallback)
100 {
101 try
102 {
103 AdblockPlus::FilterEngine::CreationParameters creationParameters;
104 if (jIsSubscriptionDownloadAllowedCallback)
105 {
106 auto callback = std::make_shared<JniIsAllowedConnectionTypeCallback>(env, jIsSubscriptionDownloadAllowedCallback);
107 auto scheduler = JniLongToTypePtr<JniPlatform>(ptr)->scheduler;
108 creationParameters.isSubscriptionDownloadAllowedCallback =
109 [scheduler, callback](const std::string* allowedConnectionTypeArg, const std::function<void(bool)>& doneCallback)
110 {
111 std::shared_ptr<std::string> allowedConnectionType;
112 if (allowedConnectionTypeArg)
113 {
114 allowedConnectionType = std::make_shared<std::string>(*allowedConnec tionTypeArg);
115 }
116 scheduler([callback, allowedConnectionType, doneCallback]
117 {
118 doneCallback(callback->Callback(allowedConnectionType.get()));
119 });
120 };
121 }
122 GetPlatformRef(ptr).CreateFilterEngineAsync(creationParameters);
123 }
124 CATCH_AND_THROW(env)
125 }
126
127 static void JNICALL JniEnsureFilterEngine(JNIEnv* env, jclass clazz, jlong ptr)
128 {
129 try
130 {
131 GetPlatformRef(ptr).GetFilterEngine();
132 }
133 CATCH_AND_THROW(env)
134 }
135
136 static JNINativeMethod methods[] =
137 {
138 { (char*)"ctor", (char*)"(" TYP("LogSystem") TYP("WebRequest") "Ljava/lang/Str ing;)J", (void*)JniCtor },
139 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
140
141 { (char*)"setUpJsEngine", (char*)"(J" TYP("AppInfo") "J)V", (void*)JniSetUpJsE ngine },
142 { (char*)"getJsEnginePtr", (char*)"(J)J", (void*)JniGetJsEnginePtr },
143 { (char*)"setUpFilterEngine", (char*)"(J" TYP("IsAllowedConnectionCallback") " )V", (void*)JniSetUpFilterEngine },
144 { (char*)"ensureFilterEngine", (char*)"(J)V", (void*)JniEnsureFilterEngine }
145 };
146
147 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_Platform_r egisterNatives(JNIEnv *env, jclass clazz)
148 {
149 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
150 }
OLDNEW

Powered by Google App Engine
This is Rietveld