2010-08-05 06:15:55 +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/. */
|
2010-07-28 22:33:06 +04:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2010-07-28 22:33:06 +04:00
|
|
|
#include "nsAsyncRedirectVerifyHelper.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
#include "nsIOService.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIHttpChannelInternal.h"
|
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
|
2013-11-21 02:55:44 +04:00
|
|
|
#undef LOG
|
2012-10-30 03:32:10 +04:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetRedirectLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsRedirect");
|
|
|
|
return sLog;
|
|
|
|
}
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(GetRedirectLog(), mozilla::LogLevel::Debug, args)
|
2010-08-05 06:15:55 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsAsyncRedirectVerifyHelper,
|
|
|
|
nsIAsyncVerifyRedirectCallback,
|
|
|
|
nsIRunnable)
|
2010-08-05 06:15:55 +04:00
|
|
|
|
|
|
|
class nsAsyncVerifyRedirectCallbackEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsAsyncVerifyRedirectCallbackEvent(nsIAsyncVerifyRedirectCallback *cb,
|
|
|
|
nsresult result)
|
|
|
|
: mCallback(cb), mResult(result) {
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
LOG(("nsAsyncVerifyRedirectCallbackEvent::Run() "
|
|
|
|
"callback to %p with result %x",
|
|
|
|
mCallback.get(), mResult));
|
|
|
|
(void) mCallback->OnRedirectVerifyCallback(mResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIAsyncVerifyRedirectCallback> mCallback;
|
|
|
|
nsresult mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsAsyncRedirectVerifyHelper::nsAsyncRedirectVerifyHelper()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mCallbackInitiated(false),
|
2010-08-05 06:15:55 +04:00
|
|
|
mExpectedCallbacks(0),
|
|
|
|
mResult(NS_OK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAsyncRedirectVerifyHelper::~nsAsyncRedirectVerifyHelper()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_FAILED(mResult) || mExpectedCallbacks == 0,
|
|
|
|
"Did not receive all required callbacks!");
|
|
|
|
}
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsAsyncRedirectVerifyHelper::Init(nsIChannel* oldChan, nsIChannel* newChan,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags, bool synchronize)
|
2010-07-28 22:33:06 +04:00
|
|
|
{
|
2010-08-05 06:15:55 +04:00
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::Init() "
|
|
|
|
"oldChan=%p newChan=%p", oldChan, newChan));
|
|
|
|
mOldChan = oldChan;
|
|
|
|
mNewChan = newChan;
|
|
|
|
mFlags = flags;
|
|
|
|
mCallbackThread = do_GetCurrentThread();
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
if (synchronize)
|
2011-10-17 18:59:28 +04:00
|
|
|
mWaitingForRedirectCallback = true;
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = NS_DispatchToMainThread(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (synchronize) {
|
|
|
|
nsIThread *thread = NS_GetCurrentThread();
|
|
|
|
while (mWaitingForRedirectCallback) {
|
|
|
|
if (!NS_ProcessNextEvent(thread)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncRedirectVerifyHelper::OnRedirectVerifyCallback(nsresult result)
|
|
|
|
{
|
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::OnRedirectVerifyCallback() "
|
|
|
|
"result=%x expectedCBs=%u mResult=%x",
|
|
|
|
result, mExpectedCallbacks, mResult));
|
|
|
|
|
|
|
|
--mExpectedCallbacks;
|
|
|
|
|
|
|
|
// If response indicates failure we may call back immediately
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
// We chose to store the first failure-value (as opposed to the last)
|
|
|
|
if (NS_SUCCEEDED(mResult))
|
|
|
|
mResult = result;
|
|
|
|
|
|
|
|
// If InitCallback() has been called, just invoke the callback and
|
|
|
|
// return. Otherwise it will be invoked from InitCallback()
|
|
|
|
if (mCallbackInitiated) {
|
|
|
|
ExplicitCallback(mResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the expected-counter is in balance and InitCallback() was called, all
|
|
|
|
// sinks have agreed that the redirect is ok and we can invoke our callback
|
|
|
|
if (mCallbackInitiated && mExpectedCallbacks == 0) {
|
|
|
|
ExplicitCallback(mResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsAsyncRedirectVerifyHelper::DelegateOnChannelRedirect(nsIChannelEventSink *sink,
|
|
|
|
nsIChannel *oldChannel,
|
|
|
|
nsIChannel *newChannel,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags)
|
2010-08-05 06:15:55 +04:00
|
|
|
{
|
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::DelegateOnChannelRedirect() "
|
|
|
|
"sink=%p expectedCBs=%u mResult=%x",
|
|
|
|
sink, mExpectedCallbacks, mResult));
|
|
|
|
|
|
|
|
++mExpectedCallbacks;
|
|
|
|
|
|
|
|
if (IsOldChannelCanceled()) {
|
|
|
|
LOG((" old channel has been canceled, cancel the redirect by "
|
|
|
|
"emulating OnRedirectVerifyCallback..."));
|
|
|
|
(void) OnRedirectVerifyCallback(NS_BINDING_ABORTED);
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
sink->AsyncOnChannelRedirect(oldChannel, newChannel, flags, this);
|
|
|
|
|
|
|
|
LOG((" result=%x expectedCBs=%u", rv, mExpectedCallbacks));
|
|
|
|
|
|
|
|
// If the sink returns failure from this call the redirect is vetoed. We
|
|
|
|
// emulate a callback from the sink in this case in order to perform all
|
|
|
|
// the necessary logic.
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG((" emulating OnRedirectVerifyCallback..."));
|
|
|
|
(void) OnRedirectVerifyCallback(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv; // Return the actual status since our caller may need it
|
|
|
|
}
|
|
|
|
|
2010-07-28 22:33:06 +04:00
|
|
|
void
|
2010-08-05 06:15:55 +04:00
|
|
|
nsAsyncRedirectVerifyHelper::ExplicitCallback(nsresult result)
|
2010-07-28 22:33:06 +04:00
|
|
|
{
|
2010-08-05 06:15:55 +04:00
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::ExplicitCallback() "
|
|
|
|
"result=%x expectedCBs=%u mCallbackInitiated=%u mResult=%x",
|
|
|
|
result, mExpectedCallbacks, mCallbackInitiated, mResult));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncVerifyRedirectCallback>
|
|
|
|
callback(do_QueryInterface(mOldChan));
|
2010-07-28 22:33:06 +04:00
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
if (!callback || !mCallbackThread) {
|
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::ExplicitCallback() "
|
2010-08-06 14:49:22 +04:00
|
|
|
"callback=%p mCallbackThread=%p", callback.get(), mCallbackThread.get()));
|
2010-08-05 06:15:55 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-07-28 22:33:06 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCallbackInitiated = false; // reset to ensure only one callback
|
|
|
|
mWaitingForRedirectCallback = false;
|
2010-08-05 06:15:55 +04:00
|
|
|
|
|
|
|
// Now, dispatch the callback on the event-target which called Init()
|
2015-03-17 19:29:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-08-05 06:15:55 +04:00
|
|
|
new nsAsyncVerifyRedirectCallbackEvent(callback, result);
|
|
|
|
if (!event) {
|
|
|
|
NS_WARNING("nsAsyncRedirectVerifyHelper::ExplicitCallback() "
|
|
|
|
"failed creating callback event!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsresult rv = mCallbackThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("nsAsyncRedirectVerifyHelper::ExplicitCallback() "
|
|
|
|
"failed dispatching callback event!");
|
|
|
|
} else {
|
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::ExplicitCallback() "
|
|
|
|
"dispatched callback event=%p", event.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsAsyncRedirectVerifyHelper::InitCallback()
|
|
|
|
{
|
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::InitCallback() "
|
|
|
|
"expectedCBs=%d mResult=%x", mExpectedCallbacks, mResult));
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCallbackInitiated = true;
|
2010-08-05 06:15:55 +04:00
|
|
|
|
|
|
|
// Invoke the callback if we are done
|
|
|
|
if (mExpectedCallbacks == 0)
|
|
|
|
ExplicitCallback(mResult);
|
2010-07-28 22:33:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncRedirectVerifyHelper::Run()
|
|
|
|
{
|
|
|
|
/* If the channel got canceled after it fired AsyncOnChannelRedirect
|
2010-08-05 06:15:55 +04:00
|
|
|
* and before we got here, mostly because docloader load has been canceled,
|
|
|
|
* we must completely ignore this notification and prevent any further
|
|
|
|
* notification.
|
2010-07-28 22:33:06 +04:00
|
|
|
*/
|
2010-08-05 06:15:55 +04:00
|
|
|
if (IsOldChannelCanceled()) {
|
|
|
|
ExplicitCallback(NS_BINDING_ABORTED);
|
|
|
|
return NS_OK;
|
2010-07-28 22:33:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// First, the global observer
|
|
|
|
NS_ASSERTION(gIOService, "Must have an IO service at this point");
|
2010-08-05 06:15:55 +04:00
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::Run() calling gIOService..."));
|
|
|
|
nsresult rv = gIOService->AsyncOnChannelRedirect(mOldChan, mNewChan,
|
|
|
|
mFlags, this);
|
2010-07-28 22:33:06 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-08-05 06:15:55 +04:00
|
|
|
ExplicitCallback(rv);
|
2010-07-28 22:33:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, the per-channel observers
|
|
|
|
nsCOMPtr<nsIChannelEventSink> sink;
|
|
|
|
NS_QueryNotificationCallbacks(mOldChan, sink);
|
2010-08-05 06:15:55 +04:00
|
|
|
if (sink) {
|
|
|
|
LOG(("nsAsyncRedirectVerifyHelper::Run() calling sink..."));
|
|
|
|
rv = DelegateOnChannelRedirect(sink, mOldChan, mNewChan, mFlags);
|
|
|
|
}
|
2010-07-28 22:33:06 +04:00
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
// All invocations to AsyncOnChannelRedirect has been done - call
|
|
|
|
// InitCallback() to flag this
|
|
|
|
InitCallback();
|
2010-07-28 22:33:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-08-05 06:15:55 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsAsyncRedirectVerifyHelper::IsOldChannelCanceled()
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool canceled;
|
2010-08-05 06:15:55 +04:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal> oldChannelInternal =
|
|
|
|
do_QueryInterface(mOldChan);
|
|
|
|
if (oldChannelInternal) {
|
|
|
|
oldChannelInternal->GetCanceled(&canceled);
|
2014-11-10 18:10:41 +03:00
|
|
|
if (canceled) {
|
2010-08-05 06:15:55 +04:00
|
|
|
return true;
|
2014-11-10 18:10:41 +03:00
|
|
|
}
|
|
|
|
} else if (mOldChan) {
|
|
|
|
// For non-HTTP channels check on the status, failure
|
|
|
|
// indicates the channel has probably been canceled.
|
|
|
|
nsresult status = NS_ERROR_FAILURE;
|
|
|
|
mOldChan->GetStatus(&status);
|
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-05 06:15:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-08-06 14:49:22 +04:00
|
|
|
}
|