1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/nsMaybeWeakPtr.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsMaybeWeakPtr.h" 1.10 + 1.11 +void* 1.12 +nsMaybeWeakPtr_base::GetValueAs(const nsIID &iid) const 1.13 +{ 1.14 + nsresult rv; 1.15 + void *ref; 1.16 + if (mPtr) { 1.17 + rv = mPtr->QueryInterface(iid, &ref); 1.18 + if (NS_SUCCEEDED(rv)) { 1.19 + return ref; 1.20 + } 1.21 + } 1.22 + 1.23 + nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(mPtr); 1.24 + if (weakRef) { 1.25 + rv = weakRef->QueryReferent(iid, &ref); 1.26 + if (NS_SUCCEEDED(rv)) { 1.27 + return ref; 1.28 + } 1.29 + } 1.30 + 1.31 + return nullptr; 1.32 +} 1.33 + 1.34 +nsresult 1.35 +NS_AppendWeakElementBase(isupports_array_type *aArray, 1.36 + nsISupports *aElement, 1.37 + bool aOwnsWeak) 1.38 +{ 1.39 + nsCOMPtr<nsISupports> ref; 1.40 + if (aOwnsWeak) { 1.41 + nsCOMPtr<nsIWeakReference> weakRef; 1.42 + weakRef = do_GetWeakReference(aElement); 1.43 + reinterpret_cast<nsCOMPtr<nsISupports>*>(&weakRef)->swap(ref); 1.44 + } else { 1.45 + ref = aElement; 1.46 + } 1.47 + 1.48 + if (aArray->IndexOf(ref) != aArray->NoIndex) { 1.49 + return NS_ERROR_INVALID_ARG; // already present 1.50 + } 1.51 + if (!aArray->AppendElement(ref)) { 1.52 + return NS_ERROR_OUT_OF_MEMORY; 1.53 + } 1.54 + return NS_OK; 1.55 +} 1.56 + 1.57 +nsresult 1.58 +NS_RemoveWeakElementBase(isupports_array_type *aArray, 1.59 + nsISupports *aElement) 1.60 +{ 1.61 + uint32_t index = aArray->IndexOf(aElement); 1.62 + if (index != aArray->NoIndex) { 1.63 + aArray->RemoveElementAt(index); 1.64 + return NS_OK; 1.65 + } 1.66 + 1.67 + // Don't use do_GetWeakReference; it should only be called if we know 1.68 + // the object supports weak references. 1.69 + nsCOMPtr<nsISupportsWeakReference> supWeakRef = do_QueryInterface(aElement); 1.70 + NS_ENSURE_TRUE(supWeakRef, NS_ERROR_INVALID_ARG); 1.71 + 1.72 + nsCOMPtr<nsIWeakReference> weakRef; 1.73 + nsresult rv = supWeakRef->GetWeakReference(getter_AddRefs(weakRef)); 1.74 + NS_ENSURE_SUCCESS(rv, rv); 1.75 + 1.76 + index = aArray->IndexOf(weakRef); 1.77 + if (index == aArray->NoIndex) { 1.78 + return NS_ERROR_INVALID_ARG; 1.79 + } 1.80 + 1.81 + aArray->RemoveElementAt(index); 1.82 + return NS_OK; 1.83 +}