FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
FB::variant NPObjectAPI::Invoke ( const std::string &  methodName,
const std::vector< variant > &  args 
)
virtual

Called by the browser to invoke a method on the JSAPI object.

Parameters
methodNameName of the method.
argsThe arguments.
Returns
result of method call

Implements FB::JSAPI.

Definition at line 278 of file NPObjectAPI.cpp.

279 {
280  if (m_browser.expired())
281  return false;
282 
283  NpapiBrowserHostPtr browser(getHost());
284  if (!browser->isMainThread()) {
285  return browser->CallOnMainThread(boost::bind((FB::InvokeType)&NPObjectAPI::Invoke, this, methodName, args));
286  }
287  if (is_JSAPI) {
288  FB::JSAPIPtr tmp = inner.lock();
289  if (tmp)
290  return tmp->Invoke(methodName, args);
291  else
292  return false;
293  }
294  NPVariant retVal;
295 
296  // Convert the arguments to NPVariants
297  boost::scoped_array<NPVariant> npargs(new NPVariant[args.size()]);
298  for (unsigned int i = 0; i < args.size(); i++) {
299  browser->getNPVariant(&npargs[i], args[i]);
300  }
301 
302  bool res = false;
303  // Invoke the method ("" means invoke default method)
304  if (methodName.size() > 0) {
305  res = browser->Invoke(obj, browser->GetStringIdentifier(methodName.c_str()), npargs.get(), args.size(), &retVal);
306  } else {
307  res = browser->InvokeDefault(obj, npargs.get(), args.size(), &retVal);
308  }
309 
310  // Free the NPVariants that we earlier allocated
311  for (unsigned int i = 0; i < args.size(); i++) {
312  browser->ReleaseVariantValue(&npargs[i]);
313  }
314 
315  if (!res) { // If the method call failed, throw an exception
316  browser->ReleaseVariantValue(&retVal); // Always release the return value!
317  throw script_error(methodName.c_str());
318  } else {
319  FB::variant ret = browser->getVariant(&retVal);
320  browser->ReleaseVariantValue(&retVal); // Always release the return value!
321  return ret;
322  }
323 }
variant(JSAPI::* InvokeType)(const std::string &, const std::vector< variant > &)
Defines an alias representing a function pointer to JSAPI::Invoke.
Definition: APITypes.h:172
variant Invoke(const std::string &methodName, const std::vector< variant > &args)
Called by the browser to invoke a method on the JSAPI object.
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
boost::shared_ptr< FB::JSAPI > JSAPIPtr
Defines an alias for a JSAPI shared_ptr (you should never use a JSAPI* directly)
Definition: APITypes.h:94