FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
NpapiPluginModule.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Oct 15, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 Richard Bateman, Firebreath development team
13 \**********************************************************/
14 
15 #include <stdexcept>
16 
17 #include <cassert>
18 #include <boost/thread.hpp>
19 #include "logging.h"
20 #include "BrowserHost.h"
21 #include "PluginCore.h"
22 #include <assert.h>
23 #include "precompiled_headers.h" // On windows, everything above this line in PCH
24 
25 #include "NpapiPluginModule.h"
26 using namespace FB::Npapi;
27 
28 volatile uint32_t NpapiPluginModule::PluginModuleInitialized(0);
29 NpapiPluginModule::Modules NpapiPluginModule::m_modules;
30 
31 NpapiPluginModule* NpapiPluginModule::GetModule(const void* key) {
32  if (!NpapiPluginModule::PluginModuleInitialized) {
33  FB::Log::initLogging();
34  getFactoryInstance()->globalPluginInitialize();
35  }
36  NpapiPluginModule* rval = NULL;
37  Modules::iterator module = m_modules.find(key);
38  if (m_modules.end() == module) {
39  rval = new NpapiPluginModule();
40  m_modules[key] = rval;
41  NpapiPluginModule::PluginModuleInitialized++;
42  } else
43  rval = module->second;
44  return rval;
45 }
46 void NpapiPluginModule::ReleaseModule(const void* key) {
47  Modules::iterator module = m_modules.find(key);
48  if (m_modules.end() != module) {
49  delete module->second;
50  m_modules.erase(module);
51  NpapiPluginModule::PluginModuleInitialized--;
52  }
53 
54  if (!NpapiPluginModule::PluginModuleInitialized) {
55 
56  getFactoryInstance()->globalPluginDeinitialize();
57 
58  // NOTE: If this assertion fails you have some sort of memory leak; BrowserHost objects
59  // are reference counted, so you have a shared_ptr to your browserhost sometime. This
60  // can be a big problem because BrowserHost manages a lot of your memory and if you get
61  // a new one for each instances on each page (including page reloads).
62  assert(BrowserHost::getInstanceCount() == 0);
63  assert(PluginCore::getActivePluginCount() == 0);
64  FB::Log::stopLogging();
65  }
66 }
67 
68 NpapiPluginModule::NpapiPluginModule()
69  : m_threadId(boost::this_thread::get_id())
70 {
71  memset(&NPNFuncs, 0, sizeof(NPNetscapeFuncs));
72 }
73 
74 NpapiPluginModule::~NpapiPluginModule()
75 {
76 }
77 
78 
79 void NpapiPluginModule::assertMainThread()
80 {
81 #ifdef _DEBUG
82  assert(m_threadId == boost::this_thread::get_id());
83 #endif
84 }
85 
86 void NpapiPluginModule::setNetscapeFuncs(NPNetscapeFuncs *npnFuncs)
87 {
88  copyNPBrowserFuncs(&NPNFuncs, npnFuncs);
89 }
90 
91 void NpapiPluginModule::getPluginFuncs(NPPluginFuncs* pFuncs)
92 {
93  pFuncs->newp = &NpapiPluginModule::NPP_New;
94  pFuncs->destroy = &NpapiPluginModule::NPP_Destroy;
95  pFuncs->setwindow = &NpapiPluginModule::NPP_SetWindow;
96  pFuncs->newstream = &NpapiPluginModule::NPP_NewStream;
97  pFuncs->destroystream = &NpapiPluginModule::NPP_DestroyStream;
98  pFuncs->asfile = &NpapiPluginModule::NPP_StreamAsFile;
99  pFuncs->writeready = &NpapiPluginModule::NPP_WriteReady;
100  pFuncs->write = &NpapiPluginModule::NPP_Write;
101  pFuncs->print = &NpapiPluginModule::NPP_Print;
102  pFuncs->event = &NpapiPluginModule::NPP_HandleEvent;
103  pFuncs->urlnotify = &NpapiPluginModule::NPP_URLNotify;
104  pFuncs->getvalue = &NpapiPluginModule::NPP_GetValue;
105  pFuncs->setvalue = &NpapiPluginModule::NPP_SetValue;
106 }
107 
108 /* npapi.h definitions */
109 void NpapiPluginModule::Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
110 {
111  *plugin_major = NP_VERSION_MAJOR;
112  *plugin_minor = NP_VERSION_MINOR;
113  *netscape_major = ( ((uint32_t)NPNFuncs.version) & 0xFF00 ) >> 8;
114  *netscape_minor = (int)(((uint32_t)NPNFuncs.version) & 0xFF);
115 }
116 
117 
118 void* NpapiPluginModule::MemAlloc(uint32_t size)
119 {
120  if (NPNFuncs.memalloc != NULL) {
121  return NPNFuncs.memalloc(size);
122  } else {
123  return NULL;
124  }
125 }
126 
127 void NpapiPluginModule::MemFree(void* ptr)
128 {
129  if (NPNFuncs.memfree != NULL) {
130  NPNFuncs.memfree(ptr);
131  }
132 }
133 
134 uint32_t NpapiPluginModule::MemFlush(uint32_t size)
135 {
136  assertMainThread();
137  if (NPNFuncs.memflush != NULL) {
138  return NPNFuncs.memflush(size);
139  } else {
140  return 0;
141  }
142 }
143 
144 void NpapiPluginModule::ReloadPlugins(NPBool reloadPages)
145 {
146  assertMainThread();
147  if (NPNFuncs.reloadplugins != NULL) {
148  NPNFuncs.reloadplugins(reloadPages);
149  }
150 }
151 
152 void NpapiPluginModule::ReleaseVariantValue(NPVariant *variant)
153 {
154  assertMainThread();
155  if (NPNFuncs.releasevariantvalue != NULL) {
156  NPNFuncs.releasevariantvalue(variant);
157  }
158 }
159 
160 NPIdentifier NpapiPluginModule::GetStringIdentifier(const NPUTF8 *name)
161 {
162  assertMainThread();
163  if (NPNFuncs.getstringidentifier != NULL) {
164  return NPNFuncs.getstringidentifier(name);
165  } else {
166  return NULL;
167  }
168 }
169 
170 void NpapiPluginModule::GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
171 {
172  assertMainThread();
173  if (NPNFuncs.getstringidentifiers != NULL) {
174  NPNFuncs.getstringidentifiers(names, nameCount, identifiers);
175  }
176 }
177 
178 NPIdentifier NpapiPluginModule::GetIntIdentifier(int32_t intid)
179 {
180  assertMainThread();
181  if (NPNFuncs.getintidentifier != NULL) {
182  return NPNFuncs.getintidentifier(intid);
183  } else {
184  return NULL;
185  }
186 }
187 
188 bool NpapiPluginModule::IdentifierIsString(NPIdentifier identifier)
189 {
190  assertMainThread();
191  if (NPNFuncs.identifierisstring != NULL) {
192  return NPNFuncs.identifierisstring(identifier);
193  } else {
194  return NPERR_GENERIC_ERROR;
195  }
196 }
197 
198 NPUTF8 *NpapiPluginModule::UTF8FromIdentifier(NPIdentifier identifier)
199 {
200  assertMainThread();
201  if (NPNFuncs.utf8fromidentifier != NULL) {
202  return NPNFuncs.utf8fromidentifier(identifier);
203  } else {
204  return NULL;
205  }
206 }
207 
208 // This is the preferred method to get strings from NPIdentifiers, since you
209 // don't have to worry about cleaning it up =]
210 std::string NpapiPluginModule::StringFromIdentifier(NPIdentifier identifier)
211 {
212  assertMainThread();
213  NPUTF8* idStr = UTF8FromIdentifier(identifier);
214  std::string str;
215  if (idStr != NULL)
216  str = idStr;
217  MemFree(idStr);
218  return str;
219 }
220 
221 int32_t NpapiPluginModule::IntFromIdentifier(NPIdentifier identifier)
222 {
223  assertMainThread();
224  if (NPNFuncs.intfromidentifier != NULL) {
225  return NPNFuncs.intfromidentifier(identifier);
226  } else {
227  return NPERR_GENERIC_ERROR;
228  }
229 }
230 
231 NPObject *NpapiPluginModule::RetainObject(NPObject *npobj)
232 {
233  assertMainThread();
234  if (NPNFuncs.retainobject != NULL) {
235  return NPNFuncs.retainobject(npobj);
236  } else {
237  return npobj;
238  }
239 }
240 
241 void NpapiPluginModule::ReleaseObject(NPObject *npobj)
242 {
243  assertMainThread();
244  if (NPNFuncs.releaseobject != NULL) {
245  NPNFuncs.releaseobject(npobj);
246  }
247 }
248 
static int getInstanceCount()
Returns the count of how many BrowserHost object instances are active.
Definition: BrowserHost.h:496