Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsComponentManager_h__ |
michael@0 | 7 | #define nsComponentManager_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsXPCOM.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "xpcom-private.h" |
michael@0 | 12 | #include "nsIComponentManager.h" |
michael@0 | 13 | #include "nsIComponentRegistrar.h" |
michael@0 | 14 | #include "nsIMemoryReporter.h" |
michael@0 | 15 | #include "nsIServiceManager.h" |
michael@0 | 16 | #include "nsIFile.h" |
michael@0 | 17 | #include "mozilla/MemoryReporting.h" |
michael@0 | 18 | #include "mozilla/Module.h" |
michael@0 | 19 | #include "mozilla/ModuleLoader.h" |
michael@0 | 20 | #include "mozilla/Mutex.h" |
michael@0 | 21 | #include "nsXULAppAPI.h" |
michael@0 | 22 | #include "nsNativeComponentLoader.h" |
michael@0 | 23 | #include "nsIFactory.h" |
michael@0 | 24 | #include "nsIInterfaceRequestor.h" |
michael@0 | 25 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 26 | #include "pldhash.h" |
michael@0 | 27 | #include "prtime.h" |
michael@0 | 28 | #include "nsCOMPtr.h" |
michael@0 | 29 | #include "nsAutoPtr.h" |
michael@0 | 30 | #include "nsWeakReference.h" |
michael@0 | 31 | #include "plarena.h" |
michael@0 | 32 | #include "nsCOMArray.h" |
michael@0 | 33 | #include "nsDataHashtable.h" |
michael@0 | 34 | #include "nsInterfaceHashtable.h" |
michael@0 | 35 | #include "nsClassHashtable.h" |
michael@0 | 36 | #include "nsTArray.h" |
michael@0 | 37 | |
michael@0 | 38 | #include "mozilla/Omnijar.h" |
michael@0 | 39 | #include "mozilla/Attributes.h" |
michael@0 | 40 | |
michael@0 | 41 | struct nsFactoryEntry; |
michael@0 | 42 | class nsIServiceManager; |
michael@0 | 43 | struct PRThread; |
michael@0 | 44 | |
michael@0 | 45 | #define NS_COMPONENTMANAGER_CID \ |
michael@0 | 46 | { /* 91775d60-d5dc-11d2-92fb-00e09805570f */ \ |
michael@0 | 47 | 0x91775d60, \ |
michael@0 | 48 | 0xd5dc, \ |
michael@0 | 49 | 0x11d2, \ |
michael@0 | 50 | {0x92, 0xfb, 0x00, 0xe0, 0x98, 0x05, 0x57, 0x0f} \ |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | /* keys for registry use */ |
michael@0 | 54 | extern const char xpcomKeyName[]; |
michael@0 | 55 | extern const char xpcomComponentsKeyName[]; |
michael@0 | 56 | extern const char lastModValueName[]; |
michael@0 | 57 | extern const char fileSizeValueName[]; |
michael@0 | 58 | extern const char nativeComponentType[]; |
michael@0 | 59 | extern const char staticComponentType[]; |
michael@0 | 60 | |
michael@0 | 61 | #ifdef DEBUG |
michael@0 | 62 | #define XPCOM_CHECK_PENDING_CIDS |
michael@0 | 63 | #endif |
michael@0 | 64 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 65 | |
michael@0 | 66 | extern const mozilla::Module kXPCOMModule; |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * This is a wrapper around mozilla::Mutex which provides runtime |
michael@0 | 70 | * checking for a deadlock where the same thread tries to lock a mutex while |
michael@0 | 71 | * it is already locked. This checking is present in both debug and release |
michael@0 | 72 | * builds. |
michael@0 | 73 | */ |
michael@0 | 74 | class SafeMutex |
michael@0 | 75 | { |
michael@0 | 76 | public: |
michael@0 | 77 | SafeMutex(const char* name) |
michael@0 | 78 | : mMutex(name) |
michael@0 | 79 | , mOwnerThread(nullptr) |
michael@0 | 80 | { } |
michael@0 | 81 | ~SafeMutex() |
michael@0 | 82 | { } |
michael@0 | 83 | |
michael@0 | 84 | void Lock() |
michael@0 | 85 | { |
michael@0 | 86 | AssertNotCurrentThreadOwns(); |
michael@0 | 87 | mMutex.Lock(); |
michael@0 | 88 | MOZ_ASSERT(mOwnerThread == nullptr); |
michael@0 | 89 | mOwnerThread = PR_GetCurrentThread(); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void Unlock() |
michael@0 | 93 | { |
michael@0 | 94 | MOZ_ASSERT(mOwnerThread == PR_GetCurrentThread()); |
michael@0 | 95 | mOwnerThread = nullptr; |
michael@0 | 96 | mMutex.Unlock(); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | void AssertCurrentThreadOwns() const |
michael@0 | 100 | { |
michael@0 | 101 | // This method is a debug-only check |
michael@0 | 102 | MOZ_ASSERT(mOwnerThread == PR_GetCurrentThread()); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | MOZ_NEVER_INLINE void AssertNotCurrentThreadOwns() const |
michael@0 | 106 | { |
michael@0 | 107 | // This method is a release-mode check |
michael@0 | 108 | if (PR_GetCurrentThread() == mOwnerThread) { |
michael@0 | 109 | MOZ_CRASH(); |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | private: |
michael@0 | 114 | mozilla::Mutex mMutex; |
michael@0 | 115 | volatile PRThread* mOwnerThread; |
michael@0 | 116 | }; |
michael@0 | 117 | |
michael@0 | 118 | typedef mozilla::BaseAutoLock<SafeMutex> SafeMutexAutoLock; |
michael@0 | 119 | typedef mozilla::BaseAutoUnlock<SafeMutex> SafeMutexAutoUnlock; |
michael@0 | 120 | |
michael@0 | 121 | class nsComponentManagerImpl MOZ_FINAL |
michael@0 | 122 | : public nsIComponentManager |
michael@0 | 123 | , public nsIServiceManager |
michael@0 | 124 | , public nsSupportsWeakReference |
michael@0 | 125 | , public nsIComponentRegistrar |
michael@0 | 126 | , public nsIInterfaceRequestor |
michael@0 | 127 | , public nsIMemoryReporter |
michael@0 | 128 | { |
michael@0 | 129 | public: |
michael@0 | 130 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 131 | NS_DECL_NSIINTERFACEREQUESTOR |
michael@0 | 132 | NS_DECL_NSICOMPONENTMANAGER |
michael@0 | 133 | NS_DECL_NSICOMPONENTREGISTRAR |
michael@0 | 134 | NS_DECL_NSIMEMORYREPORTER |
michael@0 | 135 | |
michael@0 | 136 | static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult); |
michael@0 | 137 | |
michael@0 | 138 | nsresult RegistryLocationForFile(nsIFile* aFile, |
michael@0 | 139 | nsCString& aResult); |
michael@0 | 140 | nsresult FileForRegistryLocation(const nsCString &aLocation, |
michael@0 | 141 | nsIFile **aSpec); |
michael@0 | 142 | |
michael@0 | 143 | NS_DECL_NSISERVICEMANAGER |
michael@0 | 144 | |
michael@0 | 145 | // nsComponentManagerImpl methods: |
michael@0 | 146 | nsComponentManagerImpl(); |
michael@0 | 147 | |
michael@0 | 148 | static nsComponentManagerImpl* gComponentManager; |
michael@0 | 149 | nsresult Init(); |
michael@0 | 150 | |
michael@0 | 151 | nsresult Shutdown(void); |
michael@0 | 152 | |
michael@0 | 153 | nsresult FreeServices(); |
michael@0 | 154 | |
michael@0 | 155 | already_AddRefed<mozilla::ModuleLoader> LoaderForExtension(const nsACString& aExt); |
michael@0 | 156 | nsInterfaceHashtable<nsCStringHashKey, mozilla::ModuleLoader> mLoaderMap; |
michael@0 | 157 | |
michael@0 | 158 | already_AddRefed<nsIFactory> FindFactory(const nsCID& aClass); |
michael@0 | 159 | already_AddRefed<nsIFactory> FindFactory(const char *contractID, |
michael@0 | 160 | uint32_t aContractIDLen); |
michael@0 | 161 | |
michael@0 | 162 | already_AddRefed<nsIFactory> LoadFactory(nsFactoryEntry *aEntry); |
michael@0 | 163 | |
michael@0 | 164 | nsFactoryEntry *GetFactoryEntry(const char *aContractID, |
michael@0 | 165 | uint32_t aContractIDLen); |
michael@0 | 166 | nsFactoryEntry *GetFactoryEntry(const nsCID &aClass); |
michael@0 | 167 | |
michael@0 | 168 | nsDataHashtable<nsIDHashKey, nsFactoryEntry*> mFactories; |
michael@0 | 169 | nsDataHashtable<nsCStringHashKey, nsFactoryEntry*> mContractIDs; |
michael@0 | 170 | |
michael@0 | 171 | SafeMutex mLock; |
michael@0 | 172 | |
michael@0 | 173 | static void InitializeStaticModules(); |
michael@0 | 174 | static void InitializeModuleLocations(); |
michael@0 | 175 | |
michael@0 | 176 | struct ComponentLocation |
michael@0 | 177 | { |
michael@0 | 178 | NSLocationType type; |
michael@0 | 179 | mozilla::FileLocation location; |
michael@0 | 180 | }; |
michael@0 | 181 | |
michael@0 | 182 | class ComponentLocationComparator |
michael@0 | 183 | { |
michael@0 | 184 | public: |
michael@0 | 185 | bool Equals(const ComponentLocation& a, const ComponentLocation& b) const |
michael@0 | 186 | { |
michael@0 | 187 | return (a.type == b.type && a.location.Equals(b.location)); |
michael@0 | 188 | } |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | static nsTArray<const mozilla::Module*>* sStaticModules; |
michael@0 | 192 | static nsTArray<ComponentLocation>* sModuleLocations; |
michael@0 | 193 | |
michael@0 | 194 | nsNativeModuleLoader mNativeModuleLoader; |
michael@0 | 195 | |
michael@0 | 196 | class KnownModule |
michael@0 | 197 | { |
michael@0 | 198 | public: |
michael@0 | 199 | /** |
michael@0 | 200 | * Static or binary module. |
michael@0 | 201 | */ |
michael@0 | 202 | KnownModule(const mozilla::Module* aModule, mozilla::FileLocation &aFile) |
michael@0 | 203 | : mModule(aModule) |
michael@0 | 204 | , mFile(aFile) |
michael@0 | 205 | , mLoaded(false) |
michael@0 | 206 | , mFailed(false) |
michael@0 | 207 | { } |
michael@0 | 208 | |
michael@0 | 209 | KnownModule(const mozilla::Module* aModule) |
michael@0 | 210 | : mModule(aModule) |
michael@0 | 211 | , mLoaded(false) |
michael@0 | 212 | , mFailed(false) |
michael@0 | 213 | { } |
michael@0 | 214 | |
michael@0 | 215 | KnownModule(mozilla::FileLocation &aFile) |
michael@0 | 216 | : mModule(nullptr) |
michael@0 | 217 | , mFile(aFile) |
michael@0 | 218 | , mLoader(nullptr) |
michael@0 | 219 | , mLoaded(false) |
michael@0 | 220 | , mFailed(false) |
michael@0 | 221 | { } |
michael@0 | 222 | |
michael@0 | 223 | ~KnownModule() |
michael@0 | 224 | { |
michael@0 | 225 | if (mLoaded && mModule->unloadProc) |
michael@0 | 226 | mModule->unloadProc(); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | bool EnsureLoader(); |
michael@0 | 230 | bool Load(); |
michael@0 | 231 | |
michael@0 | 232 | const mozilla::Module* Module() const |
michael@0 | 233 | { |
michael@0 | 234 | return mModule; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | /** |
michael@0 | 238 | * For error logging, get a description of this module, either the |
michael@0 | 239 | * file path, or <static module>. |
michael@0 | 240 | */ |
michael@0 | 241 | nsCString Description() const; |
michael@0 | 242 | |
michael@0 | 243 | private: |
michael@0 | 244 | const mozilla::Module* mModule; |
michael@0 | 245 | mozilla::FileLocation mFile; |
michael@0 | 246 | nsCOMPtr<mozilla::ModuleLoader> mLoader; |
michael@0 | 247 | bool mLoaded; |
michael@0 | 248 | bool mFailed; |
michael@0 | 249 | }; |
michael@0 | 250 | |
michael@0 | 251 | // The KnownModule is kept alive by these members, it is |
michael@0 | 252 | // referenced by pointer from the factory entries. |
michael@0 | 253 | nsTArray< nsAutoPtr<KnownModule> > mKnownStaticModules; |
michael@0 | 254 | // The key is the URI string of the module |
michael@0 | 255 | nsClassHashtable<nsCStringHashKey, KnownModule> mKnownModules; |
michael@0 | 256 | |
michael@0 | 257 | // Mutex not held |
michael@0 | 258 | void RegisterModule(const mozilla::Module* aModule, |
michael@0 | 259 | mozilla::FileLocation* aFile); |
michael@0 | 260 | |
michael@0 | 261 | |
michael@0 | 262 | // Mutex held |
michael@0 | 263 | void RegisterCIDEntryLocked(const mozilla::Module::CIDEntry* aEntry, |
michael@0 | 264 | KnownModule* aModule); |
michael@0 | 265 | void RegisterContractIDLocked(const mozilla::Module::ContractIDEntry* aEntry); |
michael@0 | 266 | |
michael@0 | 267 | // Mutex not held |
michael@0 | 268 | void RegisterManifest(NSLocationType aType, mozilla::FileLocation &aFile, |
michael@0 | 269 | bool aChromeOnly); |
michael@0 | 270 | |
michael@0 | 271 | struct ManifestProcessingContext |
michael@0 | 272 | { |
michael@0 | 273 | ManifestProcessingContext(NSLocationType aType, mozilla::FileLocation &aFile, bool aChromeOnly) |
michael@0 | 274 | : mType(aType) |
michael@0 | 275 | , mFile(aFile) |
michael@0 | 276 | , mChromeOnly(aChromeOnly) |
michael@0 | 277 | { } |
michael@0 | 278 | |
michael@0 | 279 | ~ManifestProcessingContext() { } |
michael@0 | 280 | |
michael@0 | 281 | NSLocationType mType; |
michael@0 | 282 | mozilla::FileLocation mFile; |
michael@0 | 283 | bool mChromeOnly; |
michael@0 | 284 | }; |
michael@0 | 285 | |
michael@0 | 286 | void ManifestManifest(ManifestProcessingContext& cx, int lineno, char *const * argv); |
michael@0 | 287 | void ManifestBinaryComponent(ManifestProcessingContext& cx, int lineno, char *const * argv); |
michael@0 | 288 | void ManifestXPT(ManifestProcessingContext& cx, int lineno, char *const * argv); |
michael@0 | 289 | void ManifestComponent(ManifestProcessingContext& cx, int lineno, char *const * argv); |
michael@0 | 290 | void ManifestContract(ManifestProcessingContext& cx, int lineno, char* const * argv); |
michael@0 | 291 | void ManifestCategory(ManifestProcessingContext& cx, int lineno, char* const * argv); |
michael@0 | 292 | |
michael@0 | 293 | void RereadChromeManifests(bool aChromeOnly = true); |
michael@0 | 294 | |
michael@0 | 295 | // Shutdown |
michael@0 | 296 | enum { |
michael@0 | 297 | NOT_INITIALIZED, |
michael@0 | 298 | NORMAL, |
michael@0 | 299 | SHUTDOWN_IN_PROGRESS, |
michael@0 | 300 | SHUTDOWN_COMPLETE |
michael@0 | 301 | } mStatus; |
michael@0 | 302 | |
michael@0 | 303 | PLArenaPool mArena; |
michael@0 | 304 | |
michael@0 | 305 | struct PendingServiceInfo { |
michael@0 | 306 | const nsCID* cid; |
michael@0 | 307 | PRThread* thread; |
michael@0 | 308 | }; |
michael@0 | 309 | |
michael@0 | 310 | inline PendingServiceInfo* AddPendingService(const nsCID& aServiceCID, |
michael@0 | 311 | PRThread* aThread); |
michael@0 | 312 | inline void RemovePendingService(const nsCID& aServiceCID); |
michael@0 | 313 | inline PRThread* GetPendingServiceThread(const nsCID& aServiceCID) const; |
michael@0 | 314 | |
michael@0 | 315 | nsTArray<PendingServiceInfo> mPendingServices; |
michael@0 | 316 | |
michael@0 | 317 | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
michael@0 | 318 | |
michael@0 | 319 | private: |
michael@0 | 320 | ~nsComponentManagerImpl(); |
michael@0 | 321 | }; |
michael@0 | 322 | |
michael@0 | 323 | |
michael@0 | 324 | #define NS_MAX_FILENAME_LEN 1024 |
michael@0 | 325 | |
michael@0 | 326 | #define NS_ERROR_IS_DIR NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM, 24) |
michael@0 | 327 | |
michael@0 | 328 | struct nsFactoryEntry |
michael@0 | 329 | { |
michael@0 | 330 | nsFactoryEntry(const mozilla::Module::CIDEntry* entry, |
michael@0 | 331 | nsComponentManagerImpl::KnownModule* module); |
michael@0 | 332 | |
michael@0 | 333 | // nsIComponentRegistrar.registerFactory support |
michael@0 | 334 | nsFactoryEntry(const nsCID& aClass, nsIFactory* factory); |
michael@0 | 335 | |
michael@0 | 336 | ~nsFactoryEntry(); |
michael@0 | 337 | |
michael@0 | 338 | already_AddRefed<nsIFactory> GetFactory(); |
michael@0 | 339 | |
michael@0 | 340 | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
michael@0 | 341 | |
michael@0 | 342 | const mozilla::Module::CIDEntry* mCIDEntry; |
michael@0 | 343 | nsComponentManagerImpl::KnownModule* mModule; |
michael@0 | 344 | |
michael@0 | 345 | nsCOMPtr<nsIFactory> mFactory; |
michael@0 | 346 | nsCOMPtr<nsISupports> mServiceObject; |
michael@0 | 347 | }; |
michael@0 | 348 | |
michael@0 | 349 | #endif // nsComponentManager_h__ |