roll skia to 3543
[chromium-blink-merge.git] / ipc / ipc_platform_file.cc
blobb5ec7be0d94919726854f52e4edca36ae7ec2804
1 // Copyright (c) 2011 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.
5 #include "ipc/ipc_platform_file.h"
7 #if defined(OS_ANDROID)
8 #include <unistd.h>
9 #endif
11 namespace IPC {
13 PlatformFileForTransit GetFileHandleForProcess(base::PlatformFile handle,
14 base::ProcessHandle process,
15 bool close_source_handle) {
16 IPC::PlatformFileForTransit out_handle;
17 #if defined(OS_WIN)
18 DWORD options = DUPLICATE_SAME_ACCESS;
19 if (close_source_handle)
20 options |= DUPLICATE_CLOSE_SOURCE;
21 if (!::DuplicateHandle(::GetCurrentProcess(),
22 handle,
23 process,
24 &out_handle,
26 FALSE,
27 options)) {
28 out_handle = IPC::InvalidPlatformFileForTransit();
30 #elif defined(OS_POSIX)
31 // If asked to close the source, we can simply re-use the source fd instead of
32 // dup()ing and close()ing.
33 // When we're not closing the source, we need to duplicate the handle and take
34 // ownership of that. The reason is that this function is often used to
35 // generate IPC messages, and the handle must remain valid until it's sent to
36 // the other process from the I/O thread. Without the dup, calling code might
37 // close the source handle before the message is sent, creating a race
38 // condition.
39 int fd = close_source_handle ? handle : ::dup(handle);
40 out_handle = base::FileDescriptor(fd, true);
41 #else
42 #error Not implemented.
43 #endif
44 return out_handle;
47 } // namespace IPC