Mac OS X Reference Library Apple Developer
Search

Creating Plug-ins with the Netscape API

Netscape-style plug-ins are programmed in C, and, provided that you are building in Mach-O form, can be developed and debugged with Xcode.

Using Plug-in Scripting

The scripting capabilities of Netscape-style plug-ins are provided by extensions onto the original plug-in specification. They allow a browser (through JavaScript) to access and control elements of the plug-in and its content, and allow the plug-in to access the enclosing web page and its content through the plug-in script interface.

When a plug-in is loaded, the browser calls the NPP_GetValue callback in your plug-in, which returns a retained NPObject structure that represent your plug-in. This NPObject structure contains an pointer to an NPClass structure. The NPClass structure contains a series of callbacks that define the interface between the plug-in and the scripting environment. The NPObject instance represents an instance of that plug-in that can then be used by the scripting environment.

If you want your plug-in to be scriptable, you need to return the appropriate retained NPObject by reference in your NPP_GetValue callback function.

Note: In current versions of WebKit and all non-WebKit-based browsers, you must retain the NPObject instance upon return by doing the following:

browser->retainobject((NPObject*)obj);
In versions of WebKit prior to version 420 (Safari 3 and later), the objects returned are retained by the browser. To avoid memory leaks in older browsers, you should check the WebKit version and avoid retaining returned objects when your plug-in is loaded by prior versions of WebKit.

A good demonstration of accessing plug-ins from JavaScript, as well as all the other concepts in creating a Netscape plug-in, can be found at:

    /Developer/Examples/WebKit/NetscapeMoviePlugIn

on a computer running Mac OS X v10.5 or earlier.

Core Graphics and Core Animation Drawing Models

Safari 4.0 provides two new drawing models: Core Graphics (Quartz 2D) and Core Animation (only in Mac OS X v10.5 and later). These drawing modes are strongly recommended going forwards, and if you move your plug-in to contain a 64-bit slice, that slice must use these drawing models. (See “Transitioning a Netscape-Style Plug-in to 64-bit” for more information.)

Most of the effort in using these drawing models comes from learning Core Graphics and Core Animation themselves. To learn about Core Graphics, read Quartz 2D Programming Guide. To learn about Core Animation, read Core Animation Programming Guide.

Once you understand how to draw things using Core Graphics or Core Animation, you can enable these drawing models in your NPP_New function as follows:

After you have done these two things, the browser fills the window field of the NPWindow structure with an NP_CGContext structure. This structure contains two fields, context and window, which are defined as follows:

typedef struct NP_CGContext
{
    CGContextRef context;
    WindowRef window;
} NP_CGContext;

The context value is a Core Graphics drawing context suitable for Quartz 2D drawing. For more information on how to use this context, read Quartz 2D Programming Guide. The window is a reference to an NSWindow object.

To obtain the bounds for your plug-in’s drawing region, do the following in your NPP_SetWindow callback:

NPError setwindow_cb(NPP instance, NPWindow* npw) {
    ...
 
    NP_CGContext *npcontext = npw.window;
    CGContextRef context = npcontext.context;
    CGRect boundingBox = CGContextGetClipBoundingBox(context);
 
    ...

The Core Animation model is similar, but reversed. If you set you set NPNVpluginDrawingModel to NPDrawingModelCoreAnimation, your NPN_GetValue callback must provide a retained Core Animation layer to the browser when it queries the NPPVpluginCoreAnimationLayer variable.

As with the Core graphics model, you can find out if the browser supports the Core Animation drawing model by checking the value of the NPNVsupportsCoreAnimationBool variable. For example:

NPBool supportsCA = false;
NPError error = browser->getvalue(instance,
    NPNVsupportsCoreAnimationBool,
    &supportsCA);

Out-of-Process Plug-Ins

Beginning in Mac OS X v10.6, on 64-bit-capable computers, Netscape-style plug-ins execute in an out-of-process fashion. This means that each Netscape-style plug-in gets its own process separate from the application process. This design applies to all Netscape-style plug-ins. It does not apply to WebKit plug-ins, nor at present to WebKit-based applications when running in 32-bit mode.

Out-of-process execution is used for several reasons:

For your plug-in to work correctly in this new environment, you may need to make some changes to your code, depending on how it interacts with the rest of the system beyond the browser.

Transitioning a Netscape-Style Plug-in to 64-bit

Beginning in Snow Leopard, Safari is moving to a 64-bit process. This section describes how to adapt a Netscape-style plug-in so that it works when compiled as a 64-bit plug-in. It describes only the changes specific to Netscape-style plug-ins, providing links to other documents for general 64-bit porting information.

Before you begin, do the following:

Once you have read these documents, you need to make a few additional changes specific to Netscape-style plug-ins (at least when compiling 64-bit versions of your plug-in). These changes are as follows:

Once you have accounted for these differences, you should be ready to compile your plug-in with a 64-bit slice. For more information on compiling code for 64-bit, fixing truncation bugs, and various other relevant topics, read 64-Bit Transition Guide for Cocoa and 64-Bit Transition Guide.




Last updated: 2009-03-13

Did this document help you? Yes It's good, but... Not helpful...