|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set sw=2 ts=8 et tw=80 : */ |
|
3 |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "mozilla/net/RemoteOpenFileParent.h" |
|
9 #include "mozilla/unused.h" |
|
10 #include "nsEscape.h" |
|
11 |
|
12 #if !defined(XP_WIN) && !defined(MOZ_WIDGET_COCOA) |
|
13 #include <fcntl.h> |
|
14 #include <unistd.h> |
|
15 #endif |
|
16 |
|
17 namespace mozilla { |
|
18 namespace net { |
|
19 |
|
20 bool |
|
21 RemoteOpenFileParent::OpenSendCloseDelete() |
|
22 { |
|
23 #if defined(XP_WIN) || defined(MOZ_WIDGET_COCOA) |
|
24 MOZ_CRASH("OS X and Windows shouldn't be doing IPDL here"); |
|
25 #else |
|
26 |
|
27 // TODO: make this async! |
|
28 |
|
29 FileDescriptor fileDescriptor; |
|
30 |
|
31 nsAutoCString path; |
|
32 nsresult rv = mURI->GetFilePath(path); |
|
33 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "GetFilePath failed!"); |
|
34 |
|
35 NS_UnescapeURL(path); |
|
36 |
|
37 if (NS_SUCCEEDED(rv)) { |
|
38 int fd = open(path.get(), O_RDONLY); |
|
39 if (fd == -1) { |
|
40 printf_stderr("RemoteOpenFileParent: file '%s' was not found!\n", |
|
41 path.get()); |
|
42 } else { |
|
43 fileDescriptor = FileDescriptor(fd); |
|
44 } |
|
45 } |
|
46 |
|
47 // Sending a potentially invalid file descriptor is just fine. |
|
48 unused << Send__delete__(this, fileDescriptor); |
|
49 |
|
50 if (fileDescriptor.IsValid()) { |
|
51 // close file now that other process has it open, else we'll leak fds in the |
|
52 // parent process. |
|
53 close(fileDescriptor.PlatformHandle()); |
|
54 } |
|
55 |
|
56 #endif // OS_TYPE |
|
57 |
|
58 return true; |
|
59 } |
|
60 |
|
61 } // namespace net |
|
62 } // namespace mozilla |