Necko API changes, bug 74221. r=valeski, sr=rpotts.

This commit is contained in:
darin%netscape.com 2001-04-10 05:43:26 +00:00
parent 0336388aa1
commit e938b93033
4 changed files with 463 additions and 0 deletions

View File

@ -0,0 +1,73 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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/
*
* 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.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#include "nsISupports.idl"
interface nsIRequest;
/**
* nsIRequestObserver
*
* @status UNDER_REVIEW
*/
[scriptable, uuid(fd91e2e0-1481-11d3-9333-00104ba0fd40)]
interface nsIRequestObserver : nsISupports
{
/**
* Called to signify the beginning of an asynchronous request.
*
* @param request - request being observed
* @param ctxt - user defined context
*
* A failure code returned from onStartRequest has the side-effect of
* causing the request to be canceled.
*/
void onStartRequest(in nsIRequest request,
in nsISupports ctxt);
/**
* Called to signify the end of an asynchronous request. This
* call is always preceded by a call to onStartRequest.
*
* @param request - request being observed
* @param ctxt - user defined context
* @param statusCode - reason for stopping (NS_OK if completed successfully)
*
* A failure code returned from onStopRequest is generally ignored.
*/
void onStopRequest(in nsIRequest request,
in nsISupports ctxt,
in nsresult statusCode);
};
%{C++
// Generic status codes for OnStopRequest:
#define NS_BINDING_SUCCEEDED NS_OK
#define NS_BINDING_FAILED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 1)
#define NS_BINDING_ABORTED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 2)
// The binding has been moved to another request in the same load group:
#define NS_BINDING_REDIRECTED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 3)
// The binding has been moved to another request in a different load group:
#define NS_BINDING_RETARGETED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 4)
%}

View File

@ -0,0 +1,47 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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/
*
* 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.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 2001 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@netscape.com> (original author)
*/
#include "nsIRequestObserver.idl"
interface nsIEventQueue;
/**
* A request observer proxy is used to ship data over to another thread specified
* by the thread's event queue. The "true" request observer's methods are
* invoked on the other thread.
*
* This interface only provides the initialization needed after construction. Otherwise,
* these objects are used simply as nsIRequestObserver's.
*/
[scriptable, uuid(3c9b532e-db84-4ecf-aa6a-4d38a9c4c5f0)]
interface nsIRequestObserverProxy : nsIRequestObserver
{
/**
* Initializes an nsIRequestObserverProxy.
*
* @param observer - receives observer notifications on the other thread
* @param eventQ - may be NULL indicating the calling thread's event queue
*/
void init(in nsIRequestObserver observer,
in nsIEventQueue eventQ);
};

View File

