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