1 /* 2 * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef _JNI_INSTANCE_H_ 27 #define _JNI_INSTANCE_H_ 28 29 #if ENABLE(MAC_JAVA_BRIDGE) 30 31 #include "runtime.h" 32 #include "runtime_root.h" 33 34 #include <JavaVM/jni.h> 35 36 #if PLATFORM(ANDROID) 37 namespace android { 38 class WeakJavaInstance; 39 } 40 #endif 41 42 namespace JSC { 43 44 namespace Bindings { 45 46 class JavaClass; 47 48 class JObjectWrapper 49 { 50 friend class RefPtr<JObjectWrapper>; 51 friend class JavaArray; 52 friend class JavaField; 53 friend class JavaInstance; 54 friend class JavaMethod; 55 #if PLATFORM(ANDROID) 56 friend class android::WeakJavaInstance; 57 #endif 58 59 protected: 60 JObjectWrapper(jobject instance); 61 ~JObjectWrapper(); 62 ref()63 void ref() { _refCount++; } deref()64 void deref() 65 { 66 if (--_refCount == 0) 67 delete this; 68 } 69 70 jobject _instance; 71 72 private: 73 JNIEnv *_env; 74 unsigned int _refCount; 75 }; 76 77 class JavaInstance : public Instance 78 { 79 public: create(jobject instance,PassRefPtr<RootObject> rootObject)80 static PassRefPtr<JavaInstance> create(jobject instance, PassRefPtr<RootObject> rootObject) 81 { 82 return adoptRef(new JavaInstance(instance, rootObject)); 83 } 84 85 ~JavaInstance(); 86 87 virtual Class *getClass() const; 88 89 virtual JSValue valueOf(ExecState*) const; 90 virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const; 91 92 virtual JSValue invokeMethod(ExecState* exec, const MethodList& method, const ArgList& args); 93 javaInstance()94 jobject javaInstance() const { return _instance->_instance; } 95 96 JSValue stringValue(ExecState*) const; 97 JSValue numberValue(ExecState*) const; 98 JSValue booleanValue() const; 99 100 protected: 101 virtual void virtualBegin(); 102 virtual void virtualEnd(); 103 104 #if !PLATFORM(ANDROID) // Submit patch to webkit.org 105 private: 106 #endif 107 JavaInstance(jobject instance, PassRefPtr<RootObject>); 108 109 RefPtr<JObjectWrapper> _instance; 110 mutable JavaClass *_class; 111 }; 112 113 } // namespace Bindings 114 115 } // namespace JSC 116 117 #endif // ENABLE(MAC_JAVA_BRIDGE) 118 119 #endif // _JNI_INSTANCE_H_ 120