Line data Source code
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 "nsComponentManagerUtils.h"
16 : #include "nsDebug.h"
17 : #include "nsID.h"
18 : #include "nsIXULRuntime.h"
19 : #include "nsMIMEInputStream.h"
20 : #include "nsMultiplexInputStream.h"
21 : #include "nsNetCID.h"
22 : #include "nsStringStream.h"
23 : #include "nsTemporaryFileInputStream.h"
24 : #include "nsXULAppAPI.h"
25 : #include "SlicedInputStream.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 0 : InputStreamHelper::SerializeInputStream(nsIInputStream* aInputStream,
44 : InputStreamParams& aParams,
45 : nsTArray<FileDescriptor>& aFileDescriptors)
46 : {
47 0 : MOZ_ASSERT(aInputStream);
48 :
49 : nsCOMPtr<nsIIPCSerializableInputStream> serializable =
50 0 : do_QueryInterface(aInputStream);
51 0 : if (!serializable) {
52 0 : MOZ_CRASH("Input stream is not serializable!");
53 : }
54 :
55 0 : serializable->Serialize(aParams, aFileDescriptors);
56 :
57 0 : if (aParams.type() == InputStreamParams::T__None) {
58 0 : MOZ_CRASH("Serialize failed!");
59 : }
60 0 : }
61 :
62 : already_AddRefed<nsIInputStream>
63 0 : InputStreamHelper::DeserializeInputStream(const InputStreamParams& aParams,
64 : const nsTArray<FileDescriptor>& aFileDescriptors)
65 : {
66 0 : nsCOMPtr<nsIInputStream> stream;
67 0 : nsCOMPtr<nsIIPCSerializableInputStream> serializable;
68 :
69 : // 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 : getter_AddRefs(stream));
74 0 : return stream.forget();
75 : }
76 :
77 0 : switch (aParams.type()) {
78 : case InputStreamParams::TStringInputStreamParams:
79 0 : serializable = do_CreateInstance(kStringInputStreamCID);
80 0 : break;
81 :
82 : case InputStreamParams::TFileInputStreamParams:
83 0 : serializable = do_CreateInstance(kFileInputStreamCID);
84 0 : break;
85 :
86 : case InputStreamParams::TTemporaryFileInputStreamParams:
87 0 : serializable = new nsTemporaryFileInputStream();
88 0 : break;
89 :
90 : case InputStreamParams::TBufferedInputStreamParams:
91 0 : serializable = do_CreateInstance(kBufferedInputStreamCID);
92 0 : break;
93 :
94 : case InputStreamParams::TMIMEInputStreamParams:
95 0 : serializable = do_CreateInstance(kMIMEInputStreamCID);
96 0 : break;
97 :
98 : case InputStreamParams::TMultiplexInputStreamParams:
99 0 : serializable = do_CreateInstance(kMultiplexInputStreamCID);
100 0 : break;
101 :
102 : case InputStreamParams::TSlicedInputStreamParams:
103 0 : serializable = new SlicedInputStream();
104 0 : break;
105 :
106 : default:
107 0 : MOZ_ASSERT(false, "Unknown params!");
108 : return nullptr;
109 : }
110 :
111 0 : MOZ_ASSERT(serializable);
112 :
113 0 : if (!serializable->Deserialize(aParams, aFileDescriptors)) {
114 0 : MOZ_ASSERT(false, "Deserialize failed!");
115 : return nullptr;
116 : }
117 :
118 0 : stream = do_QueryInterface(serializable);
119 0 : MOZ_ASSERT(stream);
120 :
121 0 : return stream.forget();
122 : }
123 :
124 : } // namespace ipc
125 : } // namespace mozilla
|