1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/util/NativeJSObject.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,226 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.util; 1.10 + 1.11 +import org.mozilla.gecko.mozglue.JNITarget; 1.12 + 1.13 +/** 1.14 + * NativeJSObject is a wrapper around the SpiderMonkey JSAPI to make it possible to 1.15 + * access Javascript objects in Java. 1.16 + */ 1.17 +@JNITarget 1.18 +public class NativeJSObject 1.19 +{ 1.20 + private final NativeJSContainer mContainer; 1.21 + private final int mObjectIndex; 1.22 + 1.23 + protected NativeJSObject() { 1.24 + mContainer = (NativeJSContainer)this; 1.25 + mObjectIndex = -1; 1.26 + } 1.27 + 1.28 + private NativeJSObject(NativeJSContainer container, int index) { 1.29 + mContainer = container; 1.30 + mObjectIndex = index; 1.31 + } 1.32 + 1.33 + /** 1.34 + * Returns the value of a boolean property. 1.35 + * 1.36 + * @param name 1.37 + * Property name 1.38 + * @throws IllegalArgumentException 1.39 + * If the property does not exist or if its type does not match the return type 1.40 + * @throws NullPointerException 1.41 + * If name is null or if this JS object has been disposed 1.42 + * @throws IllegalThreadStateException 1.43 + * If not called on the thread this object is attached to 1.44 + * @throws UnsupportedOperationException 1.45 + * If an internal JSAPI call failed 1.46 + */ 1.47 + public native boolean getBoolean(String name); 1.48 + 1.49 + /** 1.50 + * Returns the value of a boolean property. 1.51 + * 1.52 + * @param name 1.53 + * Property name 1.54 + * @param fallback 1.55 + * Value to return if property does not exist 1.56 + * @throws IllegalArgumentException 1.57 + * If the property exists and its type does not match the return type 1.58 + * @throws NullPointerException 1.59 + * If name is null or if this JS object has been disposed 1.60 + * @throws IllegalThreadStateException 1.61 + * If not called on the thread this object is attached to 1.62 + * @throws UnsupportedOperationException 1.63 + * If an internal JSAPI call failed 1.64 + */ 1.65 + public native boolean optBoolean(String name, boolean fallback); 1.66 + 1.67 + /** 1.68 + * Returns the value of a double property. 1.69 + * 1.70 + * @param name 1.71 + * Property name 1.72 + * @throws IllegalArgumentException 1.73 + * If the property does not exist or if its type does not match the return type 1.74 + * @throws NullPointerException 1.75 + * If name is null or if this JS object has been disposed 1.76 + * @throws IllegalThreadStateException 1.77 + * If not called on the thread this object is attached to 1.78 + * @throws UnsupportedOperationException 1.79 + * If an internal JSAPI call failed 1.80 + */ 1.81 + public native double getDouble(String name); 1.82 + 1.83 + /** 1.84 + * Returns the value of a double property. 1.85 + * 1.86 + * @param name 1.87 + * Property name 1.88 + * @param fallback 1.89 + * Value to return if property does not exist 1.90 + * @throws IllegalArgumentException 1.91 + * If the property exists and its type does not match the return type 1.92 + * @throws NullPointerException 1.93 + * If name is null or if this JS object has been disposed 1.94 + * @throws IllegalThreadStateException 1.95 + * If not called on the thread this object is attached to 1.96 + * @throws UnsupportedOperationException 1.97 + * If an internal JSAPI call failed 1.98 + */ 1.99 + public native double optDouble(String name, double fallback); 1.100 + 1.101 + /** 1.102 + * Returns the value of an int property. 1.103 + * 1.104 + * @param name 1.105 + * Property name 1.106 + * @throws IllegalArgumentException 1.107 + * If the property does not exist or if its type does not match the return type 1.108 + * @throws NullPointerException 1.109 + * If name is null or if this JS object has been disposed 1.110 + * @throws IllegalThreadStateException 1.111 + * If not called on the thread this object is attached to 1.112 + * @throws UnsupportedOperationException 1.113 + * If an internal JSAPI call failed 1.114 + */ 1.115 + public native int getInt(String name); 1.116 + 1.117 + /** 1.118 + * Returns the value of an int property. 1.119 + * 1.120 + * @param name 1.121 + * Property name 1.122 + * @param fallback 1.123 + * Value to return if property does not exist 1.124 + * @throws IllegalArgumentException 1.125 + * If the property exists and its type does not match the return type 1.126 + * @throws NullPointerException 1.127 + * If name is null or if this JS object has been disposed 1.128 + * @throws IllegalThreadStateException 1.129 + * If not called on the thread this object is attached to 1.130 + * @throws UnsupportedOperationException 1.131 + * If an internal JSAPI call failed 1.132 + */ 1.133 + public native int optInt(String name, int fallback); 1.134 + 1.135 + /** 1.136 + * Returns the value of an object property. 1.137 + * 1.138 + * @param name 1.139 + * Property name 1.140 + * @throws IllegalArgumentException 1.141 + * If the property does not exist or if its type does not match the return type 1.142 + * @throws NullPointerException 1.143 + * If name is null or if this JS object has been disposed 1.144 + * @throws IllegalThreadStateException 1.145 + * If not called on the thread this object is attached to 1.146 + * @throws UnsupportedOperationException 1.147 + * If an internal JSAPI call failed 1.148 + */ 1.149 + public native NativeJSObject getObject(String name); 1.150 + 1.151 + /** 1.152 + * Returns the value of an object property. 1.153 + * 1.154 + * @param name 1.155 + * Property name 1.156 + * @param fallback 1.157 + * Value to return if property does not exist 1.158 + * @throws IllegalArgumentException 1.159 + * If the property exists and its type does not match the return type 1.160 + * @throws NullPointerException 1.161 + * If name is null or if this JS object has been disposed 1.162 + * @throws IllegalThreadStateException 1.163 + * If not called on the thread this object is attached to 1.164 + * @throws UnsupportedOperationException 1.165 + * If an internal JSAPI call failed 1.166 + */ 1.167 + public native NativeJSObject optObject(String name, NativeJSObject fallback); 1.168 + 1.169 + /** 1.170 + * Returns the value of a string property. 1.171 + * 1.172 + * @param name 1.173 + * Property name 1.174 + * @throws IllegalArgumentException 1.175 + * If the property does not exist or if its type does not match the return type 1.176 + * @throws NullPointerException 1.177 + * If name is null or if this JS object has been disposed 1.178 + * @throws IllegalThreadStateException 1.179 + * If not called on the thread this object is attached to 1.180 + * @throws UnsupportedOperationException 1.181 + * If an internal JSAPI call failed 1.182 + */ 1.183 + public native String getString(String name); 1.184 + 1.185 + /** 1.186 + * Returns the value of a string property. 1.187 + * 1.188 + * @param name 1.189 + * Property name 1.190 + * @param fallback 1.191 + * Value to return if property does not exist 1.192 + * @throws IllegalArgumentException 1.193 + * If the property exists and its type does not match the return type 1.194 + * @throws NullPointerException 1.195 + * If name is null or if this JS object has been disposed 1.196 + * @throws IllegalThreadStateException 1.197 + * If not called on the thread this object is attached to 1.198 + * @throws UnsupportedOperationException 1.199 + * If an internal JSAPI call failed 1.200 + */ 1.201 + public native String optString(String name, String fallback); 1.202 + 1.203 + /** 1.204 + * Returns whether a property exists in this object 1.205 + * 1.206 + * @param name 1.207 + * Property name 1.208 + * @throws NullPointerException 1.209 + * If name is null or if this JS object has been disposed 1.210 + * @throws IllegalThreadStateException 1.211 + * If not called on the thread this object is attached to 1.212 + * @throws UnsupportedOperationException 1.213 + * If an internal JSAPI call failed 1.214 + */ 1.215 + public native boolean has(String name); 1.216 + 1.217 + /** 1.218 + * Returns the JSON representation of this object. 1.219 + * 1.220 + * @throws NullPointerException 1.221 + * If this JS object has been disposed 1.222 + * @throws IllegalThreadStateException 1.223 + * If not called on the thread this object is attached to 1.224 + * @throws UnsupportedOperationException 1.225 + * If an internal JSAPI call failed 1.226 + */ 1.227 + @Override 1.228 + public native String toString(); 1.229 +}