2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-04-10 05:43:26 +00:00
|
|
|
|
2012-12-14 23:58:45 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2001-08-14 07:59:59 +00:00
|
|
|
#include "nscore.h"
|
2001-04-10 05:43:26 +00:00
|
|
|
#include "nsRequestObserverProxy.h"
|
|
|
|
#include "nsIRequest.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2012-10-25 19:47:55 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2001-04-10 05:43:26 +00:00
|
|
|
|
|
|
|
static PRLogModuleInfo *gRequestObserverProxyLog;
|
|
|
|
|
2013-11-20 22:55:44 +00:00
|
|
|
#undef LOG
|
2015-06-03 22:25:57 +00:00
|
|
|
#define LOG(args) MOZ_LOG(gRequestObserverProxyLog, mozilla::LogLevel::Debug, args)
|
2001-04-10 05:43:26 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsARequestObserverEvent internal class...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-03-19 16:04:57 +00:00
|
|
|
nsARequestObserverEvent::nsARequestObserverEvent(nsIRequest *request)
|
2001-04-10 05:43:26 +00:00
|
|
|
: mRequest(request)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mRequest, "null pointer");
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOnStartRequestEvent internal class...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsOnStartRequestEvent : public nsARequestObserverEvent
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
nsRefPtr<nsRequestObserverProxy> mProxy;
|
2001-04-10 05:43:26 +00:00
|
|
|
public:
|
|
|
|
nsOnStartRequestEvent(nsRequestObserverProxy *proxy,
|
2013-03-19 16:04:57 +00:00
|
|
|
nsIRequest *request)
|
|
|
|
: nsARequestObserverEvent(request)
|
2001-04-10 05:43:26 +00:00
|
|
|
, mProxy(proxy)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mProxy, "null pointer");
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
virtual ~nsOnStartRequestEvent() {}
|
2001-04-10 05:43:26 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHOD Run()
|
2001-04-10 05:43:26 +00:00
|
|
|
{
|
|
|
|
LOG(("nsOnStartRequestEvent::HandleEvent [req=%x]\n", mRequest.get()));
|
|
|
|
|
|
|
|
if (!mProxy->mObserver) {
|
|
|
|
NS_NOTREACHED("already handled onStopRequest event (observer is null)");
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
2001-04-10 05:43:26 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 22:50:42 +00:00
|
|
|
LOG(("handle startevent=%p\n", this));
|
2013-03-19 16:04:57 +00:00
|
|
|
nsresult rv = mProxy->mObserver->OnStartRequest(mRequest, mProxy->mContext);
|
2001-04-10 05:43:26 +00:00
|
|
|
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!");
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-04-10 05:43:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOnStopRequestEvent internal class...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsOnStopRequestEvent : public nsARequestObserverEvent
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
nsRefPtr<nsRequestObserverProxy> mProxy;
|
2001-04-10 05:43:26 +00:00
|
|
|
public:
|
|
|
|
nsOnStopRequestEvent(nsRequestObserverProxy *proxy,
|
2013-03-19 16:04:57 +00:00
|
|
|
nsIRequest *request)
|
|
|
|
: nsARequestObserverEvent(request)
|
2001-04-10 05:43:26 +00:00
|
|
|
, mProxy(proxy)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mProxy, "null pointer");
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
virtual ~nsOnStopRequestEvent() {}
|
2001-04-10 05:43:26 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHOD Run()
|
2001-04-10 05:43:26 +00:00
|
|
|
{
|
|
|
|
LOG(("nsOnStopRequestEvent::HandleEvent [req=%x]\n", mRequest.get()));
|
|
|
|
|
2013-03-19 16:04:57 +00:00
|
|
|
nsMainThreadPtrHandle<nsIRequestObserver> observer = mProxy->mObserver;
|
2001-04-10 05:43:26 +00:00
|
|
|
if (!observer) {
|
|
|
|
NS_NOTREACHED("already handled onStopRequest event (observer is null)");
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
2001-04-10 05:43:26 +00:00
|
|
|
}
|
|
|
|
// Do not allow any more events to be handled after OnStopRequest
|
|
|
|
mProxy->mObserver = 0;
|
|
|
|
|
2012-10-25 19:47:55 +00:00
|
|
|
nsresult status = NS_OK;
|
|
|
|
DebugOnly<nsresult> rv = mRequest->GetStatus(&status);
|
2001-04-10 05:43:26 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetStatus failed for request!");
|
|
|
|
|
2009-08-08 22:50:42 +00:00
|
|
|
LOG(("handle stopevent=%p\n", this));
|
2013-03-19 16:04:57 +00:00
|
|
|
(void) observer->OnStopRequest(mRequest, mProxy->mContext, status);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-04-10 05:43:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsRequestObserverProxy::nsISupports implementation...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsRequestObserverProxy,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIRequestObserverProxy)
|
2001-04-10 05:43:26 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsRequestObserverProxy::nsIRequestObserver implementation...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRequestObserverProxy::OnStartRequest(nsIRequest *request,
|
|
|
|
nsISupports *context)
|
|
|
|
{
|
2013-03-19 16:04:57 +00:00
|
|
|
MOZ_ASSERT(!context || context == mContext);
|
2013-06-04 23:32:31 +00:00
|
|
|
LOG(("nsRequestObserverProxy::OnStartRequest [this=%p req=%x]\n", this, request));
|
2001-04-10 05:43:26 +00:00
|
|
|
|
|
|
|
nsOnStartRequestEvent *ev =
|
2013-03-19 16:04:57 +00:00
|
|
|
new nsOnStartRequestEvent(this, request);
|
2001-04-10 05:43:26 +00:00
|
|
|
if (!ev)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2013-03-19 16:04:57 +00:00
|
|
|
LOG(("post startevent=%p\n", ev));
|
2001-04-10 05:43:26 +00:00
|
|
|
nsresult rv = FireEvent(ev);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
delete ev;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRequestObserverProxy::OnStopRequest(nsIRequest *request,
|
|
|
|
nsISupports *context,
|
|
|
|
nsresult status)
|
|
|
|
{
|
2013-03-19 16:04:57 +00:00
|
|
|
MOZ_ASSERT(!context || context == mContext);
|
2013-06-04 23:32:31 +00:00
|
|
|
LOG(("nsRequestObserverProxy: OnStopRequest [this=%p req=%x status=%x]\n",
|
2001-04-10 05:43:26 +00:00
|
|
|
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 =
|
2013-03-19 16:04:57 +00:00
|
|
|
new nsOnStopRequestEvent(this, request);
|
2001-04-10 05:43:26 +00:00
|
|
|
if (!ev)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2013-03-19 16:04:57 +00:00
|
|
|
LOG(("post stopevent=%p\n", ev));
|
2001-04-10 05:43:26 +00:00
|
|
|
nsresult rv = FireEvent(ev);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
delete ev;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsRequestObserverProxy::nsIRequestObserverProxy implementation...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-19 16:04:57 +00:00
|
|
|
nsRequestObserverProxy::Init(nsIRequestObserver *observer, nsISupports *context)
|
2001-04-10 05:43:26 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(observer);
|
|
|
|
|
|
|
|
if (!gRequestObserverProxyLog)
|
|
|
|
gRequestObserverProxyLog = PR_NewLogModule("nsRequestObserverProxy");
|
|
|
|
|
2013-03-19 16:04:57 +00:00
|
|
|
mObserver = new nsMainThreadPtrHolder<nsIRequestObserver>(observer);
|
|
|
|
mContext = new nsMainThreadPtrHolder<nsISupports>(context);
|
2001-04-10 05:43:26 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
2001-04-10 05:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsRequestObserverProxy implementation...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRequestObserverProxy::FireEvent(nsARequestObserverEvent *event)
|
|
|
|
{
|
2013-03-19 16:04:57 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mainThread(do_GetMainThread());
|
|
|
|
return mainThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2001-04-10 05:43:26 +00:00
|
|
|
}
|