Fix for bug 560199 (Link XPConnect and caps into layout). r=jst.
authorPeter Van der Beken <peterv@propagandism.org>
Sun, 11 Apr 2010 15:55:24 +0200
changeset 41899 e701fd3950dbc78b38fb918a1b6cecfca8747cb1
parent 41898 23d5bcb2c5d2ce768227a0e1b0832bc6bf9b4656
child 41900 a5e772392c052bd4f33dfa4761caacb82a0b566b
push idunknown
push userunknown
push dateunknown
reviewersjst
bugs560199
milestone1.9.3a5pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Fix for bug 560199 (Link XPConnect and caps into layout). r=jst.
caps/include/nsScriptSecurityManager.h
caps/src/Makefile.in
caps/src/nsJSPrincipals.cpp
caps/src/nsScriptSecurityManager.cpp
caps/src/nsSecurityManagerFactory.cpp
content/base/src/Makefile.in
content/base/src/nsContentUtils.cpp
js/src/xpconnect/src/Makefile.in
js/src/xpconnect/src/xpcmodule.cpp
js/src/xpconnect/src/xpcmodule.h
js/src/xpconnect/src/xpcthreadcontext.cpp
layout/build/Makefile.in
layout/build/nsLayoutModule.cpp
toolkit/library/libxul-config.mk
toolkit/library/nsStaticXULComponents.cpp
--- a/caps/include/nsScriptSecurityManager.h
+++ b/caps/include/nsScriptSecurityManager.h
@@ -52,16 +52,17 @@
 #include "nsCOMPtr.h"
 #include "nsIPrefService.h"
 #include "nsISecurityPref.h"
 #include "nsIChannelEventSink.h"
 #include "nsIJSContextStack.h"
 #include "nsIObserver.h"
 #include "pldhash.h"
 #include "plstr.h"
+#include "nsIScriptExternalNameSet.h"
 
 class nsIDocShell;
 class nsString;
 class nsIClassInfo;
 class nsIIOService;
 class nsIXPConnect;
 class nsIStringBundle;
 class nsSystemPrincipal;
@@ -645,9 +646,25 @@ private:
 
     static nsIIOService    *sIOService;
     static nsIXPConnect    *sXPConnect;
     static nsIThreadJSContextStack* sJSContextStack;
     static nsIStringBundle *sStrBundle;
     static JSRuntime       *sRuntime;
 };
 
+#define NS_SECURITYNAMESET_CID \
+ { 0x7c02eadc, 0x76, 0x4d03, \
+ { 0x99, 0x8d, 0x80, 0xd7, 0x79, 0xc4, 0x85, 0x89 } }
+#define NS_SECURITYNAMESET_CONTRACTID "@mozilla.org/security/script/nameset;1"
+
+class nsSecurityNameSet : public nsIScriptExternalNameSet 
+{
+public:
+    nsSecurityNameSet();
+    virtual ~nsSecurityNameSet();
+    
+    NS_DECL_ISUPPORTS
+
+    NS_IMETHOD InitializeNameSet(nsIScriptContext* aScriptContext);
+};
+
 #endif // nsScriptSecurityManager_h__
--- a/caps/src/Makefile.in
+++ b/caps/src/Makefile.in
@@ -38,21 +38,18 @@
 DEPTH		= ../..
 topsrcdir	= @top_srcdir@
 srcdir		= @srcdir@
 VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE		= caps
-LIBRARY_NAME	= caps
-EXPORT_LIBRARY = 1
-IS_COMPONENT	= 1
-MODULE_NAME	= nsSecurityManagerModule
-GRE_MODULE	= 1
+LIBRARY_NAME	= caps_s
+FORCE_STATIC_LIB = 1
 LIBXUL_LIBRARY = 1
 
 
 CPPSRCS		= \
 		nsPrincipal.cpp \
 		nsSystemPrincipal.cpp \
 		nsNullPrincipal.cpp \
 		nsNullPrincipalURI.cpp \
@@ -60,19 +57,13 @@ CPPSRCS		= \
 		nsScriptSecurityManager.cpp \
 		nsSecurityManagerFactory.cpp \
 		$(NULL)
 
 ifdef XPC_IDISPATCH_SUPPORT
 DEFINES += -DXPC_IDISPATCH_SUPPORT
 endif
 
-EXTRA_DSO_LDOPTS = \
-		$(LIBS_DIR) \
-		$(ZLIB_LIBS) \
-		$(MOZ_JS_LIBS) \
-		$(MOZ_COMPONENT_LIBS) \
-		$(NULL)
-
 include $(topsrcdir)/config/rules.mk
 
-INCLUDES	+= -I$(srcdir)/../include
+INCLUDES	+= -I$(srcdir)/../include \
+		   -I$(topsrcdir)/js/src/xpconnect/src
 
--- a/caps/src/nsJSPrincipals.cpp
+++ b/caps/src/nsJSPrincipals.cpp
@@ -30,16 +30,17 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+#include "xpcprivate.h"
 #include "nsString.h"
 #include "nsIObjectOutputStream.h"
 #include "nsIObjectInputStream.h"
 #include "nsJSPrincipals.h"
 #include "plstr.h"
 #include "nsXPIDLString.h"
 #include "nsCOMPtr.h"
 #include "jsapi.h"
@@ -165,18 +166,17 @@ nsTranscodeJSPrincipals(JSXDRState *xdr,
         return JS_FALSE;
     }
     return JS_TRUE;
 }
 
 nsresult
 nsJSPrincipals::Startup()
 {
-    static const char rtsvc_id[] = "@mozilla.org/js/xpc/RuntimeService;1";
-    nsCOMPtr<nsIJSRuntimeService> rtsvc(do_GetService(rtsvc_id));
+    nsCOMPtr<nsIJSRuntimeService> rtsvc = nsXPConnect::GetXPConnect();
     if (!rtsvc)
         return NS_ERROR_FAILURE;
 
     JSRuntime *rt;
     rtsvc->GetRuntime(&rt);
     NS_ASSERTION(rt != nsnull, "no JSRuntime?!");
 
     JSSecurityCallbacks *callbacks = JS_GetRuntimeSecurityCallbacks(rt);
--- a/caps/src/nsScriptSecurityManager.cpp
+++ b/caps/src/nsScriptSecurityManager.cpp
@@ -35,16 +35,17 @@
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
+#include "xpcprivate.h"
 #include "nsScriptSecurityManager.h"
 #include "nsIServiceManager.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIScriptContext.h"
 #include "nsIURL.h"
 #include "nsINestedURI.h"
 #include "nspr.h"
 #include "nsJSPrincipals.h"
@@ -3362,33 +3363,34 @@ nsScriptSecurityManager::nsScriptSecurit
                  "PRWord and void* have different lengths on this platform. "
                  "This may cause a security failure with the SecurityLevel union.");
     mPrincipals.Init(31);
 }
 
 
 nsresult nsScriptSecurityManager::Init()
 {
-    nsresult rv = CallGetService(nsIXPConnect::GetCID(), &sXPConnect);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    rv = CallGetService("@mozilla.org/js/xpc/ContextStack;1", &sJSContextStack);
-    NS_ENSURE_SUCCESS(rv, rv);
+    nsXPConnect* xpconnect = nsXPConnect::GetXPConnect();
+     if (!xpconnect)
+        return NS_ERROR_FAILURE;
+
+    NS_ADDREF(sXPConnect = xpconnect);
+    NS_ADDREF(sJSContextStack = xpconnect);
 
     JSContext* cx = GetSafeJSContext();
     if (!cx) return NS_ERROR_FAILURE;   // this can happen of xpt loading fails
     
     ::JS_BeginRequest(cx);
     if (sEnabledID == JSVAL_VOID)
         sEnabledID = STRING_TO_JSVAL(::JS_InternString(cx, "enabled"));
     ::JS_EndRequest(cx);
 
     InitPrefs();
 
-    rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService);
+    nsresult rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = bundleService->CreateBundle("chrome://global/locale/security/caps.properties", &sStrBundle);
     NS_ENSURE_SUCCESS(rv, rv);
 
