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