1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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/. */
6 
7 #include "BlobURLChannel.h"
8 #include "mozilla/dom/BlobImpl.h"
9 #include "mozilla/dom/BlobURL.h"
10 #include "mozilla/dom/BlobURLInputStream.h"
11 
12 using namespace mozilla::dom;
13 
BlobURLChannel(nsIURI * aURI,nsILoadInfo * aLoadInfo)14 BlobURLChannel::BlobURLChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo)
15     : mContentStreamOpened(false) {
16   SetURI(aURI);
17   SetOriginalURI(aURI);
18   SetLoadInfo(aLoadInfo);
19 
20   // If we're sandboxed, make sure to clear any owner the channel
21   // might already have.
22   if (aLoadInfo && aLoadInfo->GetLoadingSandboxed()) {
23     SetOwner(nullptr);
24   }
25 }
26 
27 BlobURLChannel::~BlobURLChannel() = default;
28 
OpenContentStream(bool aAsync,nsIInputStream ** aResult,nsIChannel ** aChannel)29 nsresult BlobURLChannel::OpenContentStream(bool aAsync,
30                                            nsIInputStream** aResult,
31                                            nsIChannel** aChannel) {
32   if (mContentStreamOpened) {
33     return NS_ERROR_ALREADY_OPENED;
34   }
35 
36   mContentStreamOpened = true;
37 
38   nsCOMPtr<nsIURI> uri;
39   nsresult rv = GetURI(getter_AddRefs(uri));
40   NS_ENSURE_SUCCESS(rv, NS_ERROR_MALFORMED_URI);
41 
42   RefPtr<BlobURL> blobURL;
43   rv = uri->QueryInterface(kHOSTOBJECTURICID, getter_AddRefs(blobURL));
44 
45   if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!blobURL)) {
46     return NS_ERROR_MALFORMED_URI;
47   }
48 
49   if (blobURL->Revoked()) {
50 #ifdef MOZ_WIDGET_ANDROID
51     nsCOMPtr<nsILoadInfo> loadInfo;
52     GetLoadInfo(getter_AddRefs(loadInfo));
53     // if the channel was not triggered by the system principal,
54     // then we return here because the URL had been revoked
55     if (loadInfo && !loadInfo->TriggeringPrincipal()->IsSystemPrincipal()) {
56       return NS_ERROR_MALFORMED_URI;
57     }
58 #else
59     return NS_ERROR_MALFORMED_URI;
60 #endif
61   }
62 
63   nsCOMPtr<nsIInputStream> inputStream =
64       BlobURLInputStream::Create(this, blobURL);
65   if (NS_WARN_IF(!inputStream)) {
66     return NS_ERROR_MALFORMED_URI;
67   }
68 
69   EnableSynthesizedProgressEvents(true);
70 
71   inputStream.forget(aResult);
72 
73   return NS_OK;
74 }
75