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: 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 | #include "nsObserverList.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsAutoPtr.h" |
michael@0 | 9 | #include "nsCOMArray.h" |
michael@0 | 10 | #include "nsISimpleEnumerator.h" |
michael@0 | 11 | #include "xpcpublic.h" |
michael@0 | 12 | |
michael@0 | 13 | nsresult |
michael@0 | 14 | nsObserverList::AddObserver(nsIObserver* anObserver, bool ownsWeak) |
michael@0 | 15 | { |
michael@0 | 16 | NS_ASSERTION(anObserver, "Null input"); |
michael@0 | 17 | |
michael@0 | 18 | if (!ownsWeak) { |
michael@0 | 19 | ObserverRef* o = mObservers.AppendElement(anObserver); |
michael@0 | 20 | if (!o) |
michael@0 | 21 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 22 | |
michael@0 | 23 | return NS_OK; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(anObserver); |
michael@0 | 27 | if (!weak) |
michael@0 | 28 | return NS_NOINTERFACE; |
michael@0 | 29 | |
michael@0 | 30 | ObserverRef *o = mObservers.AppendElement(weak); |
michael@0 | 31 | if (!o) |
michael@0 | 32 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 33 | |
michael@0 | 34 | return NS_OK; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | nsresult |
michael@0 | 38 | nsObserverList::RemoveObserver(nsIObserver* anObserver) |
michael@0 | 39 | { |
michael@0 | 40 | NS_ASSERTION(anObserver, "Null input"); |
michael@0 | 41 | |
michael@0 | 42 | if (mObservers.RemoveElement(static_cast<nsISupports*>(anObserver))) |
michael@0 | 43 | return NS_OK; |
michael@0 | 44 | |
michael@0 | 45 | nsCOMPtr<nsIWeakReference> observerRef = do_GetWeakReference(anObserver); |
michael@0 | 46 | if (!observerRef) |
michael@0 | 47 | return NS_ERROR_FAILURE; |
michael@0 | 48 | |
michael@0 | 49 | if (!mObservers.RemoveElement(observerRef)) |
michael@0 | 50 | return NS_ERROR_FAILURE; |
michael@0 | 51 | |
michael@0 | 52 | return NS_OK; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | nsresult |
michael@0 | 56 | nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator) |
michael@0 | 57 | { |
michael@0 | 58 | nsRefPtr<nsObserverEnumerator> e(new nsObserverEnumerator(this)); |
michael@0 | 59 | e.forget(anEnumerator); |
michael@0 | 60 | return NS_OK; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | void |
michael@0 | 64 | nsObserverList::FillObserverArray(nsCOMArray<nsIObserver> &aArray) |
michael@0 | 65 | { |
michael@0 | 66 | aArray.SetCapacity(mObservers.Length()); |
michael@0 | 67 | |
michael@0 | 68 | nsTArray<ObserverRef> observers(mObservers); |
michael@0 | 69 | |
michael@0 | 70 | for (int32_t i = observers.Length() - 1; i >= 0; --i) { |
michael@0 | 71 | if (observers[i].isWeakRef) { |
michael@0 | 72 | nsCOMPtr<nsIObserver> o(do_QueryReferent(observers[i].asWeak())); |
michael@0 | 73 | if (o) { |
michael@0 | 74 | aArray.AppendObject(o); |
michael@0 | 75 | } |
michael@0 | 76 | else { |
michael@0 | 77 | // the object has gone away, remove the weakref |
michael@0 | 78 | mObservers.RemoveElement(observers[i].asWeak()); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | else { |
michael@0 | 82 | aArray.AppendObject(observers[i].asObserver()); |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | void |
michael@0 | 88 | nsObserverList::NotifyObservers(nsISupports *aSubject, |
michael@0 | 89 | const char *aTopic, |
michael@0 | 90 | const char16_t *someData) |
michael@0 | 91 | { |
michael@0 | 92 | nsCOMArray<nsIObserver> observers; |
michael@0 | 93 | FillObserverArray(observers); |
michael@0 | 94 | |
michael@0 | 95 | for (int32_t i = 0; i < observers.Count(); ++i) { |
michael@0 | 96 | observers[i]->Observe(aSubject, aTopic, someData); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | void |
michael@0 | 101 | nsObserverList::UnmarkGrayStrongObservers() |
michael@0 | 102 | { |
michael@0 | 103 | for (uint32_t i = 0; i < mObservers.Length(); ++i) { |
michael@0 | 104 | if (!mObservers[i].isWeakRef) { |
michael@0 | 105 | xpc_TryUnmarkWrappedGrayObject(mObservers[i].asObserver()); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | NS_IMPL_ISUPPORTS(nsObserverEnumerator, nsISimpleEnumerator) |
michael@0 | 111 | |
michael@0 | 112 | nsObserverEnumerator::nsObserverEnumerator(nsObserverList* aObserverList) |
michael@0 | 113 | : mIndex(0) |
michael@0 | 114 | { |
michael@0 | 115 | aObserverList->FillObserverArray(mObservers); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | NS_IMETHODIMP |
michael@0 | 119 | nsObserverEnumerator::HasMoreElements(bool *aResult) |
michael@0 | 120 | { |
michael@0 | 121 | *aResult = (mIndex < mObservers.Count()); |
michael@0 | 122 | return NS_OK; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | NS_IMETHODIMP |
michael@0 | 126 | nsObserverEnumerator::GetNext(nsISupports* *aResult) |
michael@0 | 127 | { |
michael@0 | 128 | if (mIndex == mObservers.Count()) { |
michael@0 | 129 | NS_ERROR("Enumerating after HasMoreElements returned false."); |
michael@0 | 130 | return NS_ERROR_UNEXPECTED; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | NS_ADDREF(*aResult = mObservers[mIndex]); |
michael@0 | 134 | ++mIndex; |
michael@0 | 135 | return NS_OK; |
michael@0 | 136 | } |