| OLD | NEW | 
| (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 package org.adblockplus.android; | 
 |   19  | 
 |   20 import java.io.IOException; | 
 |   21 import java.io.Reader; | 
 |   22  | 
 |   23 /** | 
 |   24  * Wrapper for JS executor native code. | 
 |   25  */ | 
 |   26 public class JSEngine | 
 |   27 { | 
 |   28   private long context; | 
 |   29  | 
 |   30   public JSEngine(Object callback) | 
 |   31   { | 
 |   32     context = nativeInitialize(callback); | 
 |   33   } | 
 |   34  | 
 |   35   public void release() | 
 |   36   { | 
 |   37     nativeRelease(context); | 
 |   38   } | 
 |   39  | 
 |   40   public Object evaluate(String script) | 
 |   41   { | 
 |   42     if (script == null) | 
 |   43       throw new IllegalArgumentException("empty script"); | 
 |   44     return nativeExecute(script, context); | 
 |   45   } | 
 |   46  | 
 |   47   public Object evaluate(Reader reader) throws IOException | 
 |   48   { | 
 |   49     return evaluate(readAll(reader)); | 
 |   50   } | 
 |   51  | 
 |   52   private String readAll(Reader reader) throws IOException | 
 |   53   { | 
 |   54     StringBuilder sb = new StringBuilder(); | 
 |   55  | 
 |   56     char[] buffer = new char[8192]; | 
 |   57     int read; | 
 |   58  | 
 |   59     while ((read = reader.read(buffer, 0, buffer.length)) > 0) | 
 |   60     { | 
 |   61       sb.append(buffer, 0, read); | 
 |   62     } | 
 |   63  | 
 |   64     return sb.toString(); | 
 |   65   } | 
 |   66  | 
 |   67   public Object get(String name) | 
 |   68   { | 
 |   69     return nativeGet(name, context); | 
 |   70   } | 
 |   71  | 
 |   72   public void put(String name, Object value) | 
 |   73   { | 
 |   74     nativePut(name, value, context); | 
 |   75   } | 
 |   76  | 
 |   77   public long runCallbacks() | 
 |   78   { | 
 |   79     return nativeRunCallbacks(context); | 
 |   80   } | 
 |   81  | 
 |   82   public void callback(long callback, Object[] params) | 
 |   83   { | 
 |   84     nativeCallback(callback, params, context); | 
 |   85   } | 
 |   86  | 
 |   87   private native long nativeInitialize(Object callback); | 
 |   88  | 
 |   89   private native void nativeRelease(long context); | 
 |   90  | 
 |   91   private native Object nativeExecute(String script, long context); | 
 |   92  | 
 |   93   private native Object nativeGet(String name, long context); | 
 |   94  | 
 |   95   private native Object nativePut(String name, Object value, long context); | 
 |   96  | 
 |   97   private native long nativeRunCallbacks(long context); | 
 |   98  | 
 |   99   private native void nativeCallback(long callback, Object[] params, long contex
     t); | 
 |  100  | 
 |  101   static | 
 |  102   { | 
 |  103     System.loadLibrary("jsEngine"); | 
 |  104   } | 
 |  105 } | 
| OLD | NEW |