@@ -3399,17 +3401,17 @@ nsresult nsScriptSecurityManager::Init()
     rv = system->Init();
     NS_ENSURE_SUCCESS(rv, rv);
 
     mSystemPrincipal = system;
 
     //-- Register security check callback in the JS engine
     //   Currently this is used to control access to function.caller
     nsCOMPtr<nsIJSRuntimeService> runtimeService =
-        do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
+        do_QueryInterface(sXPConnect, &rv);
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = runtimeService->GetRuntime(&sRuntime);
     NS_ENSURE_SUCCESS(rv, rv);
 
     static JSSecurityCallbacks securityCallbacks = {
         CheckObjectAccess,
         NULL,
--- a/caps/src/nsSecurityManagerFactory.cpp
+++ b/caps/src/nsSecurityManagerFactory.cpp
@@ -41,47 +41,30 @@
 #include "nsIGenericFactory.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsScriptSecurityManager.h"
 #include "nsIPrincipal.h"
 #include "nsPrincipal.h"
 #include "nsSystemPrincipal.h"
 #include "nsNullPrincipal.h"
 #include "nsIScriptNameSpaceManager.h"
-#include "nsIScriptExternalNameSet.h"
 #include "nsIScriptContext.h"
 #include "nsICategoryManager.h"
 #include "nsXPIDLString.h"
 #include "nsCOMPtr.h"
 #include "nsIServiceManager.h"
 #include "nsString.h"
 #include "nsPrefsCID.h"
 #include "nsNetCID.h"
 #include "nsIClassInfoImpl.h"
 
 ///////////////////////
 // nsSecurityNameSet //
 ///////////////////////
 
-#define NS_SECURITYNAMESET_CID \
- { 0x7c02eadc, 0x76, 0x4d03, \
- { 0x99, 0x8d, 0x80, 0xd7, 0x79, 0xc4, 0x85, 0x89 } }
-#define NS_SECURITYNAMESET_CONTRACTID "@mozilla.org/security/script/nameset;1"
-
-class nsSecurityNameSet : public nsIScriptExternalNameSet 
-{
-public:
-    nsSecurityNameSet();
-    virtual ~nsSecurityNameSet();
-    
-    NS_DECL_ISUPPORTS
-
-    NS_IMETHOD InitializeNameSet(nsIScriptContext* aScriptContext);
-};
-
 nsSecurityNameSet::nsSecurityNameSet()
 {
 }
 
 nsSecurityNameSet::~nsSecurityNameSet()
 {
 }
 
@@ -332,176 +315,8 @@ nsSecurityNameSet::InitializeNameSet(nsI
                           nsnull, 0);
     if (obj == nsnull)
         return NS_ERROR_FAILURE;
 
     return JS_DefineFunctions(cx, obj, PrivilegeManager_static_methods)
            ? NS_OK
            : NS_ERROR_FAILURE;
 }
-
-
-
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsPrincipal)
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsSecurityNameSet)
-NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsSystemPrincipal,
-    nsScriptSecurityManager::SystemPrincipalSingletonConstructor)
-NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsNullPrincipal, Init)
-
-NS_DECL_CLASSINFO(nsPrincipal)
-NS_DECL_CLASSINFO(nsSystemPrincipal)
-NS_DECL_CLASSINFO(nsNullPrincipal)
-
-static NS_IMETHODIMP
-Construct_nsIScriptSecurityManager(nsISupports *aOuter, REFNSIID aIID, 
-                                   void **aResult)
-{
-    if (!aResult)
-        return NS_ERROR_NULL_POINTER;
-    *aResult = nsnull;
-    if (aOuter)
-        return NS_ERROR_NO_AGGREGATION;
-    nsScriptSecurityManager *obj = nsScriptSecurityManager::GetScriptSecurityManager();
-    if (!obj) 
-        return NS_ERROR_OUT_OF_MEMORY;
-    if (NS_FAILED(obj->QueryInterface(aIID, aResult)))
-        return NS_ERROR_FAILURE;
-    return NS_OK;
-}
-
-static NS_METHOD 
-RegisterSecurityNameSet(nsIComponentManager *aCompMgr,
-                        nsIFile *aPath,
-                        const char *registryLocation,
-                        const char *componentType,
-                        const nsModuleComponentInfo *info)
-{
-    nsresult rv = NS_OK;
-
-    nsCOMPtr<nsICategoryManager> catman =
-        do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
-
-    if (NS_FAILED(rv))
-        return rv;
-
-    nsXPIDLCString previous;
-    rv = catman->AddCategoryEntry(JAVASCRIPT_GLOBAL_STATIC_NAMESET_CATEGORY,
-                                  "PrivilegeManager",
-                                  NS_SECURITYNAMESET_CONTRACTID,
-                                  PR_TRUE, PR_TRUE, getter_Copies(previous));
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    rv = catman->AddCategoryEntry("app-startup", "Script Security Manager",
-                                  "service," NS_SCRIPTSECURITYMANAGER_CONTRACTID,
-                                  PR_TRUE, PR_TRUE,
-                                  getter_Copies(previous));
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    return rv;
-}
-
-
-static const nsModuleComponentInfo capsComponentInfo[] =
-{
-    { NS_SCRIPTSECURITYMANAGER_CLASSNAME, 
-      NS_SCRIPTSECURITYMANAGER_CID, 
-      NS_SCRIPTSECURITYMANAGER_CONTRACTID,
-      Construct_nsIScriptSecurityManager,
-      RegisterSecurityNameSet,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsIClassInfo::MAIN_THREAD_ONLY
-    },
-
-    { NS_SCRIPTSECURITYMANAGER_CLASSNAME, 
-      NS_SCRIPTSECURITYMANAGER_CID, 
-      NS_GLOBAL_PREF_SECURITY_CHECK,
-      Construct_nsIScriptSecurityManager,
-      RegisterSecurityNameSet,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsIClassInfo::MAIN_THREAD_ONLY
-    },
-
-    { NS_SCRIPTSECURITYMANAGER_CLASSNAME,
-      NS_SCRIPTSECURITYMANAGER_CID,
-      NS_GLOBAL_CHANNELEVENTSINK_CONTRACTID,
-      Construct_nsIScriptSecurityManager,
-      RegisterSecurityNameSet,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsIClassInfo::MAIN_THREAD_ONLY
-    },
-
-
-
-    { NS_PRINCIPAL_CLASSNAME, 
-      NS_PRINCIPAL_CID, 
-      NS_PRINCIPAL_CONTRACTID,
-      nsPrincipalConstructor,
-      nsnull,
-      nsnull,
-      nsnull,
-      NS_CI_INTERFACE_GETTER_NAME(nsPrincipal),
-      nsnull,
-      &NS_CLASSINFO_NAME(nsPrincipal),
-      nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::EAGER_CLASSINFO
-    },
-
-    { NS_SYSTEMPRINCIPAL_CLASSNAME, 
-      NS_SYSTEMPRINCIPAL_CID, 
-      NS_SYSTEMPRINCIPAL_CONTRACTID,
-      nsSystemPrincipalConstructor,
-      nsnull,
-      nsnull,
-      nsnull,
-      NS_CI_INTERFACE_GETTER_NAME(nsSystemPrincipal),
-      nsnull,
-      &NS_CLASSINFO_NAME(nsSystemPrincipal),
-      nsIClassInfo::SINGLETON | nsIClassInfo::MAIN_THREAD_ONLY |
-      nsIClassInfo::EAGER_CLASSINFO
-    },
-
-    { NS_NULLPRINCIPAL_CLASSNAME, 
-      NS_NULLPRINCIPAL_CID, 
-      NS_NULLPRINCIPAL_CONTRACTID,
-      nsNullPrincipalConstructor,
-      nsnull,
-      nsnull,
-      nsnull,
-      NS_CI_INTERFACE_GETTER_NAME(nsNullPrincipal),
-      nsnull,
-      &NS_CLASSINFO_NAME(nsNullPrincipal),
-      nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::EAGER_CLASSINFO
-    },
-
-    { "Security Script Name Set",
-      NS_SECURITYNAMESET_CID,
-      NS_SECURITYNAMESET_CONTRACTID,
-      nsSecurityNameSetConstructor,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsnull,
-      nsIClassInfo::MAIN_THREAD_ONLY
-    }
-};
-
-
-void
-CapsModuleDtor(nsIModule* thisModules)
-{
-    nsScriptSecurityManager::Shutdown();
-}
-
-NS_IMPL_NSGETMODULE_WITH_DTOR(nsSecurityManagerModule, capsComponentInfo,
-                              CapsModuleDtor)
--- a/content/base/src/Makefile.in
+++ b/content/base/src/Makefile.in
@@ -172,11 +172,13 @@ INCLUDES	+= \
 		-I$(srcdir)/../../html/content/src \
 		-I$(srcdir)/../../base/src \
 		-I$(srcdir)/../../xbl/src \
 		-I$(srcdir)/../../../layout/generic \
 		-I$(srcdir)/../../../layout/style \
 		-I$(srcdir)/../../../dom/base \
 		-I$(srcdir)/../../xml/document/src \
 		-I$(topsrcdir)/xpcom/io \
