ipc/glue/FileDescriptorUtils.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:07c5077579fd
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "FileDescriptorUtils.h"
6
7 #include "nsIEventTarget.h"
8
9 #include "nsCOMPtr.h"
10 #include "nsDebug.h"
11 #include "nsNetCID.h"
12 #include "nsServiceManagerUtils.h"
13 #include "nsThreadUtils.h"
14 #include "prio.h"
15 #include "private/pprio.h"
16
17 using mozilla::ipc::CloseFileRunnable;
18
19 #ifdef DEBUG
20
21 CloseFileRunnable::CloseFileRunnable(const FileDescriptor& aFileDescriptor)
22 : mFileDescriptor(aFileDescriptor)
23 {
24 MOZ_ASSERT(aFileDescriptor.IsValid());
25 }
26
27 #endif // DEBUG
28
29 CloseFileRunnable::~CloseFileRunnable()
30 {
31 if (mFileDescriptor.IsValid()) {
32 // It's probably safer to take the main thread IO hit here rather than leak
33 // the file descriptor.
34 CloseFile();
35 }
36 }
37
38 NS_IMPL_ISUPPORTS(CloseFileRunnable, nsIRunnable)
39
40 void
41 CloseFileRunnable::Dispatch()
42 {
43 nsCOMPtr<nsIEventTarget> eventTarget =
44 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
45 NS_ENSURE_TRUE_VOID(eventTarget);
46
47 nsresult rv = eventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
48 NS_ENSURE_SUCCESS_VOID(rv);
49 }
50
51 void
52 CloseFileRunnable::CloseFile()
53 {
54 // It's possible for this to happen on the main thread if the dispatch to the
55 // stream service fails so we can't assert the thread on which we're running.
56
57 MOZ_ASSERT(mFileDescriptor.IsValid());
58
59 PRFileDesc* fd =
60 PR_ImportFile(PROsfd(mFileDescriptor.PlatformHandle()));
61 NS_WARN_IF_FALSE(fd, "Failed to import file handle!");
62
63 mFileDescriptor = FileDescriptor();
64
65 if (fd) {
66 PR_Close(fd);
67 fd = nullptr;
68 }
69 }
70
71 NS_IMETHODIMP
72 CloseFileRunnable::Run()
73 {
74 MOZ_ASSERT(!NS_IsMainThread());
75
76 CloseFile();
77 return NS_OK;
78 }

mercurial