1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=4 sw=4 et tw=78: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
24 * Patrick Beard <beard@netscape.com> (original author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "ProxyClassLoader.h"
46 #include "nsIServiceManager.h"
47 #include "nsIScriptSecurityManager.h"
48 #include "nsIJSContextStack.h"
49 #include "nsIPrincipal.h"
50 #include "nsNetUtil.h"
52 #include "nsCNullSecurityContext.h"
55 * Obtain the netscape.oji.ProxyClassLoader instance associated with the
56 * document of the currently running script. For now, store a LiveConnect
57 * wrapper for this instance in window.navigator.javaclasses. There
58 * hopefully aren't any security concerns with exposing this to scripts,
59 * as the constructor is private, and the class loader itself can
60 * only load classes from the document's URL and below.
62 static nsresult
getScriptClassLoader(JNIEnv
* env
, jobject
* classloader
)
64 // get the current JSContext from the context stack service.
66 nsCOMPtr
<nsIJSContextStack
> contexts
=
67 do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv
);
68 if (NS_FAILED(rv
)) return rv
;
70 rv
= contexts
->Peek(&cx
);
71 if (NS_FAILED(rv
)) return rv
;
72 if (!cx
) return NS_ERROR_NOT_AVAILABLE
;
74 // lookup "window.navigator.javaclasses", if it exists, this is the class
75 // loader bound to this page.
76 JSObject
* window
= JS_GetGlobalObject(cx
);
77 if (!window
) return NS_ERROR_FAILURE
;
79 jsval navigator
= JSVAL_NULL
;
80 if (!JS_LookupProperty(cx
, window
, "navigator", &navigator
))
81 return NS_ERROR_FAILURE
;
83 jsval javaclasses
= JSVAL_NULL
;
84 if (!JSVAL_IS_PRIMITIVE(navigator
)) {
88 // Make sure that we pull out the correct javaclasses object that we
89 // set. Since content can't spoof READONLY or PERMANANT properties,
90 // their presence on this property indicates that this truely is the
92 JSObject
*obj
= JSVAL_TO_OBJECT(navigator
);
93 if (!JS_GetPropertyAttributes(cx
, obj
, "javaclasses", &attrs
, &found
))
94 return NS_ERROR_FAILURE
;
95 if ((~attrs
& (JSPROP_READONLY
| JSPROP_PERMANENT
)) == 0 &&
96 !JS_GetProperty(cx
, obj
, "javaclasses", &javaclasses
)) {
97 return NS_ERROR_FAILURE
;
100 // Unwrap this, the way LiveConnect does it. Note that this function
101 // checks if javaclasses is primitive or not.
102 if (JSJ_ConvertJSValueToJavaObject(cx
, javaclasses
, classloader
))
106 // use default netscape.oji.ProxyClassLoaderFactory (which is no longer
107 // supported in recent JRE) as the classloader
108 jclass netscape_oji_ProxyClassLoaderFac
=
109 env
->FindClass("netscape/oji/ProxyClassLoaderFactory");
110 if (!netscape_oji_ProxyClassLoaderFac
) {
111 env
->ExceptionClear();
112 return NS_ERROR_FAILURE
;
114 jmethodID staticMethodID
=
115 env
->GetStaticMethodID(netscape_oji_ProxyClassLoaderFac
,
117 "(Ljava/lang/String;)Ljava/lang/ClassLoader;");
118 if (!staticMethodID
) {
119 env
->ExceptionClear();
120 return NS_ERROR_FAILURE
;
123 // Obtain the URL of the document of the currently running script. This will
124 // be used as the default location to download classes from.
125 nsCOMPtr
<nsIScriptSecurityManager
> secMan
=
126 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
127 if (NS_FAILED(rv
)) return rv
;
129 nsCOMPtr
<nsIPrincipal
> principal
, sysprincipal
;
130 rv
= secMan
->GetPrincipalFromContext(cx
, getter_AddRefs(principal
));
131 if (NS_FAILED(rv
)) return rv
;
133 rv
= secMan
->GetSystemPrincipal(getter_AddRefs(sysprincipal
));
134 if (NS_FAILED(rv
)) return rv
;
137 rv
= principal
->Equals(sysprincipal
, &equals
);
138 // Can't get URI from system principal
139 if (NS_FAILED(rv
)) return rv
;
140 if (equals
) return NS_ERROR_FAILURE
;
142 nsCOMPtr
<nsIURI
> codebase
;
143 rv
= principal
->GetURI(getter_AddRefs(codebase
));
144 if (NS_FAILED(rv
)) return rv
;
146 // create a netscape.oji.ProxyClassLoader instance.
148 rv
= codebase
->GetSpec(spec
);
149 if (NS_FAILED(rv
)) return rv
;
151 jstring jspec
= env
->NewStringUTF(spec
.get());
153 env
->ExceptionClear();
154 return NS_ERROR_FAILURE
;
157 // In order to have permission to create classloader, we need to grant
159 nsISecurityContext
* origContext
= nsnull
;
160 if (NS_FAILED(GetSecurityContext(env
, &origContext
))) {
161 return NS_ERROR_FAILURE
;
163 nsCOMPtr
<nsISecurityContext
> nullContext(new nsCNullSecurityContext());
165 return NS_ERROR_OUT_OF_MEMORY
;
168 SetSecurityContext(env
, nullContext
);
169 *classloader
= env
->CallStaticObjectMethod(netscape_oji_ProxyClassLoaderFac
,
170 staticMethodID
, jspec
);
171 SetSecurityContext(env
, origContext
);
173 env
->ExceptionClear();
174 return NS_ERROR_FAILURE
;
177 env
->DeleteLocalRef(jspec
);
178 env
->DeleteLocalRef(netscape_oji_ProxyClassLoaderFac
);
180 // now, cache the class loader in "window.navigator.javaclasses"
181 if (!JSVAL_IS_PRIMITIVE(navigator
) &&
182 JSJ_ConvertJavaObjectToJSValue(cx
, *classloader
, &javaclasses
) &&
183 !JS_DefineProperty(cx
, JSVAL_TO_OBJECT(navigator
), "javaclasses",
184 javaclasses
, NULL
, NULL
, JSPROP_ENUMERATE
|
185 JSPROP_READONLY
| JSPROP_PERMANENT
)) {
186 return NS_ERROR_FAILURE
;
192 jclass
ProxyFindClass(JNIEnv
* env
, const char* name
)
195 // TODO: prevent recursive call to ProxyFindClass, if netscape.oji.ProxyClassLoader
196 // isn't found by getScriptClassLoader().
198 jthrowable jException
= env
->ExceptionOccurred();
199 if (jException
!= NULL
) {
200 // Clean up exception
201 env
->ExceptionClear();
203 env
->DeleteLocalRef(jException
);
205 nsresult rv
= getScriptClassLoader(env
, &classloader
);
206 if (NS_FAILED(rv
)) break;
208 jclass netscape_oji_ProxyClassLoader
= env
->GetObjectClass(classloader
);
209 jmethodID loadClassID
= env
->GetMethodID(netscape_oji_ProxyClassLoader
, "loadClass",
210 "(Ljava/lang/String;)Ljava/lang/Class;");
211 env
->DeleteLocalRef(netscape_oji_ProxyClassLoader
);
213 env
->ExceptionClear();
216 jstring jname
= env
->NewStringUTF(name
);
217 jvalue jargs
[1]; jargs
[0].l
= jname
;
218 jclass c
= (jclass
) env
->CallObjectMethodA(classloader
, loadClassID
, jargs
);
219 env
->DeleteLocalRef(jname
);