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

Side by Side Diff: jni/utilityOps.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/timerOps.cpp ('k') | jni/wrap.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 "ops.h"
21 #include "wrap.h"
22
23 v8::Handle<v8::Value> loadImpl(const v8::Arguments& args)
24 {
25 v8::HandleScope handle_scope;
26 if (args.Length() < 1)
27 return v8::ThrowException(v8::String::New("File name expected"));
28
29 v8::String::Utf8Value str(args[0]);
30 if (!*str)
31 return v8::ThrowException(v8::String::New("File name isn't a string"));
32
33 jstring jstr = jniEnv->NewStringUTF(*str);
34
35 jstring result;
36 jclass cls = jniEnv->GetObjectClass(jniCallback);
37 jmethodID mid = jniEnv->GetMethodID(cls, "readJSFile", "(Ljava/lang/String;)Lj ava/lang/String;");
38 if (mid)
39 result = (jstring) jniEnv->CallObjectMethod(jniCallback, mid, jstr);
40 jniEnv->DeleteLocalRef(jstr);
41
42 /*
43 const char* src = jniEnv->GetStringUTFChars(result, 0);
44 v8::Handle<v8::String> source = v8::String::New(src, jniEnv->GetStringLength(r esult));
45 jniEnv->ReleaseStringUTFChars(result, src);
46 */
47 v8::Handle<v8::String> source = v8::Handle<v8::String>::Cast(wrapJavaObject(jn iEnv, result));
48 v8::Handle<v8::Script> script = v8::Script::Compile(source, args[0]);
49 if (!script.IsEmpty())
50 script->Run();
51
52 return v8::Undefined();
53 }
54
55 v8::Handle<v8::Value> setStatusImpl(const v8::Arguments& args)
56 {
57 v8::HandleScope handle_scope;
58
59 if (args.Length() < 2)
60 return v8::ThrowException(v8::String::New("Not enough parameters"));
61
62 if (!args[0]->IsString())
63 return v8::ThrowException(v8::String::New("Parameter 0 must be a string"));
64 if (!args[1]->IsNumber())
65 return v8::ThrowException(v8::String::New("Parameter 1 must be a number"));
66
67 v8::String::Utf8Value str(args[0]);
68 if (!*str)
69 return v8::ThrowException(v8::String::New("Parameter cannot be converted to string"));
70 jstring jstr = jniEnv->NewStringUTF(*str);
71
72 jlong jnum = (v8::Handle<v8::Integer>::Cast(args[1]))->Value();
73
74 static jclass cls = jniEnv->GetObjectClass(jniCallback);
75 static jmethodID mid = jniEnv->GetMethodID(cls, "setStatus", "(Ljava/lang/Stri ng;J)V");
76 if (mid)
77 jniEnv->CallVoidMethod(jniCallback, mid, jstr, jnum);
78 jniEnv->DeleteLocalRef(jstr);
79
80 return v8::Undefined();
81 }
82
83 v8::Handle<v8::Value> canAutoupdateImpl(const v8::Arguments& args)
84 {
85 v8::HandleScope handle_scope;
86
87 jboolean result;
88 jclass cls = jniEnv->GetObjectClass(jniCallback);
89 jmethodID mid = jniEnv->GetMethodID(cls, "canAutoupdate", "()Z");
90 if (mid)
91 result = jniEnv->CallBooleanMethod(jniCallback, mid);
92 return wrapJavaObject(jniEnv, NewBoolean(jniEnv, result));
93 }
94
95 v8::Handle<v8::Value> showToastImpl(const v8::Arguments& args)
96 {
97 v8::HandleScope handle_scope;
98 if (args.Length() < 1)
99 return v8::ThrowException(v8::String::New("String expected"));
100
101 v8::String::Utf8Value str(args[0]);
102 if (!*str)
103 return v8::ThrowException(v8::String::New("Parameter cannot be converted t o string"));
104 __android_log_print(ANDROID_LOG_INFO, "ST", *str);
105 jstring jstr = jniEnv->NewStringUTF(*str);
106
107 static jclass cls = jniEnv->GetObjectClass(jniCallback);
108 static jmethodID mid = jniEnv->GetMethodID(cls, "showToast", "(Ljava/lang/Stri ng;)V");
109 if (mid)
110 jniEnv->CallVoidMethod(jniCallback, mid, jstr);
111 jniEnv->DeleteLocalRef(jstr);
112
113 return v8::Undefined();
114 }
115
116 v8::Handle<v8::Value> printImpl(const v8::Arguments& args)
117 {
118 bool first = true;
119 std::string msg = "";
120 for (int i = 0; i < args.Length(); i++)
121 {
122 v8::HandleScope handle_scope;
123 if (first)
124 first = false;
125 else
126 msg += " ";
127 v8::String::Utf8Value str(args[i]);
128 if (!*str)
129 return v8::ThrowException(v8::String::New("Parameter cannot be converted t o string"));
130 msg += *str;
131 }
132 __android_log_print(ANDROID_LOG_INFO, "JS", msg.c_str());
133 return v8::Undefined();
134 }
OLDNEW
« no previous file with comments | « jni/timerOps.cpp ('k') | jni/wrap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld