• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 // Note: This header should be compilable as C.
6 
7 #ifndef MOJO_PUBLIC_C_SYSTEM_THUNKS_H_
8 #define MOJO_PUBLIC_C_SYSTEM_THUNKS_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "mojo/public/c/system/core.h"
14 #include "mojo/public/c/system/system_export.h"
15 
16 // The embedder needs to bind the basic Mojo Core functions of a DSO to those of
17 // the embedder when loading a DSO that is dependent on mojo_system.
18 // The typical usage would look like:
19 // base::ScopedNativeLibrary app_library(
20 //     base::LoadNativeLibrary(app_path_, &error));
21 // typedef MojoResult (*MojoSetSystemThunksFn)(MojoSystemThunks*);
22 // MojoSetSystemThunksFn mojo_set_system_thunks_fn =
23 //     reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer(
24 //         "MojoSetSystemThunks"));
25 // MojoSystemThunks system_thunks = MojoMakeSystemThunks();
26 // size_t expected_size = mojo_set_system_thunks_fn(&system_thunks);
27 // if (expected_size > sizeof(MojoSystemThunks)) {
28 //   LOG(ERROR)
29 //       << "Invalid DSO. Expected MojoSystemThunks size: "
30 //       << expected_size;
31 //   break;
32 // }
33 
34 // Structure used to bind the basic Mojo Core functions of a DSO to those of
35 // the embedder.
36 // This is the ABI between the embedder and the DSO. It can only have new
37 // functions added to the end. No other changes are supported.
38 #pragma pack(push, 8)
39 struct MojoSystemThunks {
40   size_t size;  // Should be set to sizeof(MojoSystemThunks).
41   MojoTimeTicks (*GetTimeTicksNow)();
42   MojoResult (*Close)(MojoHandle handle);
43   MojoResult (*Wait)(MojoHandle handle,
44                      MojoHandleSignals signals,
45                      MojoDeadline deadline,
46                      struct MojoHandleSignalsState* signals_state);
47   MojoResult (*WaitMany)(const MojoHandle* handles,
48                          const MojoHandleSignals* signals,
49                          uint32_t num_handles,
50                          MojoDeadline deadline,
51                          uint32_t* result_index,
52                          struct MojoHandleSignalsState* signals_states);
53   MojoResult (*CreateMessagePipe)(
54       const struct MojoCreateMessagePipeOptions* options,
55       MojoHandle* message_pipe_handle0,
56       MojoHandle* message_pipe_handle1);
57   MojoResult (*WriteMessage)(MojoHandle message_pipe_handle,
58                              const void* bytes,
59                              uint32_t num_bytes,
60                              const MojoHandle* handles,
61                              uint32_t num_handles,
62                              MojoWriteMessageFlags flags);
63   MojoResult (*ReadMessage)(MojoHandle message_pipe_handle,
64                             void* bytes,
65                             uint32_t* num_bytes,
66                             MojoHandle* handles,
67                             uint32_t* num_handles,
68                             MojoReadMessageFlags flags);
69   MojoResult (*CreateDataPipe)(const struct MojoCreateDataPipeOptions* options,
70                                MojoHandle* data_pipe_producer_handle,
71                                MojoHandle* data_pipe_consumer_handle);
72   MojoResult (*WriteData)(MojoHandle data_pipe_producer_handle,
73                           const void* elements,
74                           uint32_t* num_elements,
75                           MojoWriteDataFlags flags);
76   MojoResult (*BeginWriteData)(MojoHandle data_pipe_producer_handle,
77                                void** buffer,
78                                uint32_t* buffer_num_elements,
79                                MojoWriteDataFlags flags);
80   MojoResult (*EndWriteData)(MojoHandle data_pipe_producer_handle,
81                              uint32_t num_elements_written);
82   MojoResult (*ReadData)(MojoHandle data_pipe_consumer_handle,
83                          void* elements,
84                          uint32_t* num_elements,
85                          MojoReadDataFlags flags);
86   MojoResult (*BeginReadData)(MojoHandle data_pipe_consumer_handle,
87                               const void** buffer,
88                               uint32_t* buffer_num_elements,
89                               MojoReadDataFlags flags);
90   MojoResult (*EndReadData)(MojoHandle data_pipe_consumer_handle,
91                             uint32_t num_elements_read);
92   MojoResult (*CreateSharedBuffer)(
93       const struct MojoCreateSharedBufferOptions* options,
94       uint64_t num_bytes,
95       MojoHandle* shared_buffer_handle);
96   MojoResult (*DuplicateBufferHandle)(
97       MojoHandle buffer_handle,
98       const struct MojoDuplicateBufferHandleOptions* options,
99       MojoHandle* new_buffer_handle);
100   MojoResult (*MapBuffer)(MojoHandle buffer_handle,
101                           uint64_t offset,
102                           uint64_t num_bytes,
103                           void** buffer,
104                           MojoMapBufferFlags flags);
105   MojoResult (*UnmapBuffer)(void* buffer);
106 
107   MojoResult (*CreateWaitSet)(MojoHandle* wait_set);
108   MojoResult (*AddHandle)(MojoHandle wait_set,
109                           MojoHandle handle,
110                           MojoHandleSignals signals);
111   MojoResult (*RemoveHandle)(MojoHandle wait_set,
112                              MojoHandle handle);
113   MojoResult (*GetReadyHandles)(MojoHandle wait_set,
114                                 uint32_t* count,
115                                 MojoHandle* handles,
116                                 MojoResult* results,
117                                 struct MojoHandleSignalsState* signals_states);
118   MojoResult (*Watch)(MojoHandle handle,
119                       MojoHandleSignals signals,
120                       MojoWatchCallback callback,
121                       uintptr_t context);
122   MojoResult (*CancelWatch)(MojoHandle handle, uintptr_t context);
123   MojoResult (*FuseMessagePipes)(MojoHandle handle0, MojoHandle handle1);
124   MojoResult (*WriteMessageNew)(MojoHandle message_pipe_handle,
125                                 MojoMessageHandle message,
126                                 MojoWriteMessageFlags flags);
127   MojoResult (*ReadMessageNew)(MojoHandle message_pipe_handle,
128                                MojoMessageHandle* message,
129                                uint32_t* num_bytes,
130                                MojoHandle* handles,
131                                uint32_t* num_handles,
132                                MojoReadMessageFlags flags);
133   MojoResult (*AllocMessage)(uint32_t num_bytes,
134                              const MojoHandle* handles,
135                              uint32_t num_handles,
136                              MojoAllocMessageFlags flags,
137                              MojoMessageHandle* message);
138   MojoResult (*FreeMessage)(MojoMessageHandle message);
139   MojoResult (*GetMessageBuffer)(MojoMessageHandle message, void** buffer);
140   MojoResult (*WrapPlatformHandle)(
141       const struct MojoPlatformHandle* platform_handle,
142       MojoHandle* mojo_handle);
143   MojoResult (*UnwrapPlatformHandle)(
144       MojoHandle mojo_handle,
145       struct MojoPlatformHandle* platform_handle);
146   MojoResult (*WrapPlatformSharedBufferHandle)(
147       const struct MojoPlatformHandle* platform_handle,
148       size_t num_bytes,
149       MojoPlatformSharedBufferHandleFlags flags,
150       MojoHandle* mojo_handle);
151   MojoResult (*UnwrapPlatformSharedBufferHandle)(
152       MojoHandle mojo_handle,
153       struct MojoPlatformHandle* platform_handle,
154       size_t* num_bytes,
155       MojoPlatformSharedBufferHandleFlags* flags);
156   MojoResult (*NotifyBadMessage)(MojoMessageHandle message,
157                                  const char* error,
158                                  size_t error_num_bytes);
159   MojoResult (*GetProperty)(MojoPropertyType type, void* value);
160 };
161 #pragma pack(pop)
162 
163 // Use this type for the function found by dynamically discovering it in
164 // a DSO linked with mojo_system. For example:
165 // MojoSetSystemThunksFn mojo_set_system_thunks_fn =
166 //     reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer(
167 //         "MojoSetSystemThunks"));
168 // The expected size of |system_thunks| is returned.
169 // The contents of |system_thunks| are copied.
170 typedef size_t (*MojoSetSystemThunksFn)(
171     const struct MojoSystemThunks* system_thunks);
172 
173 // A function for setting up the embedder's own system thunks. This should only
174 // be called by Mojo embedder code.
175 MOJO_SYSTEM_EXPORT size_t MojoEmbedderSetSystemThunks(
176     const struct MojoSystemThunks* system_thunks);
177 
178 #endif  // MOJO_PUBLIC_C_SYSTEM_THUNKS_H_
179