ipc/glue/FileDescriptorUtils.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial