Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / file / ipc / TemporaryIPCBlobParent.cpp
blobbd2c6beefc67ecd1d162c6d5eff2a64012256bc4
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.
20 if (mFile) {
21 mFile->Remove(false);
25 mozilla::ipc::IPCResult TemporaryIPCBlobParent::CreateAndShareFile() {
26 MOZ_ASSERT(mActive);
27 MOZ_ASSERT(!mFile);
29 nsresult rv = NS_OpenAnonymousTemporaryNsIFile(getter_AddRefs(mFile));
30 if (NS_WARN_IF(NS_FAILED(rv))) {
31 return SendDeleteError(rv);
34 PRFileDesc* fd;
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).
45 PR_Close(fd);
47 (void)SendFileDesc(fdd);
48 return IPC_OK();
51 mozilla::ipc::IPCResult TemporaryIPCBlobParent::RecvOperationFailed() {
52 MOZ_ASSERT(mActive);
53 mActive = false;
55 // Nothing to do.
56 (void)Send__delete__(this, NS_ERROR_FAILURE);
57 return IPC_OK();
60 mozilla::ipc::IPCResult TemporaryIPCBlobParent::RecvOperationDone(
61 const nsCString& aContentType, const FileDescriptor& aFD) {
62 MOZ_ASSERT(mActive);
63 mActive = false;
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));
77 PR_Close(prfile);
79 IPCBlob ipcBlob;
80 nsresult rv = IPCBlobUtils::Serialize(blobImpl, ipcBlob);
81 if (NS_WARN_IF(NS_FAILED(rv))) {
82 (void)Send__delete__(this, NS_ERROR_FAILURE);
83 return IPC_OK();
86 (void)Send__delete__(this, ipcBlob);
87 return IPC_OK();
90 void TemporaryIPCBlobParent::ActorDestroy(ActorDestroyReason aWhy) {
91 mActive = false;
94 mozilla::ipc::IPCResult TemporaryIPCBlobParent::SendDeleteError(nsresult aRv) {
95 MOZ_ASSERT(mActive);
96 mActive = false;
98 (void)Send__delete__(this, aRv);
99 return IPC_OK();
102 } // namespace mozilla::dom