+		-I$(topsrcdir)/js/src/xpconnect/src \
+		-I$(topsrcdir)/caps/include \
 		$(NULL)
 
 DEFINES += -D_IMPL_NS_LAYOUT
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -188,16 +188,18 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_
 
 #include "mozAutoDocUpdate.h"
 #include "imgICache.h"
 #include "jsinterp.h"
 #include "jsarray.h"
 #include "jsdate.h"
 #include "jsregexp.h"
 #include "jstypedarray.h"
+#include "xpcprivate.h"
+#include "nsScriptSecurityManager.h"
 
 using namespace mozilla::dom;
 
 const char kLoadAsData[] = "loadAsData";
 
 static const char kJSStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";
 static NS_DEFINE_CID(kParserServiceCID, NS_PARSERSERVICE_CID);
 static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
@@ -352,31 +354,32 @@ nsContentUtils::Init()
   if (sInitialized) {
     NS_WARNING("Init() called twice");
 
     return NS_OK;
   }
 
   sPrefCacheData = new nsTArray<nsAutoPtr<PrefCacheData> >();
 
-  nsresult rv = CallGetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID,
-                               &sSecurityManager);
-  NS_ENSURE_SUCCESS(rv, rv);
-
   // It's ok to not have a pref service.
   CallGetService(NS_PREFSERVICE_CONTRACTID, &sPrefBranch);
 
-  rv = NS_GetNameSpaceManager(&sNameSpaceManager);
+  nsresult rv = NS_GetNameSpaceManager(&sNameSpaceManager);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = CallGetService(nsIXPConnect::GetCID(), &sXPConnect);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = CallGetService(kJSStackContractID, &sThreadJSContextStack);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsXPConnect* xpconnect = nsXPConnect::GetXPConnect();
+  NS_ENSURE_TRUE(xpconnect, NS_ERROR_FAILURE);
+
+  sXPConnect = xpconnect;
+  sThreadJSContextStack = xpconnect;
+
+  sSecurityManager = nsScriptSecurityManager::GetScriptSecurityManager();
+  if(!sSecurityManager)
+    return NS_ERROR_FAILURE;
+  NS_ADDREF(sSecurityManager);
 
   rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService);
   if (NS_FAILED(rv)) {
     // This makes life easier, but we can live without it.
 
     sIOService = nsnull;
   }
 
@@ -971,20 +974,19 @@ nsContentUtils::Shutdown()
   }
 
   delete sPrefCacheData;
   sPrefCacheData = nsnull;
 
   NS_IF_RELEASE(sStringBundleService);
   NS_IF_RELEASE(sConsoleService);
   NS_IF_RELEASE(sDOMScriptObjectFactory);
-  if (sJSGCThingRootCount == 0 && sXPConnect)
-    NS_RELEASE(sXPConnect);
+  sXPConnect = nsnull;
+  sThreadJSContextStack = nsnull;
   NS_IF_RELEASE(sSecurityManager);
-  NS_IF_RELEASE(sThreadJSContextStack);
   NS_IF_RELEASE(sNameSpaceManager);
   NS_IF_RELEASE(sParserService);
   NS_IF_RELEASE(sIOService);
   NS_IF_RELEASE(sLineBreaker);
   NS_IF_RELEASE(sWordBreaker);
   NS_IF_RELEASE(sCaseConv);
   NS_IF_RELEASE(sGenCat);
 #ifdef MOZ_XTF
