uriloader/exthandler/ExternalHelperAppChild.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/uriloader/exthandler/ExternalHelperAppChild.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "ExternalHelperAppChild.h"
    1.11 +#include "mozilla/net/ChannelDiverterChild.h"
    1.12 +#include "nsIDivertableChannel.h"
    1.13 +#include "nsIInputStream.h"
    1.14 +#include "nsIFTPChannel.h"
    1.15 +#include "nsIRequest.h"
    1.16 +#include "nsIResumableChannel.h"
    1.17 +#include "nsNetUtil.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace dom {
    1.21 +
    1.22 +NS_IMPL_ISUPPORTS(ExternalHelperAppChild,
    1.23 +                  nsIStreamListener,
    1.24 +                  nsIRequestObserver)
    1.25 +
    1.26 +ExternalHelperAppChild::ExternalHelperAppChild()
    1.27 +  : mStatus(NS_OK)
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +ExternalHelperAppChild::~ExternalHelperAppChild()
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +//-----------------------------------------------------------------------------
    1.36 +// nsIStreamListener
    1.37 +//-----------------------------------------------------------------------------
    1.38 +NS_IMETHODIMP
    1.39 +ExternalHelperAppChild::OnDataAvailable(nsIRequest *request,
    1.40 +                                        nsISupports *ctx,
    1.41 +                                        nsIInputStream *input,
    1.42 +                                        uint64_t offset,
    1.43 +                                        uint32_t count)
    1.44 +{
    1.45 +  if (NS_FAILED(mStatus))
    1.46 +    return mStatus;
    1.47 +
    1.48 +  nsCString data;
    1.49 +  nsresult rv = NS_ReadInputStreamToString(input, data, count);
    1.50 +  if (NS_FAILED(rv))
    1.51 +    return rv;
    1.52 +
    1.53 +  if (!SendOnDataAvailable(data, offset, count))
    1.54 +    return NS_ERROR_UNEXPECTED;
    1.55 +
    1.56 +  return NS_OK;
    1.57 +}
    1.58 +
    1.59 +//////////////////////////////////////////////////////////////////////////////
    1.60 +// nsIRequestObserver
    1.61 +//////////////////////////////////////////////////////////////////////////////
    1.62 +
    1.63 +NS_IMETHODIMP
    1.64 +ExternalHelperAppChild::OnStartRequest(nsIRequest *request, nsISupports *ctx)
    1.65 +{
    1.66 +  nsCOMPtr<nsIDivertableChannel> divertable = do_QueryInterface(request);
    1.67 +  if (divertable) {
    1.68 +    return DivertToParent(divertable, request);
    1.69 +  }
    1.70 +
    1.71 +  nsresult rv = mHandler->OnStartRequest(request, ctx);
    1.72 +  NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED);
    1.73 +
    1.74 +  nsCString entityID;
    1.75 +  nsCOMPtr<nsIResumableChannel> resumable(do_QueryInterface(request));
    1.76 +  if (resumable) {
    1.77 +    resumable->GetEntityID(entityID);
    1.78 +  }
    1.79 +  SendOnStartRequest(entityID);
    1.80 +  return NS_OK;
    1.81 +}
    1.82 +
    1.83 +NS_IMETHODIMP
    1.84 +ExternalHelperAppChild::OnStopRequest(nsIRequest *request,
    1.85 +                                      nsISupports *ctx,
    1.86 +                                      nsresult status)
    1.87 +{
    1.88 +  // mHandler can be null if we diverted the request to the parent
    1.89 +  if (mHandler) {
    1.90 +    nsresult rv = mHandler->OnStopRequest(request, ctx, status);
    1.91 +    SendOnStopRequest(status);
    1.92 +    NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED);
    1.93 +  }
    1.94 +
    1.95 +  return NS_OK;
    1.96 +}
    1.97 +
    1.98 +nsresult
    1.99 +ExternalHelperAppChild::DivertToParent(nsIDivertableChannel *divertable, nsIRequest *request)
   1.100 +{
   1.101 +  mozilla::net::ChannelDiverterChild *diverter = nullptr;
   1.102 +  nsresult rv = divertable->DivertToParent(&diverter);
   1.103 +  if (NS_WARN_IF(NS_FAILED(rv))) {
   1.104 +    return rv;
   1.105 +  }
   1.106 +
   1.107 +  MOZ_ASSERT(diverter);
   1.108 +  if (SendDivertToParentUsing(diverter)) {
   1.109 +    mHandler->DidDivertRequest(request);
   1.110 +    mHandler = nullptr;
   1.111 +    return NS_OK;
   1.112 +  }
   1.113 +
   1.114 +  return NS_ERROR_FAILURE;
   1.115 +}
   1.116 +
   1.117 +bool
   1.118 +ExternalHelperAppChild::RecvCancel(const nsresult& aStatus)
   1.119 +{
   1.120 +  mStatus = aStatus;
   1.121 +  return true;
   1.122 +}
   1.123 +
   1.124 +} // namespace dom
   1.125 +} // namespace mozilla

mercurial