/src/mozilla-central/dom/file/uri/BlobURLChannel.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | using namespace mozilla::dom; |
11 | | |
12 | | BlobURLChannel::BlobURLChannel(nsIURI* aURI, |
13 | | nsILoadInfo* aLoadInfo) |
14 | | : mInitialized(false) |
15 | 0 | { |
16 | 0 | SetURI(aURI); |
17 | 0 | SetOriginalURI(aURI); |
18 | 0 | SetLoadInfo(aLoadInfo); |
19 | 0 |
|
20 | 0 | // If we're sandboxed, make sure to clear any owner the channel |
21 | 0 | // might already have. |
22 | 0 | if (aLoadInfo && aLoadInfo->GetLoadingSandboxed()) { |
23 | 0 | SetOwner(nullptr); |
24 | 0 | } |
25 | 0 | } |
26 | | |
27 | 0 | BlobURLChannel::~BlobURLChannel() = default; |
28 | | |
29 | | void |
30 | | BlobURLChannel::InitFailed() |
31 | 0 | { |
32 | 0 | MOZ_ASSERT(!mInitialized); |
33 | 0 | MOZ_ASSERT(!mInputStream); |
34 | 0 | mInitialized = true; |
35 | 0 | } |
36 | | |
37 | | void |
38 | | BlobURLChannel::Initialize(BlobImpl* aBlobImpl) |
39 | 0 | { |
40 | 0 | MOZ_ASSERT(!mInitialized); |
41 | 0 |
|
42 | 0 | nsAutoString contentType; |
43 | 0 | aBlobImpl->GetType(contentType); |
44 | 0 | SetContentType(NS_ConvertUTF16toUTF8(contentType)); |
45 | 0 |
|
46 | 0 | if (aBlobImpl->IsFile()) { |
47 | 0 | nsString filename; |
48 | 0 | aBlobImpl->GetName(filename); |
49 | 0 | SetContentDispositionFilename(filename); |
50 | 0 | } |
51 | 0 |
|
52 | 0 | ErrorResult rv; |
53 | 0 | uint64_t size = aBlobImpl->GetSize(rv); |
54 | 0 | if (NS_WARN_IF(rv.Failed())) { |
55 | 0 | InitFailed(); |
56 | 0 | return; |
57 | 0 | } |
58 | 0 | |
59 | 0 | SetContentLength(size); |
60 | 0 |
|
61 | 0 | aBlobImpl->CreateInputStream(getter_AddRefs(mInputStream), rv); |
62 | 0 | if (NS_WARN_IF(rv.Failed())) { |
63 | 0 | InitFailed(); |
64 | 0 | return; |
65 | 0 | } |
66 | 0 | |
67 | 0 | MOZ_ASSERT(mInputStream); |
68 | 0 | mInitialized = true; |
69 | 0 | } |
70 | | |
71 | | nsresult |
72 | | BlobURLChannel::OpenContentStream(bool aAsync, nsIInputStream** aResult, |
73 | | nsIChannel** aChannel) |
74 | 0 | { |
75 | 0 | MOZ_ASSERT(mInitialized); |
76 | 0 |
|
77 | 0 | if (!mInputStream) { |
78 | 0 | return NS_ERROR_MALFORMED_URI; |
79 | 0 | } |
80 | 0 | |
81 | 0 | EnableSynthesizedProgressEvents(true); |
82 | 0 |
|
83 | 0 | nsCOMPtr<nsIInputStream> stream = mInputStream; |
84 | 0 | stream.forget(aResult); |
85 | 0 |
|
86 | 0 | return NS_OK; |
87 | 0 | } |
88 | | |
89 | | void |
90 | | BlobURLChannel::OnChannelDone() |
91 | 0 | { |
92 | 0 | mInputStream = nullptr; |
93 | 0 | } |