gecko-dev/netwerk/base/nsRequestObserverProxy.cpp

201 строка
6.7 KiB
C++
Исходник Обычный вид История

/* -*- 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/. */
#include "mozilla/DebugOnly.h"
#include "nscore.h"
#include "nsRequestObserverProxy.h"
#include "nsIRequest.h"
#include "nsAutoPtr.h"
#include "mozilla/Logging.h"
#include "mozilla/IntegerPrintfMacros.h"
namespace mozilla {
namespace net {
static LazyLogModule gRequestObserverProxyLog("nsRequestObserverProxy");
#undef LOG
#define LOG(args) MOZ_LOG(gRequestObserverProxyLog, LogLevel::Debug, args)
//-----------------------------------------------------------------------------
// nsARequestObserverEvent internal class...
//-----------------------------------------------------------------------------
nsARequestObserverEvent::nsARequestObserverEvent(nsIRequest* request)
: Runnable("net::nsARequestObserverEvent")
, mRequest(request)
{
MOZ_ASSERT(mRequest, "null pointer");
}
//-----------------------------------------------------------------------------
// nsOnStartRequestEvent internal class...
//-----------------------------------------------------------------------------
class nsOnStartRequestEvent : public nsARequestObserverEvent
{
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<nsRequestObserverProxy> mProxy;
public:
nsOnStartRequestEvent(nsRequestObserverProxy *proxy,
nsIRequest *request)
: nsARequestObserverEvent(request)
, mProxy(proxy)
{
MOZ_ASSERT(mProxy, "null pointer");
}
virtual ~nsOnStartRequestEvent() = default;
NS_IMETHOD Run() override
{
LOG(("nsOnStartRequestEvent::HandleEvent [req=%p]\n", mRequest.get()));
if (!mProxy->mObserver) {
NS_NOTREACHED("already handled onStopRequest event (observer is null)");
return NS_OK;
}
LOG(("handle startevent=%p\n", this));
nsresult rv = mProxy->mObserver->OnStartRequest(mRequest, mProxy->mContext);
if (NS_FAILED(rv)) {
LOG(("OnStartRequest failed [rv=%" PRIx32 "] canceling request!\n",
static_cast<uint32_t>(rv)));
rv = mRequest->Cancel(rv);
NS_ASSERTION(NS_SUCCEEDED(rv), "Cancel failed for request!");
}
return NS_OK;
}
};
//-----------------------------------------------------------------------------
// nsOnStopRequestEvent internal class...
//-----------------------------------------------------------------------------
class nsOnStopRequestEvent : public nsARequestObserverEvent
{
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<nsRequestObserverProxy> mProxy;
public:
nsOnStopRequestEvent(nsRequestObserverProxy *proxy,
nsIRequest *request)
: nsARequestObserverEvent(request)
, mProxy(proxy)
{
MOZ_ASSERT(mProxy, "null pointer");
}
virtual ~nsOnStopRequestEvent() = default;
NS_IMETHOD Run() override
{
LOG(("nsOnStopRequestEvent::HandleEvent [req=%p]\n", mRequest.get()));
nsMainThreadPtrHandle<nsIRequestObserver> observer = mProxy->mObserver;
if (!observer) {
NS_NOTREACHED("already handled onStopRequest event (observer is null)");
return NS_OK;
}
// Do not allow any more events to be handled after OnStopRequest
mProxy->mObserver = nullptr;
nsresult status = NS_OK;
DebugOnly<nsresult> rv = mRequest->GetStatus(&status);
NS_ASSERTION(NS_SUCCEEDED(rv), "GetStatus failed for request!");
LOG(("handle stopevent=%p\n", this));
(void) observer->OnStopRequest(mRequest, mProxy->mContext, status);
return NS_OK;
}
};
//-----------------------------------------------------------------------------
// nsRequestObserverProxy::nsISupports implementation...
//-----------------------------------------------------------------------------
NS_IMPL_ISUPPORTS(nsRequestObserverProxy,
nsIRequestObserver,
nsIRequestObserverProxy)
//-----------------------------------------------------------------------------
// nsRequestObserverProxy::nsIRequestObserver implementation...
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsRequestObserverProxy::OnStartRequest(nsIRequest *request,
nsISupports *context)
{
MOZ_ASSERT(!context || context == mContext);
LOG(("nsRequestObserverProxy::OnStartRequest [this=%p req=%p]\n", this, request));
nsOnStartRequestEvent *ev =
new nsOnStartRequestEvent(this, request);
if (!ev)
return NS_ERROR_OUT_OF_MEMORY;
LOG(("post startevent=%p\n", ev));
nsresult rv = FireEvent(ev);
if (NS_FAILED(rv))
delete ev;
return rv;
}
NS_IMETHODIMP
nsRequestObserverProxy::OnStopRequest(nsIRequest *request,
nsISupports *context,
nsresult status)
{
MOZ_ASSERT(!context || context == mContext);
LOG(("nsRequestObserverProxy: OnStopRequest [this=%p req=%p status=%" PRIx32 "]\n",
this, request, static_cast<uint32_t>(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);
if (!ev)
return NS_ERROR_OUT_OF_MEMORY;
LOG(("post stopevent=%p\n", ev));
nsresult rv = FireEvent(ev);
if (NS_FAILED(rv))
delete ev;
return rv;
}
//-----------------------------------------------------------------------------
// nsRequestObserverProxy::nsIRequestObserverProxy implementation...
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsRequestObserverProxy::Init(nsIRequestObserver *observer, nsISupports *context)
{
NS_ENSURE_ARG_POINTER(observer);
mObserver = new nsMainThreadPtrHolder<nsIRequestObserver>(
"nsRequestObserverProxy::mObserver", observer);
mContext = new nsMainThreadPtrHolder<nsISupports>(
"nsRequestObserverProxy::mContext", context);
return NS_OK;
}
//-----------------------------------------------------------------------------
// nsRequestObserverProxy implementation...
//-----------------------------------------------------------------------------
nsresult
nsRequestObserverProxy::FireEvent(nsARequestObserverEvent *event)
{
nsCOMPtr<nsIEventTarget> mainThread(GetMainThreadEventTarget());
return mainThread->Dispatch(event, NS_DISPATCH_NORMAL);
}
} // namespace net
} // namespace mozilla