Search code examples
objective-csafarinpapiopenfiledialog

Why calling NSOpenPanel from NPAPI plugin safari crashes?


The below code crashes my plugin:

NSOpenPanel *openFileDig = [NSOpenPanel openPanel];    
[openFileDig setCanChooseFiles:true];      
[openFileDig setCanChooseDirectories:false];     
[openFileDig setAllowsMultipleSelection:false];     
[openFileDig setDirectory:NSHomeDirectory()];     
if ([openFileDig runModal] == NSOKButton)   
{       
    .........     
}

Without doing anything, in about 3 minutes, while the openFileDig is running, the plugin will disappear. Can someone tell me why?


Solution

  • You're blocking the main thread in the middle of an NPAPI call -- this is pretty much NPAPI no-no #1.

    Never ever block the main thread during an NPAPI call. if you need to do something that will take awhile and require calling back you should do it asynchronously and then fire a callback back into javascript. (a js function passed into your npapi function will be a NPObject that you can call NPN_InvokeDefault on to call)

    Since your runModal will need to run on the main thread you'll want to use something like performSelectorOnMainThread to call a function that will call that and then call your js callback when it's done. Also remember that all NPAPI calls have to happen on the main thread and you should be good.

    What happens is that your plugin blocks and so the browser after not hearing back from the plugin for a long time assumes it crashed and kills it.