summaryrefslogtreecommitdiffstats
path: root/mojo/public/platform/native/system_thunks.h
blob: 679043640fbbc22975813fdd3425c0186ee51b67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Note: This header should be compilable as C.

#ifndef MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_
#define MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_

#include <stddef.h>
#include <stdint.h>

#include "mojo/public/c/system/core.h"

// The embedder needs to bind the basic Mojo Core functions of a DSO to those of
// the embedder when loading a DSO that is dependent on mojo_system.
// The typical usage would look like:
// base::ScopedNativeLibrary app_library(
//     base::LoadNativeLibrary(app_path_, &error));
// typedef MojoResult (*MojoSetSystemThunksFn)(MojoSystemThunks*);
// MojoSetSystemThunksFn mojo_set_system_thunks_fn =
//     reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer(
//         "MojoSetSystemThunks"));
// MojoSystemThunks system_thunks = MojoMakeSystemThunks();
// size_t expected_size = mojo_set_system_thunks_fn(&system_thunks);
// if (expected_size > sizeof(MojoSystemThunks)) {
//   LOG(ERROR)
//       << "Invalid DSO. Expected MojoSystemThunks size: "
//       << expected_size;
//   break;
// }

// Structure used to bind the basic Mojo Core functions of a DSO to those of
// the embedder.
// This is the ABI between the embedder and the DSO. It can only have new
// functions added to the end. No other changes are supported.
#pragma pack(push, 8)
struct MojoSystemThunks {
  size_t size;  // Should be set to sizeof(MojoSystemThunks).
  MojoTimeTicks (*GetTimeTicksNow)();
  MojoResult (*Close)(MojoHandle handle);
  MojoResult (*Wait)(MojoHandle handle,
                     MojoHandleSignals signals,
                     MojoDeadline deadline,
                     struct MojoHandleSignalsState* signals_state);
  MojoResult (*WaitMany)(const MojoHandle* handles,
                         const MojoHandleSignals* signals,
                         uint32_t num_handles,
                         MojoDeadline deadline,
                         uint32_t* result_index,
                         struct MojoHandleSignalsState* signals_states);
  MojoResult (*CreateMessagePipe)(
      const struct MojoCreateMessagePipeOptions* options,
      MojoHandle* message_pipe_handle0,
      MojoHandle* message_pipe_handle1);
  MojoResult (*WriteMessage)(MojoHandle message_pipe_handle,
                             const void* bytes,
                             uint32_t num_bytes,
                             const MojoHandle* handles,
                             uint32_t num_handles,
                             MojoWriteMessageFlags flags);
  MojoResult (*ReadMessage)(MojoHandle message_pipe_handle,
                            void* bytes,
                            uint32_t* num_bytes,
                            MojoHandle* handles,
                            uint32_t* num_handles,
                            MojoReadMessageFlags flags);
  MojoResult (*CreateDataPipe)(const struct MojoCreateDataPipeOptions* options,
                               MojoHandle* data_pipe_producer_handle,
                               MojoHandle* data_pipe_consumer_handle);
  MojoResult (*WriteData)(MojoHandle data_pipe_producer_handle,
                          const void* elements,
                          uint32_t* num_elements,
                          MojoWriteDataFlags flags);
  MojoResult (*BeginWriteData)(MojoHandle data_pipe_producer_handle,
                               void** buffer,
                               uint32_t* buffer_num_elements,
                               MojoWriteDataFlags flags);
  MojoResult (*EndWriteData)(MojoHandle data_pipe_producer_handle,
                             uint32_t num_elements_written);
  MojoResult (*ReadData)(MojoHandle data_pipe_consumer_handle,
                         void* elements,
                         uint32_t* num_elements,
                         MojoReadDataFlags flags);
  MojoResult (*BeginReadData)(MojoHandle data_pipe_consumer_handle,
                              const void** buffer,
                              uint32_t* buffer_num_elements,
                              MojoReadDataFlags flags);
  MojoResult (*EndReadData)(MojoHandle data_pipe_consumer_handle,
                            uint32_t num_elements_read);
  MojoResult (*CreateSharedBuffer)(
      const struct MojoCreateSharedBufferOptions* options,
      uint64_t num_bytes,
      MojoHandle* shared_buffer_handle);
  MojoResult (*DuplicateBufferHandle)(
      MojoHandle buffer_handle,
      const struct MojoDuplicateBufferHandleOptions* options,
      MojoHandle* new_buffer_handle);
  MojoResult (*MapBuffer)(MojoHandle buffer_handle,
                          uint64_t offset,
                          uint64_t num_bytes,
                          void** buffer,
                          MojoMapBufferFlags flags);
  MojoResult (*UnmapBuffer)(void* buffer);

  MojoResult (*CreateWaitSet)(MojoHandle* wait_set);
  MojoResult (*AddHandle)(MojoHandle wait_set,
                          MojoHandle handle,
                          MojoHandleSignals signals);
  MojoResult (*RemoveHandle)(MojoHandle wait_set,
                             MojoHandle handle);
  MojoResult (*GetReadyHandles)(MojoHandle wait_set,
                                uint32_t* count,
                                MojoHandle* handles,
                                MojoResult* results,
                                struct MojoHandleSignalsState* signals_states);
  MojoResult (*Watch)(MojoHandle handle,
                      MojoHandleSignals signals,
                      MojoWatchCallback callback,
                      uintptr_t context);
  MojoResult (*CancelWatch)(MojoHandle handle, uintptr_t context);
  MojoResult (*FuseMessagePipes)(MojoHandle handle0, MojoHandle handle1);
};
#pragma pack(pop)


#ifdef __cplusplus
// Intended to be called from the embedder. Returns a |MojoCore| initialized
// to contain pointers to each of the embedder's MojoCore functions.
inline MojoSystemThunks MojoMakeSystemThunks() {
  MojoSystemThunks system_thunks = {sizeof(MojoSystemThunks),
                                    MojoGetTimeTicksNow,
                                    MojoClose,
                                    MojoWait,
                                    MojoWaitMany,
                                    MojoCreateMessagePipe,
                                    MojoWriteMessage,
                                    MojoReadMessage,
                                    MojoCreateDataPipe,
                                    MojoWriteData,
                                    MojoBeginWriteData,
                                    MojoEndWriteData,
                                    MojoReadData,
                                    MojoBeginReadData,
                                    MojoEndReadData,
                                    MojoCreateSharedBuffer,
                                    MojoDuplicateBufferHandle,
                                    MojoMapBuffer,
                                    MojoUnmapBuffer,
                                    MojoCreateWaitSet,
                                    MojoAddHandle,
                                    MojoRemoveHandle,
                                    MojoGetReadyHandles,
                                    MojoWatch,
                                    MojoCancelWatch,
                                    MojoFuseMessagePipes};
  return system_thunks;
}
#endif


// Use this type for the function found by dynamically discovering it in
// a DSO linked with mojo_system. For example:
// MojoSetSystemThunksFn mojo_set_system_thunks_fn =
//     reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer(
//         "MojoSetSystemThunks"));
// The expected size of |system_thunks} is returned.
// The contents of |system_thunks| are copied.
typedef size_t (*MojoSetSystemThunksFn)(
    const struct MojoSystemThunks* system_thunks);

#endif  // MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_