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