OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 #include "ipc/ipc_platform_file.h" | 6 #include "ipc/ipc_platform_file.h" |
7 | 7 |
8 #if defined(OS_POSIX) | 8 #if defined(OS_POSIX) |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #endif | 10 #endif |
11 | 11 |
12 namespace IPC { | 12 namespace IPC { |
13 | 13 |
| 14 #if defined(OS_WIN) |
| 15 PlatformFileForTransit::PlatformFileForTransit() : handle_(nullptr) {} |
| 16 |
| 17 PlatformFileForTransit::PlatformFileForTransit(HANDLE handle) |
| 18 : handle_(handle) {} |
| 19 |
| 20 bool PlatformFileForTransit::operator==( |
| 21 const PlatformFileForTransit& platform_file) const { |
| 22 return handle_ == platform_file.handle_; |
| 23 } |
| 24 |
| 25 bool PlatformFileForTransit::operator!=( |
| 26 const PlatformFileForTransit& platform_file) const { |
| 27 return !(*this == platform_file); |
| 28 } |
| 29 |
| 30 HANDLE PlatformFileForTransit::GetHandle() const { |
| 31 return handle_; |
| 32 } |
| 33 |
| 34 bool PlatformFileForTransit::IsValid() const { |
| 35 return handle_ != nullptr; |
| 36 } |
| 37 |
| 38 #endif // defined(OS_WIN) |
| 39 |
14 PlatformFileForTransit GetPlatformFileForTransit(base::PlatformFile handle, | 40 PlatformFileForTransit GetPlatformFileForTransit(base::PlatformFile handle, |
15 bool close_source_handle) { | 41 bool close_source_handle) { |
16 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
17 HANDLE raw_handle = INVALID_HANDLE_VALUE; | 43 HANDLE raw_handle = INVALID_HANDLE_VALUE; |
18 DWORD options = DUPLICATE_SAME_ACCESS; | 44 DWORD options = DUPLICATE_SAME_ACCESS; |
19 if (close_source_handle) | 45 if (close_source_handle) |
20 options |= DUPLICATE_CLOSE_SOURCE; | 46 options |= DUPLICATE_CLOSE_SOURCE; |
21 if (handle == INVALID_HANDLE_VALUE || | 47 if (handle == INVALID_HANDLE_VALUE || |
22 !::DuplicateHandle(::GetCurrentProcess(), handle, ::GetCurrentProcess(), | 48 !::DuplicateHandle(::GetCurrentProcess(), handle, ::GetCurrentProcess(), |
23 &raw_handle, 0, FALSE, options)) { | 49 &raw_handle, 0, FALSE, options)) { |
24 return IPC::InvalidPlatformFileForTransit(); | 50 return IPC::InvalidPlatformFileForTransit(); |
25 } | 51 } |
26 | 52 |
27 IPC::PlatformFileForTransit out_handle = IPC::PlatformFileForTransit( | 53 return IPC::PlatformFileForTransit(raw_handle); |
28 raw_handle, base::GetCurrentProcId()); | |
29 out_handle.SetOwnershipPassesToIPC(true); | |
30 return out_handle; | |
31 #elif defined(OS_POSIX) | 54 #elif defined(OS_POSIX) |
32 // If asked to close the source, we can simply re-use the source fd instead of | 55 // If asked to close the source, we can simply re-use the source fd instead of |
33 // dup()ing and close()ing. | 56 // dup()ing and close()ing. |
34 // When we're not closing the source, we need to duplicate the handle and take | 57 // When we're not closing the source, we need to duplicate the handle and take |
35 // ownership of that. The reason is that this function is often used to | 58 // ownership of that. The reason is that this function is often used to |
36 // generate IPC messages, and the handle must remain valid until it's sent to | 59 // generate IPC messages, and the handle must remain valid until it's sent to |
37 // the other process from the I/O thread. Without the dup, calling code might | 60 // the other process from the I/O thread. Without the dup, calling code might |
38 // close the source handle before the message is sent, creating a race | 61 // close the source handle before the message is sent, creating a race |
39 // condition. | 62 // condition. |
40 int fd = close_source_handle ? handle : ::dup(handle); | 63 int fd = close_source_handle ? handle : ::dup(handle); |
41 return base::FileDescriptor(fd, true); | 64 return base::FileDescriptor(fd, true); |
42 #else | 65 #else |
43 #error Not implemented. | 66 #error Not implemented. |
44 #endif | 67 #endif |
45 } | 68 } |
46 | 69 |
47 PlatformFileForTransit TakePlatformFileForTransit(base::File file) { | 70 PlatformFileForTransit TakePlatformFileForTransit(base::File file) { |
48 return GetPlatformFileForTransit(file.TakePlatformFile(), true); | 71 return GetPlatformFileForTransit(file.TakePlatformFile(), true); |
49 } | 72 } |
50 | 73 |
51 } // namespace IPC | 74 } // namespace IPC |
OLD | NEW |