Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ExternalHelperAppChild.h"
8 #include "mozilla/net/ChannelDiverterChild.h"
9 #include "nsIDivertableChannel.h"
10 #include "nsIInputStream.h"
11 #include "nsIFTPChannel.h"
12 #include "nsIRequest.h"
13 #include "nsIResumableChannel.h"
14 #include "nsNetUtil.h"
16 namespace mozilla {
17 namespace dom {
19 NS_IMPL_ISUPPORTS(ExternalHelperAppChild,
20 nsIStreamListener,
21 nsIRequestObserver)
23 ExternalHelperAppChild::ExternalHelperAppChild()
24 : mStatus(NS_OK)
25 {
26 }
28 ExternalHelperAppChild::~ExternalHelperAppChild()
29 {
30 }
32 //-----------------------------------------------------------------------------
33 // nsIStreamListener
34 //-----------------------------------------------------------------------------
35 NS_IMETHODIMP
36 ExternalHelperAppChild::OnDataAvailable(nsIRequest *request,
37 nsISupports *ctx,
38 nsIInputStream *input,
39 uint64_t offset,
40 uint32_t count)
41 {
42 if (NS_FAILED(mStatus))
43 return mStatus;
45 nsCString data;
46 nsresult rv = NS_ReadInputStreamToString(input, data, count);
47 if (NS_FAILED(rv))
48 return rv;
50 if (!SendOnDataAvailable(data, offset, count))
51 return NS_ERROR_UNEXPECTED;
53 return NS_OK;
54 }
56 //////////////////////////////////////////////////////////////////////////////
57 // nsIRequestObserver
58 //////////////////////////////////////////////////////////////////////////////
60 NS_IMETHODIMP
61 ExternalHelperAppChild::OnStartRequest(nsIRequest *request, nsISupports *ctx)
62 {
63 nsCOMPtr<nsIDivertableChannel> divertable = do_QueryInterface(request);
64 if (divertable) {
65 return DivertToParent(divertable, request);
66 }
68 nsresult rv = mHandler->OnStartRequest(request, ctx);
69 NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED);
71 nsCString entityID;
72 nsCOMPtr<nsIResumableChannel> resumable(do_QueryInterface(request));
73 if (resumable) {
74 resumable->GetEntityID(entityID);
75 }
76 SendOnStartRequest(entityID);
77 return NS_OK;
78 }
80 NS_IMETHODIMP
81 ExternalHelperAppChild::OnStopRequest(nsIRequest *request,
82 nsISupports *ctx,
83 nsresult status)
84 {
85 // mHandler can be null if we diverted the request to the parent
86 if (mHandler) {
87 nsresult rv = mHandler->OnStopRequest(request, ctx, status);
88 SendOnStopRequest(status);
89 NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED);
90 }
92 return NS_OK;
93 }
95 nsresult
96 ExternalHelperAppChild::DivertToParent(nsIDivertableChannel *divertable, nsIRequest *request)
97 {
98 mozilla::net::ChannelDiverterChild *diverter = nullptr;
99 nsresult rv = divertable->DivertToParent(&diverter);
100 if (NS_WARN_IF(NS_FAILED(rv))) {
101 return rv;
102 }
104 MOZ_ASSERT(diverter);
105 if (SendDivertToParentUsing(diverter)) {
106 mHandler->DidDivertRequest(request);
107 mHandler = nullptr;
108 return NS_OK;
109 }
111 return NS_ERROR_FAILURE;
112 }
114 bool
115 ExternalHelperAppChild::RecvCancel(const nsresult& aStatus)
116 {
117 mStatus = aStatus;
118 return true;
119 }
121 } // namespace dom
122 } // namespace mozilla