LCOV - code coverage report
Current view: directory - caps/src - nsJSPrincipals.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 47 35 74.5 %
Date: 2012-06-02 Functions: 4 2 50.0 %

       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                 : 
      53                 : // for mozilla::dom::workers::kJSPrincipalsDebugToken
      54                 : #include "mozilla/dom/workers/Workers.h"
      55                 : 
      56                 : /* static */ JSBool
      57               0 : nsJSPrincipals::Subsume(JSPrincipals *jsprin, JSPrincipals *other)
      58                 : {
      59                 :     bool result;
      60               0 :     nsresult rv = nsJSPrincipals::get(jsprin)->Subsumes(nsJSPrincipals::get(other), &result);
      61               0 :     return NS_SUCCEEDED(rv) && result;
      62                 : }
      63                 : 
      64                 : /* static */ void
      65            1413 : nsJSPrincipals::Destroy(JSPrincipals *jsprin)
      66                 : {
      67                 :     // The JS runtime can call this method during the last GC when
      68                 :     // nsScriptSecurityManager is destroyed. So we must not assume here that
      69                 :     // the security manager still exists.
      70                 : 
      71            1413 :     nsJSPrincipals *nsjsprin = nsJSPrincipals::get(jsprin);
      72                 : 
      73                 :     // We need to destroy the nsIPrincipal. We'll do this by adding
      74                 :     // to the refcount and calling release
      75                 : 
      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            1413 :     nsjsprin->refcount++;
      81            1413 :     nsjsprin->AddRef();
      82            1413 :     nsjsprin->refcount--;
      83                 : #else
      84                 :     nsjsprin->refcount++;
      85                 : #endif
      86            1413 :     nsjsprin->Release();
      87            1413 : }
      88                 : 
      89                 : /* static */ JSBool
      90            8017 : nsJSPrincipals::Transcode(JSXDRState *xdr, JSPrincipals **jsprinp)
      91                 : {
      92                 :     nsresult rv;
      93                 : 
      94            8017 :     if (xdr->mode == JSXDR_ENCODE) {
      95                 :         nsIObjectOutputStream *stream =
      96            7375 :             reinterpret_cast<nsIObjectOutputStream*>(xdr->userdata);
      97                 : 
      98                 :         // Flush xdr'ed data to the underlying object output stream.
      99                 :         uint32_t size;
     100            7375 :         char *data = (char*) ::JS_XDRMemGetData(xdr, &size);
     101                 : 
     102            7375 :         rv = stream->Write32(size);
     103            7375 :         if (NS_SUCCEEDED(rv)) {
     104            7375 :             rv = stream->WriteBytes(data, size);
     105            7375 :             if (NS_SUCCEEDED(rv)) {
     106            7375 :                 ::JS_XDRMemResetData(xdr);
     107                 : 
     108            7375 :                 rv = stream->WriteObject(nsJSPrincipals::get(*jsprinp), true);
     109                 :             }
     110                 :         }
     111                 :     } else {
     112             642 :         NS_ASSERTION(JS_XDRMemDataLeft(xdr) == 0, "XDR out of sync?!");
     113                 :         nsIObjectInputStream *stream =
     114             642 :             reinterpret_cast<nsIObjectInputStream*>(xdr->userdata);
     115                 : 
     116            1284 :         nsCOMPtr<nsIPrincipal> prin;
     117             642 :         rv = stream->ReadObject(true, getter_AddRefs(prin));
     118             642 :         if (NS_SUCCEEDED(rv)) {
     119                 :             PRUint32 size;
     120             642 :             rv = stream->Read32(&size);
     121             642 :             if (NS_SUCCEEDED(rv)) {
     122             642 :                 char *data = nsnull;
     123             642 :                 if (size != 0)
     124             642 :                     rv = stream->ReadBytes(size, &data);
     125             642 :                 if (NS_SUCCEEDED(rv)) {
     126                 :                     char *olddata;
     127                 :                     uint32_t oldsize;
     128                 : 
     129                 :                     // Any decode-mode JSXDRState whose userdata points to an
     130                 :                     // nsIObjectInputStream instance must use nsMemory to Alloc
     131                 :                     // and Free its data buffer.  Swap the new buffer we just
     132                 :                     // read for the old, exhausted data.
     133             642 :                     olddata = (char*) ::JS_XDRMemGetData(xdr, &oldsize);
     134             642 :                     nsMemory::Free(olddata);
     135             642 :                     ::JS_XDRMemSetData(xdr, data, size);
     136                 : 
     137             642 :                     *jsprinp = nsJSPrincipals::get(prin);
     138             642 :                     JS_HoldPrincipals(*jsprinp);
     139                 :                 }
     140                 :             }
     141                 :         }
     142                 :     }
     143                 : 
     144            8017 :     if (NS_FAILED(rv)) {
     145                 :         ::JS_ReportError(xdr->cx, "can't %scode principals (failure code %x)",
     146                 :                          (xdr->mode == JSXDR_ENCODE) ? "en" : "de",
     147               0 :                          (unsigned int) rv);
     148               0 :         return JS_FALSE;
     149                 :     }
     150            8017 :     return JS_TRUE;
     151                 : }
     152                 : 
     153                 : #ifdef DEBUG
     154                 : 
     155                 : // Defined here so one can do principals->dump() in the debugger
     156                 : JS_EXPORT_API(void)
     157               0 : JSPrincipals::dump()
     158                 : {
     159               0 :     if (debugToken == nsJSPrincipals::DEBUG_TOKEN) {
     160               0 :         static_cast<nsJSPrincipals *>(this)->dumpImpl();
     161               0 :     } else if (debugToken == mozilla::dom::workers::kJSPrincipalsDebugToken) {
     162               0 :         fprintf(stderr, "Web Worker principal singleton (%p)\n", this);
     163                 :     } else {
     164                 :         fprintf(stderr,
     165                 :                 "!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
     166                 :                 "actual=0x%x expected=0x%x\n",
     167               0 :                 this, unsigned(debugToken), unsigned(nsJSPrincipals::DEBUG_TOKEN));
     168                 :     }
     169               0 : }
     170                 : 
     171                 : #endif 

Generated by: LCOV version 1.7