2012-08-15 21:02:32 -07: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 09:15:23 -07:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/ipc/BlobParent.h"
|
2012-08-15 21:02:32 -07:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsID.h"
|
2012-09-21 10:26:13 -07:00
|
|
|
#include "nsIXULRuntime.h"
|
2012-08-22 19:13:54 -07:00
|
|
|
#include "nsMIMEInputStream.h"
|
2012-08-15 21:02:32 -07:00
|
|
|
#include "nsMultiplexInputStream.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsStringStream.h"
|
2012-09-21 10:26:13 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-08-15 21:02:32 -07:00
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-08-15 21:02:32 -07: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-22 19:13:54 -07:00
|
|
|
NS_DEFINE_CID(kBufferedInputStreamCID, NS_BUFFEREDINPUTSTREAM_CID);
|
|
|
|
NS_DEFINE_CID(kMIMEInputStreamCID, NS_MIMEINPUTSTREAM_CID);
|
2012-08-15 21:02:32 -07:00
|
|
|
NS_DEFINE_CID(kMultiplexInputStreamCID, NS_MULTIPLEXINPUTSTREAM_CID);
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2012-08-22 19:13:54 -07:00
|
|
|
void
|
|
|
|
SerializeInputStream(nsIInputStream* aInputStream,
|
2014-03-25 11:37:13 -07:00
|
|
|
InputStreamParams& aParams,
|
|
|
|
nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-22 19:13:54 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aInputStream);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializable =
|
|
|
|
do_QueryInterface(aInputStream);
|
|
|
|
if (!serializable) {
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Input stream is not serializable!");
|
2012-08-22 19:13:54 -07:00
|
|
|
}
|
|
|
|
|
2014-03-25 11:37:13 -07:00
|
|
|
serializable->Serialize(aParams, aFileDescriptors);
|
2012-08-22 19:13:54 -07:00
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
if (aParams.type() == InputStreamParams::T__None) {
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Serialize failed!");
|
2012-08-23 12:33:46 -07:00
|
|
|
}
|
2012-08-22 19:13:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SerializeInputStream(nsIInputStream* aInputStream,
|
2014-03-25 11:37:13 -07:00
|
|
|
OptionalInputStreamParams& aParams,
|
|
|
|
nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-22 19:13:54 -07:00
|
|
|
{
|
|
|
|
if (aInputStream) {
|
|
|
|
InputStreamParams params;
|
2014-03-25 11:37:13 -07:00
|
|
|
SerializeInputStream(aInputStream, params, aFileDescriptors);
|
2012-08-22 19:13:54 -07:00
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aParams = mozilla::void_t();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
already_AddRefed<nsIInputStream>
|
2014-03-25 11:37:13 -07:00
|
|
|
DeserializeInputStream(const InputStreamParams& aParams,
|
|
|
|
const nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-15 21:02:32 -07:00
|
|
|
{
|
2014-09-26 16:21:57 -07:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2012-08-15 21:02:32 -07: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-22 19:13:54 -07:00
|
|
|
case InputStreamParams::TBufferedInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kBufferedInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputStreamParams::TMIMEInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kMIMEInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
case InputStreamParams::TMultiplexInputStreamParams:
|
|
|
|
serializable = do_CreateInstance(kMultiplexInputStreamCID);
|
|
|
|
break;
|
|
|
|
|
2012-09-21 10:26:13 -07: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: {
|
2014-10-07 12:26:59 -07:00
|
|
|
if (NS_WARN_IF(XRE_GetProcessType() != GeckoProcessType_Default)) {
|
|
|
|
return nullptr;
|
2014-09-26 16:21:57 -07:00
|
|
|
}
|
2012-09-21 10:26:13 -07:00
|
|
|
|
2014-10-07 12:26:59 -07:00
|
|
|
const nsID& id = aParams.get_RemoteInputStreamParams().id();
|
|
|
|
|
2014-10-08 09:15:23 -07:00
|
|
|
nsRefPtr<FileImpl> blobImpl = BlobParent::GetBlobImplForID(id);
|
2014-10-07 12:26:59 -07:00
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
MOZ_ASSERT(blobImpl, "Invalid blob contents");
|
2012-09-21 10:26:13 -07:00
|
|
|
|
|
|
|
// If fetching the internal stream fails, we ignore it and return a
|
|
|
|
// null stream.
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2014-09-26 16:21:57 -07:00
|
|
|
nsresult rv = blobImpl->GetInternalStream(getter_AddRefs(stream));
|
2012-09-21 10:26:13 -07:00
|
|
|
if (NS_FAILED(rv) || !stream) {
|
|
|
|
NS_WARNING("Couldn't obtain a valid stream from the blob");
|
|
|
|
}
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-26 16:21:57 -07: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-15 21:02:32 -07:00
|
|
|
default:
|
2012-08-22 19:13:54 -07:00
|
|
|
MOZ_ASSERT(false, "Unknown params!");
|
2012-08-15 21:02:32 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(serializable);
|
|
|
|
|
2014-03-25 11:37:13 -07:00
|
|
|
if (!serializable->Deserialize(aParams, aFileDescriptors)) {
|
2012-08-22 19:13:54 -07:00
|
|
|
MOZ_ASSERT(false, "Deserialize failed!");
|
2012-08-15 21:02:32 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-26 16:21:57 -07:00
|
|
|
stream = do_QueryInterface(serializable);
|
2012-08-15 21:02:32 -07:00
|
|
|
MOZ_ASSERT(stream);
|
|
|
|
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:13:54 -07:00
|
|
|
already_AddRefed<nsIInputStream>
|
2014-03-25 11:37:13 -07:00
|
|
|
DeserializeInputStream(const OptionalInputStreamParams& aParams,
|
|
|
|
const nsTArray<FileDescriptor>& aFileDescriptors)
|
2012-08-22 19:13:54 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
|
|
|
|
switch (aParams.type()) {
|
|
|
|
case OptionalInputStreamParams::Tvoid_t:
|
|
|
|
// Leave stream null.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionalInputStreamParams::TInputStreamParams:
|
2014-03-25 11:37:13 -07:00
|
|
|
stream = DeserializeInputStream(aParams.get_InputStreamParams(),
|
|
|
|
aFileDescriptors);
|
2012-08-22 19:13:54 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unknown params!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|