sbDeviceEventTesterRemoval.cpp
Go to the documentation of this file.
1 /* vim: set sw=2 :miv */
2 /*
3 //
4 // BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2008 POTI, Inc.
9 // http://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 
29 
58 #include <nsAutoPtr.h>
59 #include <nsCOMPtr.h>
60 #include <nsServiceManagerUtils.h>
61 #include <nsIGenericFactory.h>
62 
63 #include <sbIDevice.h>
64 #include <sbIDeviceEvent.h>
65 #include <sbIDeviceEventTarget.h>
66 #include <sbIDeviceManager.h>
67 
68 #include <stdio.h>
69 
71  nsIRunnable)
72 
74 {
75  /* member initializers and constructor code */
76 }
77 
78 sbDeviceEventTesterRemoval::~sbDeviceEventTesterRemoval()
79 {
80  /* destructor code */
81 }
82 
83 /* void run (); */
84 NS_IMETHODIMP sbDeviceEventTesterRemoval::Run()
85 {
86  nsresult rv;
87  nsCOMPtr<sbIDeviceManager2> manager =
88  do_GetService("@songbirdnest.com/Songbird/DeviceManager;2", &rv);
89  NS_ENSURE_SUCCESS(rv, rv);
90 
91  nsCOMPtr<sbIDeviceEventTarget> target = do_QueryInterface(manager, &rv);
92  NS_ENSURE_SUCCESS(rv, rv);
93 
95  nsRefPtr<sbDeviceEventTesterRemovalHelper> listenerA =
97  NS_ENSURE_TRUE(listenerA, NS_ERROR_OUT_OF_MEMORY);
98  mListeners.AppendElement(listenerA);
99  rv = target->AddEventListener(listenerA);
100  NS_ENSURE_SUCCESS(rv, rv);
101 
102  nsRefPtr<sbDeviceEventTesterRemovalHelper> listenerB =
104  NS_ENSURE_TRUE(listenerB, NS_ERROR_OUT_OF_MEMORY);
105  mListeners.AppendElement(listenerB);
106  rv = target->AddEventListener(listenerB);
107  NS_ENSURE_SUCCESS(rv, rv);
108 
109  nsRefPtr<sbDeviceEventTesterRemovalHelper> listenerC =
111  NS_ENSURE_TRUE(listenerC, NS_ERROR_OUT_OF_MEMORY);
112  mListeners.AppendElement(listenerC);
113  rv = target->AddEventListener(listenerC);
114  NS_ENSURE_SUCCESS(rv, rv);
115 
116  nsRefPtr<sbDeviceEventTesterRemovalHelper> listenerD =
118  NS_ENSURE_TRUE(listenerD, NS_ERROR_OUT_OF_MEMORY);
119  mListeners.AppendElement(listenerD);
120  // listener D not initially listening
121 
122  rv = listenerA->SetFlag(0, PR_TRUE);
123  NS_ENSURE_SUCCESS(rv, rv);
124  rv = listenerA->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_CHECK_FLAG,
125  PR_TRUE,
126  0, 1);
127  NS_ENSURE_SUCCESS(rv, rv);
128  rv = listenerA->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_ADDREMOVE,
129  PR_FALSE,
130  listenerB);
131  NS_ENSURE_SUCCESS(rv, rv);
132  rv = listenerA->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_ADDREMOVE,
133  PR_TRUE,
134  listenerD);
135  NS_ENSURE_SUCCESS(rv, rv);
136  rv = listenerA->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_SET_FLAG,
137  PR_FALSE,
138  0, 1);
139  NS_ENSURE_SUCCESS(rv, rv);
140 
141  rv = listenerB->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_CHECK_FLAG,
142  PR_TRUE,
143  0, 1);
144  NS_ENSURE_SUCCESS(rv, rv);
145 
146  rv = listenerC->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_CHECK_FLAG,
147  PR_FALSE,
148  0, 3);
149  NS_ENSURE_SUCCESS(rv, rv);
150  rv = listenerC->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_ADDREMOVE,
151  PR_FALSE,
152  listenerA);
153  NS_ENSURE_SUCCESS(rv, rv);
154  rv = listenerC->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_SET_FLAG,
155  PR_TRUE,
156  0, 1);
157  NS_ENSURE_SUCCESS(rv, rv);
158  rv = listenerC->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_DISPATCH,
159  PR_FALSE,
160  0, 1);
161  NS_ENSURE_SUCCESS(rv, rv);
162 
163  rv = listenerD->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_CHECK_FLAG,
164  PR_FALSE,
165  1, 2);
166  NS_ENSURE_SUCCESS(rv, rv);
167  rv = listenerD->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_SET_FLAG,
168  PR_TRUE,
169  0, 1);
170  NS_ENSURE_SUCCESS(rv, rv);
171  rv = listenerD->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_SET_FLAG,
172  PR_TRUE,
173  1, 1);
174  NS_ENSURE_SUCCESS(rv, rv);
175  rv = listenerD->AddAction(sbDeviceEventTesterRemovalHelper::ACTION_CHECK_FLAG,
176  PR_TRUE,
177  0, 1);
178  NS_ENSURE_SUCCESS(rv, rv);
179 
180  nsCOMPtr<sbIDeviceEvent> event;
181  rv = manager->CreateEvent(sbIDeviceEvent::EVENT_CLIENT_DEFINED,
182  nsnull,
183  nsnull,
186  getter_AddRefs(event));
187  NS_ENSURE_SUCCESS(rv, rv);
188 
189  rv = target->DispatchEvent(event, PR_FALSE, nsnull);
190  NS_ENSURE_SUCCESS(rv, rv);
191 
192  rv = target->DispatchEvent(event, PR_FALSE, nsnull);
193  NS_ENSURE_SUCCESS(rv, rv);
194 
195  return NS_OK;
196 }
197 
198 
201 
203  : mName(aName)
204 {
205  mFlags.Init();
206 }
207 
209 {
210  return mActions.AppendElement(aAction) ? NS_OK : NS_ERROR_FAILURE;
211 }
212 
213 nsresult sbDeviceEventTesterRemovalHelper::SetFlag(PRUint32 aFlag, PRBool aSet)
214 {
215  return mFlags.Put(aFlag, aSet) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
216 }
217 
218 /* void onDeviceEvent (in sbIDeviceEvent aEvent); */
219 NS_IMETHODIMP sbDeviceEventTesterRemovalHelper::OnDeviceEvent(sbIDeviceEvent *aEvent)
220 {
221  if (mActions.Length() < 1) {
222  // no actions left
223  return NS_OK;
224  }
225 
226  nsresult rv;
227  PRBool succeeded;
228 
229  int length = mActions.Length();
230 
231  nsCOMPtr<sbIDeviceEventTarget> target;
232  rv = aEvent->GetTarget(getter_AddRefs(target));
233  NS_ENSURE_SUCCESS(rv, rv);
234 
235  for (int index = 0; index < length; ++index) {
236  printf("Removal event: %c#%i\n", mName, index);
237 
238  switch (mActions[index].type) {
239  case ACTION_ADDREMOVE:
240  if (mActions[index].set) {
241  rv = target->AddEventListener(mActions[index].listener);
242  } else {
243  rv = target->RemoveEventListener(mActions[index].listener);
244  }
245  NS_ENSURE_SUCCESS(rv, rv);
246  break;
247 
248  case ACTION_SET_FLAG:
249  succeeded = mFlags.Put(mActions[index].flag, mActions[index].set);
250  NS_ENSURE_TRUE(succeeded, NS_ERROR_FAILURE);
251  break;
252 
253  case ACTION_CHECK_FLAG:
254  if (mActions[index].set) {
255  // check flag, fail if false
256  PRBool value = PR_FALSE; // value is not touched for missing keys
257  succeeded = mFlags.Get(mActions[index].flag, &value);
258  NS_ENSURE_TRUE(value, NS_ERROR_ABORT);
259  } else {
260  // check flag, if not set, do the next (n) actions
261  // if set, do the rest
262  PRBool value = PR_FALSE; // value is not touched for missing keys
263  succeeded = mFlags.Get(mActions[index].flag, &value);
264  if (value) {
265  // skip next (n) actions
266  index += mActions[index].count;
267  } else {
268  // do next (n) actions only
269  length = index + 1 + mActions[index].count;
270  }
271  }
272  break;
273 
274  case ACTION_DISPATCH:
275  nsCOMPtr<sbIDeviceManager2> manager =
276  do_GetService("@songbirdnest.com/Songbird/DeviceManager;2", &rv);
277  PRUint32 eventType;
278  rv = aEvent->GetType(&eventType);
279  NS_ENSURE_SUCCESS(rv, rv);
280 
281  nsCOMPtr<sbIDeviceEvent> event;
282  rv = manager->CreateEvent(eventType,
283  nsnull,
284  nsnull,
287  getter_AddRefs(event));
288  NS_ENSURE_SUCCESS(rv, rv);
289 
290  nsCOMPtr<sbIDeviceEventTarget> target;
291  rv = aEvent->GetTarget(getter_AddRefs(target));
292  NS_ENSURE_SUCCESS(rv, rv);
293  rv = target->DispatchEvent(event, PR_FALSE, nsnull);
294  NS_ENSURE_SUCCESS(rv, rv);
295 
296  break;
297  }
298  }
299 
300  return NS_OK;
301 }
nsresult AddAction(ACTION_TYPE aType, PRBool aSet, sbIDeviceEventListener *aListener)
const unsigned long EVENT_CLIENT_DEFINED
return NS_OK
function succeeded(ch, cx, status, data)
var event
const NS_ERROR_ABORT
nsresult SetFlag(PRUint32 aFlag, PRBool aSet)
const unsigned long STATE_IDLE
Definition: sbIDevice.idl:220
_updateCookies aName
countRef value
Definition: FeedWriter.js:1423
nsTArray< nsRefPtr< sbDeviceEventTesterRemovalHelper > > mListeners
nsDataHashtableMT< nsUint32HashKey, PRBool > mFlags
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceEventTesterRemoval, nsIRunnable) sbDeviceEventTesterRemoval