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 "FileDescriptorUtils.h"
9 #include "nsIEventTarget.h"
14 #include "nsServiceManagerUtils.h"
16 #include "private/pprio.h"
25 using mozilla::ipc::CloseFileRunnable
;
27 CloseFileRunnable::CloseFileRunnable(const FileDescriptor
& aFileDescriptor
)
28 : Runnable("CloseFileRunnable"), mFileDescriptor(aFileDescriptor
) {
29 MOZ_ASSERT(aFileDescriptor
.IsValid());
32 CloseFileRunnable::~CloseFileRunnable() {
33 if (mFileDescriptor
.IsValid()) {
34 // It's probably safer to take the main thread IO hit here rather than leak
35 // the file descriptor.
40 NS_IMPL_ISUPPORTS_INHERITED0(CloseFileRunnable
, Runnable
)
42 void CloseFileRunnable::Dispatch() {
43 nsCOMPtr
<nsIEventTarget
> eventTarget
=
44 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID
);
45 NS_ENSURE_TRUE_VOID(eventTarget
);
47 nsresult rv
= eventTarget
->Dispatch(this, NS_DISPATCH_NORMAL
);
48 NS_ENSURE_SUCCESS_VOID(rv
);
51 void CloseFileRunnable::CloseFile() {
52 // It's possible for this to happen on the main thread if the dispatch to the
53 // stream service fails so we can't assert the thread on which we're running.
54 mFileDescriptor
= FileDescriptor();
58 CloseFileRunnable::Run() {
59 MOZ_ASSERT(!NS_IsMainThread());
68 FILE* FileDescriptorToFILE(const FileDescriptor
& aDesc
, const char* aOpenMode
) {
69 if (!aDesc
.IsValid()) {
73 auto handle
= aDesc
.ClonePlatformHandle();
75 int fd
= _open_osfhandle(static_cast<intptr_t>(handle
.get()), 0);
79 Unused
<< handle
.release();
81 int fd
= handle
.release();
83 FILE* file
= fdopen(fd
, aOpenMode
);
85 int saved_errno
= errno
;
92 FileDescriptor
FILEToFileDescriptor(FILE* aStream
) {
95 return FileDescriptor();
98 int fd
= _fileno(aStream
);
100 return FileDescriptor();
102 return FileDescriptor(reinterpret_cast<HANDLE
>(_get_osfhandle(fd
)));
104 return FileDescriptor(fileno(aStream
));
109 } // namespace mozilla