sbJobUtils.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2009 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #ifndef _SB_JOB_UTILS_H_
28 #define _SB_JOB_UTILS_H_
29 
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 //
33 // Songbird job utility defs.
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
43 //------------------------------------------------------------------------------
44 //
45 // Songbird job imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 // Mozilla interface imports.
50 #include <nsISimpleEnumerator.h>
51 #include <nsIStringEnumerator.h>
52 #include <nsISupportsPrimitives.h>
53 
54 // Songbird imports.
55 #include <sbIJobCancelable.h>
56 #include <sbMemoryUtils.h>
57 
58 
59 //------------------------------------------------------------------------------
60 //
61 // Songbird job services.
62 //
63 //------------------------------------------------------------------------------
64 
65 //
66 // sbAutoJobCancel Wrapper to auto-cancel a job.
67 //
68 
69 SB_AUTO_NULL_CLASS(sbAutoJobCancel, sbIJobCancelable*, mValue->Cancel());
70 
71 //
72 // sbJobErrorEnumerator Wrapper class for job errors
73 //
74 template <class Interface>
75 class sbJobErrorEnumerator : public nsIStringEnumerator,
76  public nsISimpleEnumerator
77 {
78 public:
79  typedef nsTArray<nsCOMPtr<Interface> > ArrayType;
80  // default constructor
81  explicit sbJobErrorEnumerator() : mIndex(0){}
82  // construct an enumerator from a copy of an array
83  sbJobErrorEnumerator(ArrayType& aArray)
84  : mArray(aArray), mIndex(0) {}
85 
92  PRBool Adopt(ArrayType& aArray)
93  {
94  mIndex = 0;
95  mArray.Clear();
96  return mArray.SwapElements(aArray);
97  }
98 
100  NS_IMETHODIMP HasMore(PRBool *_retval)
101  {
102  NS_ENSURE_ARG_POINTER(_retval);
103  *_retval = mIndex < mArray.Length();
104  return NS_OK;
105  }
106 
107  NS_IMETHODIMP GetNext(nsAString & _retval)
108  {
109  nsresult rv;
110 
111  if (!(mIndex < mArray.Length())) {
112  return NS_ERROR_FAILURE;
113  }
114  nsCOMPtr<Interface> next(mArray[mIndex]);
115  NS_ENSURE_TRUE(next, NS_ERROR_FAILURE);
116 
117  nsCOMPtr<nsISupportsString> string = do_QueryInterface(next, &rv);
118  NS_ENSURE_SUCCESS(rv, rv);
119 
120  rv = string->GetData(_retval);
121  NS_ENSURE_SUCCESS(rv, rv);
122 
123  ++mIndex;
124  return NS_OK;
125  }
126 
128  NS_IMETHODIMP HasMoreElements(PRBool *_retval)
129  {
130  return HasMore(_retval);
131  }
132  NS_IMETHODIMP GetNext(nsISupports** _retval)
133  {
134  nsresult rv;
135 
136  if (!(mIndex < mArray.Length())) {
137  return NS_ERROR_FAILURE;
138  }
139  nsCOMPtr<Interface> next(mArray[mIndex]);
140  NS_ENSURE_TRUE(next, NS_ERROR_FAILURE);
141 
142  rv = CallQueryInterface(next, _retval);
143  NS_ENSURE_SUCCESS(rv, rv);
144 
145  ++mIndex;
146  return NS_OK;
147  }
148 
150  NS_IMETHODIMP_(nsrefcnt) AddRef(void)
151  {
152  NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
153  nsrefcnt count;
154  count = PR_AtomicIncrement((PRInt32*)&mRefCnt);
155  NS_LOG_ADDREF(this, count, "sbJobErrorEnumerator<>", sizeof(*this));
156  return count;
157  }
158 
159  NS_IMETHODIMP_(nsrefcnt) Release(void)
160  {
161  nsrefcnt count;
162  NS_PRECONDITION(0 != mRefCnt, "dup release");
163  count = PR_AtomicDecrement((PRInt32 *)&mRefCnt);
164  NS_LOG_RELEASE(this, count, "sbJobErrorEnumerator<>");
165  if (0 == count) {
166  mRefCnt = 1; /* stabilize */
167  NS_DELETEXPCOM(this);
168  }
169  return count;
170  }
171 
172  NS_IMETHODIMP QueryInterface(REFNSIID aIID, void** aInstancePtr)
173  {
174  NS_ASSERTION(aInstancePtr,
175  "QueryInterface requires a non-NULL destination!");
176  nsresult rv = NS_ERROR_FAILURE;
177  NS_INTERFACE_TABLE2(sbJobErrorEnumerator<Interface>,
180  NS_INTERFACE_TABLE_TAIL
181 
182 protected:
183  nsAutoRefCnt mRefCnt;
184  ArrayType mArray;
185  PRUint32 mIndex;
186 };
187 
188 #endif /* _SB_JOB_UTILS_H_ */
189 
return NS_OK
sbOSDControlService prototype QueryInterface
var count
Definition: test_bug7406.js:32
NS_IMETHODIMP_(nsresult) sbVariant
Definition: sbVariant.cpp:1954
SB_AUTO_NULL_CLASS(sbAutoJobCancel, sbIJobCancelable *, mValue->Cancel())
function next()