@@ -4173,34 +4175,38 @@ nsContentUtils::DropScriptObject(PRUint3
   }
 }
 
 /* static */
 nsresult
 nsContentUtils::HoldJSObjects(void* aScriptObjectHolder,
                               nsScriptObjectTracer* aTracer)
 {
+  NS_ENSURE_TRUE(sXPConnect, NS_ERROR_UNEXPECTED);
+
   nsresult rv = sXPConnect->AddJSHolder(aScriptObjectHolder, aTracer);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  ++sJSGCThingRootCount;
+  if (sJSGCThingRootCount++ == 0) {
+    nsLayoutStatics::AddRef();
+  }
   NS_LOG_ADDREF(sXPConnect, sJSGCThingRootCount, "HoldJSObjects",
                 sizeof(void*));
 
   return NS_OK;
 }
 
 /* static */
 nsresult
 nsContentUtils::DropJSObjects(void* aScriptObjectHolder)
 {
   NS_LOG_RELEASE(sXPConnect, sJSGCThingRootCount - 1, "HoldJSObjects");
   nsresult rv = sXPConnect->RemoveJSHolder(aScriptObjectHolder);
-  if (--sJSGCThingRootCount == 0 && !sInitialized) {
-    NS_RELEASE(sXPConnect);
+  if (--sJSGCThingRootCount == 0) {
+    nsLayoutStatics::Release();
   }
   return rv;
 }
 
 /* static */
 PRUint32
 nsContentUtils::GetWidgetStatusFromIMEStatus(PRUint32 aState)
 {
--- a/js/src/xpconnect/src/Makefile.in
+++ b/js/src/xpconnect/src/Makefile.in
@@ -40,33 +40,34 @@
 DEPTH		= ../../../..
 topsrcdir	= @top_srcdir@
 srcdir		= @srcdir@
 VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE		= xpconnect
+
+ifeq (xpconnect, $(findstring xpconnect, $(BUILD_MODULES)))
 LIBRARY_NAME	= xpconnect
 EXPORT_LIBRARY = 1
 SHORT_LIBNAME	= xpconect
 IS_COMPONENT	= 1
 MODULE_NAME	= xpconnect
 GRE_MODULE	= 1
 ifeq ($(OS_ARCH)$(MOZ_ENABLE_LIBXUL),WINNT)
 LIBRARY_NAME	= xpc32$(VERSION_NUMBER)
 SHORT_LIBNAME	= xpc32$(VERSION_NUMBER)
 endif
+else
+LIBRARY_NAME    = xpconnect_s
+FORCE_STATIC_LIB = 1
+endif
 LIBXUL_LIBRARY = 1
 
-
-# These modules are required because the auto-generated file
-# dom_quickstubs.cpp #includes header files from many places.
-
-
 CPPSRCS		= \
 		nsScriptError.cpp \
 		nsXPConnect.cpp \
 		xpccallcontext.cpp \
 		xpccomponents.cpp \
 		xpccontext.cpp \
 		xpcconvert.cpp \
 		xpcdebug.cpp \
@@ -113,16 +114,17 @@ CPPSRCS +=	XPCDispObject.cpp	\
 endif
 
 include $(topsrcdir)/config/config.mk
 
 LOCAL_INCLUDES = \
 		-I$(srcdir)/../loader \
 		-I$(topsrcdir)/js/src \
 		-I$(topsrcdir)/js/src/nanojit \
+		-I$(topsrcdir)/caps/include \
 		$(NULL)
 
 EXTRA_DSO_LDOPTS += \
 		$(MOZ_COMPONENT_LIBS) \
 		$(MOZ_JS_LIBS) \
 		$(NULL)
 
 ifdef MOZ_JSLOADER
--- a/js/src/xpconnect/src/xpcmodule.cpp
+++ b/js/src/xpconnect/src/xpcmodule.cpp
@@ -35,102 +35,53 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 /* Module level methods. */
 
-#include "xpcprivate.h"
-#ifdef MOZ_JSLOADER
-#include "mozJSLoaderConstructors.h"
-#endif
-
-/* Module implementation for the xpconnect library. */
-
-NS_DECL_CLASSINFO(XPCVariant)
-
-// {DC524540-487E-4501-9AC7-AAA784B17C1C}
-#define XPCVARIANT_CID \
-    {0xdc524540, 0x487e, 0x4501, \
-      { 0x9a, 0xc7, 0xaa, 0xa7, 0x84, 0xb1, 0x7c, 0x1c } }
-
-#define XPCVARIANT_CONTRACTID "@mozilla.org/xpcvariant;1"
-#define XPC_JSCONTEXT_STACK_ITERATOR_CONTRACTID "@mozilla.org/js/xpc/ContextStackIterator;1"
-
-// {FE4F7592-C1FC-4662-AC83-538841318803}
-#define SCRIPTABLE_INTERFACES_CID \
-    {0xfe4f7592, 0xc1fc, 0x4662, \
-      { 0xac, 0x83, 0x53, 0x88, 0x41, 0x31, 0x88, 0x3 } }
-
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSID)
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCException)
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCJSContextStackIterator)
-NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIXPConnect, nsXPConnect::GetSingleton)
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptError)
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCComponents_Interfaces)
-
-#ifdef XPC_IDISPATCH_SUPPORT
-NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIDispatchSupport, nsDispatchSupport::GetSingleton)
-#endif
-
-NS_DECL_CLASSINFO(nsXPCException)
-
 #ifdef XPCONNECT_STANDALONE
 #define NO_SUBSCRIPT_LOADER
 #endif
 
-static const nsModuleComponentInfo components[] = {
-  {nsnull, NS_JS_ID_CID,                         XPC_ID_CONTRACTID,            nsJSIDConstructor             },
-  {nsnull, NS_XPCONNECT_CID,                     XPC_XPCONNECT_CONTRACTID,     nsIXPConnectConstructor       },
-  {nsnull, NS_XPC_THREAD_JSCONTEXT_STACK_CID,    XPC_CONTEXT_STACK_CONTRACTID, nsIXPConnectConstructor  },
-  {nsnull, NS_XPCEXCEPTION_CID,                  XPC_EXCEPTION_CONTRACTID,     nsXPCExceptionConstructor, nsnull, nsnull, nsnull, NS_CI_INTERFACE_GETTER_NAME(nsXPCException), nsnull, &NS_CLASSINFO_NAME(nsXPCException), nsIClassInfo::DOM_OBJECT },
-  {nsnull, NS_JS_RUNTIME_SERVICE_CID,            XPC_RUNTIME_CONTRACTID,       nsIXPConnectConstructor},
-  {NS_SCRIPTERROR_CLASSNAME, NS_SCRIPTERROR_CID, NS_SCRIPTERROR_CONTRACTID,    nsScriptErrorConstructor      },
-  {nsnull, SCRIPTABLE_INTERFACES_CID,            NS_SCRIPTABLE_INTERFACES_CONTRACTID,        nsXPCComponents_InterfacesConstructor, 0, 0, 0, 0, 0, 0, nsIClassInfo::THREADSAFE },
-  {nsnull, XPCVARIANT_CID,                       XPCVARIANT_CONTRACTID,        nsnull, nsnull, nsnull, nsnull, NS_CI_INTERFACE_GETTER_NAME(XPCVariant), nsnull, &NS_CLASSINFO_NAME(XPCVariant)},
-  {nsnull, NS_XPC_JSCONTEXT_STACK_ITERATOR_CID,  XPC_JSCONTEXT_STACK_ITERATOR_CONTRACTID, nsXPCJSContextStackIteratorConstructor }
+#include "xpcmodule.h"
 
-#ifdef MOZ_JSLOADER
-  // jsloader stuff
- ,{ "JS component loader", MOZJSCOMPONENTLOADER_CID,
-    MOZJSCOMPONENTLOADER_CONTRACTID, mozJSComponentLoaderConstructor,
-    RegisterJSLoader, UnregisterJSLoader }
-#ifndef NO_SUBSCRIPT_LOADER
- ,{ "JS subscript loader", MOZ_JSSUBSCRIPTLOADER_CID,
-    mozJSSubScriptLoadContractID, mozJSSubScriptLoaderConstructor }
-#endif
-#endif
-#ifdef XPC_IDISPATCH_SUPPORT
- ,{ nsnull, NS_IDISPATCH_SUPPORT_CID,            NS_IDISPATCH_SUPPORT_CONTRACTID,
-    nsIDispatchSupportConstructor }
-#endif
-};
-
-static nsresult
+nsresult
 xpcModuleCtor(nsIModule* self)
 {
     nsXPConnect::InitStatics();
     nsXPCException::InitStatics();
     XPCWrappedNativeScope::InitStatics();
     XPCPerThreadData::InitStatics();
 
 #ifdef XPC_IDISPATCH_SUPPORT
     XPCIDispatchExtension::InitStatics();
 #endif
 
     return NS_OK;
 }
 
