Search code examples
cocoafirefoxwebkitnpapibrowser-plugin

How do I get an NSView in a cocoa event model NPAPI plugin


I've followed the NetscapeCocoaPlugin example from the nightly Webkit build, and I'm able to build a NPAPI style plugin that uses the Cocoa Event Model.

My question now, is how I can get the NSView inside NPP_SetWindow.

A poster in this thread, says that it's possible using [NSView focusView], but I haven't been able to get this to work

My current function looks like this:

NPError NPP_SetWindow(NPP instance, NPWindow* window)
{
PluginObject *obj = instance->pdata;
obj->window = *window;

NSLog(@"Set Window called");

NSView* currentView = [NSView focusView];

[[NSColor redColor] set]; // Sets current drawing color.
NSRectFill(NSMakeRect(10, 10, 2, 20)); // Defines a rectangle and then fills it with the current drawing color.
[[NSColor colorWithCalibratedRed:0.7 green:0.9 blue:0.3 alpha:1.0] set]; // Sets a new color.
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(5, 0, 10, 10)] fill]; // Draws a circle in the new color.

[currentView setNeedsDisplay:YES];

return NPERR_NO_ERROR;
}

Solution

  • You can't. There was a time when you could get the NSView using a hack, but it was never supported, never a good idea, and no longer possible because all three browsers have switched to using out of process plugins, which means you can't get access to the NSView.

    You can get a CGContextRef and then create your own offscreen NSWindow and NSView and render them into the CGContextRef, but then you'd have to proxy all events as well. There is a WebView wrapper in FireBreath that is experimental still that does this, but it is quite a pain. Eventually I plan to turn it into something more generic so that an NSView can (kinda) be used in a plugin, but there is no native way to do so.

    There is an excellent blog post about mac drawing models here: http://www.escapedthoughts.com/weblog/geek/P110308-mac-npapi-plugins.writeback