michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "xpcprivate.h" michael@0: #include "nsString.h" michael@0: #include "nsIObjectOutputStream.h" michael@0: #include "nsIObjectInputStream.h" michael@0: #include "nsJSPrincipals.h" michael@0: #include "plstr.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIJSRuntimeService.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsMemory.h" michael@0: #include "nsStringBuffer.h" michael@0: michael@0: // for mozilla::dom::workers::kJSPrincipalsDebugToken michael@0: #include "mozilla/dom/workers/Workers.h" michael@0: michael@0: /* static */ bool michael@0: nsJSPrincipals::Subsume(JSPrincipals *jsprin, JSPrincipals *other) michael@0: { michael@0: bool result; michael@0: nsresult rv = nsJSPrincipals::get(jsprin)->Subsumes(nsJSPrincipals::get(other), &result); michael@0: return NS_SUCCEEDED(rv) && result; michael@0: } michael@0: michael@0: /* static */ void michael@0: nsJSPrincipals::Destroy(JSPrincipals *jsprin) michael@0: { michael@0: // The JS runtime can call this method during the last GC when michael@0: // nsScriptSecurityManager is destroyed. So we must not assume here that michael@0: // the security manager still exists. michael@0: michael@0: nsJSPrincipals *nsjsprin = nsJSPrincipals::get(jsprin); michael@0: michael@0: // We need to destroy the nsIPrincipal. We'll do this by adding michael@0: // to the refcount and calling release michael@0: michael@0: #ifdef NS_BUILD_REFCNT_LOGGING michael@0: // The refcount logging considers AddRef-to-1 to indicate creation, michael@0: // so trick it into thinking it's otherwise, but balance the michael@0: // Release() we do below. michael@0: nsjsprin->refcount++; michael@0: nsjsprin->AddRef(); michael@0: nsjsprin->refcount--; michael@0: #else michael@0: nsjsprin->refcount++; michael@0: #endif michael@0: nsjsprin->Release(); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: michael@0: // Defined here so one can do principals->dump() in the debugger michael@0: JS_EXPORT_API(void) michael@0: JSPrincipals::dump() michael@0: { michael@0: if (debugToken == nsJSPrincipals::DEBUG_TOKEN) { michael@0: static_cast(this)->dumpImpl(); michael@0: } else if (debugToken == mozilla::dom::workers::kJSPrincipalsDebugToken) { michael@0: fprintf(stderr, "Web Worker principal singleton (%p)\n", this); michael@0: } else { michael@0: fprintf(stderr, michael@0: "!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: " michael@0: "actual=0x%x expected=0x%x\n", michael@0: this, unsigned(debugToken), unsigned(nsJSPrincipals::DEBUG_TOKEN)); michael@0: } michael@0: } michael@0: michael@0: #endif