-static void
-xpcModuleDtor(nsIModule* self)
+void
+xpcModuleDtor(nsIModule*)
 {
     // Release our singletons
     nsXPConnect::ReleaseXPConnectSingleton();
     xpc_DestroyJSxIDClassObjects();
 #ifdef XPC_IDISPATCH_SUPPORT
     nsDispatchSupport::FreeSingleton();
     XPCIDispatchClassInfo::FreeSingleton();
 #endif
 }
 
+#ifdef XPCONNECT_STANDALONE
+
+/* Module implementation for the xpconnect library. */
+
+XPCONNECT_FACTORIES
+
+static const nsModuleComponentInfo components[] = {
+  XPCONNECT_COMPONENTS
+};
+
 NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(xpconnect, components, xpcModuleCtor, xpcModuleDtor)
+#endif
new file mode 100644
--- /dev/null
+++ b/js/src/xpconnect/src/xpcmodule.h
@@ -0,0 +1,237 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Communicator client code, released
+ * March 31, 1998.
+ *
+ * The Initial Developer of the Original Code is
+ * Netscape Communications Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 1998
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   John Bandhauer <jband@netscape.com> (original author)
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef xpcmodule_h___
+#define xpcmodule_h___
+
+#include "xpcprivate.h"
+#ifdef MOZ_JSLOADER
+#include "mozJSLoaderConstructors.h"
+#endif
+
+/* Module implementation for the xpconnect library. */
+
+// {DC524540-487E-4501-9AC7-AAA784B17C1C}
+#define XPCVARIANT_CID                                                        \
+    {0xdc524540, 0x487e, 0x4501,                                              \
+      { 0x9a, 0xc7, 0xaa, 0xa7, 0x84, 0xb1, 0x7c, 0x1c } }
+
+#define XPCVARIANT_CONTRACTID "@mozilla.org/xpcvariant;1"
+#define XPC_JSCONTEXT_STACK_ITERATOR_CONTRACTID                               \
+    "@mozilla.org/js/xpc/ContextStackIterator;1"
+
+// {FE4F7592-C1FC-4662-AC83-538841318803}
+#define SCRIPTABLE_INTERFACES_CID                                             \
+    {0xfe4f7592, 0xc1fc, 0x4662,                                              \
+      { 0xac, 0x83, 0x53, 0x88, 0x41, 0x31, 0x88, 0x3 } }
+
+
+#define XPCONNECT_GENERAL_FACTORIES                                           \
+  NS_DECL_CLASSINFO(XPCVariant)                                               \
+  NS_DECL_CLASSINFO(nsXPCException)                                           \
+                                                                              \
+  NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSID)                                      \
+  NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCException)                              \
+  NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCJSContextStackIterator)                 \
+  NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIXPConnect,                      \
+                                           nsXPConnect::GetSingleton)         \
+  NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptError)                               \
+  NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCComponents_Interfaces)
+
+
+#ifdef XPC_IDISPATCH_SUPPORT
+
+#define XPCONNECT_FACTORIES                                                   \
+  NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIDispatchSupport,                \
+                                           nsDispatchSupport::GetSingleton)   \
+  XPCONNECT_GENERAL_FACTORIES
+
+#else
+
+#define XPCONNECT_FACTORIES XPCONNECT_GENERAL_FACTORIES
+
+#endif // XPC_IDISPATCH_SUPPORT
+
+
+#ifdef XPCONNECT_STANDALONE
+#define NO_SUBSCRIPT_LOADER
+#endif
+
+
+#define XPCONNECT_GENERAL_COMPONENTS                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    NS_JS_ID_CID,                                                             \
+    XPC_ID_CONTRACTID,                                                        \
+    nsJSIDConstructor                                                         \
+  },                                                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    NS_XPCONNECT_CID,                                                         \
+    XPC_XPCONNECT_CONTRACTID,                                                 \
+    nsIXPConnectConstructor                                                   \
+  },                                                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    NS_XPC_THREAD_JSCONTEXT_STACK_CID,                                        \
+    XPC_CONTEXT_STACK_CONTRACTID,                                             \
+    nsIXPConnectConstructor                                                   \
+  },                                                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    NS_XPCEXCEPTION_CID,                                                      \
+    XPC_EXCEPTION_CONTRACTID,                                                 \
+    nsXPCExceptionConstructor,                                                \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    NS_CI_INTERFACE_GETTER_NAME(nsXPCException),                              \
+    nsnull,                                                                   \
+    &NS_CLASSINFO_NAME(nsXPCException),                                       \
+    nsIClassInfo::DOM_OBJECT                                                  \
+  },                                                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    NS_JS_RUNTIME_SERVICE_CID,                                                \
+    XPC_RUNTIME_CONTRACTID,                                                   \
+    nsIXPConnectConstructor                                                   \
+  },                                                                          \
+  {                                                                           \
+    NS_SCRIPTERROR_CLASSNAME,                                                 \
+    NS_SCRIPTERROR_CID,                                                       \
+    NS_SCRIPTERROR_CONTRACTID,                                                \
+    nsScriptErrorConstructor                                                  \
+  },                                                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    SCRIPTABLE_INTERFACES_CID,                                                \
+    NS_SCRIPTABLE_INTERFACES_CONTRACTID,                                      \
+    nsXPCComponents_InterfacesConstructor,                                    \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsIClassInfo::THREADSAFE                                                  \
+  },                                                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    XPCVARIANT_CID,                                                           \
+    XPCVARIANT_CONTRACTID,                                                    \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    nsnull,                                                                   \
+    NS_CI_INTERFACE_GETTER_NAME(XPCVariant),                                  \
+    nsnull,                                                                   \
+    &NS_CLASSINFO_NAME(XPCVariant)                                            \
+  },                                                                          \
+  {                                                                           \
+    nsnull,                                                                   \
+    NS_XPC_JSCONTEXT_STACK_ITERATOR_CID,                                      \
+    XPC_JSCONTEXT_STACK_ITERATOR_CONTRACTID,                                  \
+    nsXPCJSContextStackIteratorConstructor                                    \
+  }
+
+// jsloader stuff
+#ifdef MOZ_JSLOADER
+
+#define XPCONNECT_LOADER_COMPONENTS                                           \
+  {                                                                           \
+    "JS component loader",                                                    \
+    MOZJSCOMPONENTLOADER_CID,                                                 \
+    MOZJSCOMPONENTLOADER_CONTRACTID,                                          \
+    mozJSComponentLoaderConstructor,                                          \
+    RegisterJSLoader,                                                         \
+    UnregisterJSLoader                                                        \
+  },                                                                          \
+  XPCONNECT_SUBSCRIPT_LOADER_COMPONENTS
+
+#ifdef NO_SUBSCRIPT_LOADER
+
+#define XPCONNECT_SUBSCRIPT_LOADER_COMPONENTS
+
+#else
+
+#define XPCONNECT_SUBSCRIPT_LOADER_COMPONENTS                                 \
+  {                                                                           \
+    "JS subscript loader",                                                    \
+    MOZ_JSSUBSCRIPTLOADER_CID,                                                \
+    mozJSSubScriptLoadContractID,                                             \
+    mozJSSubScriptLoaderConstructor                                           \
+  },
+
+#endif // NO_SUBSCRIPT_LOADER
+
+#else
+
+#define XPCONNECT_LOADER_COMPONENTS
+
+#endif // MOZ_JSLOADER
+
+
+#ifdef XPC_IDISPATCH_SUPPORT
+
+#define XPCONNECT_IDISPATCH_COMPONENTS                                        \
+  {                                                                           \
+    nsnull,                                                                   \
+    NS_IDISPATCH_SUPPORT_CID,                                                 \
+    NS_IDISPATCH_SUPPORT_CONTRACTID,                                          \
+    nsIDispatchSupportConstructor                                             \
+  },
+
+#else
+
+#define XPCONNECT_IDISPATCH_COMPONENTS
+
+#endif // XPC_IDISPATCH_SUPPORT
+
+
+#define XPCONNECT_COMPONENTS                                                  \
+  XPCONNECT_LOADER_COMPONENTS                                                 \
+  XPCONNECT_IDISPATCH_COMPONENTS                                              \
+  XPCONNECT_GENERAL_COMPONENTS
+
+extern nsresult xpcModuleCtor(nsIModule* self);
+extern void xpcModuleDtor(nsIModule*);
+
+#endif /* xpcmodule_h___ */
+
--- a/js/src/xpconnect/src/xpcthreadcontext.cpp
+++ b/js/src/xpconnect/src/xpcthreadcontext.cpp
@@ -40,16 +40,17 @@
  * ***** END LICENSE BLOCK ***** */
 
 /* Implement global service to track stack of JSContext per thread. */
 
 #include "xpcprivate.h"
 #include "XPCWrapper.h"
 #include "nsDOMJSUtils.h"
 #include "nsIScriptGlobalObject.h"
