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"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2012-10-25 23:47:55 +04:00
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
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
|
2016-05-19 05:02:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(gRequestObserverProxyLog, LogLevel::Debug, args)
|
2001-04-10 09:43:26 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsARequestObserverEvent internal class...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
nsARequestObserverEvent::nsARequestObserverEvent(nsIRequest* request)
|
|
|
|
: Runnable("net::nsARequestObserverEvent")
|
|
|
|
, mRequest(request)
|
2001-04-10 09:43:26 +04:00
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mRequest, "null pointer");
|
2001-04-10 09:43:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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)
|
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mProxy, "null pointer");
|
2001-04-10 09:43:26 +04:00
|
|
|
}
|
|
|
|
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~nsOnStartRequestEvent() = default;
|
2001-04-10 09:43:26 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2001-04-10 09:43:26 +04:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsOnStartRequestEvent::HandleEvent [req=%p]\n", mRequest.get()));
|
2001-04-10 09:43:26 +04:00
|
|
|
|
|
|
|
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)) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("OnStartRequest failed [rv=%" PRIx32 "] canceling request!\n",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2001-04-10 09:43:26 +04:00
|
|
|
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)
|
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mProxy, "null pointer");
|
2001-04-10 09:43:26 +04:00
|
|
|
}
|
|
|
|
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~nsOnStopRequestEvent() = default;
|
2001-04-10 09:43:26 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2001-04-10 09:43:26 +04:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsOnStopRequestEvent::HandleEvent [req=%p]\n", mRequest.get()));
|
2001-04-10 09:43:26 +04:00
|
|
|
|
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
|
2018-04-30 19:46:04 +03:00
|
|
|
mProxy->mObserver = nullptr;
|
2001-04-10 09:43:26 +04:00
|
|
|
|
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...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2001-04-10 09:43:26 +04:00
|
|
|
nsRequestObserverProxy::OnStartRequest(nsIRequest *request,
|
|
|
|
nsISupports *context)
|
|
|
|
{
|
2013-03-19 20:04:57 +04:00
|
|
|
MOZ_ASSERT(!context || context == mContext);
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsRequestObserverProxy::OnStartRequest [this=%p req=%p]\n", this, request));
|
2001-04-10 09:43:26 +04:00
|
|
|
|
2017-07-06 15:00:35 +03: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;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2001-04-10 09:43:26 +04:00
|
|
|
nsRequestObserverProxy::OnStopRequest(nsIRequest *request,
|
|
|
|
nsISupports *context,
|
|
|
|
nsresult status)
|
|
|
|
{
|
2013-03-19 20:04:57 +04:00
|
|
|
MOZ_ASSERT(!context || context == mContext);
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsRequestObserverProxy: OnStopRequest [this=%p req=%p status=%" PRIx32 "]\n",
|
|
|
|
this, request, static_cast<uint32_t>(status)));
|
2001-04-10 09:43:26 +04:00
|
|
|
|
|
|
|
// The status argument is ignored because, by the time the OnStopRequestEvent
|
2017-07-06 15:00:35 +03:00
|
|
|
// is actually processed, the status of the request may have changed :-(
|
2001-04-10 09:43:26 +04:00
|
|
|
// To make sure that an accurate status code is always used, GetStatus() is
|
|
|
|
// called when the OnStopRequestEvent is actually processed (see above).
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
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);
|
2017-06-14 04:27:17 +03:00
|
|
|
mObserver = new nsMainThreadPtrHolder<nsIRequestObserver>(
|
|
|
|
"nsRequestObserverProxy::mObserver", observer);
|
|
|
|
mContext = new nsMainThreadPtrHolder<nsISupports>(
|
|
|
|
"nsRequestObserverProxy::mContext", 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)
|
|
|
|
{
|
2017-06-01 23:43:44 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mainThread(GetMainThreadEventTarget());
|
2013-03-19 20:04:57 +04:00
|
|
|
return mainThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2001-04-10 09:43:26 +04:00
|
|
|
}
|
2016-05-19 05:02:57 +03:00
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|