1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Places code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Google Inc.
19 : * Portions created by the Initial Developer are Copyright (C) 2005
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Brian Ryner <bryner@brianryner.com> (original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsMaybeWeakPtr.h"
40 :
41 : void*
42 14583 : nsMaybeWeakPtr_base::GetValueAs(const nsIID &iid) const
43 : {
44 : nsresult rv;
45 : void *ref;
46 14583 : if (mPtr) {
47 14583 : rv = mPtr->QueryInterface(iid, &ref);
48 14583 : if (NS_SUCCEEDED(rv)) {
49 3630 : return ref;
50 : }
51 : }
52 :
53 21906 : nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(mPtr);
54 10953 : if (weakRef) {
55 10953 : rv = weakRef->QueryReferent(iid, &ref);
56 10953 : if (NS_SUCCEEDED(rv)) {
57 10953 : return ref;
58 : }
59 : }
60 :
61 0 : return nsnull;
62 : }
63 :
64 : nsresult
65 1480 : NS_AppendWeakElementBase(isupports_array_type *aArray,
66 : nsISupports *aElement,
67 : bool aOwnsWeak)
68 : {
69 2960 : nsCOMPtr<nsISupports> ref;
70 1480 : if (aOwnsWeak) {
71 2694 : nsCOMPtr<nsIWeakReference> weakRef;
72 1347 : weakRef = do_GetWeakReference(aElement);
73 1347 : reinterpret_cast<nsCOMPtr<nsISupports>*>(&weakRef)->swap(ref);
74 : } else {
75 133 : ref = aElement;
76 : }
77 :
78 1480 : if (aArray->IndexOf(ref) != aArray->NoIndex) {
79 0 : return NS_ERROR_INVALID_ARG; // already present
80 : }
81 1480 : if (!aArray->AppendElement(ref)) {
82 0 : return NS_ERROR_OUT_OF_MEMORY;
83 : }
84 1480 : return NS_OK;
85 : }
86 :
87 : nsresult
88 1211 : NS_RemoveWeakElementBase(isupports_array_type *aArray,
89 : nsISupports *aElement)
90 : {
91 1211 : PRUint32 index = aArray->IndexOf(aElement);
92 1211 : if (index != aArray->NoIndex) {
93 120 : aArray->RemoveElementAt(index);
94 120 : return NS_OK;
95 : }
96 :
97 : // Don't use do_GetWeakReference; it should only be called if we know
98 : // the object supports weak references.
99 2182 : nsCOMPtr<nsISupportsWeakReference> supWeakRef = do_QueryInterface(aElement);
100 1091 : NS_ENSURE_TRUE(supWeakRef, NS_ERROR_INVALID_ARG);
101 :
102 2178 : nsCOMPtr<nsIWeakReference> weakRef;
103 1089 : nsresult rv = supWeakRef->GetWeakReference(getter_AddRefs(weakRef));
104 1089 : NS_ENSURE_SUCCESS(rv, rv);
105 :
106 1089 : index = aArray->IndexOf(weakRef);
107 1089 : if (index == aArray->NoIndex) {
108 0 : return NS_ERROR_INVALID_ARG;
109 : }
110 :
111 1089 : aArray->RemoveElementAt(index);
112 1089 : return NS_OK;
113 : }
|