|
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/. */ |
|
6 |
|
7 #ifndef mozJSComponentLoader_h |
|
8 #define mozJSComponentLoader_h |
|
9 |
|
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" |
|
20 |
|
21 #include "xpcIJSGetFactory.h" |
|
22 |
|
23 class nsIFile; |
|
24 class nsIJSRuntimeService; |
|
25 class nsIPrincipal; |
|
26 class nsIXPConnectJSObjectHolder; |
|
27 |
|
28 /* 6bd13476-1dd2-11b2-bbef-f0ccb5fa64b6 (thanks, mozbot) */ |
|
29 |
|
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" |
|
34 |
|
35 class JSCLContextHelper; |
|
36 |
|
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 |
|
46 |
|
47 mozJSComponentLoader(); |
|
48 virtual ~mozJSComponentLoader(); |
|
49 |
|
50 // ModuleLoader |
|
51 const mozilla::Module* LoadModule(mozilla::FileLocation &aFile); |
|
52 |
|
53 nsresult FindTargetObject(JSContext* aCx, |
|
54 JS::MutableHandleObject aTargetObject); |
|
55 |
|
56 static mozJSComponentLoader* Get() { return sSelf; } |
|
57 |
|
58 void NoteSubScript(JS::HandleScript aScript, JS::HandleObject aThisObject); |
|
59 |
|
60 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
|
61 |
|
62 protected: |
|
63 static mozJSComponentLoader* sSelf; |
|
64 |
|
65 nsresult ReallyInit(); |
|
66 void UnloadModules(); |
|
67 |
|
68 JSObject* PrepareObjectForLocation(JSCLContextHelper& aCx, |
|
69 nsIFile* aComponentFile, |
|
70 nsIURI *aComponent, |
|
71 bool aReuseLoaderGlobal, |
|
72 bool *aRealFile); |
|
73 |
|
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); |
|
81 |
|
82 nsresult ImportInto(const nsACString &aLocation, |
|
83 JS::HandleObject targetObj, |
|
84 JSContext *callercx, |
|
85 JS::MutableHandleObject vp); |
|
86 |
|
87 nsCOMPtr<nsIComponentManager> mCompMgr; |
|
88 nsCOMPtr<nsIJSRuntimeService> mRuntimeService; |
|
89 nsCOMPtr<nsIPrincipal> mSystemPrincipal; |
|
90 nsCOMPtr<nsIXPConnectJSObjectHolder> mLoaderGlobal; |
|
91 JSRuntime *mRuntime; |
|
92 JSContext *mContext; |
|
93 |
|
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; |
|
107 |
|
108 location = nullptr; |
|
109 } |
|
110 |
|
111 ~ModuleEntry() { |
|
112 Clear(); |
|
113 } |
|
114 |
|
115 void Clear() { |
|
116 getfactoryobj = nullptr; |
|
117 |
|
118 if (obj) { |
|
119 mozilla::AutoJSContext cx; |
|
120 JSAutoCompartment ac(cx, obj); |
|
121 |
|
122 JS_SetAllNonReservedSlotsToUndefined(cx, obj); |
|
123 obj = nullptr; |
|
124 thisObjectKey = nullptr; |
|
125 } |
|
126 |
|
127 if (location) |
|
128 NS_Free(location); |
|
129 |
|
130 obj = nullptr; |
|
131 thisObjectKey = nullptr; |
|
132 location = nullptr; |
|
133 } |
|
134 |
|
135 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
|
136 |
|
137 static already_AddRefed<nsIFactory> GetFactory(const mozilla::Module& module, |
|
138 const mozilla::Module::CIDEntry& entry); |
|
139 |
|
140 nsCOMPtr<xpcIJSGetFactory> getfactoryobj; |
|
141 JS::PersistentRootedObject obj; |
|
142 JS::PersistentRootedScript thisObjectKey; |
|
143 char *location; |
|
144 }; |
|
145 |
|
146 friend class ModuleEntry; |
|
147 |
|
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); |
|
153 |
|
154 // Modules are intentionally leaked, but still cleared. |
|
155 static PLDHashOperator ClearModules(const nsACString& key, ModuleEntry*& entry, void* cx); |
|
156 nsDataHashtable<nsCStringHashKey, ModuleEntry*> mModules; |
|
157 |
|
158 nsClassHashtable<nsCStringHashKey, ModuleEntry> mImports; |
|
159 nsDataHashtable<nsCStringHashKey, ModuleEntry*> mInProgressImports; |
|
160 nsDataHashtable<nsPtrHashKey<JSScript>, JSObject*> mThisObjects; |
|
161 |
|
162 bool mInitialized; |
|
163 bool mReuseLoaderGlobal; |
|
164 }; |
|
165 |
|
166 #endif |