FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
np_macmain.cpp
1 /**********************************************************\
2  Default auto-generated np_winmain.cpp
3 
4  This file contains the entrypoints for a Mac NPAPI
5  plugin. Don't change this unless you know what you're
6  doing!
7 \**********************************************************/
8 
9 #include "NpapiTypes.h"
10 #include "NpapiPluginModule.h"
11 
12 #include <dlfcn.h>
13 #include <sys/sysctl.h>
14 
15 #ifndef NDEBUG
16 #define WAIT_FOR_DEBUGGER 0
17 #else
18 #define WAIT_FOR_DEBUGGER 0
19 #endif
20 
21 #if WAIT_FOR_DEBUGGER
22 static bool beingDebugged() {
23  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}; size_t mib_size = 4;
24  struct kinfo_proc kp; size_t kp_size = sizeof(kp);
25  int result = sysctl(mib, mib_size, &kp, &kp_size, NULL, 0);
26  return (0 == result) ? (P_TRACED & kp.kp_proc.p_flag) : false;
27 }
28 #endif
29 
30 using namespace FB::Npapi;
31 
32 typedef void (*NPP_ShutdownProcPtr)(void);
33 
34 #pragma GCC visibility push(default)
35 
36 extern "C" {
37  NPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
38  NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
39  NPError NP_Shutdown(void);
40 }
41 
42 #pragma GCC visibility pop
43 
44 void initPluginModule()
45 {
46 #if WAIT_FOR_DEBUGGER
47  #warning "WILL BLOCK ON P_TRACED"
48  while (!beingDebugged())
49  sleep(1);
50 #endif
51 }
52 
53 NPError OSCALL NP_GetEntryPoints(NPPluginFuncs* pFuncs)
54 {
55  FBLOG_INFO("NPAPI", "");
56  initPluginModule();
57  Dl_info info;
58  dladdr(__builtin_return_address(0), &info);
59  NpapiPluginModule *module = NpapiPluginModule::GetModule(info.dli_fbase);
60  module->getPluginFuncs(pFuncs);
61  return NPERR_NO_ERROR;
62 }
63 
64 NPError OSCALL NP_Initialize(NPNetscapeFuncs* pFuncs)
65 {
66  /* can't use FBLOG_XXX before GetModule returns, as it calls InitLogging */
67  initPluginModule();
68  Dl_info info;
69  dladdr(__builtin_return_address(0), &info);
70  NpapiPluginModule *module = NpapiPluginModule::GetModule(info.dli_fbase);
71  module->setNetscapeFuncs(pFuncs);
72 
73  FBLOG_INFO("NPAPI", "Initialization done");
74  return NPERR_NO_ERROR;
75 }
76 
77 NPError OSCALL NP_Shutdown()
78 {
79  FBLOG_INFO("NPAPI", "");
80  Dl_info info;
81  dladdr(__builtin_return_address(0), &info);
82  NpapiPluginModule::ReleaseModule(info.dli_fbase);
83  return NPERR_NO_ERROR;
84 }