michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ExternalHelperAppChild.h" michael@0: #include "mozilla/net/ChannelDiverterChild.h" michael@0: #include "nsIDivertableChannel.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsIFTPChannel.h" michael@0: #include "nsIRequest.h" michael@0: #include "nsIResumableChannel.h" michael@0: #include "nsNetUtil.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_ISUPPORTS(ExternalHelperAppChild, michael@0: nsIStreamListener, michael@0: nsIRequestObserver) michael@0: michael@0: ExternalHelperAppChild::ExternalHelperAppChild() michael@0: : mStatus(NS_OK) michael@0: { michael@0: } michael@0: michael@0: ExternalHelperAppChild::~ExternalHelperAppChild() michael@0: { michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsIStreamListener michael@0: //----------------------------------------------------------------------------- michael@0: NS_IMETHODIMP michael@0: ExternalHelperAppChild::OnDataAvailable(nsIRequest *request, michael@0: nsISupports *ctx, michael@0: nsIInputStream *input, michael@0: uint64_t offset, michael@0: uint32_t count) michael@0: { michael@0: if (NS_FAILED(mStatus)) michael@0: return mStatus; michael@0: michael@0: nsCString data; michael@0: nsresult rv = NS_ReadInputStreamToString(input, data, count); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: if (!SendOnDataAvailable(data, offset, count)) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: // nsIRequestObserver michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: NS_IMETHODIMP michael@0: ExternalHelperAppChild::OnStartRequest(nsIRequest *request, nsISupports *ctx) michael@0: { michael@0: nsCOMPtr divertable = do_QueryInterface(request); michael@0: if (divertable) { michael@0: return DivertToParent(divertable, request); michael@0: } michael@0: michael@0: nsresult rv = mHandler->OnStartRequest(request, ctx); michael@0: NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED); michael@0: michael@0: nsCString entityID; michael@0: nsCOMPtr resumable(do_QueryInterface(request)); michael@0: if (resumable) { michael@0: resumable->GetEntityID(entityID); michael@0: } michael@0: SendOnStartRequest(entityID); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ExternalHelperAppChild::OnStopRequest(nsIRequest *request, michael@0: nsISupports *ctx, michael@0: nsresult status) michael@0: { michael@0: // mHandler can be null if we diverted the request to the parent michael@0: if (mHandler) { michael@0: nsresult rv = mHandler->OnStopRequest(request, ctx, status); michael@0: SendOnStopRequest(status); michael@0: NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: ExternalHelperAppChild::DivertToParent(nsIDivertableChannel *divertable, nsIRequest *request) michael@0: { michael@0: mozilla::net::ChannelDiverterChild *diverter = nullptr; michael@0: nsresult rv = divertable->DivertToParent(&diverter); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) { michael@0: return rv; michael@0: } michael@0: michael@0: MOZ_ASSERT(diverter); michael@0: if (SendDivertToParentUsing(diverter)) { michael@0: mHandler->DidDivertRequest(request); michael@0: mHandler = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: bool michael@0: ExternalHelperAppChild::RecvCancel(const nsresult& aStatus) michael@0: { michael@0: mStatus = aStatus; michael@0: return true; michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla