问题
A plugin, which works fine in firefox on windows, is being now ported to safari on Mac. we are using Xcode for development. we want a window in safari browser over which a video can be displayed. I read a code regarding NSwindow being used in NPP_SetWindow function as following:
NPError NPP_SetWindow(NPP instance, NPWindow* npWindow)
{
// Get a Cocoa window reference of the browser window
NP_CGContext* npContext = (NP_CGContext*)npWindow->window;
WindowRef window = npContext->window;
NSWindow* browserWindow = [[[NSWindow alloc] initWithWindowRef:window] autorelease];
// Get a Cocoa reference of my carbon window
// yourCarbonWindow should be replaced with the window handle of the carbon
// window that should be tied to the Safari window.
NSWindow* myWindow = [[[NSWindow alloc] initWithWindowRef:yourCarbonWindow] autorelease];
// Now create a parent child relationship
[browserWindow addChildWindow:myWindow ordered:NSWindowAbove];
}
But the problem is that npWindow->window is not carrying any value. When checked with printf, it shows 0 value, means it was not initialized or NULL.
But in Firefox it was carrying some value. Could someone please tell how to get a NSWindow in Safari or where might be the problem ? And what is this concept of Carbon window?
回答1:
Modern versions of Safari don't support the Carbon event model (which is what your code snippet is using), only the Cocoa event model, and per the documentation the window ref is null in that event model.
Mucking around with the window directly has been an anti-pattern in NPAPI plugins (at least on Mac) for quite a while, and with 64-bit and out-of-process plugins it has become impossible; the NSWindow isn't in your plugin's process, so you can't get a pointer to it. You should be drawing into the context or layer (depending on your drawing model) established by the API, rather than trying to show your own child window.
来源:https://stackoverflow.com/questions/33332608/npwindow-window-gives-0-value-in-npp-setwindow-function-plugin-for-safari-brow