1 // Copyright 2013 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 #ifndef MOJO_EDK_EMBEDDER_PLATFORM_HANDLE_H_ 6 #define MOJO_EDK_EMBEDDER_PLATFORM_HANDLE_H_ 7 8 #include "build/build_config.h" 9 #include "mojo/edk/system/system_impl_export.h" 10 11 #if defined(OS_WIN) 12 #include <windows.h> 13 14 #include "base/process/process_handle.h" 15 #elif defined(OS_MACOSX) && !defined(OS_IOS) 16 #include <mach/mach.h> 17 #endif 18 19 namespace mojo { 20 namespace edk { 21 22 #if defined(OS_POSIX) 23 struct MOJO_SYSTEM_IMPL_EXPORT PlatformHandle { PlatformHandlePlatformHandle24 PlatformHandle() {} PlatformHandlePlatformHandle25 explicit PlatformHandle(int handle) : handle(handle) {} 26 #if defined(OS_MACOSX) && !defined(OS_IOS) PlatformHandlePlatformHandle27 explicit PlatformHandle(mach_port_t port) 28 : type(Type::MACH), port(port) {} 29 #endif 30 31 void CloseIfNecessary(); 32 is_validPlatformHandle33 bool is_valid() const { 34 #if defined(OS_MACOSX) && !defined(OS_IOS) 35 if (type == Type::MACH || type == Type::MACH_NAME) 36 return port != MACH_PORT_NULL; 37 #endif 38 return handle != -1; 39 } 40 41 enum class Type { 42 POSIX, 43 #if defined(OS_MACOSX) && !defined(OS_IOS) 44 MACH, 45 // MACH_NAME isn't a real Mach port. But rather the "name" of one that can 46 // be resolved to a real port later. This distinction is needed so that the 47 // "port" doesn't try to be closed if CloseIfNecessary() is called. Having 48 // this also allows us to do checks in other places. 49 MACH_NAME, 50 #endif 51 }; 52 Type type = Type::POSIX; 53 54 int handle = -1; 55 56 // A POSIX handle may be a listen handle that can accept a connection. 57 bool needs_connection = false; 58 59 #if defined(OS_MACOSX) && !defined(OS_IOS) 60 mach_port_t port = MACH_PORT_NULL; 61 #endif 62 }; 63 #elif defined(OS_WIN) 64 struct MOJO_SYSTEM_IMPL_EXPORT PlatformHandle { 65 PlatformHandle() : PlatformHandle(INVALID_HANDLE_VALUE) {} 66 explicit PlatformHandle(HANDLE handle) 67 : handle(handle), owning_process(base::GetCurrentProcessHandle()) {} 68 69 void CloseIfNecessary(); 70 71 bool is_valid() const { return handle != INVALID_HANDLE_VALUE; } 72 73 HANDLE handle; 74 75 // A Windows HANDLE may be duplicated to another process but not yet sent to 76 // that process. This tracks the handle's owning process. 77 base::ProcessHandle owning_process; 78 79 // A Windows HANDLE may be an unconnected named pipe. In this case, we need to 80 // wait for a connection before communicating on the pipe. 81 bool needs_connection = false; 82 }; 83 #else 84 #error "Platform not yet supported." 85 #endif 86 87 } // namespace edk 88 } // namespace mojo 89 90 #endif // MOJO_EDK_EMBEDDER_PLATFORM_HANDLE_H_ 91