Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=8 sts=4 et sw=4 tw=99: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozJSComponentLoader_h
8 #define mozJSComponentLoader_h
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/ModuleLoader.h"
12 #include "nsISupports.h"
13 #include "nsIObserver.h"
14 #include "nsIURI.h"
15 #include "xpcIJSModuleLoader.h"
16 #include "nsClassHashtable.h"
17 #include "nsCxPusher.h"
18 #include "nsDataHashtable.h"
19 #include "jsapi.h"
21 #include "xpcIJSGetFactory.h"
23 class nsIFile;
24 class nsIJSRuntimeService;
25 class nsIPrincipal;
26 class nsIXPConnectJSObjectHolder;
28 /* 6bd13476-1dd2-11b2-bbef-f0ccb5fa64b6 (thanks, mozbot) */
30 #define MOZJSCOMPONENTLOADER_CID \
31 {0x6bd13476, 0x1dd2, 0x11b2, \
32 { 0xbb, 0xef, 0xf0, 0xcc, 0xb5, 0xfa, 0x64, 0xb6 }}
33 #define MOZJSCOMPONENTLOADER_CONTRACTID "@mozilla.org/moz/jsloader;1"
35 class JSCLContextHelper;
37 class mozJSComponentLoader : public mozilla::ModuleLoader,
38 public xpcIJSModuleLoader,
39 public nsIObserver
40 {
41 friend class JSCLContextHelper;
42 public:
43 NS_DECL_ISUPPORTS
44 NS_DECL_XPCIJSMODULELOADER
45 NS_DECL_NSIOBSERVER
47 mozJSComponentLoader();
48 virtual ~mozJSComponentLoader();
50 // ModuleLoader
51 const mozilla::Module* LoadModule(mozilla::FileLocation &aFile);
53 nsresult FindTargetObject(JSContext* aCx,
54 JS::MutableHandleObject aTargetObject);
56 static mozJSComponentLoader* Get() { return sSelf; }
58 void NoteSubScript(JS::HandleScript aScript, JS::HandleObject aThisObject);
60 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
62 protected:
63 static mozJSComponentLoader* sSelf;
65 nsresult ReallyInit();
66 void UnloadModules();
68 JSObject* PrepareObjectForLocation(JSCLContextHelper& aCx,
69 nsIFile* aComponentFile,
70 nsIURI *aComponent,
71 bool aReuseLoaderGlobal,
72 bool *aRealFile);
74 nsresult ObjectForLocation(nsIFile* aComponentFile,
75 nsIURI *aComponent,
76 JS::MutableHandleObject aObject,
77 JS::MutableHandleScript aTableScript,
78 char **location,
79 bool aCatchException,
80 JS::MutableHandleValue aException);
82 nsresult ImportInto(const nsACString &aLocation,
83 JS::HandleObject targetObj,
84 JSContext *callercx,
85 JS::MutableHandleObject vp);
87 nsCOMPtr<nsIComponentManager> mCompMgr;
88 nsCOMPtr<nsIJSRuntimeService> mRuntimeService;
89 nsCOMPtr<nsIPrincipal> mSystemPrincipal;
90 nsCOMPtr<nsIXPConnectJSObjectHolder> mLoaderGlobal;
91 JSRuntime *mRuntime;
92 JSContext *mContext;
94 class ModuleEntry : public mozilla::Module
95 {
96 public:
97 ModuleEntry(JSContext* aCx)
98 : mozilla::Module(), obj(aCx, nullptr), thisObjectKey(aCx, nullptr)
99 {
100 mVersion = mozilla::Module::kVersion;
101 mCIDs = nullptr;
102 mContractIDs = nullptr;
103 mCategoryEntries = nullptr;
104 getFactoryProc = GetFactory;
105 loadProc = nullptr;
106 unloadProc = nullptr;
108 location = nullptr;
109 }
111 ~ModuleEntry() {
112 Clear();
113 }
115 void Clear() {
116 getfactoryobj = nullptr;
118 if (obj) {
119 mozilla::AutoJSContext cx;
120 JSAutoCompartment ac(cx, obj);
122 JS_SetAllNonReservedSlotsToUndefined(cx, obj);
123 obj = nullptr;
124 thisObjectKey = nullptr;
125 }
127 if (location)
128 NS_Free(location);
130 obj = nullptr;
131 thisObjectKey = nullptr;
132 location = nullptr;
133 }
135 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
137 static already_AddRefed<nsIFactory> GetFactory(const mozilla::Module& module,
138 const mozilla::Module::CIDEntry& entry);
140 nsCOMPtr<xpcIJSGetFactory> getfactoryobj;
141 JS::PersistentRootedObject obj;
142 JS::PersistentRootedScript thisObjectKey;
143 char *location;
144 };
146 friend class ModuleEntry;
148 static size_t DataEntrySizeOfExcludingThis(const nsACString& aKey, ModuleEntry* const& aData,
149 mozilla::MallocSizeOf aMallocSizeOf, void* arg);
150 static size_t ClassEntrySizeOfExcludingThis(const nsACString& aKey,
151 const nsAutoPtr<ModuleEntry>& aData,
152 mozilla::MallocSizeOf aMallocSizeOf, void* arg);
154 // Modules are intentionally leaked, but still cleared.
155 static PLDHashOperator ClearModules(const nsACString& key, ModuleEntry*& entry, void* cx);
156 nsDataHashtable<nsCStringHashKey, ModuleEntry*> mModules;
158 nsClassHashtable<nsCStringHashKey, ModuleEntry> mImports;
159 nsDataHashtable<nsCStringHashKey, ModuleEntry*> mInProgressImports;
160 nsDataHashtable<nsPtrHashKey<JSScript>, JSObject*> mThisObjects;
162 bool mInitialized;
163 bool mReuseLoaderGlobal;
164 };
166 #endif