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
|
|
|
*
|
|
|
|
* ***** 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) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
|
|
|
#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();
|
|
|
|
|
2010-06-15 12:38:46 -07:00
|
|
|
nsresult GlobalForLocation(nsILocalFile* aComponentFile,
|
|
|
|
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
|
|
|
};
|