2011-12-18 02:09:16 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "plhash.h"
|
|
|
|
#include "jsapi.h"
|
2010-06-10 11:11:11 -07:00
|
|
|
#include "mozilla/ModuleLoader.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIJSRuntimeService.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIObserver.h"
|
2007-05-15 16:27:40 -07:00
|
|
|
#include "xpcIJSModuleLoader.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsClassHashtable.h"
|
2007-06-19 23:29:49 -07:00
|
|
|
#include "nsDataHashtable.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2010-08-12 12:37:52 -07:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
#include "xpcIJSGetFactory.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* 6bd13476-1dd2-11b2-bbef-f0ccb5fa64b6 (thanks, mozbot) */
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
#define MOZJSCOMPONENTLOADER_CID \
|
|
|
|
{0x6bd13476, 0x1dd2, 0x11b2, \
|
2007-03-22 10:30:00 -07:00
|
|
|
{ 0xbb, 0xef, 0xf0, 0xcc, 0xb5, 0xfa, 0x64, 0xb6 }}
|
|
|
|
#define MOZJSCOMPONENTLOADER_CONTRACTID "@mozilla.org/moz/jsloader;1"
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
class mozJSComponentLoader : public mozilla::ModuleLoader,
|
2007-05-15 16:27:40 -07:00
|
|
|
public xpcIJSModuleLoader,
|
2007-03-22 10:30:00 -07:00
|
|
|
public nsIObserver
|
|
|
|
{
|
2008-07-21 16:56:45 -07:00
|
|
|
friend class JSCLContextHelper;
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2007-05-15 16:27:40 -07:00
|
|
|
NS_DECL_XPCIJSMODULELOADER
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
mozJSComponentLoader();
|
|
|
|
virtual ~mozJSComponentLoader();
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
// ModuleLoader
|
2011-11-08 09:10:51 -08:00
|
|
|
const mozilla::Module* LoadModule(mozilla::FileLocation &aFile);
|
2010-06-10 11:11:11 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
|
|
|
static mozJSComponentLoader* sSelf;
|
|
|
|
|
|
|
|
nsresult ReallyInit();
|
|
|
|
void UnloadModules();
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsresult GlobalForLocation(nsIFile* aComponentFile,
|
2010-06-15 12:38:46 -07:00
|
|
|
nsIURI *aComponent,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject **aGlobal,
|
2008-08-04 10:03:34 -07:00
|
|
|
char **location,
|
|
|
|
jsval *exception);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-18 02:09:16 -08:00
|
|
|
nsresult ImportInto(const nsACString & aLocation,
|
|
|
|
JSObject * targetObj,
|
|
|
|
JSContext * callercx,
|
|
|
|
JSObject * *_retval);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIComponentManager> mCompMgr;
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> mRuntimeService;
|
2008-07-21 16:56:45 -07:00
|
|
|
nsCOMPtr<nsIThreadJSContextStack> mContextStack;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> mSystemPrincipal;
|
|
|
|
JSRuntime *mRuntime;
|
|
|
|
JSContext *mContext;
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
class ModuleEntry : public mozilla::Module
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-06-10 11:11:11 -07:00
|
|
|
ModuleEntry() : mozilla::Module() {
|
2010-06-11 13:13:26 -07:00
|
|
|
mVersion = mozilla::Module::kVersion;
|
|
|
|
mCIDs = NULL;
|
|
|
|
mContractIDs = NULL;
|
|
|
|
mCategoryEntries = NULL;
|
2010-06-21 09:46:26 -07:00
|
|
|
getFactoryProc = GetFactory;
|
|
|
|
loadProc = NULL;
|
|
|
|
unloadProc = NULL;
|
2010-06-11 13:13:26 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
global = nsnull;
|
|
|
|
location = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ModuleEntry() {
|
2010-06-11 13:13:26 -07:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
getfactoryobj = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (global) {
|
|
|
|
JSAutoRequest ar(sSelf->mContext);
|
2010-09-27 15:55:15 -07:00
|
|
|
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
ac.enterAndIgnoreErrors(sSelf->mContext, global);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ClearScope(sSelf->mContext, global);
|
2010-06-07 17:05:02 -07:00
|
|
|
JS_RemoveObjectRoot(sSelf->mContext, &global);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (location)
|
|
|
|
NS_Free(location);
|
2010-06-11 13:13:26 -07:00
|
|
|
|
|
|
|
global = NULL;
|
|
|
|
location = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
static already_AddRefed<nsIFactory> GetFactory(const mozilla::Module& module,
|
|
|
|
const mozilla::Module::CIDEntry& entry);
|
|
|
|
|
2010-06-11 13:13:26 -07:00
|
|
|
nsCOMPtr<xpcIJSGetFactory> getfactoryobj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *global;
|
|
|
|
char *location;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class ModuleEntry;
|
|
|
|
|
2010-06-11 13:13:26 -07:00
|
|
|
// Modules are intentionally leaked, but still cleared.
|
2011-11-08 09:08:49 -08:00
|
|
|
static PLDHashOperator ClearModules(const nsACString& key, ModuleEntry*& entry, void* cx);
|
|
|
|
nsDataHashtable<nsCStringHashKey, ModuleEntry*> mModules;
|
2010-06-11 13:13:26 -07:00
|
|
|
|
2011-11-08 09:08:49 -08:00
|
|
|
nsClassHashtable<nsCStringHashKey, ModuleEntry> mImports;
|
|
|
|
nsDataHashtable<nsCStringHashKey, ModuleEntry*> mInProgressImports;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|