• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "build/build_config.h"
6 #include "ipc/ipc_platform_file.h"
7 
8 #if defined(OS_WIN)
9 #include <windows.h>
10 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
11 #include <unistd.h>
12 
13 #include "base/posix/eintr_wrapper.h"
14 #endif
15 
16 namespace IPC {
17 
18 #if defined(OS_WIN)
PlatformFileForTransit()19 PlatformFileForTransit::PlatformFileForTransit() : handle_(nullptr) {}
20 
PlatformFileForTransit(HANDLE handle)21 PlatformFileForTransit::PlatformFileForTransit(HANDLE handle)
22     : handle_(handle) {}
23 
operator ==(const PlatformFileForTransit & platform_file) const24 bool PlatformFileForTransit::operator==(
25     const PlatformFileForTransit& platform_file) const {
26   return handle_ == platform_file.handle_;
27 }
28 
operator !=(const PlatformFileForTransit & platform_file) const29 bool PlatformFileForTransit::operator!=(
30     const PlatformFileForTransit& platform_file) const {
31   return !(*this == platform_file);
32 }
33 
GetHandle() const34 HANDLE PlatformFileForTransit::GetHandle() const {
35   return handle_;
36 }
37 
IsValid() const38 bool PlatformFileForTransit::IsValid() const {
39   return handle_ != nullptr;
40 }
41 
42 #endif  // defined(OS_WIN)
43 
GetPlatformFileForTransit(base::PlatformFile handle,bool close_source_handle)44 PlatformFileForTransit GetPlatformFileForTransit(base::PlatformFile handle,
45                                                  bool close_source_handle) {
46 #if defined(OS_WIN)
47   HANDLE raw_handle = INVALID_HANDLE_VALUE;
48   DWORD options = DUPLICATE_SAME_ACCESS;
49   if (close_source_handle)
50     options |= DUPLICATE_CLOSE_SOURCE;
51   if (handle == INVALID_HANDLE_VALUE ||
52       !::DuplicateHandle(::GetCurrentProcess(), handle, ::GetCurrentProcess(),
53                          &raw_handle, 0, FALSE, options)) {
54     return IPC::InvalidPlatformFileForTransit();
55   }
56 
57   return IPC::PlatformFileForTransit(raw_handle);
58 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
59   // If asked to close the source, we can simply re-use the source fd instead of
60   // dup()ing and close()ing.
61   // When we're not closing the source, we need to duplicate the handle and take
62   // ownership of that. The reason is that this function is often used to
63   // generate IPC messages, and the handle must remain valid until it's sent to
64   // the other process from the I/O thread. Without the dup, calling code might
65   // close the source handle before the message is sent, creating a race
66   // condition.
67   int fd = close_source_handle ? handle : HANDLE_EINTR(::dup(handle));
68   return base::FileDescriptor(fd, true);
69 #endif
70 }
71 
TakePlatformFileForTransit(base::File file)72 PlatformFileForTransit TakePlatformFileForTransit(base::File file) {
73   return GetPlatformFileForTransit(file.TakePlatformFile(), true);
74 }
75 
76 }  // namespace IPC
77