+#include "nsNullPrincipal.h"
 
 /***************************************************************************/
 
 XPCJSContextStack::XPCJSContextStack()
     : mStack(),
       mSafeJSContext(nsnull),
       mOwnSafeJSContext(nsnull)
 {
@@ -221,22 +222,23 @@ static JSClass global_class = {
 NS_IMETHODIMP
 XPCJSContextStack::GetSafeJSContext(JSContext * *aSafeJSContext)
 {
     if(!mSafeJSContext)
     {
 #ifndef XPCONNECT_STANDALONE
         // Start by getting the principal holder and principal for this
         // context.  If we can't manage that, don't bother with the rest.
-        nsCOMPtr<nsIPrincipal> principal =
-            do_CreateInstance("@mozilla.org/nullprincipal;1");
+        nsRefPtr<nsNullPrincipal> principal = new nsNullPrincipal();
         nsCOMPtr<nsIScriptObjectPrincipal> sop;
         if(principal)
         {
-            sop = new PrincipalHolder(principal);
+            nsresult rv = principal->Init();
+            if(NS_SUCCEEDED(rv))
+              sop = new PrincipalHolder(principal);
         }
         if(!sop)
         {
             *aSafeJSContext = nsnull;
             return NS_ERROR_FAILURE;
         }        
 #endif /* !XPCONNECT_STANDALONE */
         
--- a/layout/build/Makefile.in
+++ b/layout/build/Makefile.in
@@ -103,16 +103,17 @@ SHARED_LIBRARY_LIBS = \
 	$(DEPTH)/dom/src/jsurl/$(LIB_PREFIX)jsurl_s.$(LIB_SUFFIX) \
 	$(DEPTH)/dom/src/storage/$(LIB_PREFIX)jsdomstorage_s.$(LIB_SUFFIX) \
 	$(DEPTH)/dom/src/offline/$(LIB_PREFIX)jsdomoffline_s.$(LIB_SUFFIX) \
 	$(DEPTH)/dom/src/geolocation/$(LIB_PREFIX)jsdomgeolocation_s.$(LIB_SUFFIX) \
 	$(DEPTH)/dom/src/threads/$(LIB_PREFIX)domthreads_s.$(LIB_SUFFIX) \
 	$(DEPTH)/editor/libeditor/text/$(LIB_PREFIX)texteditor_s.$(LIB_SUFFIX) \
 	$(DEPTH)/editor/libeditor/base/$(LIB_PREFIX)editorbase_s.$(LIB_SUFFIX) \
 	$(DEPTH)/parser/html/$(LIB_PREFIX)html5p_s.$(LIB_SUFFIX) \
+	$(DEPTH)/caps/src/$(LIB_PREFIX)caps_s.$(LIB_SUFFIX) \
 	$(NULL)
 
 ifdef MOZ_MEDIA
 SHARED_LIBRARY_LIBS 	+= \
 	$(DEPTH)/content/media/$(LIB_PREFIX)gkconmedia_s.$(LIB_SUFFIX) \
 	$(NULL)
 endif
 
@@ -195,16 +196,19 @@ SHARED_LIBRARY_LIBS += \
 	$(DEPTH)/editor/txtsvc/src/$(LIB_PREFIX)txtsvc_s.$(LIB_SUFFIX) \
 	$(NULL)
 endif
 
 ifdef ENABLE_EDITOR_API_LOG
 DEFINES += -DENABLE_EDITOR_API_LOG
 endif
 
+SHARED_LIBRARY_LIBS += \
+	$(DEPTH)/js/src/xpconnect/src/$(LIB_PREFIX)xpconnect_s.$(LIB_SUFFIX)
+
 EXTRA_DSO_LDOPTS = \
 		$(LIBS_DIR) \
 		$(EXTRA_DSO_LIBS) \
 		$(MOZ_UNICHARUTIL_LIBS) \
 		$(MOZ_COMPONENT_LIBS) \
 		$(MOZ_JS_LIBS) \
 		$(NULL)
 
@@ -264,16 +268,19 @@ LOCAL_INCLUDES	+= -I$(srcdir)/../base \
 		   -I$(topsrcdir)/dom/src/geolocation \
 		   -I$(topsrcdir)/dom/src/threads \
 		   -I. \
 		   -I$(topsrcdir)/editor/libeditor/base \
 		   -I$(topsrcdir)/editor/libeditor/text \
 		   -I$(topsrcdir)/editor/libeditor/html \
 		   -I$(topsrcdir)/editor/txtsvc/src \
 		   -I$(topsrcdir)/editor/composer/src \
+		   -I$(topsrcdir)/js/src/xpconnect/src \
+		   -I$(topsrcdir)/js/src/xpconnect/loader \
+		   -I$(topsrcdir)/caps/include \
 		   $(NULL)
 
 ifdef MOZ_MATHML
 LOCAL_INCLUDES	+= -I$(srcdir)/../mathml
 endif
 
 ifdef MOZ_SVG
 LOCAL_INCLUDES	+= -I$(topsrcdir)/content/svg/content/src
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -31,16 +31,17 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+#include "xpcmodule.h"
 #include "nsLayoutStatics.h"
 #include "nsContentCID.h"
 #include "nsContentDLF.h"
 #include "nsContentPolicyUtils.h"
 #include "nsDataDocumentContentPolicy.h"
 #include "nsNoDataProtocolContentPolicy.h"
 #include "nsDOMCID.h"
 #include "nsHTMLContentSerializer.h"
@@ -138,16 +139,23 @@
 #include "nsIControllerCommandTable.h"
 
 #ifndef MOZILLA_PLAINTEXT_EDITOR_ONLY
 #include "nsHTMLEditor.h"
 #include "nsTextServicesDocument.h"
 #include "nsTextServicesCID.h"
 #endif
 
+#include "nsScriptSecurityManager.h"
+#include "nsPrincipal.h"
+#include "nsSystemPrincipal.h"
+#include "nsNullPrincipal.h"
+#include "nsPrefsCID.h"
+#include "nsNetCID.h"
+
 #define NS_EDITORCOMMANDTABLE_CID \
 { 0x4f5e62b8, 0xd659, 0x4156, { 0x84, 0xfc, 0x2f, 0x60, 0x99, 0x40, 0x03, 0x69 }}
 
 static NS_DEFINE_CID(kEditorCommandTableCID, NS_EDITORCOMMANDTABLE_CID);
 
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsPlaintextEditor)
 
 // Constructor of a controller which is set up to use, internally, a
@@ -256,17 +264,16 @@ static NS_DEFINE_CID(kWindowCommandTable
 
 NS_IMETHODIMP
 NS_NewXULContentBuilder(nsISupports* aOuter, REFNSIID aIID, void** aResult);
 
 NS_IMETHODIMP
 NS_NewXULTreeBuilder(nsISupports* aOuter, REFNSIID aIID, void** aResult);
 #endif
 
-static nsresult Initialize(nsIModule* aSelf);
 static void Shutdown();
 
 #ifdef MOZ_XTF
 #include "nsIXTFService.h"
 #include "nsIXMLContentBuilder.h"
 #endif
 
 #include "nsGeolocation.h"
@@ -916,18 +923,78 @@ CSPServiceUnregistration(nsIComponentMan
                                    "CSPService",
                                    PR_TRUE);
 
   return rv;
 }
 
 NS_GENERIC_FACTORY_CONSTRUCTOR(CSPService)
 
+XPCONNECT_FACTORIES
+
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsPrincipal)
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsSecurityNameSet)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsSystemPrincipal,
+    nsScriptSecurityManager::SystemPrincipalSingletonConstructor)
+NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsNullPrincipal, Init)
+
+NS_DECL_CLASSINFO(nsPrincipal)
+NS_DECL_CLASSINFO(nsSystemPrincipal)
+NS_DECL_CLASSINFO(nsNullPrincipal)
+
+static NS_IMETHODIMP
+Construct_nsIScriptSecurityManager(nsISupports *aOuter, REFNSIID aIID, 
+                                   void **aResult)
+{
+    if (!aResult)
+        return NS_ERROR_NULL_POINTER;
+    *aResult = nsnull;
+    if (aOuter)
+        return NS_ERROR_NO_AGGREGATION;
+    nsScriptSecurityManager *obj = nsScriptSecurityManager::GetScriptSecurityManager();
+    if (!obj) 
+        return NS_ERROR_OUT_OF_MEMORY;
+    if (NS_FAILED(obj->QueryInterface(aIID, aResult)))
+        return NS_ERROR_FAILURE;
+    return NS_OK;
+}
+
+static NS_METHOD 
+RegisterSecurityNameSet(nsIComponentManager *aCompMgr,
+                        nsIFile *aPath,
+                        const char *registryLocation,
+                        const char *componentType,
+                        const nsModuleComponentInfo *info)
+{
+    nsresult rv = NS_OK;
+
+    nsCOMPtr<nsICategoryManager> catman =
+        do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
+
+    if (NS_FAILED(rv))
+        return rv;
+
+    nsXPIDLCString previous;
+    rv = catman->AddCategoryEntry(JAVASCRIPT_GLOBAL_STATIC_NAMESET_CATEGORY,
+                                  "PrivilegeManager",
+                                  NS_SECURITYNAMESET_CONTRACTID,
+                                  PR_TRUE, PR_TRUE, getter_Copies(previous));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = catman->AddCategoryEntry("app-startup", "Script Security Manager",
+                                  "service," NS_SCRIPTSECURITYMANAGER_CONTRACTID,
+                                  PR_TRUE, PR_TRUE,
+                                  getter_Copies(previous));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    return rv;
+}
+
 // The list of components we register
