1999-05-05 03:35:47 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-05-05 03:35:47 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-05-05 03:35:47 +04:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
1999-11-06 06:43:54 +03:00
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-05-05 03:35:47 +04:00
|
|
|
*/
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-05-05 03:35:47 +04:00
|
|
|
#include "nsEventQueue.h"
|
1999-10-18 18:59:57 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsString2.h"
|
|
|
|
|
|
|
|
// in a real system, these would be members in a header class...
|
|
|
|
static char *gActivatedNotification = "nsIEventQueueActivated";
|
|
|
|
static char *gDestroyedNotification = "nsIEventQueueDestroyed";
|
1999-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
nsEventQueueImpl::nsEventQueueImpl()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
1999-10-18 18:59:57 +04:00
|
|
|
AddRef();
|
1999-10-20 02:00:12 +04:00
|
|
|
/* The slightly weird ownership model for eventqueues goes like this:
|
|
|
|
there's an addref from the factory generally held by whoever asked for
|
|
|
|
the queue. The queue addrefs itself (right here) and releases itself
|
|
|
|
when it goes dark and empty. Chained queues also hold references to
|
|
|
|
their immediate elder link, because we have code that assumes the random
|
|
|
|
release of a queue can't break a chain earlier than our current pointer.
|
|
|
|
A queue releases itself immediately upon being chained, dropping
|
|
|
|
the addref given it by the factory. Only the references held to it by
|
|
|
|
a younger chained queue (and from the outside), and this one here in the
|
|
|
|
constructor, keep it alive. The release when it goes dark and empty
|
|
|
|
will queue it, as it were, for destruction.
|
|
|
|
(A dark queue no longer accepts events, an empty one has none.)
|
|
|
|
*/
|
1999-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
mEventQueue = NULL;
|
1999-10-18 18:59:57 +04:00
|
|
|
mYoungerQueue = NULL;
|
|
|
|
mElderQueue = NULL;
|
|
|
|
mAcceptingEvents = PR_TRUE;
|
|
|
|
mCouldHaveEvents = PR_TRUE;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsEventQueueImpl::~nsEventQueueImpl()
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
Unlink();
|
|
|
|
if (mEventQueue != NULL) {
|
|
|
|
NotifyObservers(gDestroyedNotification);
|
1999-05-05 03:35:47 +04:00
|
|
|
PL_DestroyEventQueue(mEventQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::Init()
|
|
|
|
{
|
|
|
|
mEventQueue = PL_CreateNativeEventQueue("Thread event queue...", PR_GetCurrentThread());
|
1999-10-18 18:59:57 +04:00
|
|
|
NotifyObservers(gActivatedNotification);
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::InitFromPLQueue(PLEventQueue* aQueue)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
mEventQueue = aQueue;
|
|
|
|
NotifyObservers(gActivatedNotification);
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsISupports interface implementation... */
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(nsEventQueueImpl,nsIEventQueue,nsPIEventQueueChain)
|
1999-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
/* nsIEventQueue interface implementation... */
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::StopAcceptingEvents()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mElderQueue, "attempted to disable eldest queue in chain");
|
|
|
|
mAcceptingEvents = PR_FALSE;
|
|
|
|
CheckForDeactivation();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility funtion to send observers a notification
|
|
|
|
void
|
|
|
|
nsEventQueueImpl::NotifyObservers(const char *aTopic)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString topic(aTopic);
|
|
|
|
nsISupports *us = NS_STATIC_CAST(nsISupports *,(NS_STATIC_CAST(nsIEventQueue *,this)));
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIObserverService, os, NS_OBSERVERSERVICE_PROGID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
os->Notify(us, topic.GetUnicode(), NULL);
|
|
|
|
}
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP_(PRStatus)
|
|
|
|
nsEventQueueImpl::PostEvent(PLEvent* aEvent)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
if (!mAcceptingEvents) {
|
|
|
|
PRStatus rv = PR_FAILURE;
|
|
|
|
NS_ASSERTION(mElderQueue, "event dropped because event chain is dead");
|
|
|
|
if (mElderQueue) {
|
|
|
|
nsCOMPtr<nsIEventQueue> elder(do_QueryInterface(mElderQueue));
|
|
|
|
if (elder)
|
|
|
|
rv = elder->PostEvent(aEvent);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return PL_PostEvent(mEventQueue, aEvent);
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-05-06 03:55:10 +04:00
|
|
|
nsEventQueueImpl::PostSynchronousEvent(PLEvent* aEvent, void** aResult)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
if (!mAcceptingEvents) {
|
|
|
|
nsresult rv = NS_ERROR_NO_INTERFACE;
|
|
|
|
NS_ASSERTION(mElderQueue, "event dropped because event chain is dead");
|
|
|
|
if (mElderQueue) {
|
|
|
|
nsCOMPtr<nsIEventQueue> elder(do_QueryInterface(mElderQueue));
|
|
|
|
if (elder)
|
|
|
|
rv = elder->PostSynchronousEvent(aEvent, aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
1999-05-06 03:55:10 +04:00
|
|
|
void* result = PL_PostSynchronousEvent(mEventQueue, aEvent);
|
1999-10-18 18:59:57 +04:00
|
|
|
if (aResult)
|
1999-05-06 03:55:10 +04:00
|
|
|
*aResult = result;
|
1999-10-18 18:59:57 +04:00
|
|
|
|
1999-05-06 03:55:10 +04:00
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
1999-05-30 02:50:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::EnterMonitor()
|
|
|
|
{
|
|
|
|
PL_ENTER_EVENT_QUEUE_MONITOR(mEventQueue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::ExitMonitor()
|
|
|
|
{
|
|
|
|
PL_EXIT_EVENT_QUEUE_MONITOR(mEventQueue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::RevokeEvents(void* owner)
|
|
|
|
{
|
|
|
|
PL_RevokeEvents(mEventQueue, owner);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetPLEventQueue(PLEventQueue** aEventQueue)
|
|
|
|
{
|
|
|
|
*aEventQueue = mEventQueue;
|
|
|
|
|
|
|
|
if (mEventQueue == NULL)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-15 00:20:33 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::IsQueueOnCurrentThread(PRBool *aResult)
|
|
|
|
{
|
|
|
|
*aResult = PL_IsQueueOnCurrentThread( mEventQueue );
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-30 02:50:25 +04:00
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::ProcessPendingEvents()
|
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-10-18 18:59:57 +04:00
|
|
|
PL_ProcessPendingEvents(mEventQueue);
|
1999-10-20 02:00:12 +04:00
|
|
|
CheckForDeactivation();
|
1999-10-18 18:59:57 +04:00
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
1999-05-06 03:55:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::EventLoop()
|
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-05-06 03:55:10 +04:00
|
|
|
PL_EventLoop(mEventQueue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::EventAvailable(PRBool& aResult)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
aResult = PL_EventAvailable(mEventQueue);
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetEvent(PLEvent** aResult)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
*aResult = PL_GetEvent(mEventQueue);
|
|
|
|
CheckForDeactivation();
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
1999-06-04 19:02:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::HandleEvent(PLEvent* aEvent)
|
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
PL_HandleEvent(aEvent);
|
|
|
|
return NS_OK;
|
1999-06-04 19:02:27 +04:00
|
|
|
}
|
|
|
|
|
1999-11-16 19:04:14 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::WaitForEvent()
|
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-11-16 19:04:14 +03:00
|
|
|
PL_WaitForEvent(mEventQueue);
|
|
|
|
CheckForDeactivation();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP_(PRInt32)
|
|
|
|
nsEventQueueImpl::GetEventQueueSelectFD()
|
|
|
|
{
|
|
|
|
return PL_GetEventQueueSelectFD(mEventQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsEventQueueImpl::Create(nsISupports *aOuter,
|
|
|
|
REFNSIID aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
nsEventQueueImpl* evt = new nsEventQueueImpl();
|
|
|
|
if (evt == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
nsresult rv = evt->QueryInterface(aIID, aResult);
|
1999-05-26 05:38:36 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete evt;
|
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
1999-10-18 18:59:57 +04:00
|
|
|
|
|
|
|
// ---------------- nsPIEventQueueChain -----------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::AppendQueue(nsIEventQueue *aQueue)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIEventQueue *end;
|
|
|
|
nsCOMPtr<nsPIEventQueueChain> queueChain(do_QueryInterface(aQueue));
|
|
|
|
|
|
|
|
if (!aQueue)
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
|
|
|
|
/* this would be nice
|
|
|
|
NS_ASSERTION(aQueue->mYoungerQueue == NULL && aQueue->mElderQueue == NULL,
|
|
|
|
"event queue repeatedly appended to queue chain");
|
|
|
|
*/
|
|
|
|
rv = NS_ERROR_NO_INTERFACE;
|
|
|
|
|
1999-10-20 02:00:12 +04:00
|
|
|
// (be careful doing this outside nsEventQueueService's mEventQMonitor)
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
GetYoungest(&end); // addrefs. released by Unlink.
|
|
|
|
nsCOMPtr<nsPIEventQueueChain> endChain(do_QueryInterface(end));
|
|
|
|
if (endChain) {
|
|
|
|
endChain->SetYounger(queueChain);
|
|
|
|
queueChain->SetElder(endChain);
|
|
|
|
NS_RELEASE(aQueue); // the addref from the constructor
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::Unlink()
|
|
|
|
{
|
|
|
|
nsPIEventQueueChain *young = mYoungerQueue,
|
|
|
|
*old = mElderQueue;
|
|
|
|
|
1999-10-20 02:00:12 +04:00
|
|
|
// this is probably OK, but shouldn't happen by design, so tell me if it does
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_ASSERTION(!mYoungerQueue, "event queue chain broken in middle");
|
|
|
|
|
|
|
|
// break links early in case the Release cascades back onto us
|
|
|
|
mYoungerQueue = 0;
|
|
|
|
mElderQueue = 0;
|
|
|
|
|
|
|
|
if (young)
|
|
|
|
young->SetElder(old);
|
|
|
|
if (old) {
|
|
|
|
old->SetYounger(young);
|
|
|
|
if (!young)
|
1999-10-20 12:35:04 +04:00
|
|
|
NS_RELEASE(old); // release addref from AppendQueue
|
1999-10-18 18:59:57 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetYoungest(nsIEventQueue **aQueue)
|
|
|
|
{
|
|
|
|
if (mYoungerQueue)
|
|
|
|
return mYoungerQueue->GetYoungest(aQueue);
|
|
|
|
|
|
|
|
nsIEventQueue *answer = NS_STATIC_CAST(nsIEventQueue *, this);
|
|
|
|
NS_ADDREF(answer);
|
|
|
|
*aQueue = answer;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetYoungestActive(nsIEventQueue **aQueue)
|
|
|
|
{
|
|
|
|
nsIEventQueue *answer = NULL;
|
|
|
|
|
|
|
|
if (mYoungerQueue)
|
|
|
|
mYoungerQueue->GetYoungestActive(&answer);
|
1999-10-20 02:00:12 +04:00
|
|
|
if (answer == NULL)
|
|
|
|
if (mAcceptingEvents && mCouldHaveEvents) {
|
|
|
|
answer = NS_STATIC_CAST(nsIEventQueue *, this);
|
|
|
|
NS_ADDREF(answer);
|
|
|
|
} else
|
|
|
|
CheckForDeactivation();
|
1999-10-18 18:59:57 +04:00
|
|
|
*aQueue = answer;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::SetYounger(nsPIEventQueueChain *aQueue)
|
|
|
|
{
|
|
|
|
mYoungerQueue = aQueue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::SetElder(nsPIEventQueueChain *aQueue)
|
|
|
|
{
|
|
|
|
mElderQueue = aQueue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|