• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(OS_MACOSX) && !defined(OS_IOS)
57   mach_port_t port = MACH_PORT_NULL;
58 #endif
59 };
60 #elif defined(OS_WIN)
61 struct MOJO_SYSTEM_IMPL_EXPORT PlatformHandle {
62   PlatformHandle() : PlatformHandle(INVALID_HANDLE_VALUE) {}
63   explicit PlatformHandle(HANDLE handle)
64       : handle(handle), owning_process(base::GetCurrentProcessHandle()) {}
65 
66   void CloseIfNecessary();
67 
68   bool is_valid() const { return handle != INVALID_HANDLE_VALUE; }
69 
70   HANDLE handle;
71 
72   // A Windows HANDLE may be duplicated to another process but not yet sent to
73   // that process. This tracks the handle's owning process.
74   base::ProcessHandle owning_process;
75 
76   // A Windows HANDLE may be an unconnected named pipe. In this case, we need to
77   // wait for a connection before communicating on the pipe.
78   bool needs_connection = false;
79 };
80 #else
81 #error "Platform not yet supported."
82 #endif
83 
84 }  // namespace edk
85 }  // namespace mojo
86 
87 #endif  // MOJO_EDK_EMBEDDER_PLATFORM_HANDLE_H_
88