-static const nsModuleComponentInfo gComponents[] = {
+static const nsModuleComponentInfo gLayoutComponents[] = {
 #ifdef DEBUG
   { "Frame utility",
     NS_FRAME_UTIL_CID,
     nsnull,
     CreateNewFrameUtil },
   { "Layout debugger",
     NS_LAYOUT_DEBUGGER_CID,
     nsnull,
@@ -1539,9 +1606,219 @@ static const nsModuleComponentInfo gComp
       CreateEventListenerService },
 
     { "Channel Policy",
       NSCHANNELPOLICY_CID,
       NSCHANNELPOLICY_CONTRACTID,
       nsChannelPolicyConstructor }
 };
 
-NS_IMPL_NSGETMODULE_WITH_CTOR(nsLayoutModule, gComponents, Initialize)
+static nsModuleInfo const kLayoutModuleInfo = {
+    NS_MODULEINFO_VERSION,
+    "nsLayoutModule",
+    gLayoutComponents,
+    (sizeof(gLayoutComponents) / sizeof(gLayoutComponents[0])),
+    Initialize,
+    nsnull
+};
+
+static const nsModuleComponentInfo gXPConnectComponents[] = {
+    XPCONNECT_COMPONENTS,
+
+    { NS_SCRIPTSECURITYMANAGER_CLASSNAME, 
+      NS_SCRIPTSECURITYMANAGER_CID, 
+      NS_SCRIPTSECURITYMANAGER_CONTRACTID,
+      Construct_nsIScriptSecurityManager,
+      RegisterSecurityNameSet,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsIClassInfo::MAIN_THREAD_ONLY
+    },
+
+    { NS_SCRIPTSECURITYMANAGER_CLASSNAME, 
+      NS_SCRIPTSECURITYMANAGER_CID, 
+      NS_GLOBAL_PREF_SECURITY_CHECK,
+      Construct_nsIScriptSecurityManager,
+      RegisterSecurityNameSet,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsIClassInfo::MAIN_THREAD_ONLY
+    },
+
+    { NS_SCRIPTSECURITYMANAGER_CLASSNAME,
+      NS_SCRIPTSECURITYMANAGER_CID,
+      NS_GLOBAL_CHANNELEVENTSINK_CONTRACTID,
+      Construct_nsIScriptSecurityManager,
+      RegisterSecurityNameSet,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsIClassInfo::MAIN_THREAD_ONLY
+    },
+
+
+
+    { NS_PRINCIPAL_CLASSNAME, 
+      NS_PRINCIPAL_CID, 
+      NS_PRINCIPAL_CONTRACTID,
+      nsPrincipalConstructor,
+      nsnull,
+      nsnull,
+      nsnull,
+      NS_CI_INTERFACE_GETTER_NAME(nsPrincipal),
+      nsnull,
+      &NS_CLASSINFO_NAME(nsPrincipal),
+      nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::EAGER_CLASSINFO
+    },
+
+    { NS_SYSTEMPRINCIPAL_CLASSNAME, 
+      NS_SYSTEMPRINCIPAL_CID, 
+      NS_SYSTEMPRINCIPAL_CONTRACTID,
+      nsSystemPrincipalConstructor,
+      nsnull,
+      nsnull,
+      nsnull,
+      NS_CI_INTERFACE_GETTER_NAME(nsSystemPrincipal),
+      nsnull,
+      &NS_CLASSINFO_NAME(nsSystemPrincipal),
+      nsIClassInfo::SINGLETON | nsIClassInfo::MAIN_THREAD_ONLY |
+      nsIClassInfo::EAGER_CLASSINFO
+    },
+
+    { NS_NULLPRINCIPAL_CLASSNAME, 
+      NS_NULLPRINCIPAL_CID, 
+      NS_NULLPRINCIPAL_CONTRACTID,
+      nsNullPrincipalConstructor,
+      nsnull,
+      nsnull,
+      nsnull,
+      NS_CI_INTERFACE_GETTER_NAME(nsNullPrincipal),
+      nsnull,
+      &NS_CLASSINFO_NAME(nsNullPrincipal),
+      nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::EAGER_CLASSINFO
+    },
+
+    { "Security Script Name Set",
+      NS_SECURITYNAMESET_CID,
+      NS_SECURITYNAMESET_CONTRACTID,
+      nsSecurityNameSetConstructor,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsnull,
+      nsIClassInfo::MAIN_THREAD_ONLY
+    }
+};
+
+void
+XPConnectModuleDtor(nsIModule *self)
+{
+  xpcModuleDtor(self);
+
+  nsScriptSecurityManager::Shutdown();
+}
+
+static nsModuleInfo const kXPConnectModuleInfo = {
+    NS_MODULEINFO_VERSION,
+    "XPConnectModule",
+    gXPConnectComponents,
+    (sizeof(gXPConnectComponents) / sizeof(gXPConnectComponents[0])),
+    xpcModuleCtor,
+    XPConnectModuleDtor
+};
+
+// XPConnect is initialized early, because it has an XPCOM component loader.
+// Initializing nsLayoutStatics at that point leads to recursive initialization,
+// because it instantiates a bunch of other components. To get around that we
+// use two nsGenericModules, each with their own initialiser. nsLayoutStatics is
+// initialized for the module for layout components, but not for the module for
+// XPConnect components. nsLayoutModule forwards to the two nsGenericModules.
+class nsLayoutModule : public nsIModule
+{
+public:
+  nsLayoutModule(nsIModule *aXPConnectModule, nsIModule *aLayoutModule)
+    : mXPConnectModule(aXPConnectModule),
+      mLayoutModule(aLayoutModule)
+  {
+  }
+
+  NS_DECL_ISUPPORTS
+
+  // nsIModule
+  NS_SCRIPTABLE NS_IMETHOD GetClassObject(nsIComponentManager *aCompMgr,
+                                          const nsCID & aClass,
+                                          const nsIID & aIID,
+                                          void **aResult NS_OUTPARAM)
+  {
+    nsresult rv = mXPConnectModule->GetClassObject(aCompMgr, aClass, aIID,
+                                                   aResult);
+    if (rv == NS_ERROR_FACTORY_NOT_REGISTERED) {
+      rv = mLayoutModule->GetClassObject(aCompMgr, aClass, aIID, aResult);
+    }
+    return rv;
+  }
+  NS_SCRIPTABLE NS_IMETHOD RegisterSelf(nsIComponentManager *aCompMgr,
+                                        nsIFile *aLocation,
+                                        const char *aLoaderStr,
+                                        const char *aType)
+  {
+    nsresult rv = mXPConnectModule->RegisterSelf(aCompMgr, aLocation,
+                                                 aLoaderStr, aType);
+    if (NS_SUCCEEDED(rv)) {
+      rv = mLayoutModule->RegisterSelf(aCompMgr, aLocation, aLoaderStr, aType);
+    }
+    return rv;
+  }
+  NS_SCRIPTABLE NS_IMETHOD UnregisterSelf(nsIComponentManager *aCompMgr,
+                                          nsIFile *aLocation,
+                                          const char *aLoaderStr)
+  {
+    nsresult rv = mXPConnectModule->UnregisterSelf(aCompMgr, aLocation,
+                                                   aLoaderStr);
+    if (NS_SUCCEEDED(rv)) {
+      rv = mLayoutModule->UnregisterSelf(aCompMgr, aLocation, aLoaderStr);
+    }
+    return rv;
+  }
+  NS_SCRIPTABLE NS_IMETHOD CanUnload(nsIComponentManager *aCompMgr,
+                                     PRBool *aResult NS_OUTPARAM)
+  {
+    nsresult rv = mXPConnectModule->CanUnload(aCompMgr, aResult);
+    if (NS_SUCCEEDED(rv)) {
+      rv = mLayoutModule->CanUnload(aCompMgr, aResult);
+    }
+    return rv;
+  }
+
+private:
+  nsCOMPtr<nsIModule> mXPConnectModule;
+  nsCOMPtr<nsIModule> mLayoutModule;
+};
+
+NS_IMPL_ISUPPORTS1(nsLayoutModule, nsIModule)
+
+NSGETMODULE_ENTRY_POINT(nsLayoutModule)(nsIComponentManager *aServMgr,
+                                        nsIFile *aLocation,
+                                        nsIModule **aResult)
+{
+    nsCOMPtr<nsIModule> xpconnectModule;
+    nsresult rv = NS_NewGenericModule2(&kXPConnectModuleInfo,
+                                       getter_AddRefs(xpconnectModule));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    nsCOMPtr<nsIModule> layoutModule;
+    rv = NS_NewGenericModule2(&kLayoutModuleInfo, getter_AddRefs(layoutModule));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    NS_ADDREF(*aResult = new nsLayoutModule(xpconnectModule, layoutModule));
+
+    return NS_OK;
+}
--- a/toolkit/library/libxul-config.mk
+++ b/toolkit/library/libxul-config.mk
@@ -127,24 +127,22 @@ ifdef MOZ_XPINSTALL
 STATIC_LIBS += \
 	mozreg_s \
 	$(NULL)
 endif
 endif
 
 # component libraries
 COMPONENT_LIBS += \
-	xpconnect \
 	necko \
 	uconv \
 	i18n \
 	chardet \
 	jar$(VERSION_NUMBER) \
 	pref \
-	caps \
 	htmlpars \
 	imglib2 \
 	gklayout \
 	docshell \
 	embedcomponents \
 	webbrwsr \
 	nsappshell \
 	txmgr \
--- a/toolkit/library/nsStaticXULComponents.cpp
+++ b/toolkit/library/nsStaticXULComponents.cpp
@@ -229,28 +229,26 @@
 
 #if defined(BUILD_CTYPES)
 #define JSCTYPES_MODULE MODULE(jsctypes)
 #else
 #define JSCTYPES_MODULE
 #endif
 
 #define XUL_MODULES                          \
-    MODULE(xpconnect)                        \
     MODULE(nsUConvModule)                    \
     MODULE(nsI18nModule)                     \
     MODULE(nsChardetModule)                  \
     UNIVERSALCHARDET_MODULE                  \
     MODULE(necko)                            \
     PERMISSIONS_MODULES                      \
     AUTH_MODULE                              \
     MODULE(nsJarModule)                      \
     ZIPWRITER_MODULE                         \
     MODULE(nsPrefModule)                     \
-    MODULE(nsSecurityManagerModule)          \
     RDF_MODULES                              \
     MODULE(nsParserModule)                   \
     GFX_MODULES                              \
     WIDGET_MODULES                           \
     MODULE(nsImageLib2Module)                \
     ICON_MODULE                              \
     PLUGINS_MODULES                          \
     MODULE(nsLayoutModule)                   \