@ -0,0 +1,262 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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/
*
* 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.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 2001 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@netscape.com> (original author)
*/
#include "nsRequestObserverProxy.h"
#include "nsIRequest.h"
#include "nsIEventQueueService.h"
#include "nsIServiceManager.h"
#include "nsString.h"
#include "prlog.h"
#if defined(PR_LOGGING)
static PRLogModuleInfo *gRequestObserverProxyLog;
#endif
#define LOG(args) PR_LOG(gRequestObserverProxyLog, PR_LOG_DEBUG, args)
static NS_DEFINE_CID(kEventQueueService, NS_EVENTQUEUESERVICE_CID);
//-----------------------------------------------------------------------------
// nsARequestObserverEvent internal class...
//-----------------------------------------------------------------------------
nsARequestObserverEvent::nsARequestObserverEvent(nsIRequest *request,
nsISupports *context)
: mRequest(request)
, mContext(context)
{
NS_PRECONDITION(mRequest, "null pointer");
PL_InitEvent(&mEvent, nsnull,
(PLHandleEventProc) nsARequestObserverEvent::HandlePLEvent,
(PLDestroyEventProc) nsARequestObserverEvent::DestroyPLEvent);
}
void PR_CALLBACK
nsARequestObserverEvent::HandlePLEvent(PLEvent *plev)
{
nsARequestObserverEvent *ev =
NS_REINTERPRET_CAST(nsARequestObserverEvent *, plev);
NS_ASSERTION(ev, "null event");
// Pass control to the real event handler
if (ev)
ev->HandleEvent();
}
void PR_CALLBACK
nsARequestObserverEvent::DestroyPLEvent(PLEvent *plev)
{
nsARequestObserverEvent *ev =
NS_REINTERPRET_CAST(nsARequestObserverEvent *, plev);
NS_ASSERTION(ev, "null event");
delete ev;
}
//-----------------------------------------------------------------------------
// nsOnStartRequestEvent internal class...
//-----------------------------------------------------------------------------
class nsOnStartRequestEvent : public nsARequestObserverEvent
{
nsRequestObserverProxy *mProxy;
public:
nsOnStartRequestEvent(nsRequestObserverProxy *proxy,
nsIRequest *request,
nsISupports *context)
: nsARequestObserverEvent(request, context)
, mProxy(proxy)
{
NS_PRECONDITION(mProxy, "null pointer");
MOZ_COUNT_CTOR(nsOnStartRequestEvent);
NS_ADDREF(mProxy);
}
~nsOnStartRequestEvent()
{
MOZ_COUNT_DTOR(nsOnStartRequestEvent);
NS_RELEASE(mProxy);
}
void HandleEvent()
{
LOG(("nsOnStartRequestEvent::HandleEvent [req=%x]\n", mRequest.get()));
if (!mProxy->mObserver) {
NS_NOTREACHED("already handled onStopRequest event (observer is null)");
return;
}
nsresult rv = mProxy->mObserver->OnStartRequest(mRequest, mContext);
if (NS_FAILED(rv)) {
LOG(("OnStartRequest failed [rv=%x] canceling request!\n", rv));
rv = mRequest->Cancel(rv);
NS_ASSERTION(NS_SUCCEEDED(rv), "Cancel failed for request!");
}
}
};
//-----------------------------------------------------------------------------
// nsOnStopRequestEvent internal class...
//-----------------------------------------------------------------------------
class nsOnStopRequestEvent : public nsARequestObserverEvent
{
nsRequestObserverProxy *mProxy;
public:
nsOnStopRequestEvent(nsRequestObserverProxy *proxy,
nsIRequest *request, nsISupports *context)
: nsARequestObserverEvent(request, context)
, mProxy(proxy)
{
NS_PRECONDITION(mProxy, "null pointer");
MOZ_COUNT_CTOR(nsOnStopRequestEvent);
NS_ADDREF(mProxy);
}
~nsOnStopRequestEvent()
{
MOZ_COUNT_DTOR(nsOnStopRequestEvent);
NS_RELEASE(mProxy);
}
void HandleEvent()
{
nsresult rv, status = NS_OK;
LOG(("nsOnStopRequestEvent::HandleEvent [req=%x]\n", mRequest.get()));
nsCOMPtr<nsIRequestObserver> observer = mProxy->mObserver;
if (!observer) {
NS_NOTREACHED("already handled onStopRequest event (observer is null)");
return;
}
// Do not allow any more events to be handled after OnStopRequest
mProxy->mObserver = 0;
rv = mRequest->GetStatus(&status);
NS_ASSERTION(NS_SUCCEEDED(rv), "GetStatus failed for request!");
(void) observer->OnStopRequest(mRequest, mContext, status);
}
};
//-----------------------------------------------------------------------------
// nsRequestObserverProxy::nsISupports implementation...
//-----------------------------------------------------------------------------
NS_IMPL_THREADSAFE_ISUPPORTS2(nsRequestObserverProxy,
nsIRequestObserver,
nsIRequestObserverProxy)
//-----------------------------------------------------------------------------
// nsRequestObserverProxy::nsIRequestObserver implementation...
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsRequestObserverProxy::OnStartRequest(nsIRequest *request,
nsISupports *context)
{
LOG(("nsRequestObserverProxy::OnStartRequest [this=%x req=%x]\n", this, request));
nsOnStartRequestEvent *ev =
new nsOnStartRequestEvent(this, request, context);
if (!ev)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = FireEvent(ev);
if (NS_FAILED(rv))
delete ev;
return rv;
}
NS_IMETHODIMP
nsRequestObserverProxy::OnStopRequest(nsIRequest *request,
nsISupports *context,
nsresult status)
{
LOG(("nsRequestObserverProxy: OnStopRequest [this=%x req=%x status=%x]\n",
this, request, status));
// The status argument is ignored because, by the time the OnStopRequestEvent
// is actually processed, the status of the request may have changed :-(
// To make sure that an accurate status code is always used, GetStatus() is
// called when the OnStopRequestEvent is actually processed (see above).
nsOnStopRequestEvent *ev =
new nsOnStopRequestEvent(this, request, context);
if (!ev)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = FireEvent(ev);
if (NS_FAILED(rv))
delete ev;
return rv;
}
//-----------------------------------------------------------------------------
// nsRequestObserverProxy::nsIRequestObserverProxy implementation...
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsRequestObserverProxy::Init(nsIRequestObserver *observer,
nsIEventQueue *eventQ)
{
NS_ENSURE_ARG_POINTER(observer);
#if defined(PR_LOGGING)
if (!gRequestObserverProxyLog)
gRequestObserverProxyLog = PR_NewLogModule("nsRequestObserverProxy");
#endif
mObserver = observer;
return SetEventQueue(eventQ);
}
//-----------------------------------------------------------------------------
// nsRequestObserverProxy implementation...
//-----------------------------------------------------------------------------
nsresult
nsRequestObserverProxy::FireEvent(nsARequestObserverEvent *event)
{
NS_ENSURE_TRUE(mEventQ, NS_ERROR_NOT_INITIALIZED);
PRStatus status = mEventQ->PostEvent(event->GetPLEvent());
return status == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
}
nsresult
nsRequestObserverProxy::SetEventQueue(nsIEventQueue *eq)
{
nsresult rv = NS_OK;
if ((eq == NS_CURRENT_EVENTQ) || (eq == NS_UI_THREAD_EVENTQ)) {
nsCOMPtr<nsIEventQueueService> serv = do_GetService(kEventQueueService, &rv);
if (NS_FAILED(rv)) return rv;
rv = serv->GetSpecialEventQueue((PRInt32) eq, getter_AddRefs(mEventQ));
}
else
mEventQ = eq;
return rv;
}

