2012-08-16 08:02:32 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "InputStreamUtils.h"
|
|
|
|
|
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/ipc/BlobParent.h"
|
2012-08-16 08:02:32 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsID.h"
|
2012-09-21 21:26:13 +04:00
|
|
|
#include "nsIXULRuntime.h"
|
2012-08-23 06:13:54 +04:00
|
|
|
#include "nsMIMEInputStream.h"
|
2012-08-16 08:02:32 +04:00
|
|
|
#include "nsMultiplexInputStream.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsStringStream.h"
|
2012-09-21 21:26:13 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-08-16 08:02:32 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
NS_DEFINE_CID(kStringInputStreamCID, NS_STRINGINPUTSTREAM_CID);
|
|
|
|
NS_DEFINE_CID(kFileInputStreamCID, NS_LOCALFILEINPUTSTREAM_CID);
|
|
|
|
NS_DEFINE_CID(kPartialFileInputStreamCID, NS_PARTIALLOCALFILEINPUTSTREAM_CID);
|
2012-08-23 06:13:54 +04:00
|
|
|
NS_DEFINE_CID(kBufferedInputStreamCID, NS_BUFFEREDINPUTSTREAM_CID);
|
|
|
|
NS_DEFINE_CID(kMIMEInputStreamCID, NS_MIMEINPUTSTREAM_CID);
|
2012-08-16 08:02:32 +04:00
|
|
|
NS_DEFINE_CID(kMultiplexInputStreamCID, NS_MULTIPLEXINPUTSTREAM_CID);
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-08-16 08:02:32 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
void
|
|
|
|
SerializeInputStream(nsIInputStream* aInputStream,
|
2014-03-25 22:37:13 +04:00
|
|
|
InputStreamParams& aParams,
|
|
|
|
nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-23 06:13:54 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aInputStream);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializable =
|
|
|
|
do_QueryInterface(aInputStream);
|
|
|
|
if (!serializable) {
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Input stream is not serializable!");
|
2012-08-23 06:13:54 +04:00
|
|
|
}
|
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
serializable->Serialize(aParams, aFileDescriptors);
|
2012-08-23 06:13:54 +04:00
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
if (aParams.type() == InputStreamParams::T__None) {
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Serialize failed!");
|
2012-08-23 23:33:46 +04:00
|
|
|
}
|
2012-08-23 06:13:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SerializeInputStream(nsIInputStream* aInputStream,
|
2014-03-25 22:37:13 +04:00
|
|
|
OptionalInputStreamParams& aParams,
|
|
|
|
nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-23 06:13:54 +04:00
|
|
|
{
|
|
|
|
if (aInputStream) {
|
|
|
|
InputStreamParams params;
|
2014-03-25 22:37:13 +04:00
|
|
|
SerializeInputStream(aInputStream, params, aFileDescriptors);
|
2012-08-23 06:13:54 +04:00
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aParams = mozilla::void_t();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
already_AddRefed<nsIInputStream>
|
2014-03-25 22:37:13 +04:00
|
|
|
DeserializeInputStream(const InputStreamParams& aParams,
|
|
|
|
const nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-16 08:02:32 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2012-08-16 08:02:32 +04:00
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializable;
|
|
|
|
|
|
|
|
switch (aParams.type()) {
|
|
|
|
case InputStreamParams::TStringInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kStringInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputStreamParams::TFileInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kFileInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputStreamParams::TPartialFileInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kPartialFileInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
case InputStreamParams::TBufferedInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kBufferedInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputStreamParams::TMIMEInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kMIMEInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
case InputStreamParams::TMultiplexInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kMultiplexInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
2012-09-21 21:26:13 +04:00
|
|
|
// When the input stream already exists in this process, all we need to do
|
|
|
|
// is retrieve the original instead of sending any data over the wire.
|
|
|
|
case InputStreamParams::TRemoteInputStreamParams: {
|
2015-07-04 04:29:00 +03:00
|
|
|
if (NS_WARN_IF(!XRE_IsParentProcess())) {
|
2014-10-07 23:26:59 +04:00
|
|
|
return nullptr;
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
2012-09-21 21:26:13 +04:00
|
|
|
|
2014-10-07 23:26:59 +04:00
|
|
|
const nsID& id = aParams.get_RemoteInputStreamParams().id();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = BlobParent::GetBlobImplForID(id);
|
2014-10-07 23:26:59 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(blobImpl, "Invalid blob contents");
|
2012-09-21 21:26:13 +04:00
|
|
|
|
|
|
|
// If fetching the internal stream fails, we ignore it and return a
|
|
|
|
// null stream.
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
2012-09-21 21:26:13 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-05-19 17:36:37 +03:00
|
|
|
blobImpl->GetInternalStream(getter_AddRefs(stream), rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed()) || !stream) {
|
2012-09-21 21:26:13 +04:00
|
|
|
NS_WARNING("Couldn't obtain a valid stream from the blob");
|
2015-05-19 17:36:37 +03:00
|
|
|
rv.SuppressException();
|
2012-09-21 21:26:13 +04:00
|
|
|
}
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
case InputStreamParams::TSameProcessInputStreamParams: {
|
|
|
|
MOZ_ASSERT(aFileDescriptors.IsEmpty());
|
|
|
|
|
|
|
|
const SameProcessInputStreamParams& params =
|
|
|
|
aParams.get_SameProcessInputStreamParams();
|
|
|
|
|
|
|
|
stream = dont_AddRef(
|
|
|
|
reinterpret_cast<nsIInputStream*>(params.addRefedInputStream()));
|
|
|
|
MOZ_ASSERT(stream);
|
|
|
|
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
default:
|
2012-08-23 06:13:54 +04:00
|
|
|
MOZ_ASSERT(false, "Unknown params!");
|
2012-08-16 08:02:32 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(serializable);
|
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
if (!serializable->Deserialize(aParams, aFileDescriptors)) {
|
2012-08-23 06:13:54 +04:00
|
|
|
MOZ_ASSERT(false, "Deserialize failed!");
|
2012-08-16 08:02:32 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
stream = do_QueryInterface(serializable);
|
2012-08-16 08:02:32 +04:00
|
|
|
MOZ_ASSERT(stream);
|
|
|
|
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
already_AddRefed<nsIInputStream>
|
2014-03-25 22:37:13 +04:00
|
|
|
DeserializeInputStream(const OptionalInputStreamParams& aParams,
|
|
|
|
const nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-23 06:13:54 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
|
|
|
|
switch (aParams.type()) {
|
|
|
|
case OptionalInputStreamParams::Tvoid_t:
|
|
|
|
// Leave stream null.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionalInputStreamParams::TInputStreamParams:
|
2014-03-25 22:37:13 +04:00
|
|
|
stream = DeserializeInputStream(aParams.get_InputStreamParams(),
|
|
|
|
aFileDescriptors);
|
2012-08-23 06:13:54 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unknown params!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|