Wed, 31 Dec 2014 06:09:35 +0100
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 "InputStreamUtils.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "nsIIPCSerializableInputStream.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/Assertions.h" |
michael@0 | 10 | #include "mozilla/dom/ipc/Blob.h" |
michael@0 | 11 | #include "nsComponentManagerUtils.h" |
michael@0 | 12 | #include "nsDebug.h" |
michael@0 | 13 | #include "nsID.h" |
michael@0 | 14 | #include "nsIDOMFile.h" |
michael@0 | 15 | #include "nsIXULRuntime.h" |
michael@0 | 16 | #include "nsMIMEInputStream.h" |
michael@0 | 17 | #include "nsMultiplexInputStream.h" |
michael@0 | 18 | #include "nsNetCID.h" |
michael@0 | 19 | #include "nsStringStream.h" |
michael@0 | 20 | #include "nsThreadUtils.h" |
michael@0 | 21 | #include "nsXULAppAPI.h" |
michael@0 | 22 | |
michael@0 | 23 | using mozilla::dom::BlobChild; |
michael@0 | 24 | using mozilla::dom::BlobParent; |
michael@0 | 25 | |
michael@0 | 26 | namespace { |
michael@0 | 27 | |
michael@0 | 28 | NS_DEFINE_CID(kStringInputStreamCID, NS_STRINGINPUTSTREAM_CID); |
michael@0 | 29 | NS_DEFINE_CID(kFileInputStreamCID, NS_LOCALFILEINPUTSTREAM_CID); |
michael@0 | 30 | NS_DEFINE_CID(kPartialFileInputStreamCID, NS_PARTIALLOCALFILEINPUTSTREAM_CID); |
michael@0 | 31 | NS_DEFINE_CID(kBufferedInputStreamCID, NS_BUFFEREDINPUTSTREAM_CID); |
michael@0 | 32 | NS_DEFINE_CID(kMIMEInputStreamCID, NS_MIMEINPUTSTREAM_CID); |
michael@0 | 33 | NS_DEFINE_CID(kMultiplexInputStreamCID, NS_MULTIPLEXINPUTSTREAM_CID); |
michael@0 | 34 | |
michael@0 | 35 | } // anonymous namespace |
michael@0 | 36 | |
michael@0 | 37 | namespace mozilla { |
michael@0 | 38 | namespace ipc { |
michael@0 | 39 | |
michael@0 | 40 | void |
michael@0 | 41 | SerializeInputStream(nsIInputStream* aInputStream, |
michael@0 | 42 | InputStreamParams& aParams, |
michael@0 | 43 | nsTArray<FileDescriptor>& aFileDescriptors) |
michael@0 | 44 | { |
michael@0 | 45 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 46 | MOZ_ASSERT(aInputStream); |
michael@0 | 47 | |
michael@0 | 48 | nsCOMPtr<nsIIPCSerializableInputStream> serializable = |
michael@0 | 49 | do_QueryInterface(aInputStream); |
michael@0 | 50 | if (!serializable) { |
michael@0 | 51 | MOZ_CRASH("Input stream is not serializable!"); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | serializable->Serialize(aParams, aFileDescriptors); |
michael@0 | 55 | |
michael@0 | 56 | if (aParams.type() == InputStreamParams::T__None) { |
michael@0 | 57 | MOZ_CRASH("Serialize failed!"); |
michael@0 | 58 | } |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | void |
michael@0 | 62 | SerializeInputStream(nsIInputStream* aInputStream, |
michael@0 | 63 | OptionalInputStreamParams& aParams, |
michael@0 | 64 | nsTArray<FileDescriptor>& aFileDescriptors) |
michael@0 | 65 | { |
michael@0 | 66 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 67 | |
michael@0 | 68 | if (aInputStream) { |
michael@0 | 69 | InputStreamParams params; |
michael@0 | 70 | SerializeInputStream(aInputStream, params, aFileDescriptors); |
michael@0 | 71 | aParams = params; |
michael@0 | 72 | } |
michael@0 | 73 | else { |
michael@0 | 74 | aParams = mozilla::void_t(); |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | already_AddRefed<nsIInputStream> |
michael@0 | 79 | DeserializeInputStream(const InputStreamParams& aParams, |
michael@0 | 80 | const nsTArray<FileDescriptor>& aFileDescriptors) |
michael@0 | 81 | { |
michael@0 | 82 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 83 | |
michael@0 | 84 | nsCOMPtr<nsIIPCSerializableInputStream> serializable; |
michael@0 | 85 | |
michael@0 | 86 | switch (aParams.type()) { |
michael@0 | 87 | case InputStreamParams::TStringInputStreamParams: |
michael@0 | 88 | serializable = do_CreateInstance(kStringInputStreamCID); |
michael@0 | 89 | break; |
michael@0 | 90 | |
michael@0 | 91 | case InputStreamParams::TFileInputStreamParams: |
michael@0 | 92 | serializable = do_CreateInstance(kFileInputStreamCID); |
michael@0 | 93 | break; |
michael@0 | 94 | |
michael@0 | 95 | case InputStreamParams::TPartialFileInputStreamParams: |
michael@0 | 96 | serializable = do_CreateInstance(kPartialFileInputStreamCID); |
michael@0 | 97 | break; |
michael@0 | 98 | |
michael@0 | 99 | case InputStreamParams::TBufferedInputStreamParams: |
michael@0 | 100 | serializable = do_CreateInstance(kBufferedInputStreamCID); |
michael@0 | 101 | break; |
michael@0 | 102 | |
michael@0 | 103 | case InputStreamParams::TMIMEInputStreamParams: |
michael@0 | 104 | serializable = do_CreateInstance(kMIMEInputStreamCID); |
michael@0 | 105 | break; |
michael@0 | 106 | |
michael@0 | 107 | case InputStreamParams::TMultiplexInputStreamParams: |
michael@0 | 108 | serializable = do_CreateInstance(kMultiplexInputStreamCID); |
michael@0 | 109 | break; |
michael@0 | 110 | |
michael@0 | 111 | // When the input stream already exists in this process, all we need to do |
michael@0 | 112 | // is retrieve the original instead of sending any data over the wire. |
michael@0 | 113 | case InputStreamParams::TRemoteInputStreamParams: { |
michael@0 | 114 | nsCOMPtr<nsIDOMBlob> domBlob; |
michael@0 | 115 | const RemoteInputStreamParams& params = |
michael@0 | 116 | aParams.get_RemoteInputStreamParams(); |
michael@0 | 117 | |
michael@0 | 118 | domBlob = params.remoteBlobParent() ? |
michael@0 | 119 | static_cast<BlobParent*>(params.remoteBlobParent())->GetBlob() : |
michael@0 | 120 | static_cast<BlobChild*>(params.remoteBlobChild())->GetBlob(); |
michael@0 | 121 | |
michael@0 | 122 | MOZ_ASSERT(domBlob, "Invalid blob contents"); |
michael@0 | 123 | |
michael@0 | 124 | // If fetching the internal stream fails, we ignore it and return a |
michael@0 | 125 | // null stream. |
michael@0 | 126 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 127 | nsresult rv = domBlob->GetInternalStream(getter_AddRefs(stream)); |
michael@0 | 128 | if (NS_FAILED(rv) || !stream) { |
michael@0 | 129 | NS_WARNING("Couldn't obtain a valid stream from the blob"); |
michael@0 | 130 | } |
michael@0 | 131 | return stream.forget(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | default: |
michael@0 | 135 | MOZ_ASSERT(false, "Unknown params!"); |
michael@0 | 136 | return nullptr; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | MOZ_ASSERT(serializable); |
michael@0 | 140 | |
michael@0 | 141 | if (!serializable->Deserialize(aParams, aFileDescriptors)) { |
michael@0 | 142 | MOZ_ASSERT(false, "Deserialize failed!"); |
michael@0 | 143 | return nullptr; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | nsCOMPtr<nsIInputStream> stream = do_QueryInterface(serializable); |
michael@0 | 147 | MOZ_ASSERT(stream); |
michael@0 | 148 | |
michael@0 | 149 | return stream.forget(); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | already_AddRefed<nsIInputStream> |
michael@0 | 153 | DeserializeInputStream(const OptionalInputStreamParams& aParams, |
michael@0 | 154 | const nsTArray<FileDescriptor>& aFileDescriptors) |
michael@0 | 155 | { |
michael@0 | 156 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 157 | |
michael@0 | 158 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 159 | |
michael@0 | 160 | switch (aParams.type()) { |
michael@0 | 161 | case OptionalInputStreamParams::Tvoid_t: |
michael@0 | 162 | // Leave stream null. |
michael@0 | 163 | break; |
michael@0 | 164 | |
michael@0 | 165 | case OptionalInputStreamParams::TInputStreamParams: |
michael@0 | 166 | stream = DeserializeInputStream(aParams.get_InputStreamParams(), |
michael@0 | 167 | aFileDescriptors); |
michael@0 | 168 | break; |
michael@0 | 169 | |
michael@0 | 170 | default: |
michael@0 | 171 | MOZ_ASSERT(false, "Unknown params!"); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | return stream.forget(); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | } // namespace ipc |
michael@0 | 178 | } // namespace mozilla |