NS_IMETHODIMP
nsAddbookProtocolHandler::NewChannel(nsIURI *aURI, nsIChannel **_retval)
{
    nsresult rv;
    nsCOMPtr <nsIAddbookUrl> addbookUrl = do_QueryInterface(aURI, &rv);
    NS_ENSURE_SUCCESS(rv,rv);

    rv = addbookUrl->GetAddbookOperation(&mAddbookOperation);
    NS_ENSURE_SUCCESS(rv,rv);

    if (mAddbookOperation == nsIAddbookUrlOperation::InvalidUrl) {
        nsAutoString errorString;
        errorString.AssignLiteral("Unsupported format/operation requested for ");
        nsCAutoString spec;
        rv = aURI->GetSpec(spec);
        NS_ENSURE_SUCCESS(rv,rv);

        errorString.Append(NS_ConvertUTF8toUTF16(spec));
        rv = GenerateXMLOutputChannel(errorString, addbookUrl, aURI, _retval);
        NS_ENSURE_SUCCESS(rv,rv);
        return NS_OK;
    }

    if (mAddbookOperation == nsIAddbookUrlOperation::AddVCard) {
        // create an empty pipe for use with the input stream channel.
        nsCOMPtr<nsIAsyncInputStream> pipeIn;
        nsCOMPtr<nsIAsyncOutputStream> pipeOut;
        nsCOMPtr<nsIPipe> pipe = do_CreateInstance("@mozilla.org/pipe;1");

        rv = pipe->Init(PR_FALSE, PR_FALSE, 0, 0, nsnull);
        NS_ENSURE_SUCCESS(rv, rv);

        pipe->GetInputStream(getter_AddRefs(pipeIn));
        pipe->GetOutputStream(getter_AddRefs(pipeOut));

        pipeOut->Close();

        return NS_NewInputStreamChannel(_retval, aURI, pipeIn,
                                        NS_LITERAL_CSTRING("application/x-addvcard"));
    }

    nsString output;
    rv = GeneratePrintOutput(addbookUrl, output);
    if (NS_FAILED(rv)) {
        output.AssignLiteral("failed to print. url=");
        nsCAutoString spec;
        rv = aURI->GetSpec(spec);
        NS_ENSURE_SUCCESS(rv,rv);
        output.Append(NS_ConvertUTF8toUTF16(spec));
    }

    rv = GenerateXMLOutputChannel(output, addbookUrl, aURI, _retval);
    NS_ENSURE_SUCCESS(rv,rv);
    return NS_OK;
}
NS_IMETHODIMP
nsAddbookProtocolHandler::NewChannel(nsIURI *aURI, nsILoadInfo *aLoadInfo,
                                     nsIChannel **_retval) {
  nsresult rv;
  nsCOMPtr<nsIAddbookUrl> addbookUrl = do_QueryInterface(aURI, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = addbookUrl->GetAddbookOperation(&mAddbookOperation);
  NS_ENSURE_SUCCESS(rv, rv);

  if (mAddbookOperation == nsIAddbookUrlOperation::InvalidUrl) {
    nsAutoString errorString;
    errorString.AssignLiteral("Unsupported format/operation requested for ");
    nsAutoCString spec;
    rv = aURI->GetSpec(spec);
    NS_ENSURE_SUCCESS(rv, rv);

    errorString.Append(NS_ConvertUTF8toUTF16(spec));
    rv = GenerateXMLOutputChannel(errorString, addbookUrl, aURI, aLoadInfo,
                                  _retval);
    NS_ENSURE_SUCCESS(rv, rv);
    return NS_OK;
  }

  if (mAddbookOperation == nsIAddbookUrlOperation::AddVCard) {
    // create an empty pipe for use with the input stream channel.
    nsCOMPtr<nsIAsyncInputStream> pipeIn;
    nsCOMPtr<nsIAsyncOutputStream> pipeOut;
    nsCOMPtr<nsIPipe> pipe = do_CreateInstance("@mozilla.org/pipe;1");

    rv = pipe->Init(false, false, 0, 0);
    NS_ENSURE_SUCCESS(rv, rv);

    // These always succeed because the pipe is initialized above.
    MOZ_ALWAYS_SUCCEEDS(pipe->GetInputStream(getter_AddRefs(pipeIn)));
    MOZ_ALWAYS_SUCCEEDS(pipe->GetOutputStream(getter_AddRefs(pipeOut)));

    pipeOut->Close();
    if (aLoadInfo) {
      return NS_NewInputStreamChannelInternal(
          _retval, aURI, pipeIn.forget(),
          NS_LITERAL_CSTRING("application/x-addvcard"), EmptyCString(),
          aLoadInfo);
    }

    nsCOMPtr<nsIPrincipal> nullPrincipal =
        do_CreateInstance("@mozilla.org/nullprincipal;1", &rv);
    NS_ASSERTION(NS_SUCCEEDED(rv), "CreateInstance of nullprincipal failed.");
    if (NS_FAILED(rv)) return rv;

    return NS_NewInputStreamChannel(
        _retval, aURI, pipeIn.forget(), nullPrincipal,
        nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
        nsIContentPolicy::TYPE_OTHER,
        NS_LITERAL_CSTRING("application/x-addvcard"));
  }

  nsString output;
  rv = GeneratePrintOutput(addbookUrl, output);
  if (NS_FAILED(rv)) {
    output.AssignLiteral("failed to print. url=");
    nsAutoCString spec;
    rv = aURI->GetSpec(spec);
    NS_ENSURE_SUCCESS(rv, rv);
    output.Append(NS_ConvertUTF8toUTF16(spec));
  }

  rv = GenerateXMLOutputChannel(output, addbookUrl, aURI, aLoadInfo, _retval);
  NS_ENSURE_SUCCESS(rv, rv);
  return NS_OK;
}