0

I got my header files from: http://code.google.com/p/npapi-sdk/source/browse/?r=7#svn%2Fwiki

So in the Initialize method, I stored a pointer to all of the browser NPN methods like so

static NPNetscapeFuncs* browser;

NPError NP_Initialize(NPNetscapeFuncs* browserFuncs)
{  
  /* Save the browser function table. */
  browser = browserFuncs;

  return NPERR_NO_ERROR;
}

When I am creating my NPClass struct, should I just assign it the already existing browser functions like this:

struct NPClass class;
class.hasMethod = browser-> hasmethod;
etc.

Or do I need to implement the functions in the npruntimeheader using the browser funcs and assign them to the class that way. Example: class.hasMethod = NPN_HasMethod;

And then implement the function below:

bool NPN_HasMethod(NPP npp, NPObject *npobj, NPIdentifier methodName)
{
   return browser->hasmethod(npp, npobj, methodName);
}

Or are the NPN functions in the runtime header already implemented somehow?

I need to write this in c, and I don't think using firebreath would be a great idea for this particular project. Thanks in advance for your help

PendingVegan
  • 137
  • 3
  • 16

1 Answers1

1

You need to implement the functions for your NPClasses yourself, they define the behaviour of your scriptable objects. Part three of taxilians NPAPI tutorial covers this.

The functions that you receive via the browser function table are for calling into the browser (and already implemented there), e.g. to get information about NPObjects with hasmethod etc.

However the function declarations like NPN_HasMethod() need to be implemented by you if you want to use them, at their simplest just calling the corresponding functions in browser as you have shown with HasMethod().

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • So I have to implement, for example, NPObject *NPN_CreateObject(NPP npp, NPClass *aClass); myself? How would I do this if I don't want to create my own scriptable object class? Can you give me an example? – PendingVegan Jul 25 '12 at 18:00
  • @John: You don't want to create your own scriptable object class? Then you wouldn't need to setup `NPClass` or use `createobject()` either. All you need to work with existing `NPObject`s are the runtime functions in the browser function table: `hasmethod`, `retain`/`releaseobject`, `invoke`, ... – Georg Fritzsche Jul 25 '12 at 18:11
  • What I meant by that was I wanted to pass the default NPObject to the browser and call my methods using that. Is that not allowed? – PendingVegan Jul 25 '12 at 18:13
  • @John: What default `NPObject`? Do you want to implement your own scriptable objects to provide JavaScript access to your native code or do you just want to use script objects that you get from the browser? – Georg Fritzsche Jul 25 '12 at 18:15
  • I want to implement my own scriptable objects to provide JS access. Thank you for your help, by the way. – PendingVegan Jul 25 '12 at 18:19
  • @John: Then you need to implement the functions for the `NPClass` yourself as pointed out in the linked tutorial. There is no short-cut for that (besides using existing code like FireBreath). And you're welcome. – Georg Fritzsche Jul 25 '12 at 18:21
  • Okay, so I've gone through the tutorial before, and taxilian creates a class that inherits from NPObject. Do I need to do that? – PendingVegan Jul 25 '12 at 18:23
  • So would I have to implement them like this: NPObject *NPN_CreateObject(NPP npp, NPClass *aClass) { struct NPObject obj; obj->class = aClass return obj; } – PendingVegan Jul 25 '12 at 18:26
  • `NPN_CreateObject()` should just call `browser->createobject`. To create one you use `NPObject *obj = NPN_CreateObject(npp, &yourNpClass);`. You don't need to inherit, that's just a convenient way to do it in C++. – Georg Fritzsche Jul 25 '12 at 18:36
  • Okay, I think I understand now. So I implement the methods in npruntime, sometimes using the browser methods in the implementation, and have NPClass point to static methods I make myself? – PendingVegan Jul 25 '12 at 19:06
  • @John: Yes, implement the `NPN_` functions to basically just call the `browser` functions and do functions for the `NPClass` yourself. – Georg Fritzsche Jul 25 '12 at 19:25
  • So if the NPClass has its own functions, what is the purpose for many of the NPfunctions? Like, if I give NPClass its own invoke function, then what is the purpose of NPN_Invoke? – PendingVegan Jul 25 '12 at 21:16
  • It invokes function calls on any scriptable object - keep in mind that besides your own objects there are JS objects and objects from other plugins. – Georg Fritzsche Jul 25 '12 at 21:42