1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
---|
2 | /* ***** BEGIN LICENSE BLOCK ***** |
---|
3 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
---|
4 | * |
---|
5 | * The contents of this file are subject to the Netscape Public License |
---|
6 | * Version 1.1 (the "License"); you may not use this file except in |
---|
7 | * compliance with the License. You may obtain a copy of the License at |
---|
8 | * http://www.mozilla.org/NPL/ |
---|
9 | * |
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis, |
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
---|
12 | * for the specific language governing rights and limitations under the |
---|
13 | * License. |
---|
14 | * |
---|
15 | * The Original Code is mozilla.org code. |
---|
16 | * |
---|
17 | * The Initial Developer of the Original Code is |
---|
18 | * Netscape Communications Corporation. |
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1999 |
---|
20 | * the Initial Developer. All Rights Reserved. |
---|
21 | * |
---|
22 | * Contributor(s): |
---|
23 | * |
---|
24 | * |
---|
25 | * Alternatively, the contents of this file may be used under the terms of |
---|
26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
---|
27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
---|
29 | * of those above. If you wish to allow use of your version of this file only |
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
---|
31 | * use your version of this file under the terms of the NPL, indicate your |
---|
32 | * decision by deleting the provisions above and replace them with the notice |
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
---|
34 | * the provisions above, a recipient may use your version of this file under |
---|
35 | * the terms of any one of the NPL, the GPL or the LGPL. |
---|
36 | * |
---|
37 | * ***** END LICENSE BLOCK ***** */ |
---|
38 | #include "nsCodebasePrincipal.h" |
---|
39 | #include "nsJSPrincipals.h" |
---|
40 | #include "plstr.h" |
---|
41 | #include "nsXPIDLString.h" |
---|
42 | #include "nsCOMPtr.h" |
---|
43 | #include "jsapi.h" |
---|
44 | #include "jsxdrapi.h" |
---|
45 | #include "nsIJSRuntimeService.h" |
---|
46 | #include "nsIServiceManager.h" |
---|
47 | #include "nsMemory.h" |
---|
48 | |
---|
49 | JS_STATIC_DLL_CALLBACK(void *) |
---|
50 | nsGetPrincipalArray(JSContext *cx, struct JSPrincipals *prin) |
---|
51 | { |
---|
52 | return nsnull; |
---|
53 | } |
---|
54 | |
---|
55 | JS_STATIC_DLL_CALLBACK(JSBool) |
---|
56 | nsGlobalPrivilegesEnabled(JSContext *cx , struct JSPrincipals *jsprin) |
---|
57 | { |
---|
58 | return JS_TRUE; |
---|
59 | } |
---|
60 | |
---|
61 | JS_STATIC_DLL_CALLBACK(void) |
---|
62 | nsDestroyJSPrincipals(JSContext *cx, struct JSPrincipals *jsprin) |
---|
63 | { |
---|
64 | nsJSPrincipals *nsjsprin = NS_STATIC_CAST(nsJSPrincipals *, jsprin); |
---|
65 | |
---|
66 | // We need to destroy the nsIPrincipal. We'll do this by adding |
---|
67 | // to the refcount and calling release |
---|
68 | |
---|
69 | // Note that we don't want to use NS_IF_RELEASE because it will try |
---|
70 | // to set nsjsprin->nsIPrincipalPtr to nsnull *after* nsjsprin has |
---|
71 | // already been destroyed. |
---|
72 | #ifdef NS_BUILD_REFCNT_LOGGING |
---|
73 | // The refcount logging considers AddRef-to-1 to indicate creation, |
---|
74 | // so trick it into thinking it's otherwise, but balance the |
---|
75 | // Release() we do below. |
---|
76 | nsjsprin->refcount++; |
---|
77 | nsjsprin->nsIPrincipalPtr->AddRef(); |
---|
78 | nsjsprin->refcount--; |
---|
79 | #else |
---|
80 | nsjsprin->refcount++; |
---|
81 | #endif |
---|
82 | if (nsjsprin->nsIPrincipalPtr) |
---|
83 | nsjsprin->nsIPrincipalPtr->Release(); |
---|
84 | // The nsIPrincipal that we release owns the JSPrincipal struct, |
---|
85 | // so we don't need to worry about "codebase" |
---|
86 | } |
---|
87 | |
---|
88 | JS_STATIC_DLL_CALLBACK(JSBool) |
---|
89 | nsTranscodeJSPrincipals(JSXDRState *xdr, JSPrincipals **jsprinp) |
---|
90 | { |
---|
91 | nsresult rv; |
---|
92 | |
---|
93 | if (xdr->mode == JSXDR_ENCODE) { |
---|
94 | nsIObjectOutputStream *stream = |
---|
95 | NS_REINTERPRET_CAST(nsIObjectOutputStream*, xdr->userdata); |
---|
96 | |
---|
97 | // Flush xdr'ed data to the underlying object output stream. |
---|
98 | uint32 size; |
---|
99 | char *data = (char*) ::JS_XDRMemGetData(xdr, &size); |
---|
100 | |
---|
101 | rv = stream->Write32(size); |
---|
102 | if (NS_SUCCEEDED(rv)) { |
---|
103 | rv = stream->WriteBytes(data, size); |
---|
104 | if (NS_SUCCEEDED(rv)) { |
---|
105 | ::JS_XDRMemResetData(xdr); |
---|
106 | |
---|
107 | // Require that GetJSPrincipals has been called already by the |
---|
108 | // code that compiled the script that owns the principals. |
---|
109 | nsJSPrincipals *nsjsprin = |
---|
110 | NS_STATIC_CAST(nsJSPrincipals*, *jsprinp); |
---|
111 | |
---|
112 | rv = stream->WriteObject(nsjsprin->nsIPrincipalPtr, PR_TRUE); |
---|
113 | } |
---|
114 | } |
---|
115 | } else { |
---|
116 | NS_ASSERTION(JS_XDRMemDataLeft(xdr) == 0, "XDR out of sync?!"); |
---|
117 | nsIObjectInputStream *stream = |
---|
118 | NS_REINTERPRET_CAST(nsIObjectInputStream*, xdr->userdata); |
---|
119 | |
---|
120 | nsCOMPtr<nsIPrincipal> prin; |
---|
121 | rv = stream->ReadObject(PR_TRUE, getter_AddRefs(prin)); |
---|
122 | if (NS_SUCCEEDED(rv)) { |
---|
123 | PRUint32 size; |
---|
124 | rv = stream->Read32(&size); |
---|
125 | if (NS_SUCCEEDED(rv)) { |
---|
126 | char *data = nsnull; |
---|
127 | if (size != 0) |
---|
128 | rv = stream->ReadBytes(size, &data); |
---|
129 | if (NS_SUCCEEDED(rv)) { |
---|
130 | char *olddata; |
---|
131 | uint32 oldsize; |
---|
132 | |
---|
133 | // Any decode-mode JSXDRState whose userdata points to an |
---|
134 | // nsIObjectInputStream instance must use nsMemory to Alloc |
---|
135 | // and Free its data buffer. Swap the new buffer we just |
---|
136 | // read for the old, exhausted data. |
---|
137 | olddata = (char*) ::JS_XDRMemGetData(xdr, &oldsize); |
---|
138 | nsMemory::Free(olddata); |
---|
139 | ::JS_XDRMemSetData(xdr, data, size); |
---|
140 | |
---|
141 | prin->GetJSPrincipals(jsprinp); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | if (NS_FAILED(rv)) { |
---|
148 | ::JS_ReportError(xdr->cx, "can't %scode principals (failure code %x)", |
---|
149 | (xdr->mode == JSXDR_ENCODE) ? "en" : "de", |
---|
150 | (unsigned int) rv); |
---|
151 | return JS_FALSE; |
---|
152 | } |
---|
153 | return JS_TRUE; |
---|
154 | } |
---|
155 | |
---|
156 | nsresult |
---|
157 | nsJSPrincipals::Startup() |
---|
158 | { |
---|
159 | static const char rtsvc_id[] = "@mozilla.org/js/xpc/RuntimeService;1"; |
---|
160 | nsCOMPtr<nsIJSRuntimeService> rtsvc(do_GetService(rtsvc_id)); |
---|
161 | if (!rtsvc) |
---|
162 | return NS_ERROR_FAILURE; |
---|
163 | |
---|
164 | JSRuntime *rt; |
---|
165 | rtsvc->GetRuntime(&rt); |
---|
166 | NS_ASSERTION(rt != nsnull, "no JSRuntime?!"); |
---|
167 | |
---|
168 | JSPrincipalsTranscoder oldpx; |
---|
169 | oldpx = ::JS_SetPrincipalsTranscoder(rt, nsTranscodeJSPrincipals); |
---|
170 | NS_ASSERTION(oldpx == nsnull, "oops, JS_SetPrincipalsTranscoder wars!"); |
---|
171 | |
---|
172 | return NS_OK; |
---|
173 | } |
---|
174 | |
---|
175 | nsJSPrincipals::nsJSPrincipals() |
---|
176 | { |
---|
177 | codebase = nsnull; |
---|
178 | getPrincipalArray = nsGetPrincipalArray; |
---|
179 | globalPrivilegesEnabled = nsGlobalPrivilegesEnabled; |
---|
180 | refcount = 0; |
---|
181 | destroy = nsDestroyJSPrincipals; |
---|
182 | nsIPrincipalPtr = nsnull; |
---|
183 | } |
---|
184 | |
---|
185 | nsresult |
---|
186 | nsJSPrincipals::Init(char *aCodebase) |
---|
187 | { |
---|
188 | codebase = aCodebase; |
---|
189 | return NS_OK; |
---|
190 | } |
---|
191 | |
---|
192 | nsJSPrincipals::~nsJSPrincipals() |
---|
193 | { |
---|
194 | if (codebase) |
---|
195 | PL_strfree(codebase); |
---|
196 | } |
---|