LEFT | RIGHT |
(no file at all) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 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 package org.adblockplus.libadblockplus; |
| 19 |
| 20 import java.util.List; |
| 21 |
| 22 import com.github.rjeschke.neetutils.dispose.Disposable; |
| 23 import com.github.rjeschke.neetutils.dispose.Disposer; |
| 24 |
| 25 public class JsEngine implements Disposable |
| 26 { |
| 27 private final Disposer disposer; |
| 28 protected final long ptr; |
| 29 |
| 30 static |
| 31 { |
| 32 System.loadLibrary("adblockplus-jni"); |
| 33 registerNatives(); |
| 34 } |
| 35 |
| 36 public JsEngine(final AppInfo appInfo) |
| 37 { |
| 38 this(ctor(appInfo)); |
| 39 } |
| 40 |
| 41 protected JsEngine(final long ptr) |
| 42 { |
| 43 this.ptr = ptr; |
| 44 this.disposer = new Disposer(this, new DisposeWrapper(ptr)); |
| 45 } |
| 46 |
| 47 public void setEventCallback(final String eventName, final EventCallback callb
ack) |
| 48 { |
| 49 setEventCallback(this.ptr, eventName, callback.ptr); |
| 50 } |
| 51 |
| 52 public void removeEventCallback(final String eventName) |
| 53 { |
| 54 removeEventCallback(this.ptr, eventName); |
| 55 } |
| 56 |
| 57 public JsValue evaluate(final String source, final String filename) |
| 58 { |
| 59 return evaluate(this.ptr, source, filename); |
| 60 } |
| 61 |
| 62 public JsValue evaluate(final String source) |
| 63 { |
| 64 return evaluate(this.ptr, source, ""); |
| 65 } |
| 66 |
| 67 public void triggerEvent(final String eventName, final List<JsValue> params) |
| 68 { |
| 69 final long[] args = new long[params.size()]; |
| 70 |
| 71 for (int i = 0; i < args.length; i++) |
| 72 { |
| 73 args[i] = params.get(i).ptr; |
| 74 } |
| 75 |
| 76 triggerEvent(this.ptr, eventName, args); |
| 77 } |
| 78 |
| 79 public void triggerEvent(final String eventName) |
| 80 { |
| 81 triggerEvent(this.ptr, eventName, null); |
| 82 } |
| 83 |
| 84 public void setDefaultFileSystem(final String basePath) |
| 85 { |
| 86 setDefaultFileSystem(this.ptr, basePath); |
| 87 } |
| 88 |
| 89 public void setDefaultLogSystem() |
| 90 { |
| 91 setDefaultLogSystem(this.ptr); |
| 92 } |
| 93 |
| 94 public void setLogSystem(final LogSystem logSystem) |
| 95 { |
| 96 setLogSystem(this.ptr, logSystem.ptr); |
| 97 } |
| 98 |
| 99 public void setDefaultWebRequest() |
| 100 { |
| 101 setDefaultWebRequest(this.ptr); |
| 102 } |
| 103 |
| 104 public void setWebRequest(final WebRequest webRequest) |
| 105 { |
| 106 setWebRequest(this.ptr, webRequest.ptr); |
| 107 } |
| 108 |
| 109 public JsValue newValue(final long value) |
| 110 { |
| 111 return newValue(this.ptr, value); |
| 112 } |
| 113 |
| 114 public JsValue newValue(final boolean value) |
| 115 { |
| 116 return newValue(this.ptr, value); |
| 117 } |
| 118 |
| 119 public JsValue newValue(final String value) |
| 120 { |
| 121 return newValue(this.ptr, value); |
| 122 } |
| 123 |
| 124 @Override |
| 125 public void dispose() |
| 126 { |
| 127 this.disposer.dispose(); |
| 128 } |
| 129 |
| 130 private final static class DisposeWrapper implements Disposable |
| 131 { |
| 132 private final long ptr; |
| 133 |
| 134 public DisposeWrapper(final long ptr) |
| 135 { |
| 136 this.ptr = ptr; |
| 137 } |
| 138 |
| 139 @Override |
| 140 public void dispose() |
| 141 { |
| 142 dtor(this.ptr); |
| 143 } |
| 144 } |
| 145 |
| 146 private final static native void registerNatives(); |
| 147 |
| 148 private final static native long ctor(AppInfo appInfo); |
| 149 |
| 150 private final static native void setEventCallback(long ptr, String eventName,
long callback); |
| 151 |
| 152 private final static native void removeEventCallback(long ptr, String eventNam
e); |
| 153 |
| 154 private final static native JsValue evaluate(long ptr, String source, String f
ilename); |
| 155 |
| 156 private final static native void triggerEvent(long ptr, String eventName, long
[] args); |
| 157 |
| 158 private final static native void setDefaultFileSystem(long ptr, String basePat
h); |
| 159 |
| 160 private final static native void setLogSystem(long ptr, long logSystemPtr); |
| 161 |
| 162 private final static native void setDefaultLogSystem(long ptr); |
| 163 |
| 164 private final static native void setWebRequest(long ptr, long webRequestPtr); |
| 165 |
| 166 private final static native void setDefaultWebRequest(long ptr); |
| 167 |
| 168 private final static native JsValue newValue(long ptr, long value); |
| 169 |
| 170 private final static native JsValue newValue(long ptr, boolean value); |
| 171 |
| 172 private final static native JsValue newValue(long ptr, String value); |
| 173 |
| 174 private final static native void dtor(long ptr); |
| 175 } |
LEFT | RIGHT |