Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/file/ipc/TemporaryIPCBlobParent.cpp
Line
Count
Source (jump to first uncovered line)
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 "TemporaryIPCBlobParent.h"
8
9
#include "mozilla/dom/FileBlobImpl.h"
10
#include "nsAnonymousTemporaryFile.h"
11
#include "nsIFileStreams.h"
12
#include "TemporaryFileBlobImpl.h"
13
14
namespace mozilla {
15
namespace dom {
16
17
TemporaryIPCBlobParent::TemporaryIPCBlobParent()
18
  : mActive(true)
19
0
{}
20
21
TemporaryIPCBlobParent::~TemporaryIPCBlobParent()
22
0
{
23
0
  // If we still have mFile, let's remove it.
24
0
  if (mFile) {
25
0
    mFile->Remove(false);
26
0
  }
27
0
}
28
29
mozilla::ipc::IPCResult
30
TemporaryIPCBlobParent::CreateAndShareFile()
31
0
{
32
0
  MOZ_ASSERT(mActive);
33
0
  MOZ_ASSERT(!mFile);
34
0
35
0
  nsresult rv = NS_OpenAnonymousTemporaryNsIFile(getter_AddRefs(mFile));
36
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
37
0
    return SendDeleteError(rv);
38
0
  }
39
0
40
0
  PRFileDesc* fd;
41
0
  rv = mFile->OpenNSPRFileDesc(PR_RDWR, PR_IRWXU, &fd);
42
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
43
0
    return SendDeleteError(rv);
44
0
  }
45
0
46
0
  FileDescriptor fdd =
47
0
    FileDescriptor(FileDescriptor::PlatformHandleType(PR_FileDesc2NativeHandle(fd)));
48
0
49
0
  // The FileDescriptor object owns a duplicate of the file handle; we
50
0
  // must close the original (and clean up the NSPR descriptor).
51
0
  PR_Close(fd);
52
0
53
0
  Unused << SendFileDesc(fdd);
54
0
  return IPC_OK();
55
0
}
56
57
mozilla::ipc::IPCResult
58
TemporaryIPCBlobParent::RecvOperationFailed()
59
0
{
60
0
  MOZ_ASSERT(mActive);
61
0
  mActive = false;
62
0
63
0
  // Nothing to do.
64
0
  Unused << Send__delete__(this, NS_ERROR_FAILURE);
65
0
  return IPC_OK();
66
0
}
67
68
mozilla::ipc::IPCResult
69
TemporaryIPCBlobParent::RecvOperationDone(const nsCString& aContentType,
70
                                          const FileDescriptor& aFD)
71
0
{
72
0
  MOZ_ASSERT(mActive);
73
0
  mActive = false;
74
0
75
0
  // We have received a file descriptor because in this way we have kept the
76
0
  // file locked on windows during the IPC communication. After the creation of
77
0
  // the TemporaryFileBlobImpl, this prfile can be closed.
78
0
  auto rawFD = aFD.ClonePlatformHandle();
79
0
  PRFileDesc* prfile = PR_ImportFile(PROsfd(rawFD.release()));
80
0
81
0
  // Let's create the BlobImpl.
82
0
  nsCOMPtr<nsIFile> file = std::move(mFile);
83
0
84
0
  RefPtr<TemporaryFileBlobImpl> blobImpl =
85
0
    new TemporaryFileBlobImpl(file, NS_ConvertUTF8toUTF16(aContentType));
86
0
87
0
  PR_Close(prfile);
88
0
89
0
  IPCBlob ipcBlob;
90
0
  nsresult rv = IPCBlobUtils::Serialize(blobImpl, Manager(), ipcBlob);
91
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
92
0
    Unused << Send__delete__(this, NS_ERROR_FAILURE);
93
0
    return IPC_OK();
94
0
  }
95
0
96
0
  Unused << Send__delete__(this, ipcBlob);
97
0
  return IPC_OK();
98
0
}
99
100
void
101
TemporaryIPCBlobParent::ActorDestroy(ActorDestroyReason aWhy)
102
0
{
103
0
  mActive = false;
104
0
}
105
106
mozilla::ipc::IPCResult
107
TemporaryIPCBlobParent::SendDeleteError(nsresult aRv)
108
0
{
109
0
  MOZ_ASSERT(mActive);
110
0
  mActive = false;
111
0
112
0
  Unused << Send__delete__(this, aRv);
113
0
  return IPC_OK();
114
0
}
115
116
} // dom namespace
117
} // mozilla namespace
118