michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "FileDescriptorUtils.h" michael@0: michael@0: #include "nsIEventTarget.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsDebug.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "prio.h" michael@0: #include "private/pprio.h" michael@0: michael@0: using mozilla::ipc::CloseFileRunnable; michael@0: michael@0: #ifdef DEBUG michael@0: michael@0: CloseFileRunnable::CloseFileRunnable(const FileDescriptor& aFileDescriptor) michael@0: : mFileDescriptor(aFileDescriptor) michael@0: { michael@0: MOZ_ASSERT(aFileDescriptor.IsValid()); michael@0: } michael@0: michael@0: #endif // DEBUG michael@0: michael@0: CloseFileRunnable::~CloseFileRunnable() michael@0: { michael@0: if (mFileDescriptor.IsValid()) { michael@0: // It's probably safer to take the main thread IO hit here rather than leak michael@0: // the file descriptor. michael@0: CloseFile(); michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(CloseFileRunnable, nsIRunnable) michael@0: michael@0: void michael@0: CloseFileRunnable::Dispatch() michael@0: { michael@0: nsCOMPtr eventTarget = michael@0: do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); michael@0: NS_ENSURE_TRUE_VOID(eventTarget); michael@0: michael@0: nsresult rv = eventTarget->Dispatch(this, NS_DISPATCH_NORMAL); michael@0: NS_ENSURE_SUCCESS_VOID(rv); michael@0: } michael@0: michael@0: void michael@0: CloseFileRunnable::CloseFile() michael@0: { michael@0: // It's possible for this to happen on the main thread if the dispatch to the michael@0: // stream service fails so we can't assert the thread on which we're running. michael@0: michael@0: MOZ_ASSERT(mFileDescriptor.IsValid()); michael@0: michael@0: PRFileDesc* fd = michael@0: PR_ImportFile(PROsfd(mFileDescriptor.PlatformHandle())); michael@0: NS_WARN_IF_FALSE(fd, "Failed to import file handle!"); michael@0: michael@0: mFileDescriptor = FileDescriptor(); michael@0: michael@0: if (fd) { michael@0: PR_Close(fd); michael@0: fd = nullptr; michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: CloseFileRunnable::Run() michael@0: { michael@0: MOZ_ASSERT(!NS_IsMainThread()); michael@0: michael@0: CloseFile(); michael@0: return NS_OK; michael@0: }