View File

@ -0,0 +1,81 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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/
*
* 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.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@netscape.com> (original author)
*/
#ifndef nsRequestObserverProxy_h__
#define nsRequestObserverProxy_h__
#include "nsIRequestObserver.h"
#include "nsIRequestObserverProxy.h"
#include "nsIEventQueue.h"
#include "nsIRequest.h"
#include "nsCOMPtr.h"
class nsARequestObserverEvent;
class nsRequestObserverProxy : public nsIRequestObserverProxy
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSIREQUESTOBSERVERPROXY
nsRequestObserverProxy() { NS_INIT_ISUPPORTS(); }
virtual ~nsRequestObserverProxy() {}
nsIRequestObserver *Observer() { return mObserver; }
nsresult FireEvent(nsARequestObserverEvent *);
nsresult SetEventQueue(nsIEventQueue *);
protected:
nsCOMPtr<nsIRequestObserver> mObserver;
nsCOMPtr<nsIEventQueue> mEventQ;
friend class nsOnStartRequestEvent;
friend class nsOnStopRequestEvent;
};
class nsARequestObserverEvent
{
public:
nsARequestObserverEvent(nsIRequest *, nsISupports *);
virtual ~nsARequestObserverEvent() {}
PLEvent *GetPLEvent() { return &mEvent; }
/**
* Implement this method to add code to handle the event
*/
virtual void HandleEvent() = 0;
protected:
static void PR_CALLBACK HandlePLEvent(PLEvent *);
static void PR_CALLBACK DestroyPLEvent(PLEvent *);
PLEvent mEvent; // this _must_ be the first data member
nsCOMPtr<nsIRequest> mRequest;
nsCOMPtr<nsISupports> mContext;
};
#endif // nsRequestObserverProxy_h__