1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/FileDescriptorUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "FileDescriptorUtils.h" 1.9 + 1.10 +#include "nsIEventTarget.h" 1.11 + 1.12 +#include "nsCOMPtr.h" 1.13 +#include "nsDebug.h" 1.14 +#include "nsNetCID.h" 1.15 +#include "nsServiceManagerUtils.h" 1.16 +#include "nsThreadUtils.h" 1.17 +#include "prio.h" 1.18 +#include "private/pprio.h" 1.19 + 1.20 +using mozilla::ipc::CloseFileRunnable; 1.21 + 1.22 +#ifdef DEBUG 1.23 + 1.24 +CloseFileRunnable::CloseFileRunnable(const FileDescriptor& aFileDescriptor) 1.25 +: mFileDescriptor(aFileDescriptor) 1.26 +{ 1.27 + MOZ_ASSERT(aFileDescriptor.IsValid()); 1.28 +} 1.29 + 1.30 +#endif // DEBUG 1.31 + 1.32 +CloseFileRunnable::~CloseFileRunnable() 1.33 +{ 1.34 + if (mFileDescriptor.IsValid()) { 1.35 + // It's probably safer to take the main thread IO hit here rather than leak 1.36 + // the file descriptor. 1.37 + CloseFile(); 1.38 + } 1.39 +} 1.40 + 1.41 +NS_IMPL_ISUPPORTS(CloseFileRunnable, nsIRunnable) 1.42 + 1.43 +void 1.44 +CloseFileRunnable::Dispatch() 1.45 +{ 1.46 + nsCOMPtr<nsIEventTarget> eventTarget = 1.47 + do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); 1.48 + NS_ENSURE_TRUE_VOID(eventTarget); 1.49 + 1.50 + nsresult rv = eventTarget->Dispatch(this, NS_DISPATCH_NORMAL); 1.51 + NS_ENSURE_SUCCESS_VOID(rv); 1.52 +} 1.53 + 1.54 +void 1.55 +CloseFileRunnable::CloseFile() 1.56 +{ 1.57 + // It's possible for this to happen on the main thread if the dispatch to the 1.58 + // stream service fails so we can't assert the thread on which we're running. 1.59 + 1.60 + MOZ_ASSERT(mFileDescriptor.IsValid()); 1.61 + 1.62 + PRFileDesc* fd = 1.63 + PR_ImportFile(PROsfd(mFileDescriptor.PlatformHandle())); 1.64 + NS_WARN_IF_FALSE(fd, "Failed to import file handle!"); 1.65 + 1.66 + mFileDescriptor = FileDescriptor(); 1.67 + 1.68 + if (fd) { 1.69 + PR_Close(fd); 1.70 + fd = nullptr; 1.71 + } 1.72 +} 1.73 + 1.74 +NS_IMETHODIMP 1.75 +CloseFileRunnable::Run() 1.76 +{ 1.77 + MOZ_ASSERT(!NS_IsMainThread()); 1.78 + 1.79 + CloseFile(); 1.80 + return NS_OK; 1.81 +}