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