Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
5 #include "FileDescriptorUtils.h"
7 #include "nsIEventTarget.h"
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"
17 using mozilla::ipc::CloseFileRunnable;
19 #ifdef DEBUG
21 CloseFileRunnable::CloseFileRunnable(const FileDescriptor& aFileDescriptor)
22 : mFileDescriptor(aFileDescriptor)
23 {
24 MOZ_ASSERT(aFileDescriptor.IsValid());
25 }
27 #endif // DEBUG
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 }
38 NS_IMPL_ISUPPORTS(CloseFileRunnable, nsIRunnable)
40 void
41 CloseFileRunnable::Dispatch()
42 {
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);
49 }
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.
57 MOZ_ASSERT(mFileDescriptor.IsValid());
59 PRFileDesc* fd =
60 PR_ImportFile(PROsfd(mFileDescriptor.PlatformHandle()));
61 NS_WARN_IF_FALSE(fd, "Failed to import file handle!");
63 mFileDescriptor = FileDescriptor();
65 if (fd) {
66 PR_Close(fd);
67 fd = nullptr;
68 }
69 }
71 NS_IMETHODIMP
72 CloseFileRunnable::Run()
73 {
74 MOZ_ASSERT(!NS_IsMainThread());
76 CloseFile();
77 return NS_OK;
78 }