michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.util; michael@0: michael@0: import org.mozilla.gecko.mozglue.JNITarget; michael@0: michael@0: /** michael@0: * NativeJSObject is a wrapper around the SpiderMonkey JSAPI to make it possible to michael@0: * access Javascript objects in Java. michael@0: */ michael@0: @JNITarget michael@0: public class NativeJSObject michael@0: { michael@0: private final NativeJSContainer mContainer; michael@0: private final int mObjectIndex; michael@0: michael@0: protected NativeJSObject() { michael@0: mContainer = (NativeJSContainer)this; michael@0: mObjectIndex = -1; michael@0: } michael@0: michael@0: private NativeJSObject(NativeJSContainer container, int index) { michael@0: mContainer = container; michael@0: mObjectIndex = index; michael@0: } michael@0: michael@0: /** michael@0: * Returns the value of a boolean property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @throws IllegalArgumentException michael@0: * If the property does not exist or if its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native boolean getBoolean(String name); michael@0: michael@0: /** michael@0: * Returns the value of a boolean property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @param fallback michael@0: * Value to return if property does not exist michael@0: * @throws IllegalArgumentException michael@0: * If the property exists and its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native boolean optBoolean(String name, boolean fallback); michael@0: michael@0: /** michael@0: * Returns the value of a double property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @throws IllegalArgumentException michael@0: * If the property does not exist or if its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native double getDouble(String name); michael@0: michael@0: /** michael@0: * Returns the value of a double property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @param fallback michael@0: * Value to return if property does not exist michael@0: * @throws IllegalArgumentException michael@0: * If the property exists and its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native double optDouble(String name, double fallback); michael@0: michael@0: /** michael@0: * Returns the value of an int property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @throws IllegalArgumentException michael@0: * If the property does not exist or if its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native int getInt(String name); michael@0: michael@0: /** michael@0: * Returns the value of an int property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @param fallback michael@0: * Value to return if property does not exist michael@0: * @throws IllegalArgumentException michael@0: * If the property exists and its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native int optInt(String name, int fallback); michael@0: michael@0: /** michael@0: * Returns the value of an object property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @throws IllegalArgumentException michael@0: * If the property does not exist or if its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native NativeJSObject getObject(String name); michael@0: michael@0: /** michael@0: * Returns the value of an object property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @param fallback michael@0: * Value to return if property does not exist michael@0: * @throws IllegalArgumentException michael@0: * If the property exists and its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native NativeJSObject optObject(String name, NativeJSObject fallback); michael@0: michael@0: /** michael@0: * Returns the value of a string property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @throws IllegalArgumentException michael@0: * If the property does not exist or if its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native String getString(String name); michael@0: michael@0: /** michael@0: * Returns the value of a string property. michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @param fallback michael@0: * Value to return if property does not exist michael@0: * @throws IllegalArgumentException michael@0: * If the property exists and its type does not match the return type michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native String optString(String name, String fallback); michael@0: michael@0: /** michael@0: * Returns whether a property exists in this object michael@0: * michael@0: * @param name michael@0: * Property name michael@0: * @throws NullPointerException michael@0: * If name is null or if this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: public native boolean has(String name); michael@0: michael@0: /** michael@0: * Returns the JSON representation of this object. michael@0: * michael@0: * @throws NullPointerException michael@0: * If this JS object has been disposed michael@0: * @throws IllegalThreadStateException michael@0: * If not called on the thread this object is attached to michael@0: * @throws UnsupportedOperationException michael@0: * If an internal JSAPI call failed michael@0: */ michael@0: @Override michael@0: public native String toString(); michael@0: }