1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla Communicator client code, released
17 : * March 31, 1998.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 1999
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributors:
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "plhash.h"
41 : #include "jsapi.h"
42 : #include "mozilla/ModuleLoader.h"
43 : #include "nsIJSRuntimeService.h"
44 : #include "nsIJSContextStack.h"
45 : #include "nsISupports.h"
46 : #include "nsIXPConnect.h"
47 : #include "nsIFile.h"
48 : #include "nsAutoPtr.h"
49 : #include "nsIObjectInputStream.h"
50 : #include "nsIObjectOutputStream.h"
51 : #include "nsITimer.h"
52 : #include "nsIObserver.h"
53 : #include "xpcIJSModuleLoader.h"
54 : #include "nsClassHashtable.h"
55 : #include "nsDataHashtable.h"
56 : #include "nsIPrincipal.h"
57 : #include "mozilla/scache/StartupCache.h"
58 :
59 : #include "xpcIJSGetFactory.h"
60 :
61 : /* 6bd13476-1dd2-11b2-bbef-f0ccb5fa64b6 (thanks, mozbot) */
62 :
63 : #define MOZJSCOMPONENTLOADER_CID \
64 : {0x6bd13476, 0x1dd2, 0x11b2, \
65 : { 0xbb, 0xef, 0xf0, 0xcc, 0xb5, 0xfa, 0x64, 0xb6 }}
66 : #define MOZJSCOMPONENTLOADER_CONTRACTID "@mozilla.org/moz/jsloader;1"
67 :
68 : class mozJSComponentLoader : public mozilla::ModuleLoader,
69 : public xpcIJSModuleLoader,
70 : public nsIObserver
71 : {
72 : friend class JSCLContextHelper;
73 : public:
74 : NS_DECL_ISUPPORTS
75 : NS_DECL_XPCIJSMODULELOADER
76 : NS_DECL_NSIOBSERVER
77 :
78 : mozJSComponentLoader();
79 : virtual ~mozJSComponentLoader();
80 :
81 : // ModuleLoader
82 : const mozilla::Module* LoadModule(mozilla::FileLocation &aFile);
83 :
84 : protected:
85 : static mozJSComponentLoader* sSelf;
86 :
87 : nsresult ReallyInit();
88 : void UnloadModules();
89 :
90 : nsresult GlobalForLocation(nsILocalFile* aComponentFile,
91 : nsIURI *aComponent,
92 : JSObject **aGlobal,
93 : char **location,
94 : jsval *exception);
95 :
96 : nsresult ImportInto(const nsACString & aLocation,
97 : JSObject * targetObj,
98 : JSContext * callercx,
99 : JSObject * *_retval);
100 :
101 : nsCOMPtr<nsIComponentManager> mCompMgr;
102 : nsCOMPtr<nsIJSRuntimeService> mRuntimeService;
103 : nsCOMPtr<nsIThreadJSContextStack> mContextStack;
104 : nsCOMPtr<nsIPrincipal> mSystemPrincipal;
105 : JSRuntime *mRuntime;
106 : JSContext *mContext;
107 :
108 : class ModuleEntry : public mozilla::Module
109 : {
110 : public:
111 11830 : ModuleEntry() : mozilla::Module() {
112 11830 : mVersion = mozilla::Module::kVersion;
113 11830 : mCIDs = NULL;
114 11830 : mContractIDs = NULL;
115 11830 : mCategoryEntries = NULL;
116 11830 : getFactoryProc = GetFactory;
117 11830 : loadProc = NULL;
118 11830 : unloadProc = NULL;
119 :
120 11830 : global = nsnull;
121 11830 : location = nsnull;
122 11830 : }
123 :
124 19534 : ~ModuleEntry() {
125 9767 : Clear();
126 9767 : }
127 :
128 11830 : void Clear() {
129 11830 : getfactoryobj = NULL;
130 :
131 11830 : if (global) {
132 23650 : JSAutoRequest ar(sSelf->mContext);
133 :
134 23650 : JSAutoEnterCompartment ac;
135 11825 : ac.enterAndIgnoreErrors(sSelf->mContext, global);
136 :
137 11825 : JS_ClearScope(sSelf->mContext, global);
138 11825 : JS_RemoveObjectRoot(sSelf->mContext, &global);
139 : }
140 :
141 11830 : if (location)
142 11825 : NS_Free(location);
143 :
144 11830 : global = NULL;
145 11830 : location = NULL;
146 11830 : }
147 :
148 : static already_AddRefed<nsIFactory> GetFactory(const mozilla::Module& module,
149 : const mozilla::Module::CIDEntry& entry);
150 :
151 : nsCOMPtr<xpcIJSGetFactory> getfactoryobj;
152 : JSObject *global;
153 : char *location;
154 : };
155 :
156 : friend class ModuleEntry;
157 :
158 : // Modules are intentionally leaked, but still cleared.
159 : static PLDHashOperator ClearModules(const nsACString& key, ModuleEntry*& entry, void* cx);
160 : nsDataHashtable<nsCStringHashKey, ModuleEntry*> mModules;
161 :
162 : nsClassHashtable<nsCStringHashKey, ModuleEntry> mImports;
163 : nsDataHashtable<nsCStringHashKey, ModuleEntry*> mInProgressImports;
164 :
165 : bool mInitialized;
166 : };
|