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/. */
7 #include "private/pprio.h"
8 #include "TemporaryIPCBlobParent.h"
9 #include "mozilla/dom/FileBlobImpl.h"
10 #include "nsAnonymousTemporaryFile.h"
11 #include "TemporaryFileBlobImpl.h"
12 #include "mozilla/dom/IPCBlobUtils.h"
14 namespace mozilla::dom
{
16 TemporaryIPCBlobParent::TemporaryIPCBlobParent() : mActive(true) {}
18 TemporaryIPCBlobParent::~TemporaryIPCBlobParent() {
19 // If we still have mFile, let's remove it.
25 mozilla::ipc::IPCResult
TemporaryIPCBlobParent::CreateAndShareFile() {
29 nsresult rv
= NS_OpenAnonymousTemporaryNsIFile(getter_AddRefs(mFile
));
30 if (NS_WARN_IF(NS_FAILED(rv
))) {
31 return SendDeleteError(rv
);
35 rv
= mFile
->OpenNSPRFileDesc(PR_RDWR
, PR_IRWXU
, &fd
);
36 if (NS_WARN_IF(NS_FAILED(rv
))) {
37 return SendDeleteError(rv
);
40 FileDescriptor fdd
= FileDescriptor(
41 FileDescriptor::PlatformHandleType(PR_FileDesc2NativeHandle(fd
)));
43 // The FileDescriptor object owns a duplicate of the file handle; we
44 // must close the original (and clean up the NSPR descriptor).
47 (void)SendFileDesc(fdd
);
51 mozilla::ipc::IPCResult
TemporaryIPCBlobParent::RecvOperationFailed() {
56 (void)Send__delete__(this, NS_ERROR_FAILURE
);
60 mozilla::ipc::IPCResult
TemporaryIPCBlobParent::RecvOperationDone(
61 const nsCString
& aContentType
, const FileDescriptor
& aFD
) {
65 // We have received a file descriptor because in this way we have kept the
66 // file locked on windows during the IPC communication. After the creation of
67 // the TemporaryFileBlobImpl, this prfile can be closed.
68 auto rawFD
= aFD
.ClonePlatformHandle();
69 PRFileDesc
* prfile
= PR_ImportFile(PROsfd(rawFD
.release()));
71 // Let's create the BlobImpl.
72 nsCOMPtr
<nsIFile
> file
= std::move(mFile
);
74 RefPtr
<TemporaryFileBlobImpl
> blobImpl
=
75 new TemporaryFileBlobImpl(file
, NS_ConvertUTF8toUTF16(aContentType
));
80 nsresult rv
= IPCBlobUtils::Serialize(blobImpl
, ipcBlob
);
81 if (NS_WARN_IF(NS_FAILED(rv
))) {
82 (void)Send__delete__(this, NS_ERROR_FAILURE
);
86 (void)Send__delete__(this, ipcBlob
);
90 void TemporaryIPCBlobParent::ActorDestroy(ActorDestroyReason aWhy
) {
94 mozilla::ipc::IPCResult
TemporaryIPCBlobParent::SendDeleteError(nsresult aRv
) {
98 (void)Send__delete__(this, aRv
);
102 } // namespace mozilla::dom