uriloader/exthandler/ExternalHelperAppParent.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "mozilla/DebugOnly.h"
michael@0 8
michael@0 9 #include "ExternalHelperAppParent.h"
michael@0 10 #include "nsIContent.h"
michael@0 11 #include "nsCExternalHandlerService.h"
michael@0 12 #include "nsIExternalHelperAppService.h"
michael@0 13 #include "mozilla/dom/ContentParent.h"
michael@0 14 #include "mozilla/dom/Element.h"
michael@0 15 #include "mozilla/dom/TabParent.h"
michael@0 16 #include "nsIBrowserDOMWindow.h"
michael@0 17 #include "nsStringStream.h"
michael@0 18 #include "mozilla/ipc/URIUtils.h"
michael@0 19 #include "nsNetUtil.h"
michael@0 20 #include "nsIDocument.h"
michael@0 21 #include "mozilla/net/ChannelDiverterParent.h"
michael@0 22
michael@0 23 #include "mozilla/unused.h"
michael@0 24
michael@0 25 using namespace mozilla::ipc;
michael@0 26
michael@0 27 namespace mozilla {
michael@0 28 namespace dom {
michael@0 29
michael@0 30 NS_IMPL_ISUPPORTS_INHERITED(ExternalHelperAppParent,
michael@0 31 nsHashPropertyBag,
michael@0 32 nsIRequest,
michael@0 33 nsIChannel,
michael@0 34 nsIMultiPartChannel,
michael@0 35 nsIResumableChannel,
michael@0 36 nsIStreamListener)
michael@0 37
michael@0 38 ExternalHelperAppParent::ExternalHelperAppParent(
michael@0 39 const OptionalURIParams& uri,
michael@0 40 const int64_t& aContentLength)
michael@0 41 : mURI(DeserializeURI(uri))
michael@0 42 , mPending(false)
michael@0 43 , mDiverted(false)
michael@0 44 , mIPCClosed(false)
michael@0 45 , mLoadFlags(0)
michael@0 46 , mStatus(NS_OK)
michael@0 47 , mContentLength(aContentLength)
michael@0 48 {
michael@0 49 }
michael@0 50
michael@0 51 void
michael@0 52 ExternalHelperAppParent::Init(ContentParent *parent,
michael@0 53 const nsCString& aMimeContentType,
michael@0 54 const nsCString& aContentDispositionHeader,
michael@0 55 const uint32_t& aContentDispositionHint,
michael@0 56 const nsString& aContentDispositionFilename,
michael@0 57 const bool& aForceSave,
michael@0 58 const OptionalURIParams& aReferrer,
michael@0 59 PBrowserParent* aBrowser)
michael@0 60 {
michael@0 61 nsCOMPtr<nsIExternalHelperAppService> helperAppService =
michael@0 62 do_GetService(NS_EXTERNALHELPERAPPSERVICE_CONTRACTID);
michael@0 63 NS_ASSERTION(helperAppService, "No Helper App Service!");
michael@0 64
michael@0 65 nsCOMPtr<nsIURI> referrer = DeserializeURI(aReferrer);
michael@0 66 if (referrer)
michael@0 67 SetPropertyAsInterface(NS_LITERAL_STRING("docshell.internalReferrer"), referrer);
michael@0 68
michael@0 69 mContentDispositionHeader = aContentDispositionHeader;
michael@0 70 if (!mContentDispositionHeader.IsEmpty()) {
michael@0 71 NS_GetFilenameFromDisposition(mContentDispositionFilename,
michael@0 72 mContentDispositionHeader,
michael@0 73 mURI);
michael@0 74 mContentDisposition =
michael@0 75 NS_GetContentDispositionFromHeader(mContentDispositionHeader, this);
michael@0 76 }
michael@0 77 else {
michael@0 78 mContentDisposition = aContentDispositionHint;
michael@0 79 mContentDispositionFilename = aContentDispositionFilename;
michael@0 80 }
michael@0 81
michael@0 82 nsCOMPtr<nsIInterfaceRequestor> window;
michael@0 83 if (aBrowser) {
michael@0 84 TabParent* tabParent = static_cast<TabParent*>(aBrowser);
michael@0 85 if (tabParent->GetOwnerElement())
michael@0 86 window = do_QueryInterface(tabParent->GetOwnerElement()->OwnerDoc()->GetWindow());
michael@0 87 }
michael@0 88
michael@0 89 helperAppService->DoContent(aMimeContentType, this, window,
michael@0 90 aForceSave, getter_AddRefs(mListener));
michael@0 91 }
michael@0 92
michael@0 93 void
michael@0 94 ExternalHelperAppParent::ActorDestroy(ActorDestroyReason why)
michael@0 95 {
michael@0 96 mIPCClosed = true;
michael@0 97 }
michael@0 98
michael@0 99 void
michael@0 100 ExternalHelperAppParent::Delete()
michael@0 101 {
michael@0 102 if (!mIPCClosed) {
michael@0 103 unused << Send__delete__(this);
michael@0 104 }
michael@0 105 }
michael@0 106
michael@0 107 bool
michael@0 108 ExternalHelperAppParent::RecvOnStartRequest(const nsCString& entityID)
michael@0 109 {
michael@0 110 MOZ_ASSERT(!mDiverted, "child forwarding callbacks after request was diverted");
michael@0 111
michael@0 112 mEntityID = entityID;
michael@0 113 mPending = true;
michael@0 114 mStatus = mListener->OnStartRequest(this, nullptr);
michael@0 115 return true;
michael@0 116 }
michael@0 117
michael@0 118 bool
michael@0 119 ExternalHelperAppParent::RecvOnDataAvailable(const nsCString& data,
michael@0 120 const uint64_t& offset,
michael@0 121 const uint32_t& count)
michael@0 122 {
michael@0 123 if (NS_FAILED(mStatus))
michael@0 124 return true;
michael@0 125
michael@0 126 MOZ_ASSERT(!mDiverted, "child forwarding callbacks after request was diverted");
michael@0 127 MOZ_ASSERT(mPending, "must be pending!");
michael@0 128
michael@0 129 nsCOMPtr<nsIInputStream> stringStream;
michael@0 130 DebugOnly<nsresult> rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data.get(), count, NS_ASSIGNMENT_DEPEND);
michael@0 131 NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create dependent string!");
michael@0 132 mStatus = mListener->OnDataAvailable(this, nullptr, stringStream, offset, count);
michael@0 133
michael@0 134 return true;
michael@0 135 }
michael@0 136
michael@0 137 bool
michael@0 138 ExternalHelperAppParent::RecvOnStopRequest(const nsresult& code)
michael@0 139 {
michael@0 140 MOZ_ASSERT(!mDiverted, "child forwarding callbacks after request was diverted");
michael@0 141
michael@0 142 mPending = false;
michael@0 143 mListener->OnStopRequest(this, nullptr,
michael@0 144 (NS_SUCCEEDED(code) && NS_FAILED(mStatus)) ? mStatus : code);
michael@0 145 Delete();
michael@0 146 return true;
michael@0 147 }
michael@0 148
michael@0 149 bool
michael@0 150 ExternalHelperAppParent::RecvDivertToParentUsing(PChannelDiverterParent* diverter)
michael@0 151 {
michael@0 152 MOZ_ASSERT(diverter);
michael@0 153 auto p = static_cast<mozilla::net::ChannelDiverterParent*>(diverter);
michael@0 154 p->DivertTo(this);
michael@0 155 mDiverted = true;
michael@0 156 unused << p->Send__delete__(p);
michael@0 157 return true;
michael@0 158 }
michael@0 159
michael@0 160 //
michael@0 161 // nsIStreamListener
michael@0 162 //
michael@0 163
michael@0 164 NS_IMETHODIMP
michael@0 165 ExternalHelperAppParent::OnDataAvailable(nsIRequest *request,
michael@0 166 nsISupports *ctx,
michael@0 167 nsIInputStream *input,
michael@0 168 uint64_t offset,
michael@0 169 uint32_t count)
michael@0 170 {
michael@0 171 MOZ_ASSERT(mDiverted);
michael@0 172 return mListener->OnDataAvailable(request, ctx, input, offset, count);
michael@0 173 }
michael@0 174
michael@0 175 NS_IMETHODIMP
michael@0 176 ExternalHelperAppParent::OnStartRequest(nsIRequest *request, nsISupports *ctx)
michael@0 177 {
michael@0 178 MOZ_ASSERT(mDiverted);
michael@0 179 return mListener->OnStartRequest(request, ctx);
michael@0 180 }
michael@0 181
michael@0 182 NS_IMETHODIMP
michael@0 183 ExternalHelperAppParent::OnStopRequest(nsIRequest *request,
michael@0 184 nsISupports *ctx,
michael@0 185 nsresult status)
michael@0 186 {
michael@0 187 MOZ_ASSERT(mDiverted);
michael@0 188 nsresult rv = mListener->OnStopRequest(request, ctx, status);
michael@0 189 Delete();
michael@0 190 return rv;
michael@0 191 }
michael@0 192
michael@0 193 ExternalHelperAppParent::~ExternalHelperAppParent()
michael@0 194 {
michael@0 195 }
michael@0 196
michael@0 197 //
michael@0 198 // nsIRequest implementation...
michael@0 199 //
michael@0 200
michael@0 201 NS_IMETHODIMP
michael@0 202 ExternalHelperAppParent::GetName(nsACString& aResult)
michael@0 203 {
michael@0 204 if (!mURI) {
michael@0 205 aResult.Truncate();
michael@0 206 return NS_ERROR_NOT_AVAILABLE;
michael@0 207 }
michael@0 208 mURI->GetAsciiSpec(aResult);
michael@0 209 return NS_OK;
michael@0 210 }
michael@0 211
michael@0 212 NS_IMETHODIMP
michael@0 213 ExternalHelperAppParent::IsPending(bool *aResult)
michael@0 214 {
michael@0 215 *aResult = mPending;
michael@0 216 return NS_OK;
michael@0 217 }
michael@0 218
michael@0 219 NS_IMETHODIMP
michael@0 220 ExternalHelperAppParent::GetStatus(nsresult *aResult)
michael@0 221 {
michael@0 222 *aResult = mStatus;
michael@0 223 return NS_OK;
michael@0 224 }
michael@0 225
michael@0 226 NS_IMETHODIMP
michael@0 227 ExternalHelperAppParent::Cancel(nsresult aStatus)
michael@0 228 {
michael@0 229 mStatus = aStatus;
michael@0 230 unused << SendCancel(aStatus);
michael@0 231 return NS_OK;
michael@0 232 }
michael@0 233
michael@0 234 NS_IMETHODIMP
michael@0 235 ExternalHelperAppParent::Suspend()
michael@0 236 {
michael@0 237 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 238 }
michael@0 239
michael@0 240 NS_IMETHODIMP
michael@0 241 ExternalHelperAppParent::Resume()
michael@0 242 {
michael@0 243 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 244 }
michael@0 245
michael@0 246 //
michael@0 247 // nsIChannel implementation
michael@0 248 //
michael@0 249
michael@0 250 NS_IMETHODIMP
michael@0 251 ExternalHelperAppParent::GetOriginalURI(nsIURI * *aURI)
michael@0 252 {
michael@0 253 NS_IF_ADDREF(*aURI = mURI);
michael@0 254 return NS_OK;
michael@0 255 }
michael@0 256
michael@0 257 NS_IMETHODIMP
michael@0 258 ExternalHelperAppParent::SetOriginalURI(nsIURI *aURI)
michael@0 259 {
michael@0 260 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 261 }
michael@0 262
michael@0 263 NS_IMETHODIMP
michael@0 264 ExternalHelperAppParent::GetURI(nsIURI **aURI)
michael@0 265 {
michael@0 266 NS_IF_ADDREF(*aURI = mURI);
michael@0 267 return NS_OK;
michael@0 268 }
michael@0 269
michael@0 270 NS_IMETHODIMP
michael@0 271 ExternalHelperAppParent::Open(nsIInputStream **aResult)
michael@0 272 {
michael@0 273 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 274 }
michael@0 275
michael@0 276 NS_IMETHODIMP
michael@0 277 ExternalHelperAppParent::AsyncOpen(nsIStreamListener *aListener,
michael@0 278 nsISupports *aContext)
michael@0 279 {
michael@0 280 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 281 }
michael@0 282
michael@0 283
michael@0 284 NS_IMETHODIMP
michael@0 285 ExternalHelperAppParent::GetLoadFlags(nsLoadFlags *aLoadFlags)
michael@0 286 {
michael@0 287 *aLoadFlags = mLoadFlags;
michael@0 288 return NS_OK;
michael@0 289 }
michael@0 290
michael@0 291 NS_IMETHODIMP
michael@0 292 ExternalHelperAppParent::SetLoadFlags(nsLoadFlags aLoadFlags)
michael@0 293 {
michael@0 294 mLoadFlags = aLoadFlags;
michael@0 295 return NS_OK;
michael@0 296 }
michael@0 297
michael@0 298 NS_IMETHODIMP
michael@0 299 ExternalHelperAppParent::GetLoadGroup(nsILoadGroup* *aLoadGroup)
michael@0 300 {
michael@0 301 *aLoadGroup = nullptr;
michael@0 302 return NS_OK;
michael@0 303 }
michael@0 304
michael@0 305 NS_IMETHODIMP
michael@0 306 ExternalHelperAppParent::SetLoadGroup(nsILoadGroup* aLoadGroup)
michael@0 307 {
michael@0 308 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 309 }
michael@0 310
michael@0 311 NS_IMETHODIMP
michael@0 312 ExternalHelperAppParent::GetOwner(nsISupports* *aOwner)
michael@0 313 {
michael@0 314 *aOwner = nullptr;
michael@0 315 return NS_OK;
michael@0 316 }
michael@0 317
michael@0 318 NS_IMETHODIMP
michael@0 319 ExternalHelperAppParent::SetOwner(nsISupports* aOwner)
michael@0 320 {
michael@0 321 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 322 }
michael@0 323
michael@0 324 NS_IMETHODIMP
michael@0 325 ExternalHelperAppParent::GetNotificationCallbacks(nsIInterfaceRequestor* *aCallbacks)
michael@0 326 {
michael@0 327 *aCallbacks = nullptr;
michael@0 328 return NS_OK;
michael@0 329 }
michael@0 330
michael@0 331 NS_IMETHODIMP
michael@0 332 ExternalHelperAppParent::SetNotificationCallbacks(nsIInterfaceRequestor* aCallbacks)
michael@0 333 {
michael@0 334 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 335 }
michael@0 336
michael@0 337 NS_IMETHODIMP
michael@0 338 ExternalHelperAppParent::GetSecurityInfo(nsISupports * *aSecurityInfo)
michael@0 339 {
michael@0 340 *aSecurityInfo = nullptr;
michael@0 341 return NS_OK;
michael@0 342 }
michael@0 343
michael@0 344 NS_IMETHODIMP
michael@0 345 ExternalHelperAppParent::GetContentType(nsACString& aContentType)
michael@0 346 {
michael@0 347 aContentType.Truncate();
michael@0 348 return NS_OK;
michael@0 349 }
michael@0 350
michael@0 351 NS_IMETHODIMP
michael@0 352 ExternalHelperAppParent::SetContentType(const nsACString& aContentType)
michael@0 353 {
michael@0 354 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 355 }
michael@0 356
michael@0 357 NS_IMETHODIMP
michael@0 358 ExternalHelperAppParent::GetContentCharset(nsACString& aContentCharset)
michael@0 359 {
michael@0 360 aContentCharset.Truncate();
michael@0 361 return NS_OK;
michael@0 362 }
michael@0 363
michael@0 364 NS_IMETHODIMP
michael@0 365 ExternalHelperAppParent::SetContentCharset(const nsACString& aContentCharset)
michael@0 366 {
michael@0 367 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 368 }
michael@0 369
michael@0 370 NS_IMETHODIMP
michael@0 371 ExternalHelperAppParent::GetContentDisposition(uint32_t *aContentDisposition)
michael@0 372 {
michael@0 373 if (mContentDispositionHeader.IsEmpty())
michael@0 374 return NS_ERROR_NOT_AVAILABLE;
michael@0 375
michael@0 376 *aContentDisposition = mContentDisposition;
michael@0 377 return NS_OK;
michael@0 378 }
michael@0 379
michael@0 380 NS_IMETHODIMP
michael@0 381 ExternalHelperAppParent::SetContentDisposition(uint32_t aContentDisposition)
michael@0 382 {
michael@0 383 mContentDisposition = aContentDisposition;
michael@0 384 return NS_OK;
michael@0 385 }
michael@0 386
michael@0 387 NS_IMETHODIMP
michael@0 388 ExternalHelperAppParent::GetContentDispositionFilename(nsAString& aContentDispositionFilename)
michael@0 389 {
michael@0 390 if (mContentDispositionFilename.IsEmpty())
michael@0 391 return NS_ERROR_NOT_AVAILABLE;
michael@0 392
michael@0 393 aContentDispositionFilename = mContentDispositionFilename;
michael@0 394 return NS_OK;
michael@0 395 }
michael@0 396
michael@0 397 NS_IMETHODIMP
michael@0 398 ExternalHelperAppParent::SetContentDispositionFilename(const nsAString& aContentDispositionFilename)
michael@0 399 {
michael@0 400 mContentDispositionFilename = aContentDispositionFilename;
michael@0 401 return NS_OK;
michael@0 402 }
michael@0 403
michael@0 404 NS_IMETHODIMP
michael@0 405 ExternalHelperAppParent::GetContentDispositionHeader(nsACString& aContentDispositionHeader)
michael@0 406 {
michael@0 407 if (mContentDispositionHeader.IsEmpty())
michael@0 408 return NS_ERROR_NOT_AVAILABLE;
michael@0 409
michael@0 410 aContentDispositionHeader = mContentDispositionHeader;
michael@0 411 return NS_OK;
michael@0 412 }
michael@0 413
michael@0 414 NS_IMETHODIMP
michael@0 415 ExternalHelperAppParent::GetContentLength(int64_t *aContentLength)
michael@0 416 {
michael@0 417 if (mContentLength < 0)
michael@0 418 *aContentLength = -1;
michael@0 419 else
michael@0 420 *aContentLength = mContentLength;
michael@0 421 return NS_OK;
michael@0 422 }
michael@0 423
michael@0 424 NS_IMETHODIMP
michael@0 425 ExternalHelperAppParent::SetContentLength(int64_t aContentLength)
michael@0 426 {
michael@0 427 mContentLength = aContentLength;
michael@0 428 return NS_OK;
michael@0 429 }
michael@0 430
michael@0 431 //
michael@0 432 // nsIResumableChannel implementation
michael@0 433 //
michael@0 434
michael@0 435 NS_IMETHODIMP
michael@0 436 ExternalHelperAppParent::ResumeAt(uint64_t startPos, const nsACString& entityID)
michael@0 437 {
michael@0 438 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 439 }
michael@0 440
michael@0 441 NS_IMETHODIMP
michael@0 442 ExternalHelperAppParent::GetEntityID(nsACString& aEntityID)
michael@0 443 {
michael@0 444 aEntityID = mEntityID;
michael@0 445 return NS_OK;
michael@0 446 }
michael@0 447
michael@0 448 //
michael@0 449 // nsIMultiPartChannel implementation
michael@0 450 //
michael@0 451
michael@0 452 NS_IMETHODIMP
michael@0 453 ExternalHelperAppParent::GetBaseChannel(nsIChannel* *aChannel)
michael@0 454 {
michael@0 455 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 456 }
michael@0 457
michael@0 458 NS_IMETHODIMP
michael@0 459 ExternalHelperAppParent::GetPartID(uint32_t* aPartID)
michael@0 460 {
michael@0 461 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 462 }
michael@0 463
michael@0 464 NS_IMETHODIMP
michael@0 465 ExternalHelperAppParent::GetIsLastPart(bool* aIsLastPart)
michael@0 466 {
michael@0 467 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 468 }
michael@0 469
michael@0 470 } // namespace dom
michael@0 471 } // namespace mozilla

mercurial