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

Side by Side Diff: jni/jsEngine.cpp

Issue 9271056: ABP/Android V8 integration code (Closed)
Patch Set: Created Jan. 30, 2013, 9:27 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
« no previous file with comments | « jni/fileOps.cpp ('k') | jni/ops.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of the Adblock Plus,
3 * Copyright (C) 2006-2012 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 <string>
19 #include <android/log.h>
20 #include <jni.h>
21 #include "wrap.h"
22 #include "ops.h"
23
24 const char* scriptDir;
25 const char* dataDir;
26 JNIEnv* jniEnv;
27 jobject jniCallback;
28
29 extern "C"
30 {
31 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize (JNIEnv *pEnv, jobject, jobject pCallback);
32 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease(JNI Env *pEnv, jobject pObj, jlong pContext);
33 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute( JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext);
34 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet(JNIE nv *pEnv, jobject pObj, jstring pKey, jlong pContext);
35 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut(JNIE nv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext);
36 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback(JN IEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pContext) ;
37 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeRunCallbac ks(JNIEnv *pEnv, jobject pObj, jlong pContext);
38 };
39
40 typedef struct __ObjMethod
41 {
42 const char* name;
43 v8::InvocationCallback callback;
44 } ObjMethod;
45
46 static ObjMethod methods[] =
47 {
48 { "load", loadImpl },
49 { "print", printImpl },
50 { "showToast", showToastImpl },
51 { "canAutoupdate", canAutoupdateImpl },
52 { "setStatus", setStatusImpl },
53 { "fileExists", fileExistsImpl },
54 { "fileLastModified", fileLastModifiedImpl },
55 { "fileRemove", fileRemoveImpl },
56 { "fileRename", fileRenameImpl },
57 { "fileRead", fileReadImpl },
58 { "fileWrite", fileWriteImpl },
59 { "setTimeout", setTimeoutImpl },
60 { "httpSend", httpSendImpl },
61 { NULL, NULL }, };
62
63 void reportException(v8::TryCatch* try_catch)
64 {
65 v8::HandleScope handle_scope;
66 v8::String::Utf8Value exception(try_catch->Exception());
67 v8::Handle < v8::Message > message = try_catch->Message();
68 if (message.IsEmpty())
69 {
70 // Exception context is unknown
71 __android_log_print(ANDROID_LOG_ERROR, "JS", "Uncaught exception: %s", *exce ption);
72 }
73 else
74 {
75 v8::String::Utf8Value filename(message->GetScriptResourceName());
76 int linenum = message->GetLineNumber();
77 __android_log_print(ANDROID_LOG_ERROR, "JS", "Uncaught exception in %s:%i: % s\n", *filename, linenum, *exception);
78
79 v8::String::Utf8Value sourceLine(message->GetSourceLine());
80 if (*sourceLine)
81 {
82 fprintf(stderr, "\n%s\n", *sourceLine);
83
84 int startcol = message->GetStartColumn();
85 int endcol = message->GetEndColumn();
86 for (int i = 0; i < startcol; i++)
87 fprintf(stderr, " ");
88 for (int i = startcol; i < endcol; i++)
89 fprintf(stderr, "^");
90 fprintf(stderr, "\n");
91 }
92 }
93 }
94
95 const std::string getString(JNIEnv *pEnv, jstring str)
96 {
97 jboolean iscopy;
98
99 const char *s = pEnv->GetStringUTFChars(str, &iscopy);
100 jsize len = pEnv->GetStringUTFLength(str);
101
102 const std::string value(s, len);
103
104 pEnv->ReleaseStringUTFChars(str, s);
105
106 return value;
107 }
108
109 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
110 {
111 return JNI_VERSION_1_2;
112 }
113
114 void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
115 {
116 }
117
118 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize
119 (JNIEnv *pEnv, jobject, jobject pCallback)
120 {
121 jniEnv = pEnv;
122 jniCallback = pEnv->NewGlobalRef(pCallback);
123
124 v8::HandleScope handle_scope;
125
126 v8::Handle < v8::ObjectTemplate > system = v8::ObjectTemplate::New();
127 for (int i = 0; methods[i].name; i++)
128 system->Set(v8::String::New(methods[i].name), v8::FunctionTemplate::New(meth ods[i].callback));
129
130 v8::Handle < v8::ObjectTemplate > global = v8::ObjectTemplate::New();
131 global->Set(v8::String::New("Android"), system);
132
133 return (jlong) *v8::Persistent<v8::Context>::New(v8::Context::New(NULL, global ));
134 }
135
136 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease
137 (JNIEnv *pEnv, jobject, jlong pContext)
138 {
139 ClearQueue();
140 if (pContext)
141 {
142 v8::Persistent<v8::Context> context((v8::Context *) pContext);
143 context.Dispose();
144 }
145 jniCallback = NULL;
146 jniEnv = NULL;
147 }
148
149 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute
150 (JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext)
151 {
152 v8::HandleScope handle_scope;
153
154 v8::Persistent<v8::Context> context((v8::Context *) pContext);
155 v8::Context::Scope context_scope(context);
156
157 const std::string script = getString(pEnv, pScript);
158
159 v8::Handle<v8::String> source = v8::String::New(script.c_str(), script.size()) ;
160 v8::Handle<v8::Script> compiledScript = v8::Script::Compile(source);
161 {
162 v8::TryCatch try_catch;
163 v8::Handle<v8::Value> result = compiledScript->Run();
164 if (try_catch.HasCaught())
165 {
166 reportException(&try_catch);
167 return NULL;
168 }
169 else
170 {
171 return wrapJSObject(pEnv, result);
172 }
173 }
174 }
175
176 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet
177 (JNIEnv *pEnv, jobject pObj, jstring pKey, jlong pContext)
178 {
179 v8::HandleScope handle_scope;
180
181 v8::Persistent<v8::Context> context((v8::Context *) pContext);
182 v8::Context::Scope context_scope(context);
183
184 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global ()));
185 const std::string key = getString(pEnv, pKey);
186
187 {
188 v8::TryCatch try_catch;
189 v8::Handle<v8::Value> value = obj->Get(v8::String::New(key.c_str(), key.size ()));
190 if (try_catch.HasCaught())
191 {
192 reportException(&try_catch);
193 return NULL;
194 }
195 else
196 {
197 return wrapJSObject(pEnv, value);
198 }
199 }
200 }
201
202 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut
203 (JNIEnv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext)
204 {
205 v8::HandleScope handle_scope;
206
207 v8::Persistent<v8::Context> context((v8::Context *) pContext);
208 v8::Context::Scope context_scope(context);
209
210 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global ()));
211
212 const std::string key = getString(pEnv, pKey);
213 v8::Handle<v8::String> name = v8::String::New(key.c_str(), key.size());
214
215 // v8::Handle<v8::Value> value = obj->Get(name);
216 obj->Set(name, wrapJavaObject(pEnv, pValue));
217
218 return NULL;
219 // return env.HasCaught() ? NULL : env.Wrap(value);
220 }
221
222 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback
223 (JNIEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pCon text)
224 {
225 v8::HandleScope handle_scope;
226
227 v8::Persistent<v8::Context> context((v8::Context *) pContext);
228 v8::Context::Scope context_scope(context);
229
230 v8::Persistent<v8::Function> callback((v8::Function *) pCallback);
231
232 jsize pnum = pEnv->GetArrayLength(pParams);
233 v8::Handle<v8::Value> *args = new v8::Handle<v8::Value>[pnum];
234
235 for (int i = 0; i < pnum; i++)
236 {
237 jobject param = pEnv->GetObjectArrayElement(pParams, i);
238 args[i] = wrapJavaObject(pEnv, param);
239 pEnv->DeleteLocalRef(param);
240 }
241
242 {
243 v8::TryCatch try_catch;
244 callback->Call(context->Global(), pnum, args);
245 callback.Dispose();
246 delete [] args;
247 if (try_catch.HasCaught())
248 reportException(&try_catch);
249 }
250 }
251
252 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeRunCallbacks
253 (JNIEnv *pEnv, jobject pObj, jlong pContext)
254 {
255 v8::HandleScope handle_scope;
256
257 v8::Persistent<v8::Context> context((v8::Context *) pContext);
258 v8::Context::Scope context_scope(context);
259
260 long r = 0;
261 while (r == 0)
262 {
263 v8::TryCatch try_catch;
264 r = RunNextCallback(context);
265 if (try_catch.HasCaught())
266 reportException(&try_catch);
267 }
268 return (jlong) r;
269 }
OLDNEW
« no previous file with comments | « jni/fileOps.cpp ('k') | jni/ops.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld