DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (dcc6d7a0dc00)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set expandtab ts=2 sw=2 sts=2 cin: */
/* 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 "InterceptedJARChannel.h"
#include "nsIPipe.h"
#include "mozilla/dom/ChannelInfo.h"

using namespace mozilla::net;

NS_IMPL_ISUPPORTS(InterceptedJARChannel, nsIInterceptedChannel)

InterceptedJARChannel::InterceptedJARChannel(nsJARChannel* aChannel,
                                             nsINetworkInterceptController* aController)
: mController(aController)
, mChannel(aChannel)
{
}

NS_IMETHODIMP
InterceptedJARChannel::GetResponseBody(nsIOutputStream** aStream)
{
  NS_IF_ADDREF(*aStream = mResponseBody);
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::GetInternalContentPolicyType(nsContentPolicyType* aPolicyType)
{
  NS_ENSURE_ARG(aPolicyType);
  nsCOMPtr<nsILoadInfo> loadInfo;
  nsresult rv = mChannel->GetLoadInfo(getter_AddRefs(loadInfo));
  NS_ENSURE_SUCCESS(rv, rv);

  *aPolicyType = loadInfo->InternalContentPolicyType();
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::GetChannel(nsIChannel** aChannel)
{
  NS_IF_ADDREF(*aChannel = mChannel);
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::ResetInterception()
{
  if (!mChannel) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  mResponseBody = nullptr;
  mSynthesizedInput = nullptr;

  mChannel->ResetInterception();
  mReleaseHandle = nullptr;
  mChannel = nullptr;
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::SynthesizeStatus(uint16_t aStatus,
                                        const nsACString& aReason)
{
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::SynthesizeHeader(const nsACString& aName,
                                        const nsACString& aValue)
{
  if (aName.LowerCaseEqualsLiteral("content-type")) {
    mContentType = aValue;
  }
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::FinishSynthesizedResponse(const nsACString& aFinalURLSpec)
{
  if (NS_WARN_IF(!mChannel)) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  if (!aFinalURLSpec.IsEmpty()) {
    // We don't support rewriting responses for JAR channels where the principal
    // needs to be modified.
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  mChannel->OverrideWithSynthesizedResponse(mSynthesizedInput, mContentType);

  mResponseBody = nullptr;
  mReleaseHandle = nullptr;
  mChannel = nullptr;
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::Cancel(nsresult aStatus)
{
  MOZ_ASSERT(NS_FAILED(aStatus));

  if (!mChannel) {
    return NS_ERROR_FAILURE;
  }

  nsresult rv = mChannel->Cancel(aStatus);
  NS_ENSURE_SUCCESS(rv, rv);
  mResponseBody = nullptr;
  mReleaseHandle = nullptr;
  mChannel = nullptr;
  return NS_OK;
}

NS_IMETHODIMP
InterceptedJARChannel::SetChannelInfo(mozilla::dom::ChannelInfo* aChannelInfo)
{
  if (!mChannel) {
    return NS_ERROR_FAILURE;
  }

  return aChannelInfo->ResurrectInfoOnChannel(mChannel);
}

NS_IMETHODIMP
InterceptedJARChannel::GetConsoleReportCollector(nsIConsoleReportCollector**)
{
  return NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP
InterceptedJARChannel::SetReleaseHandle(nsISupports* aHandle)
{
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!mReleaseHandle);
  MOZ_ASSERT(aHandle);
  mReleaseHandle = aHandle;
  return NS_OK;
}

void
InterceptedJARChannel::NotifyController()
{
  nsresult rv = NS_NewPipe(getter_AddRefs(mSynthesizedInput),
                           getter_AddRefs(mResponseBody),
                           0, UINT32_MAX, true, true);
  NS_ENSURE_SUCCESS_VOID(rv);

  nsCOMPtr<nsIFetchEventDispatcher> dispatcher;
  rv = mController->ChannelIntercepted(this, getter_AddRefs(dispatcher));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    rv = ResetInterception();
    NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
        "Failed to resume intercepted network request");
  }
  rv = dispatcher->Dispatch();
  if (NS_WARN_IF(NS_FAILED(rv))) {
    rv = ResetInterception();
    NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
        "Failed to resume intercepted network request");
  }
  mController = nullptr;
}