sbMediacoreVotingChain.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
31 #include "sbMediacoreVotingChain.h"
32 
33 #include <nsIMutableArray.h>
34 
35 #include <nsComponentManagerUtils.h>
36 
41 #ifdef PR_LOGGING
42 static PRLogModuleInfo* gMediacoreVotingChain = nsnull;
43 #define TRACE(args) PR_LOG(gMediacoreVotingChain, PR_LOG_DEBUG, args)
44 #define LOG(args) PR_LOG(gMediacoreVotingChain, PR_LOG_WARN, args)
45 #else
46 #define TRACE(args) /* nothing */
47 #define LOG(args) /* nothing */
48 #endif
49 
52 
54 : mLock(nsnull)
55 {
56  MOZ_COUNT_CTOR(sbMediacoreVotingChain);
57 
58 #ifdef PR_LOGGING
59  if (!gMediacoreVotingChain)
60  gMediacoreVotingChain= PR_NewLogModule("sbMediacoreVotingChain");
61 #endif
62 
63  TRACE(("sbMediacoreVotingChain[0x%x] - Created", this));
64 }
65 
67 {
68  TRACE(("sbMediacoreVotingChain[0x%x] - Destroyed", this));
69  MOZ_COUNT_DTOR(sbMediacoreVotingChain);
70 
71  if(mLock) {
72  nsAutoLock::DestroyLock(mLock);
73  }
74 
75  mResults.clear();
76 }
77 
78 nsresult
80 {
81  TRACE(("sbMediacoreVotingChain[0x%x] - Init", this));
82 
83  mLock = nsAutoLock::NewLock("sbMediacoreVotingChain::mLock");
84  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
85 
86  return NS_OK;
87 }
88 
89 nsresult
91  sbIMediacore *aMediacore)
92 {
93  TRACE(("sbMediacoreVotingChain[0x%x] - AddVoteResult", this));
94  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
95  NS_ENSURE_ARG_POINTER(aMediacore);
96 
97  nsAutoLock lock(mLock);
98  mResults[aVoteResult] = aMediacore;
99 
100  return NS_OK;
101 }
102 
103 NS_IMETHODIMP
104 sbMediacoreVotingChain::GetValid(PRBool *aValid)
105 {
106  TRACE(("sbMediacoreVotingChain[0x%x] - GetValid", this));
107  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
108  NS_ENSURE_ARG_POINTER(aValid);
109 
110  nsAutoLock lock(mLock);
111  *aValid = !mResults.empty();
112 
113  return NS_OK;
114 }
115 
116 NS_IMETHODIMP
117 sbMediacoreVotingChain::GetMediacoreChain(nsIArray * *aMediacoreChain)
118 {
119  TRACE(("sbMediacoreVotingChain[0x%x] - GetMediacoreChain", this));
120  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
121  NS_ENSURE_ARG_POINTER(aMediacoreChain);
122 
123  nsresult rv = NS_ERROR_UNEXPECTED;
124  nsCOMPtr<nsIMutableArray> mutableArray =
125  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
126  NS_ENSURE_SUCCESS(rv, rv);
127 
128  nsAutoLock lock(mLock);
129 
130  votingmap_t::const_reverse_iterator cit = mResults.rbegin();
131  votingmap_t::const_reverse_iterator endCit = mResults.rend();
132 
133  for(; cit != endCit; ++cit) {
134  rv = mutableArray->AppendElement((*cit).second, PR_FALSE);
135  NS_ENSURE_SUCCESS(rv, rv);
136  }
137 
138  rv = CallQueryInterface(mutableArray, aMediacoreChain);
139  NS_ENSURE_SUCCESS(rv, rv);
140 
141  return NS_OK;
142 }
143 
144 NS_IMETHODIMP
145 sbMediacoreVotingChain::GetVote(sbIMediacore *aMediacore,
146  PRUint32 *_retval)
147 {
148  TRACE(("sbMediacoreVotingChain[0x%x] - GetVote", this));
149  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
150  NS_ENSURE_ARG_POINTER(aMediacore);
151  NS_ENSURE_ARG_POINTER(_retval);
152 
153  nsAutoLock lock(mLock);
154 
155  votingmap_t::const_reverse_iterator cit = mResults.rbegin();
156  votingmap_t::const_reverse_iterator endCit = mResults.rend();
157  for(; cit != endCit; ++cit) {
158  if((*cit).second == aMediacore) {
159  *_retval = (*cit).first;
160  return NS_OK;
161  }
162  }
163 
164  return NS_ERROR_NOT_AVAILABLE;
165 }
return NS_OK
NS_IMPL_THREADSAFE_ISUPPORTS1(sbMediacoreVotingChain, sbIMediacoreVotingChain) sbMediacoreVotingChain
#define TRACE(args)
Songbird Mediacore Voting Chain Definition.
nsresult AddVoteResult(PRUint32 aVoteResult, sbIMediacore *aMediacore)