xpcom/components/ModuleUtils.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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: 4; 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 mozilla_GenericModule_h
michael@0 7 #define mozilla_GenericModule_h
michael@0 8
michael@0 9 #include "mozilla/Attributes.h"
michael@0 10 #include "mozilla/Module.h"
michael@0 11
michael@0 12 #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass) \
michael@0 13 static nsresult \
michael@0 14 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
michael@0 15 void **aResult) \
michael@0 16 { \
michael@0 17 nsresult rv; \
michael@0 18 \
michael@0 19 _InstanceClass * inst; \
michael@0 20 \
michael@0 21 *aResult = nullptr; \
michael@0 22 if (nullptr != aOuter) { \
michael@0 23 rv = NS_ERROR_NO_AGGREGATION; \
michael@0 24 return rv; \
michael@0 25 } \
michael@0 26 \
michael@0 27 inst = new _InstanceClass(); \
michael@0 28 if (nullptr == inst) { \
michael@0 29 rv = NS_ERROR_OUT_OF_MEMORY; \
michael@0 30 return rv; \
michael@0 31 } \
michael@0 32 NS_ADDREF(inst); \
michael@0 33 rv = inst->QueryInterface(aIID, aResult); \
michael@0 34 NS_RELEASE(inst); \
michael@0 35 \
michael@0 36 return rv; \
michael@0 37 }
michael@0 38
michael@0 39 #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \
michael@0 40 static nsresult \
michael@0 41 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
michael@0 42 void **aResult) \
michael@0 43 { \
michael@0 44 nsresult rv; \
michael@0 45 \
michael@0 46 _InstanceClass * inst; \
michael@0 47 \
michael@0 48 *aResult = nullptr; \
michael@0 49 if (nullptr != aOuter) { \
michael@0 50 rv = NS_ERROR_NO_AGGREGATION; \
michael@0 51 return rv; \
michael@0 52 } \
michael@0 53 \
michael@0 54 inst = new _InstanceClass(); \
michael@0 55 if (nullptr == inst) { \
michael@0 56 rv = NS_ERROR_OUT_OF_MEMORY; \
michael@0 57 return rv; \
michael@0 58 } \
michael@0 59 NS_ADDREF(inst); \
michael@0 60 rv = inst->_InitMethod(); \
michael@0 61 if(NS_SUCCEEDED(rv)) { \
michael@0 62 rv = inst->QueryInterface(aIID, aResult); \
michael@0 63 } \
michael@0 64 NS_RELEASE(inst); \
michael@0 65 \
michael@0 66 return rv; \
michael@0 67 }
michael@0 68
michael@0 69 // 'Constructor' that uses an existing getter function that gets a singleton.
michael@0 70 // NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
michael@0 71 #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \
michael@0 72 static nsresult \
michael@0 73 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
michael@0 74 void **aResult) \
michael@0 75 { \
michael@0 76 nsresult rv; \
michael@0 77 \
michael@0 78 _InstanceClass * inst; \
michael@0 79 \
michael@0 80 *aResult = nullptr; \
michael@0 81 if (nullptr != aOuter) { \
michael@0 82 rv = NS_ERROR_NO_AGGREGATION; \
michael@0 83 return rv; \
michael@0 84 } \
michael@0 85 \
michael@0 86 inst = already_AddRefed<_InstanceClass>(_GetterProc()).take(); \
michael@0 87 if (nullptr == inst) { \
michael@0 88 rv = NS_ERROR_OUT_OF_MEMORY; \
michael@0 89 return rv; \
michael@0 90 } \
michael@0 91 /* NS_ADDREF(inst); */ \
michael@0 92 rv = inst->QueryInterface(aIID, aResult); \
michael@0 93 NS_RELEASE(inst); \
michael@0 94 \
michael@0 95 return rv; \
michael@0 96 }
michael@0 97
michael@0 98 #ifndef MOZILLA_INTERNAL_API
michael@0 99
michael@0 100 #include "nsIModule.h"
michael@0 101 #include "nsISupportsUtils.h"
michael@0 102
michael@0 103 namespace mozilla {
michael@0 104
michael@0 105 class GenericModule MOZ_FINAL : public nsIModule
michael@0 106 {
michael@0 107 public:
michael@0 108 GenericModule(const mozilla::Module* aData)
michael@0 109 : mData(aData)
michael@0 110 {
michael@0 111 }
michael@0 112
michael@0 113 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 114 NS_DECL_NSIMODULE
michael@0 115
michael@0 116 private:
michael@0 117 const mozilla::Module* mData;
michael@0 118 };
michael@0 119
michael@0 120 } // namespace mozilla
michael@0 121
michael@0 122 #define NS_IMPL_MOZILLA192_NSGETMODULE(module) \
michael@0 123 extern "C" NS_EXPORT nsresult \
michael@0 124 NSGetModule(nsIComponentManager* aCompMgr, \
michael@0 125 nsIFile* aLocation, \
michael@0 126 nsIModule** aResult) \
michael@0 127 { \
michael@0 128 *aResult = new mozilla::GenericModule(module); \
michael@0 129 NS_ADDREF(*aResult); \
michael@0 130 return NS_OK; \
michael@0 131 }
michael@0 132
michael@0 133 #endif
michael@0 134
michael@0 135 #endif // mozilla_GenericModule_h

mercurial