sbBaseMediacoreEventTarget.h
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 
27 #ifndef SBBASEMEDIACOREEVENTTARGET_H_
28 #define SBBASEMEDIACOREEVENTTARGET_H_
29 
30 #include <nsAutoPtr.h>
31 #include <nsCOMArray.h>
32 #include <nsCOMPtr.h>
33 #include <nsDeque.h>
34 #include <nsIThread.h>
35 #include <nsThreadUtils.h>
36 #include <prmon.h>
37 #include "sbMediacoreEvent.h"
38 
39 #include <nsIVariant.h>
40 #include <sbIMediacoreEventTarget.h>
41 
42 // Forward declared interfaces
43 
44 class sbIMediacore;
45 class sbIMediacoreError;
46 class sbIMediacoreEvent;
48 
56 public:
62  //sbIMediacoreEventTarget interface methods @see sbIMediacoreEventTarget
66  virtual
67  nsresult AddListener(sbIMediacoreEventListener * aListener);
71  virtual
72  nsresult RemoveListener(sbIMediacoreEventListener * aListener);
76  virtual
77  nsresult DispatchEvent(sbIMediacoreEvent * aEvent,
78  PRBool aAsync, PRBool * aDispatched);
79 
80 protected:
81  nsresult DispatchEventInternal(sbIMediacoreEvent *aEvent, PRBool* _retval);
82 
83 private:
84  sbIMediacoreEventTarget * mTarget;
85  nsCOMArray<sbIMediacoreEventListener> mListeners;
86  PRMonitor* mMonitor;
93  struct DispatchState {
94  // these are indices into mListeners
95  PRInt32 index; // the currently processing listener index
96  PRInt32 length; // the number of listeners at the start of this dispatch
97  };
98  // our stack of states (holds *pointers* to DispatchStates)
99  nsDeque mStates;
100  friend class RemovalHelper;
101 
106  class RemovalHelper : public nsDequeFunctor {
107  public:
111  RemovalHelper(PRInt32 aIndex)
112  : mIndexToRemove(aIndex) {}
116  virtual void* operator()(void* aObject)
117  {
118  sbBaseMediacoreEventTarget::DispatchState *state =
119  (sbBaseMediacoreEventTarget::DispatchState*)aObject;
120  if (state->length > mIndexToRemove) {
121  --state->length;
122  }
123  if (state->index >= mIndexToRemove) {
124  --state->index;
125  }
126  /* no return value (it's only useful for FirstThat, not ForEach) */
127  return nsnull;
128  }
129  protected:
130  PRInt32 mIndexToRemove;
131  };
132 
133  // helper class for async event dispatch, because XPCOM proxies can't deal
134  // with having out-params. Note that we discard the result anyway.
135  class AsyncDispatchHelper : public nsIRunnable
136  {
138  public:
142  AsyncDispatchHelper(sbIMediacoreEventTarget* aTarget,
143  sbIMediacoreEvent* aEvent)
144  : mTarget(aTarget), mEvent(aEvent)
145  {
146  NS_ASSERTION(aTarget, "sbMediacoreEventTargetAsyncDispatchHelper: no target");
147  NS_ASSERTION(aEvent, "sbMediacoreEventTargetAsyncDispatchHelper: no event");
148  }
152  NS_IMETHODIMP Run()
153  {
154  NS_ASSERTION(NS_IsMainThread(),
155  "sbMediacoreEventTargetAsyncDispatchHelper: not on main thread!");
156 
157  /* ignore return value */
158  mTarget->DispatchEvent(mEvent,
159  PR_FALSE, /* don't re-async */
160  nsnull); /* ignore retval */
161  return NS_OK;
162  }
163  private:
164  nsCOMPtr<sbIMediacoreEventTarget> mTarget;
165  nsCOMPtr<sbIMediacoreEvent> mEvent;
166  };
167 };
168 
169 #endif /* SBBASEMEDIACOREEVENTTARGET_H_ */
sbBaseMediacoreEventTarget(sbIMediacoreEventTarget *aTarget)
return NS_OK
Songbird Mediacore Event Definition.
virtual nsresult AddListener(sbIMediacoreEventListener *aListener)
Definition of the sbIMediacoreEvent interface.
virtual nsresult RemoveListener(sbIMediacoreEventListener *aListener)
virtual nsresult DispatchEvent(sbIMediacoreEvent *aEvent, PRBool aAsync, PRBool *aDispatched)
nsresult DispatchEventInternal(sbIMediacoreEvent *aEvent, PRBool *_retval)