30 #include <nsIThread.h>
31 #include <nsAutoLock.h>
32 #include <nsAutoPtr.h>
35 #include <nsThreadUtils.h>
37 #include "sbIDeviceEventListener.h"
67 mMonitor = nsAutoMonitor::NewMonitor(__FILE__);
68 NS_ASSERTION(mMonitor,
"Failed to create monitor");
75 nsAutoMonitor::DestroyMonitor(mMonitor);
81 NS_IMETHODIMP sbBaseDeviceEventTarget::DispatchEvent(
sbIDeviceEvent *aEvent,
98 nsCOMPtr<sbIDeviceEvent>
event(aEvent);
108 if (!NS_IsMainThread()) {
109 nsCOMPtr<sbIDeviceEvent>
event(aEvent);
135 nsCOMPtr<sbDeviceEvent>
event = do_QueryInterface(aEvent, &rv);
138 NS_ENSURE_SUCCESS(rv, rv);
139 NS_ENSURE_FALSE(
event->WasDispatched(), NS_ERROR_ALREADY_INITIALIZED);
142 rv =
event->SetTarget(
this);
143 NS_ENSURE_SUCCESS(rv, rv);
154 NS_WARNING(
"Device event listener returned error");
169 nsCOMPtr<sbIDeviceEventTarget> parentEventTarget =
171 if (NS_FAILED(rv) || !parentEventTarget) {
178 rv = parentEventTarget->DispatchEvent(aEvent, PR_FALSE, nsnull);
179 NS_ENSURE_SUCCESS(rv, rv);
190 if (!NS_IsMainThread()) {
194 nsCOMPtr<sbIDeviceEventTarget> proxiedSelf;
196 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
197 nsAutoMonitor mon(mMonitor);
201 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
202 getter_AddRefs(proxiedSelf));
203 NS_ENSURE_SUCCESS(rv, rv);
208 return proxiedSelf->AddEventListener(aListener);
211 PRInt32 index =
mListeners.IndexOf(aListener);
214 return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA;
217 return succeeded ?
NS_OK : NS_ERROR_FAILURE;
226 if (!NS_IsMainThread()) {
228 nsCOMPtr<sbIDeviceEventTarget> proxiedSelf;
230 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
231 nsAutoMonitor mon(mMonitor);
235 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
236 getter_AddRefs(proxiedSelf));
237 NS_ENSURE_SUCCESS(rv, rv);
239 return proxiedSelf->RemoveEventListener(aListener);
243 PRInt32 indexToRemove =
mListeners.IndexOf(aListener);
244 if (indexToRemove < 0) {
250 PRBool succeeded =
mListeners.RemoveObjectAt(indexToRemove);
251 NS_ENSURE_TRUE(succeeded, NS_ERROR_FAILURE);
nsCOMPtr< nsIWeakReference > mParentEventTarget
nsresult sbInvokeOnMainThread1Async(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1)
sbBaseDeviceEventTarget()
function succeeded(ch, cx, status, data)
nsresult DispatchEventInternal(nsCOMPtr< sbIDeviceEvent > aEvent)
nsCOMArray< sbIDeviceEventListener > mListeners
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
sbDeviceEventTargetRemovalHelper(PRInt32 aIndex)
virtual ~sbBaseDeviceEventTarget()
Songbird Thread Utilities Definitions.
virtual void * operator()(void *aObject)
RT sbInvokeOnMainThread1(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1)