PageRenderTime 55ms CodeModel.GetById 12ms app.highlight 38ms RepoModel.GetById 2ms app.codeStats 0ms

/caps/src/nsJSPrincipals.cpp

http://github.com/zpao/v8monkey
C++ | 224 lines | 144 code | 27 blank | 53 comment | 21 complexity | c5a20dfbd0dad332d545aec0ba0b4940 MD5 | raw file
  1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2/* ***** BEGIN LICENSE BLOCK *****
  3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with
  7 * the License. You may obtain a copy of the License at
  8 * http://www.mozilla.org/MPL/
  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 * Alternatively, the contents of this file may be used under the terms of
 25 * either of the GNU General Public License Version 2 or later (the "GPL"),
 26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 27 * in which case the provisions of the GPL or the LGPL are applicable instead
 28 * of those above. If you wish to allow use of your version of this file only
 29 * under the terms of either the GPL or the LGPL, and not to allow others to
 30 * use your version of this file under the terms of the MPL, indicate your
 31 * decision by deleting the provisions above and replace them with the notice
 32 * and other provisions required by the GPL or the LGPL. If you do not delete
 33 * the provisions above, a recipient may use your version of this file under
 34 * the terms of any one of the MPL, the GPL or the LGPL.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37
 38#include "xpcprivate.h"
 39#include "nsString.h"
 40#include "nsIObjectOutputStream.h"
 41#include "nsIObjectInputStream.h"
 42#include "nsJSPrincipals.h"
 43#include "plstr.h"
 44#include "nsXPIDLString.h"
 45#include "nsCOMPtr.h"
 46#include "jsapi.h"
 47#include "jsxdrapi.h"
 48#include "nsIJSRuntimeService.h"
 49#include "nsIServiceManager.h"
 50#include "nsMemory.h"
 51#include "nsStringBuffer.h"
 52
 53static JSBool
 54nsJSPrincipalsSubsume(JSPrincipals *jsprin, JSPrincipals *other)
 55{
 56    nsJSPrincipals *nsjsprin = static_cast<nsJSPrincipals *>(jsprin);
 57    nsJSPrincipals *nsother  = static_cast<nsJSPrincipals *>(other);
 58
 59    bool result;
 60    nsresult rv = nsjsprin->nsIPrincipalPtr->Subsumes(nsother->nsIPrincipalPtr,
 61                                                      &result);
 62    return NS_SUCCEEDED(rv) && result;
 63}
 64
 65static void
 66nsDestroyJSPrincipals(JSContext *cx, struct JSPrincipals *jsprin)
 67{
 68    nsJSPrincipals *nsjsprin = static_cast<nsJSPrincipals *>(jsprin);
 69
 70    // We need to destroy the nsIPrincipal. We'll do this by adding
 71    // to the refcount and calling release
 72
 73    // Note that we don't want to use NS_IF_RELEASE because it will try
 74    // to set nsjsprin->nsIPrincipalPtr to nsnull *after* nsjsprin has
 75    // already been destroyed.
 76#ifdef NS_BUILD_REFCNT_LOGGING
 77    // The refcount logging considers AddRef-to-1 to indicate creation,
 78    // so trick it into thinking it's otherwise, but balance the
 79    // Release() we do below.
 80    nsjsprin->refcount++;
 81    nsjsprin->nsIPrincipalPtr->AddRef();
 82    nsjsprin->refcount--;
 83#else
 84    nsjsprin->refcount++;
 85#endif
 86    nsjsprin->nsIPrincipalPtr->Release();
 87    // The nsIPrincipal that we release owns the JSPrincipal struct,
 88    // so we don't need to worry about "codebase"
 89}
 90
 91static JSBool
 92nsTranscodeJSPrincipals(JSXDRState *xdr, JSPrincipals **jsprinp)
 93{
 94    nsresult rv;
 95
 96    if (xdr->mode == JSXDR_ENCODE) {
 97        nsIObjectOutputStream *stream =
 98            reinterpret_cast<nsIObjectOutputStream*>(xdr->userdata);
 99
100        // Flush xdr'ed data to the underlying object output stream.
101        uint32_t size;
102        char *data = (char*) ::JS_XDRMemGetData(xdr, &size);
103
104        rv = stream->Write32(size);
105        if (NS_SUCCEEDED(rv)) {
106            rv = stream->WriteBytes(data, size);
107            if (NS_SUCCEEDED(rv)) {
108                ::JS_XDRMemResetData(xdr);
109
110                // Require that GetJSPrincipals has been called already by the
111                // code that compiled the script that owns the principals.
112                nsJSPrincipals *nsjsprin =
113                    static_cast<nsJSPrincipals*>(*jsprinp);
114
115                rv = stream->WriteObject(nsjsprin->nsIPrincipalPtr, true);
116            }
117        }
118    } else {
119        NS_ASSERTION(JS_XDRMemDataLeft(xdr) == 0, "XDR out of sync?!");
120        nsIObjectInputStream *stream =
121            reinterpret_cast<nsIObjectInputStream*>(xdr->userdata);
122
123        nsCOMPtr<nsIPrincipal> prin;
124        rv = stream->ReadObject(true, getter_AddRefs(prin));
125        if (NS_SUCCEEDED(rv)) {
126            PRUint32 size;
127            rv = stream->Read32(&size);
128            if (NS_SUCCEEDED(rv)) {
129                char *data = nsnull;
130                if (size != 0)
131                    rv = stream->ReadBytes(size, &data);
132                if (NS_SUCCEEDED(rv)) {
133                    char *olddata;
134                    uint32_t oldsize;
135
136                    // Any decode-mode JSXDRState whose userdata points to an
137                    // nsIObjectInputStream instance must use nsMemory to Alloc
138                    // and Free its data buffer.  Swap the new buffer we just
139                    // read for the old, exhausted data.
140                    olddata = (char*) ::JS_XDRMemGetData(xdr, &oldsize);
141                    nsMemory::Free(olddata);
142                    ::JS_XDRMemSetData(xdr, data, size);
143
144                    prin->GetJSPrincipals(xdr->cx, jsprinp);
145                }
146            }
147        }
148    }
149
150    if (NS_FAILED(rv)) {
151        ::JS_ReportError(xdr->cx, "can't %scode principals (failure code %x)",
152                         (xdr->mode == JSXDR_ENCODE) ? "en" : "de",
153                         (unsigned int) rv);
154        return JS_FALSE;
155    }
156    return JS_TRUE;
157}
158
159nsresult
160nsJSPrincipals::Startup()
161{
162    nsCOMPtr<nsIJSRuntimeService> rtsvc = nsXPConnect::GetXPConnect();
163    if (!rtsvc)
164        return NS_ERROR_FAILURE;
165
166    JSRuntime *rt;
167    rtsvc->GetRuntime(&rt);
168    NS_ASSERTION(rt != nsnull, "no JSRuntime?!");
169
170    JSSecurityCallbacks *callbacks = JS_GetRuntimeSecurityCallbacks(rt);
171    NS_ASSERTION(callbacks, "Need a callbacks struct by now!");
172
173    NS_ASSERTION(!callbacks->principalsTranscoder,
174                 "oops, JS_SetPrincipalsTranscoder wars!");
175
176    callbacks->principalsTranscoder = nsTranscodeJSPrincipals;
177    return NS_OK;
178}
179
180nsJSPrincipals::nsJSPrincipals()
181{
182    codebase = nsnull;
183    refcount = 0;
184    destroy = nsDestroyJSPrincipals;
185    subsume = nsJSPrincipalsSubsume;
186    nsIPrincipalPtr = nsnull;
187}
188
189nsresult
190nsJSPrincipals::Init(nsIPrincipal *aPrincipal, const nsCString& aCodebase)
191{
192    if (nsIPrincipalPtr) {
193        NS_ERROR("Init called twice!");
194        return NS_ERROR_UNEXPECTED;
195    }
196
197    nsIPrincipalPtr = aPrincipal;
198    nsStringBuffer* buf = nsStringBuffer::FromString(aCodebase);
199    char* data;
200    if (buf) {
201        buf->AddRef();
202        data = static_cast<char*>(buf->Data());
203    } else {
204        PRUint32 len = aCodebase.Length();
205        buf = nsStringBuffer::Alloc(len + 1); // addrefs
206        if (!buf) {
207            return NS_ERROR_OUT_OF_MEMORY;
208        }
209        data = static_cast<char*>(buf->Data());
210        memcpy(data, aCodebase.get(), len);
211        data[len] = '\0';
212    }
213    
214    codebase = data;
215
216    return NS_OK;
217}
218
219nsJSPrincipals::~nsJSPrincipals()
220{
221    if (codebase) {
222        nsStringBuffer::FromData(codebase)->Release();
223    }
224}