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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * |
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 | // nsWeakReference.cpp |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/Attributes.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsWeakReference.h" |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | |
michael@0 | 14 | class nsWeakReference MOZ_FINAL : public nsIWeakReference |
michael@0 | 15 | { |
michael@0 | 16 | public: |
michael@0 | 17 | // nsISupports... |
michael@0 | 18 | NS_DECL_ISUPPORTS |
michael@0 | 19 | |
michael@0 | 20 | // nsIWeakReference... |
michael@0 | 21 | NS_DECL_NSIWEAKREFERENCE |
michael@0 | 22 | |
michael@0 | 23 | private: |
michael@0 | 24 | friend class nsSupportsWeakReference; |
michael@0 | 25 | |
michael@0 | 26 | nsWeakReference( nsSupportsWeakReference* referent ) |
michael@0 | 27 | : mReferent(referent) |
michael@0 | 28 | // ...I can only be constructed by an |nsSupportsWeakReference| |
michael@0 | 29 | { |
michael@0 | 30 | // nothing else to do here |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | ~nsWeakReference() |
michael@0 | 34 | // ...I will only be destroyed by calling |delete| myself. |
michael@0 | 35 | { |
michael@0 | 36 | if ( mReferent ) |
michael@0 | 37 | mReferent->NoticeProxyDestruction(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | void |
michael@0 | 41 | NoticeReferentDestruction() |
michael@0 | 42 | // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor. |
michael@0 | 43 | { |
michael@0 | 44 | mReferent = 0; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | nsSupportsWeakReference* mReferent; |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | nsresult |
michael@0 | 51 | nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const |
michael@0 | 52 | { |
michael@0 | 53 | nsresult status; |
michael@0 | 54 | if ( mWeakPtr ) |
michael@0 | 55 | { |
michael@0 | 56 | if ( NS_FAILED(status = mWeakPtr->QueryReferent(aIID, answer)) ) |
michael@0 | 57 | *answer = 0; |
michael@0 | 58 | } |
michael@0 | 59 | else |
michael@0 | 60 | status = NS_ERROR_NULL_POINTER; |
michael@0 | 61 | |
michael@0 | 62 | if ( mErrorPtr ) |
michael@0 | 63 | *mErrorPtr = status; |
michael@0 | 64 | return status; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | NS_COM_GLUE nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>| |
michael@0 | 68 | NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr ) |
michael@0 | 69 | { |
michael@0 | 70 | nsresult status; |
michael@0 | 71 | |
michael@0 | 72 | nsIWeakReference* result = nullptr; |
michael@0 | 73 | |
michael@0 | 74 | if ( aInstancePtr ) |
michael@0 | 75 | { |
michael@0 | 76 | nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status); |
michael@0 | 77 | if ( factoryPtr ) |
michael@0 | 78 | { |
michael@0 | 79 | status = factoryPtr->GetWeakReference(&result); |
michael@0 | 80 | } |
michael@0 | 81 | // else, |status| has already been set by |do_QueryInterface| |
michael@0 | 82 | } |
michael@0 | 83 | else |
michael@0 | 84 | status = NS_ERROR_NULL_POINTER; |
michael@0 | 85 | |
michael@0 | 86 | if ( aErrorPtr ) |
michael@0 | 87 | *aErrorPtr = status; |
michael@0 | 88 | return result; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | NS_COM_GLUE nsresult |
michael@0 | 92 | nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr ) |
michael@0 | 93 | { |
michael@0 | 94 | if ( !aInstancePtr ) |
michael@0 | 95 | return NS_ERROR_NULL_POINTER; |
michael@0 | 96 | |
michael@0 | 97 | if ( !mProxy ) |
michael@0 | 98 | mProxy = new nsWeakReference(this); |
michael@0 | 99 | *aInstancePtr = mProxy; |
michael@0 | 100 | |
michael@0 | 101 | nsresult status; |
michael@0 | 102 | if ( !*aInstancePtr ) |
michael@0 | 103 | status = NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 104 | else |
michael@0 | 105 | { |
michael@0 | 106 | NS_ADDREF(*aInstancePtr); |
michael@0 | 107 | status = NS_OK; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | return status; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | NS_IMPL_ISUPPORTS(nsWeakReference, nsIWeakReference) |
michael@0 | 114 | |
michael@0 | 115 | NS_IMETHODIMP |
michael@0 | 116 | nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr ) |
michael@0 | 117 | { |
michael@0 | 118 | return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | void |
michael@0 | 122 | nsSupportsWeakReference::ClearWeakReferences() |
michael@0 | 123 | { |
michael@0 | 124 | if ( mProxy ) |
michael@0 | 125 | { |
michael@0 | 126 | mProxy->NoticeReferentDestruction(); |
michael@0 | 127 | mProxy = 0; |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 |