• 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 #ifndef MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_
6 #define MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_
7 
8 #include <stddef.h>
9 
10 #include "mojo/public/c/system/core.h"
11 
12 // The embedder needs to bind the basic Mojo Core functions of a DSO to those of
13 // the embedder when loading a DSO that is dependent on mojo_system.
14 // The typical usage would look like:
15 // base::ScopedNativeLibrary app_library(
16 //     base::LoadNativeLibrary(app_path_, &error));
17 // typedef MojoResult (*MojoSetSystemThunksFn)(MojoSystemThunks*);
18 // MojoSetSystemThunksFn mojo_set_system_thunks_fn =
19 //     reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer(
20 //         "MojoSetSystemThunks"));
21 // MojoSystemThunks system_thunks = MojoMakeSystemThunks();
22 // size_t expected_size = mojo_set_system_thunks_fn(&system_thunks);
23 // if (expected_size > sizeof(MojoSystemThunks)) {
24 //   LOG(ERROR)
25 //       << "Invalid DSO. Expected MojoSystemThunks size: "
26 //       << expected_size;
27 //   break;
28 // }
29 
30 // Structure used to bind the basic Mojo Core functions of a DSO to those of
31 // the embedder.
32 // This is the ABI between the embedder and the DSO. It can only have new
33 // functions added to the end. No other changes are supported.
34 #pragma pack(push, 8)
35 struct MojoSystemThunks {
36   size_t size;  // Should be set to sizeof(MojoSystemThunks).
37   MojoTimeTicks (*GetTimeTicksNow)();
38   MojoResult (*Close)(MojoHandle handle);
39   MojoResult (*Wait)(MojoHandle handle,
40                      MojoHandleSignals signals,
41                      MojoDeadline deadline);
42   MojoResult (*WaitMany)(const MojoHandle* handles,
43                          const MojoHandleSignals* signals,
44                          uint32_t num_handles,
45                          MojoDeadline deadline);
46   MojoResult (*CreateMessagePipe)(const MojoCreateMessagePipeOptions* options,
47                                   MojoHandle* message_pipe_handle0,
48                                   MojoHandle* message_pipe_handle1);
49   MojoResult (*WriteMessage)(MojoHandle message_pipe_handle,
50                              const void* bytes,
51                              uint32_t num_bytes,
52                              const MojoHandle* handles,
53                              uint32_t num_handles,
54                              MojoWriteMessageFlags flags);
55   MojoResult (*ReadMessage)(MojoHandle message_pipe_handle,
56                             void* bytes,
57                             uint32_t* num_bytes,
58                             MojoHandle* handles,
59                             uint32_t* num_handles,
60                             MojoReadMessageFlags flags);
61   MojoResult (*CreateDataPipe)(const MojoCreateDataPipeOptions* options,
62                                MojoHandle* data_pipe_producer_handle,
63                                MojoHandle* data_pipe_consumer_handle);
64   MojoResult (*WriteData)(MojoHandle data_pipe_producer_handle,
65                           const void* elements,
66                           uint32_t* num_elements,
67                           MojoWriteDataFlags flags);
68   MojoResult (*BeginWriteData)(MojoHandle data_pipe_producer_handle,
69                                void** buffer,
70                                uint32_t* buffer_num_elements,
71                                MojoWriteDataFlags flags);
72   MojoResult (*EndWriteData)(MojoHandle data_pipe_producer_handle,
73                              uint32_t num_elements_written);
74   MojoResult (*ReadData)(MojoHandle data_pipe_consumer_handle,
75                          void* elements,
76                          uint32_t* num_elements,
77                          MojoReadDataFlags flags);
78   MojoResult (*BeginReadData)(MojoHandle data_pipe_consumer_handle,
79                               const void** buffer,
80                               uint32_t* buffer_num_elements,
81                               MojoReadDataFlags flags);
82   MojoResult (*EndReadData)(MojoHandle data_pipe_consumer_handle,
83                             uint32_t num_elements_read);
84   MojoResult (*CreateSharedBuffer)(
85       const MojoCreateSharedBufferOptions* options,
86       uint64_t num_bytes,
87       MojoHandle* shared_buffer_handle);
88   MojoResult (*DuplicateBufferHandle)(
89       MojoHandle buffer_handle,
90       const MojoDuplicateBufferHandleOptions* options,
91       MojoHandle* new_buffer_handle);
92   MojoResult (*MapBuffer)(MojoHandle buffer_handle,
93                           uint64_t offset,
94                           uint64_t num_bytes,
95                           void** buffer,
96                           MojoMapBufferFlags flags);
97   MojoResult (*UnmapBuffer)(void* buffer);
98 };
99 #pragma pack(pop)
100 
101 // Intended to be called from the embedder. Returns a |MojoCore| initialized
102 // to contain pointers to each of the embedder's MojoCore functions.
MojoMakeSystemThunks()103 inline MojoSystemThunks MojoMakeSystemThunks() {
104   MojoSystemThunks system_thunks = {
105     sizeof(MojoSystemThunks),
106     MojoGetTimeTicksNow,
107     MojoClose,
108     MojoWait,
109     MojoWaitMany,
110     MojoCreateMessagePipe,
111     MojoWriteMessage,
112     MojoReadMessage,
113     MojoCreateDataPipe,
114     MojoWriteData,
115     MojoBeginWriteData,
116     MojoEndWriteData,
117     MojoReadData,
118     MojoBeginReadData,
119     MojoEndReadData,
120     MojoCreateSharedBuffer,
121     MojoDuplicateBufferHandle,
122     MojoMapBuffer,
123     MojoUnmapBuffer
124   };
125   return system_thunks;
126 }
127 
128 // Use this type for the function found by dynamically discovering it in
129 // a DSO linked with mojo_system. For example:
130 // MojoSetSystemThunksFn mojo_set_system_thunks_fn =
131 //     reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer(
132 //         "MojoSetSystemThunks"));
133 // The expected size of |system_thunks} is returned.
134 // The contents of |system_thunks| are copied.
135 typedef size_t (*MojoSetSystemThunksFn)(const MojoSystemThunks* system_thunks);
136 
137 #endif  // MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_
138