1/*
2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include "ppapi/native_client/src/untrusted/pnacl_irt_shim/irt_shim_ppapi.h"
8
9#include "native_client/src/untrusted/irt/irt.h"
10#include "ppapi/nacl_irt/public/irt_ppapi.h"
11#include "ppapi/native_client/src/untrusted/pnacl_irt_shim/pnacl_shim.h"
12#include "ppapi/nacl_irt/plugin_main.h"
13
14/*
15 * Defines a version of the version irt_ppapi_start and of the irt_ppapihook
16 * that returns a shimmed GetInterface for PNaCl.
17 *
18 * The hook will be linked into the IRT but it is considered unstable.
19 * Stable nexes should not use that IRT hook (and a filter prevents
20 * it from being used). Instead PNaCl nexes should embed the
21 * irt_shim_ppapi_start and the shim functions directly into the nexe
22 * for ABI stability.
23 */
24
25static struct PP_StartFunctions g_user_start_functions;
26
27static int32_t shim_PPPInitializeModule(PP_Module module_id,
28                                        PPB_GetInterface get_browser_intf) {
29  /* Record the original PPB_GetInterface and provide a shimmed one. */
30  __set_real_Pnacl_PPBGetInterface(get_browser_intf);
31  return (*g_user_start_functions.PPP_InitializeModule)(
32      module_id,
33      &__Pnacl_PPBGetInterface);
34}
35
36static void shim_PPPShutdownModule(void) {
37  (*g_user_start_functions.PPP_ShutdownModule)();
38}
39
40#ifdef PNACL_SHIM_AOT
41 /*
42  * This will be discovered and set by the shim, since we cannot link
43  * against the IRT directly in the AOT library.
44  */
45int (*real_irt_ppapi_start)(const struct PP_StartFunctions *) = NULL;
46#else
47 /*
48  * Otherwise, when linking directly into the IRT, we can refer to the
49  * real irt_ppapi_start from irt_ppapi.
50  */
51extern int irt_ppapi_start(const struct PP_StartFunctions *);
52static int (* const real_irt_ppapi_start)(const struct PP_StartFunctions *) =
53    &irt_ppapi_start;
54#endif
55
56int irt_shim_ppapi_start(const struct PP_StartFunctions *funcs) {
57  g_user_start_functions = *funcs;
58  /*
59   * Record the original PPP_GetInterface and provide a shimmed one
60   * via wrapped_ppapi_methods.
61   */
62  const struct PP_StartFunctions wrapped_ppapi_methods = {
63    shim_PPPInitializeModule,
64    shim_PPPShutdownModule,
65    __Pnacl_PPPGetInterface
66  };
67  __set_real_Pnacl_PPPGetInterface(g_user_start_functions.PPP_GetInterface);
68  /*
69   * Invoke the IRT's ppapi_start interface with the wrapped interface.
70   */
71  return (*real_irt_ppapi_start)(&wrapped_ppapi_methods);
72}
73
74#ifndef PNACL_SHIM_AOT
75const struct nacl_irt_ppapihook nacl_irt_ppapihook_pnacl_private = {
76  irt_shim_ppapi_start,
77  PpapiPluginRegisterThreadCreator,
78};
79#endif
80