29 #include <nsAutoLock.h>
30 #include <nsComponentManagerUtils.h>
32 #include <sbIMediacore.h>
33 #include <sbIMediacoreError.h>
34 #include <sbIMediacoreEventListener.h>
42 mMonitor(nsAutoMonitor::NewMonitor(
"sbBaseMediacoreEventTarget::mMonitor"))
47 nsAutoMonitor::DestroyMonitor(mMonitor);
62 nsRefPtr<AsyncDispatchHelper> dispatchHelper =
63 new AsyncDispatchHelper(static_cast<sbIMediacoreEventTarget*>(mTarget), aEvent);
64 NS_ENSURE_TRUE(dispatchHelper, NS_ERROR_OUT_OF_MEMORY);
65 rv = NS_DispatchToMainThread(dispatchHelper, NS_DISPATCH_NORMAL);
66 NS_ENSURE_SUCCESS(rv, rv);
69 if (!NS_IsMainThread()) {
71 nsCOMPtr<sbIMediacoreEventTarget> proxiedSelf;
73 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
74 nsAutoMonitor mon(mMonitor);
78 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
79 getter_AddRefs(proxiedSelf));
80 NS_ENSURE_SUCCESS(rv, rv);
84 return proxiedSelf->DispatchEvent(aEvent, PR_FALSE, _retval);
96 state.length = mListeners.Count();
101 nsCOMPtr<sbMediacoreEvent>
event = do_QueryInterface(aEvent, &rv);
102 NS_ENSURE_SUCCESS(rv, rv);
103 NS_ENSURE_FALSE(
event->WasDispatched(), NS_ERROR_ALREADY_INITIALIZED);
106 rv =
event->SetTarget(mTarget);
107 NS_ENSURE_SUCCESS(rv, rv);
111 mStates.Push(&state);
117 for (state.index = 0; state.index < state.length; ++state.index) {
118 rv = mListeners[state.index]->OnMediacoreEvent(aEvent);
122 NS_WARNING(
"Mediacore event listener returned error");
143 if (!NS_IsMainThread()) {
147 nsCOMPtr<sbIMediacoreEventTarget> proxiedSelf;
149 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
150 nsAutoMonitor mon(mMonitor);
154 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
155 getter_AddRefs(proxiedSelf));
156 NS_ENSURE_SUCCESS(rv, rv);
161 return proxiedSelf->AddListener(aListener);
164 PRInt32 index = mListeners.IndexOf(aListener);
167 return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA;
169 PRBool
succeeded = mListeners.AppendObject(aListener);
170 return succeeded ?
NS_OK : NS_ERROR_FAILURE;
180 if (!NS_IsMainThread()) {
182 nsCOMPtr<sbIMediacoreEventTarget> proxiedSelf;
184 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
185 nsAutoMonitor mon(mMonitor);
189 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
190 getter_AddRefs(proxiedSelf));
191 NS_ENSURE_SUCCESS(rv, rv);
193 return proxiedSelf->RemoveListener(aListener);
197 PRInt32 indexToRemove = mListeners.IndexOf(aListener);
198 if (indexToRemove < 0) {
204 PRBool
succeeded = mListeners.RemoveObjectAt(indexToRemove);
205 NS_ENSURE_TRUE(succeeded, NS_ERROR_FAILURE);
210 mStates.ForEach(helper);
function succeeded(ch, cx, status, data)
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)