/src/mozilla-central/ipc/glue/InputStreamUtils.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 "InputStreamUtils.h" |
8 | | |
9 | | #include "nsIIPCSerializableInputStream.h" |
10 | | |
11 | | #include "mozilla/Assertions.h" |
12 | | #include "mozilla/dom/File.h" |
13 | | #include "mozilla/dom/ipc/IPCBlobInputStream.h" |
14 | | #include "mozilla/dom/ipc/IPCBlobInputStreamStorage.h" |
15 | | #include "mozilla/SlicedInputStream.h" |
16 | | #include "mozilla/InputStreamLengthWrapper.h" |
17 | | #include "nsComponentManagerUtils.h" |
18 | | #include "nsDebug.h" |
19 | | #include "nsID.h" |
20 | | #include "nsIXULRuntime.h" |
21 | | #include "nsMIMEInputStream.h" |
22 | | #include "nsMultiplexInputStream.h" |
23 | | #include "nsNetCID.h" |
24 | | #include "nsStringStream.h" |
25 | | #include "nsXULAppAPI.h" |
26 | | |
27 | | using namespace mozilla::dom; |
28 | | |
29 | | namespace { |
30 | | |
31 | | NS_DEFINE_CID(kStringInputStreamCID, NS_STRINGINPUTSTREAM_CID); |
32 | | NS_DEFINE_CID(kFileInputStreamCID, NS_LOCALFILEINPUTSTREAM_CID); |
33 | | NS_DEFINE_CID(kBufferedInputStreamCID, NS_BUFFEREDINPUTSTREAM_CID); |
34 | | NS_DEFINE_CID(kMIMEInputStreamCID, NS_MIMEINPUTSTREAM_CID); |
35 | | NS_DEFINE_CID(kMultiplexInputStreamCID, NS_MULTIPLEXINPUTSTREAM_CID); |
36 | | |
37 | | } // namespace |
38 | | |
39 | | namespace mozilla { |
40 | | namespace ipc { |
41 | | |
42 | | void |
43 | | InputStreamHelper::SerializeInputStream(nsIInputStream* aInputStream, |
44 | | InputStreamParams& aParams, |
45 | | nsTArray<FileDescriptor>& aFileDescriptors) |
46 | 0 | { |
47 | 0 | MOZ_ASSERT(aInputStream); |
48 | 0 |
|
49 | 0 | nsCOMPtr<nsIIPCSerializableInputStream> serializable = |
50 | 0 | do_QueryInterface(aInputStream); |
51 | 0 | if (!serializable) { |
52 | 0 | MOZ_CRASH("Input stream is not serializable!"); |
53 | 0 | } |
54 | 0 |
|
55 | 0 | serializable->Serialize(aParams, aFileDescriptors); |
56 | 0 |
|
57 | 0 | if (aParams.type() == InputStreamParams::T__None) { |
58 | 0 | MOZ_CRASH("Serialize failed!"); |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | already_AddRefed<nsIInputStream> |
63 | | InputStreamHelper::DeserializeInputStream(const InputStreamParams& aParams, |
64 | | const nsTArray<FileDescriptor>& aFileDescriptors) |
65 | 0 | { |
66 | 0 | nsCOMPtr<nsIInputStream> stream; |
67 | 0 | nsCOMPtr<nsIIPCSerializableInputStream> serializable; |
68 | 0 |
|
69 | 0 | // IPCBlobInputStreams are not deserializable on the parent side. |
70 | 0 | if (aParams.type() == InputStreamParams::TIPCBlobInputStreamParams) { |
71 | 0 | MOZ_ASSERT(XRE_IsParentProcess()); |
72 | 0 | IPCBlobInputStreamStorage::Get()->GetStream(aParams.get_IPCBlobInputStreamParams().id(), |
73 | 0 | aParams.get_IPCBlobInputStreamParams().start(), |
74 | 0 | aParams.get_IPCBlobInputStreamParams().length(), |
75 | 0 | getter_AddRefs(stream)); |
76 | 0 | return stream.forget(); |
77 | 0 | } |
78 | 0 |
|
79 | 0 | switch (aParams.type()) { |
80 | 0 | case InputStreamParams::TStringInputStreamParams: |
81 | 0 | serializable = do_CreateInstance(kStringInputStreamCID); |
82 | 0 | break; |
83 | 0 |
|
84 | 0 | case InputStreamParams::TFileInputStreamParams: |
85 | 0 | serializable = do_CreateInstance(kFileInputStreamCID); |
86 | 0 | break; |
87 | 0 |
|
88 | 0 | case InputStreamParams::TBufferedInputStreamParams: |
89 | 0 | serializable = do_CreateInstance(kBufferedInputStreamCID); |
90 | 0 | break; |
91 | 0 |
|
92 | 0 | case InputStreamParams::TMIMEInputStreamParams: |
93 | 0 | serializable = do_CreateInstance(kMIMEInputStreamCID); |
94 | 0 | break; |
95 | 0 |
|
96 | 0 | case InputStreamParams::TMultiplexInputStreamParams: |
97 | 0 | serializable = do_CreateInstance(kMultiplexInputStreamCID); |
98 | 0 | break; |
99 | 0 |
|
100 | 0 | case InputStreamParams::TSlicedInputStreamParams: |
101 | 0 | serializable = new mozilla::SlicedInputStream(); |
102 | 0 | break; |
103 | 0 |
|
104 | 0 | case InputStreamParams::TInputStreamLengthWrapperParams: |
105 | 0 | serializable = new mozilla::InputStreamLengthWrapper(); |
106 | 0 | break; |
107 | 0 |
|
108 | 0 | default: |
109 | 0 | MOZ_ASSERT(false, "Unknown params!"); |
110 | 0 | return nullptr; |
111 | 0 | } |
112 | 0 |
|
113 | 0 | MOZ_ASSERT(serializable); |
114 | 0 |
|
115 | 0 | if (!serializable->Deserialize(aParams, aFileDescriptors)) { |
116 | 0 | MOZ_ASSERT(false, "Deserialize failed!"); |
117 | 0 | return nullptr; |
118 | 0 | } |
119 | 0 |
|
120 | 0 | stream = do_QueryInterface(serializable); |
121 | 0 | MOZ_ASSERT(stream); |
122 | 0 |
|
123 | 0 | return stream.forget(); |
124 | 0 | } |
125 | | |
126 | | } // namespace ipc |
127 | | } // namespace mozilla |