netwerk/protocol/wyciwyg/WyciwygChannelParent.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "nsWyciwyg.h"
michael@0 6
michael@0 7 #include "mozilla/net/WyciwygChannelParent.h"
michael@0 8 #include "nsWyciwygChannel.h"
michael@0 9 #include "nsNetUtil.h"
michael@0 10 #include "nsCharsetSource.h"
michael@0 11 #include "nsISerializable.h"
michael@0 12 #include "nsSerializationHelper.h"
michael@0 13 #include "mozilla/ipc/URIUtils.h"
michael@0 14 #include "mozilla/net/NeckoParent.h"
michael@0 15 #include "SerializedLoadContext.h"
michael@0 16
michael@0 17 using namespace mozilla::ipc;
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20 namespace net {
michael@0 21
michael@0 22 WyciwygChannelParent::WyciwygChannelParent()
michael@0 23 : mIPCClosed(false)
michael@0 24 , mReceivedAppData(false)
michael@0 25 {
michael@0 26 #if defined(PR_LOGGING)
michael@0 27 if (!gWyciwygLog)
michael@0 28 gWyciwygLog = PR_NewLogModule("nsWyciwygChannel");
michael@0 29 #endif
michael@0 30 }
michael@0 31
michael@0 32 WyciwygChannelParent::~WyciwygChannelParent()
michael@0 33 {
michael@0 34 }
michael@0 35
michael@0 36 void
michael@0 37 WyciwygChannelParent::ActorDestroy(ActorDestroyReason why)
michael@0 38 {
michael@0 39 // We may still have refcount>0 if the channel hasn't called OnStopRequest
michael@0 40 // yet, but we must not send any more msgs to child.
michael@0 41 mIPCClosed = true;
michael@0 42
michael@0 43 // We need to force the cycle to break here
michael@0 44 mChannel->SetNotificationCallbacks(nullptr);
michael@0 45 }
michael@0 46
michael@0 47 //-----------------------------------------------------------------------------
michael@0 48 // WyciwygChannelParent::nsISupports
michael@0 49 //-----------------------------------------------------------------------------
michael@0 50
michael@0 51 NS_IMPL_ISUPPORTS(WyciwygChannelParent,
michael@0 52 nsIStreamListener,
michael@0 53 nsIInterfaceRequestor,
michael@0 54 nsIRequestObserver)
michael@0 55
michael@0 56 //-----------------------------------------------------------------------------
michael@0 57 // WyciwygChannelParent::PWyciwygChannelParent
michael@0 58 //-----------------------------------------------------------------------------
michael@0 59
michael@0 60 bool
michael@0 61 WyciwygChannelParent::RecvInit(const URIParams& aURI)
michael@0 62 {
michael@0 63 nsresult rv;
michael@0 64
michael@0 65 nsCOMPtr<nsIURI> uri = DeserializeURI(aURI);
michael@0 66 if (!uri)
michael@0 67 return false;
michael@0 68
michael@0 69 nsCString uriSpec;
michael@0 70 uri->GetSpec(uriSpec);
michael@0 71 LOG(("WyciwygChannelParent RecvInit [this=%p uri=%s]\n",
michael@0 72 this, uriSpec.get()));
michael@0 73
michael@0 74 nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv));
michael@0 75 if (NS_FAILED(rv))
michael@0 76 return SendCancelEarly(rv);
michael@0 77
michael@0 78 nsCOMPtr<nsIChannel> chan;
michael@0 79 rv = NS_NewChannel(getter_AddRefs(chan), uri, ios);
michael@0 80 if (NS_FAILED(rv))
michael@0 81 return SendCancelEarly(rv);
michael@0 82
michael@0 83 mChannel = do_QueryInterface(chan, &rv);
michael@0 84 if (NS_FAILED(rv))
michael@0 85 return SendCancelEarly(rv);
michael@0 86
michael@0 87 return true;
michael@0 88 }
michael@0 89
michael@0 90 bool
michael@0 91 WyciwygChannelParent::RecvAppData(const IPC::SerializedLoadContext& loadContext,
michael@0 92 PBrowserParent* parent)
michael@0 93 {
michael@0 94 LOG(("WyciwygChannelParent RecvAppData [this=%p]\n", this));
michael@0 95
michael@0 96 if (!SetupAppData(loadContext, parent))
michael@0 97 return false;
michael@0 98
michael@0 99 mChannel->SetNotificationCallbacks(this);
michael@0 100 return true;
michael@0 101 }
michael@0 102
michael@0 103 bool
michael@0 104 WyciwygChannelParent::SetupAppData(const IPC::SerializedLoadContext& loadContext,
michael@0 105 PBrowserParent* aParent)
michael@0 106 {
michael@0 107 if (!mChannel)
michael@0 108 return true;
michael@0 109
michael@0 110 const char* error = NeckoParent::CreateChannelLoadContext(aParent,
michael@0 111 Manager()->Manager(),
michael@0 112 loadContext,
michael@0 113 mLoadContext);
michael@0 114 if (error) {
michael@0 115 printf_stderr("WyciwygChannelParent::SetupAppData: FATAL ERROR: %s\n",
michael@0 116 error);
michael@0 117 return false;
michael@0 118 }
michael@0 119
michael@0 120 if (!mLoadContext && loadContext.IsPrivateBitValid()) {
michael@0 121 nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(mChannel);
michael@0 122 if (pbChannel)
michael@0 123 pbChannel->SetPrivate(loadContext.mUsePrivateBrowsing);
michael@0 124 }
michael@0 125
michael@0 126 mReceivedAppData = true;
michael@0 127 return true;
michael@0 128 }
michael@0 129
michael@0 130 bool
michael@0 131 WyciwygChannelParent::RecvAsyncOpen(const URIParams& aOriginal,
michael@0 132 const uint32_t& aLoadFlags,
michael@0 133 const IPC::SerializedLoadContext& loadContext,
michael@0 134 PBrowserParent* aParent)
michael@0 135 {
michael@0 136 nsCOMPtr<nsIURI> original = DeserializeURI(aOriginal);
michael@0 137 if (!original)
michael@0 138 return false;
michael@0 139
michael@0 140 LOG(("WyciwygChannelParent RecvAsyncOpen [this=%p]\n", this));
michael@0 141
michael@0 142 if (!mChannel)
michael@0 143 return true;
michael@0 144
michael@0 145 nsresult rv;
michael@0 146
michael@0 147 rv = mChannel->SetOriginalURI(original);
michael@0 148 if (NS_FAILED(rv))
michael@0 149 return SendCancelEarly(rv);
michael@0 150
michael@0 151 rv = mChannel->SetLoadFlags(aLoadFlags);
michael@0 152 if (NS_FAILED(rv))
michael@0 153 return SendCancelEarly(rv);
michael@0 154
michael@0 155 if (!mReceivedAppData && !SetupAppData(loadContext, aParent)) {
michael@0 156 return false;
michael@0 157 }
michael@0 158
michael@0 159 rv = mChannel->SetNotificationCallbacks(this);
michael@0 160 if (NS_FAILED(rv))
michael@0 161 return SendCancelEarly(rv);
michael@0 162
michael@0 163 rv = mChannel->AsyncOpen(this, nullptr);
michael@0 164 if (NS_FAILED(rv))
michael@0 165 return SendCancelEarly(rv);
michael@0 166
michael@0 167 return true;
michael@0 168 }
michael@0 169
michael@0 170 bool
michael@0 171 WyciwygChannelParent::RecvWriteToCacheEntry(const nsString& data)
michael@0 172 {
michael@0 173 if (!mReceivedAppData) {
michael@0 174 printf_stderr("WyciwygChannelParent::RecvWriteToCacheEntry: FATAL ERROR: didn't receive app data\n");
michael@0 175 return false;
michael@0 176 }
michael@0 177
michael@0 178 if (mChannel)
michael@0 179 mChannel->WriteToCacheEntry(data);
michael@0 180
michael@0 181 return true;
michael@0 182 }
michael@0 183
michael@0 184 bool
michael@0 185 WyciwygChannelParent::RecvCloseCacheEntry(const nsresult& reason)
michael@0 186 {
michael@0 187 if (mChannel) {
michael@0 188 mChannel->CloseCacheEntry(reason);
michael@0 189 }
michael@0 190
michael@0 191 return true;
michael@0 192 }
michael@0 193
michael@0 194 bool
michael@0 195 WyciwygChannelParent::RecvSetCharsetAndSource(const int32_t& aCharsetSource,
michael@0 196 const nsCString& aCharset)
michael@0 197 {
michael@0 198 if (mChannel)
michael@0 199 mChannel->SetCharsetAndSource(aCharsetSource, aCharset);
michael@0 200
michael@0 201 return true;
michael@0 202 }
michael@0 203
michael@0 204 bool
michael@0 205 WyciwygChannelParent::RecvSetSecurityInfo(const nsCString& aSecurityInfo)
michael@0 206 {
michael@0 207 if (mChannel) {
michael@0 208 nsCOMPtr<nsISupports> securityInfo;
michael@0 209 NS_DeserializeObject(aSecurityInfo, getter_AddRefs(securityInfo));
michael@0 210 mChannel->SetSecurityInfo(securityInfo);
michael@0 211 }
michael@0 212
michael@0 213 return true;
michael@0 214 }
michael@0 215
michael@0 216 bool
michael@0 217 WyciwygChannelParent::RecvCancel(const nsresult& aStatusCode)
michael@0 218 {
michael@0 219 if (mChannel)
michael@0 220 mChannel->Cancel(aStatusCode);
michael@0 221 return true;
michael@0 222 }
michael@0 223
michael@0 224 //-----------------------------------------------------------------------------
michael@0 225 // WyciwygChannelParent::nsIRequestObserver
michael@0 226 //-----------------------------------------------------------------------------
michael@0 227
michael@0 228 NS_IMETHODIMP
michael@0 229 WyciwygChannelParent::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
michael@0 230 {
michael@0 231 LOG(("WyciwygChannelParent::OnStartRequest [this=%p]\n", this));
michael@0 232
michael@0 233 nsresult rv;
michael@0 234
michael@0 235 nsCOMPtr<nsIWyciwygChannel> chan = do_QueryInterface(aRequest, &rv);
michael@0 236 NS_ENSURE_SUCCESS(rv, rv);
michael@0 237
michael@0 238 nsresult status;
michael@0 239 chan->GetStatus(&status);
michael@0 240
michael@0 241 int64_t contentLength = -1;
michael@0 242 chan->GetContentLength(&contentLength);
michael@0 243
michael@0 244 int32_t charsetSource = kCharsetUninitialized;
michael@0 245 nsAutoCString charset;
michael@0 246 chan->GetCharsetAndSource(&charsetSource, charset);
michael@0 247
michael@0 248 nsCOMPtr<nsISupports> securityInfo;
michael@0 249 chan->GetSecurityInfo(getter_AddRefs(securityInfo));
michael@0 250 nsCString secInfoStr;
michael@0 251 if (securityInfo) {
michael@0 252 nsCOMPtr<nsISerializable> serializable = do_QueryInterface(securityInfo);
michael@0 253 if (serializable)
michael@0 254 NS_SerializeToString(serializable, secInfoStr);
michael@0 255 else {
michael@0 256 NS_ERROR("Can't serialize security info");
michael@0 257 return NS_ERROR_UNEXPECTED;
michael@0 258 }
michael@0 259 }
michael@0 260
michael@0 261 if (mIPCClosed ||
michael@0 262 !SendOnStartRequest(status, contentLength, charsetSource, charset, secInfoStr)) {
michael@0 263 return NS_ERROR_UNEXPECTED;
michael@0 264 }
michael@0 265
michael@0 266 return NS_OK;
michael@0 267 }
michael@0 268
michael@0 269 NS_IMETHODIMP
michael@0 270 WyciwygChannelParent::OnStopRequest(nsIRequest *aRequest,
michael@0 271 nsISupports *aContext,
michael@0 272 nsresult aStatusCode)
michael@0 273 {
michael@0 274 LOG(("WyciwygChannelParent::OnStopRequest: [this=%p status=%ul]\n",
michael@0 275 this, aStatusCode));
michael@0 276
michael@0 277 if (mIPCClosed || !SendOnStopRequest(aStatusCode)) {
michael@0 278 return NS_ERROR_UNEXPECTED;
michael@0 279 }
michael@0 280
michael@0 281 return NS_OK;
michael@0 282 }
michael@0 283
michael@0 284 //-----------------------------------------------------------------------------
michael@0 285 // WyciwygChannelParent::nsIStreamListener
michael@0 286 //-----------------------------------------------------------------------------
michael@0 287
michael@0 288 NS_IMETHODIMP
michael@0 289 WyciwygChannelParent::OnDataAvailable(nsIRequest *aRequest,
michael@0 290 nsISupports *aContext,
michael@0 291 nsIInputStream *aInputStream,
michael@0 292 uint64_t aOffset,
michael@0 293 uint32_t aCount)
michael@0 294 {
michael@0 295 LOG(("WyciwygChannelParent::OnDataAvailable [this=%p]\n", this));
michael@0 296
michael@0 297 nsCString data;
michael@0 298 nsresult rv = NS_ReadInputStreamToString(aInputStream, data, aCount);
michael@0 299 if (NS_FAILED(rv))
michael@0 300 return rv;
michael@0 301
michael@0 302 if (mIPCClosed || !SendOnDataAvailable(data, aOffset)) {
michael@0 303 return NS_ERROR_UNEXPECTED;
michael@0 304 }
michael@0 305
michael@0 306 return NS_OK;
michael@0 307 }
michael@0 308
michael@0 309 //-----------------------------------------------------------------------------
michael@0 310 // WyciwygChannelParent::nsIInterfaceRequestor
michael@0 311 //-----------------------------------------------------------------------------
michael@0 312
michael@0 313 NS_IMETHODIMP
michael@0 314 WyciwygChannelParent::GetInterface(const nsIID& uuid, void** result)
michael@0 315 {
michael@0 316 // Only support nsILoadContext if child channel's callbacks did too
michael@0 317 if (uuid.Equals(NS_GET_IID(nsILoadContext)) && mLoadContext) {
michael@0 318 NS_ADDREF(mLoadContext);
michael@0 319 *result = static_cast<nsILoadContext*>(mLoadContext);
michael@0 320 return NS_OK;
michael@0 321 }
michael@0 322
michael@0 323 return QueryInterface(uuid, result);
michael@0 324 }
michael@0 325
michael@0 326
michael@0 327 }} // mozilla::net

mercurial