chrome高级NPAPI插件开发

##注意事项

Chrome22之后貌似完全放弃10.5 Carbon之类的支持,所以Event Model需要进行设置,否则NPAPI插件在Chrome22下无法加载。NPP_New函数中进行如此设置

browser->setvalue(instance, 
NPPVpluginEventModel, 
(void *)NPEventModelCocoa);

文档没注意看,折腾死了,用xcode新建的库工程添加了MIME类型, 编译出来的库浏览器还是不加载

经过各种检查, 终于发现, 原来需要注意一下两点:

Info.plist

The plugin communicates its MIME and filename extension information using the Info.plist file, which is packaged in the plugin bundle.  The plugin also communicates its bundle type in that file, under the key CFBundlePackageType; the type is 'BRPL'.  If the type isn't an NPAPI plugin type, the bundle won't load as an NPAPI plugin.  You can always just use 'BRPL'.

XP_MACOSX

It's important to define the GCC preprocessor definition XP_MACOSX to 1; this is used by the NPAPI headers to build properly on Mac OS X.  If you don't define it, they won't be interpreted correctly.  This is easy to miss in the sample project's build settings.

Architectures

must be 'Standard(32/64-bit Intel)

Symbol visibility

Symbol visibility is a common problem for people trying to get NPAPI plugins working.  Some symbols must be visible as standard C symbols so the browser can find them, which means they need to be prefixed by an underscore, and must not have the C++ obfuscation that is generated by the C++ compiler.

The three symbols that must always be visible are:

NP_Initialize()
NP_GetEntryPoints()
NP_Shutdown()

The sample plugin is written entirely in C, using a standard Xcode build configuration, so by default all of its symbols are C-style and visible.

If you want to implement your plugin in C++ or Objective-C++, you need to tell the compiler to export them in C format by using extern “C” in the header, like this:

#pragma GCC visibility push(default)

extern “C” { NPError NP_Initialize(NPNetscapeFuncs *browserFuncs); NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs); void NP_Shutdown(void); }

#pragma GCC visibility pop You can check to be sure your symbols are visible and in standard C format by using the nm utility provided among the Mac OS X developer tools:

nm BasicPlugin

… 00000810 T _NP_GetEntryPoints 000007fa T _NP_Initialize 000008a0 T _NP_Shutdown

简单的说就是

Info.plist

CFBundlePackageType的值必须为BRPL

XP_MACOSX

xcode项目中 BuildSettings-> Preprocesssing Macros 必须设置 XP_MACOSX=1