WebKit PlugIn Programming Topics.docx
《WebKit PlugIn Programming Topics.docx》由会员分享,可在线阅读,更多相关《WebKit PlugIn Programming Topics.docx(27页珍藏版)》请在冰豆网上搜索。
WebKitPlugInProgrammingTopics
WebKitPlug-InProgrammingTopics
CreatingPlug-inswiththeNetscapeAPI
创建插件的网景应用编程接口
Netscape-styleplug-insarewrittenusingacross-platformCAPI,and,providedthatyouarebuildinginMach-Oform,canbedevelopedanddebuggedwithXcode.Theseplug-insarecompatiblewithawiderangeofwebbrowsers.
netscape-style插件使用跨平台的接口,并提供,你是建筑在mach-o形式,可以开发和调试函数。
这些插件兼容范围广泛的网页浏览器。
TheoriginalNetscapeplug-inarchitecturewasintegratedintoNetscape2.0in1996.Sincethen,theAPIhasbeenadoptedbymostcommonwebbrowsers,includingSafari.Ithasbuilt-insupportforonscreendrawing,varioustypesofeventhandling,andnetworkingfunctions.
原网景插件架构集成到Netscape21996。
从此,原料药已经通过了最常见的网页浏览器,包括野生动物园。
它具有内置的支持在屏幕上绘制,各种类型的事件处理,和网络功能。
Inadditiontothecoreplug-infunctionality,ApplehasextendedthecapabilitiesoftheNetscape-styleplug-ins.StartingwiththeWebKitframeworkbundledwithSafari1.3(onMacOSXversion10.3)orSafari2.0(onMacOSXversion10.4),theNetscape-styleplug-inscanalsoperformscriptingfunctions.
除了核心的插件功能,苹果已经扩展能力的netscape-style插件。
从WebKit框架捆绑的Safari1.3(苹果公司的操作系统版本10.3)或2(苹果公司的操作系统版本10.4),该netscape-style插件也可以执行脚本功能。
TheNetscape-styleplug-inscriptingenvironmentallowsplug-instoaccessscriptinglanguagessuchasJavaScript(includingaccessingscriptelementssuchasawebpage’sDocumentObjectModel).Italsoallowsscriptinglanguagestoaccessandcontrolelementsoftheplug-in.
netscape-style插件的脚本环境允许插件访问等脚本语言脚本(包括访问脚本元素作为一个网页的文档对象模型)。
它还允许脚本访问和控制元件的插件。
InMacOSXv10.5andearlier,Netscape-styleplug-inscanbecompiledinMacOSXintoeithertheMach-OorPEF(CFM)binaryformat(althoughCFMisPowerPC-only).BeginninginMacOSXv10.6,Netscape-styleplug-insshouldbeupdatedtousea64-bitbinarytoworkwith64-bitinstancesofSafariorotherWebKitclients.Todothis,theymustbebuiltas32/64-bitmulti-architecturebinariesusingtheMach-Ofileformat.
在操作系统v10.5初期,netscape-style插件可以被编译的操作系统或者mach-o或呼气(立方英尺)的二进制格式(虽然是powerpc-only立方英尺)。
开始在苹果操作系统②,netscape-style插件应该更新使用64位二进制位的情况下与狩猎或其他WebKit客户。
为此,他们必须建立32/64位多建筑使用mach-o二进制文件格式。
ThoughtheAPIsupportsbothformats,MacOSXnativelysupportstheMach-Ostyle,andyouwillfindthatyourplug-inwillrunmuchfasterifcompiledasaMach-Obinary.Inthefuture,WebKitwillcontinuetosupportNetscapeplug-insbuiltwiththeMach-Oformat;nosuchassurancecanbegivenforplug-inscompiledinthePEF(CFM)format.YoucanalsodevelopanddebugMach-Oplug-insinXcode,butnotPEFplug-ins.Oncecompiled,theplug-inscanbeinstalledandusedinmostwebbrowsers.
虽然接口支持的格式,操作系统本身支持的mach-o风格,你会发现,你的插件将运行得更快,如果作为一个mach-o二进制。
在未来,它将继续支持网景插件建立的mach-o格式;没有这种保证可以得到插件编译在呼气(立方英尺)格式。
你也可以开发和调试mach-o插件在外,而不是呼气插件。
一旦编译,插件可以安装和使用在大多数网页浏览器。
UsingPlug-inScripting
使用脚本插件
ThescriptingcapabilitiesofNetscape-styleplug-insareprovidedbyextensionsontotheoriginalplug-inspecification.Theyallowabrowser(throughJavaScript)toaccessandcontrolelementsoftheplug-inanditscontent,andallowtheplug-intoaccesstheenclosingwebpageanditscontentthroughtheplug-inscriptinterface.
脚本的能力,netscape-style插件是由扩展到原来的插件规范。
他们让一个浏览器(通过代码)来访问和控制元件的插件和其内容,并允许插件访问封闭的网页和内容通过插件脚本接口。
Whenaplug-inisloaded,thebrowsercallstheNPP_GetValuecallbackinyourplug-in,whichreturnsaretainedNPObjectstructurethatrepresentyourplug-in.ThisNPObjectstructurecontainsanpointertoanNPClassstructure.TheNPClassstructurecontainsaseriesofcallbacksthatdefinetheinterfacebetweentheplug-inandthescriptingenvironment.TheNPObjectinstancerepresentsaninstanceofthatplug-inthatcanthenbeusedbythescriptingenvironment.
当一个插件加载,浏览器调用回调的npp_getvalue在你的插件,它返回一个保留npobject结构,代表你的插件。
这npobject结构包含一个指向npclass结构。
该npclass结构包含一系列的回调定义之间的接口插件和脚本环境。
该npobject实例是一个实例,插件,然后可以使用脚本环境。
Ifyouwantyourplug-intobescriptable,youneedtoreturntheappropriateretainedNPObjectbyreferenceinyourNPP_GetValuecallbackfunction.
如果你希望你的插件支持脚本,你需要返回适当的保留npobject参照你的npp_getvalue回调函数。
Note:
IncurrentversionsofWebKitandallnon-WebKit-basedbrowsers,youmustretaintheNPObjectinstanceuponreturnbydoingthefollowing:
注意:
在当前版本的Webkit和所有non-webkit-based浏览器,你必须保持npobject实例返回后做以下:
browser->retainobject((NPObject*)obj);
浏览器->retainobject((npobject*)对象);
InversionsofWebKitpriortoversion420(Safari3andearlier),theobjectsreturnedareretainedbythebrowser.Toavoidmemoryleaksinolderbrowsers,youshouldchecktheWebKitversionandavoidretainingreturnedobjectswhenyourplug-inisloadedbypriorversionsofWebKit.
在420版本之前的版本WebKit(Safari3和更早),返回的对象保留的浏览器。
避免内存泄漏在旧的浏览器,你应该检查它的版本和避免保留返回的对象时,你的插件加载事先版本WebKit。
Agooddemonstrationofaccessingplug-insfromJavaScript,aswellasalltheotherconceptsincreatingaNetscapeplug-in,canbefoundintheExamplesfolderoftheWebKitsourcecode.Seehttp:
//www.webkit.org/tolearnhowtodownloadtheWebKitsourcecode.
一个很好的演示访问插件的脚本,以及所有其他概念,创造一个网景插件,可以发现在文件夹的Webkit的源代码例子。
看到http:
//www.webkit.org/学习如何下载Webkit的源代码。
CoreGraphicsandCoreAnimationDrawingModels
核心图形和动画绘图核心模型
Safari4.0providestwonewdrawingmodels:
CoreGraphics(Quartz2D)andCoreAnimation(onlyinMacOSXv10.5andlater).Thesedrawingmodesarestronglyrecommendedgoingforwards,andifyoumoveyourplug-intocontaina64-bitslice,thatslicemustusethesedrawingmodels.(See“TransitioningaNetscape-StylePlug-into64-bit”formoreinformation.)
4提供新的绘图模式:
核心图形(石英平面)和核心动画(只在操作系统v10.5后)。
这些绘图模式是强烈建议将前锋,如果你把你的插件包含一个64位切片,切片必须使用这些模型。
(参见“过渡netscape-style插件64位”的更多信息。
)
MostoftheeffortinusingthesedrawingmodelscomesfromlearningCoreGraphicsandCoreAnimationthemselves.TolearnaboutCoreGraphics,readQuartz2DProgrammingGuide.TolearnaboutCoreAnimation,readCoreAnimationProgrammingGuide.
大部分的努力在使用这些图模型来自学习核心图形和动画本身的核心。
了解核心图形,阅读石英二维编程指南。
了解核心动画,看动画核心编程指南。
OnceyouunderstandhowtodrawthingsusingCoreGraphicsorCoreAnimation,youcanenablethesedrawingmodelsinyourNPP_Newfunctionasfollows:
一旦你了解如何借鉴的东西,使用核心图形或动画核心,你可以使这些模型在npp_new功能如下:
AddsupportinyourcodefortheCocoaeventmodel.CoreGraphicsandCoreAnimationarenotsupportedwhenusingtheCarboneventmodel.
添加支持你的代码的可可事件模型。
核心图形和动画核心不支持使用碳事件模型。
Checktoseeifthehostbrowsersupportsthedrawingmodelwiththefollowingcode:
请检查主机的浏览器支持模型与下面的代码:
NPBoolsupportsCG=false;
npboolsupportscg=假;
NPErrorerror=browser->getvalue(instance,
nperror误差=->Value(实例,
NPNVsupportsCoreGraphicsBool,
npnvsupportscoregraphicsbool,
&supportsCG);
与supportscg);
Note:
Ifyoucannotfindadrawingmodelthatboththeapplicationandyourplug-insupports,yourplug-inshouldreturnNPERR_INCOMPATIBLE_VERSION_ERROR.
注意:
如果你不能找到一个模型,应用程序和插件支持,你应该返回nperr_incompatible_version_error插件。
Setthebrowserdrawingmodelwiththefollowingcode:
设置浏览器图形模型以下代码:
if(err==NPERR_NO_ERROR&&supportsCG){
如果(错误==nperr_no_error&&supportscg){
error=browser->setvalue(instance,
误差=->的值(例如,
NPNVpluginDrawingModel,
npnvplugindrawingmodel,
(void*)NPDrawingModelCoreGraphics);
(无效*)npdrawingmodelcoregraphics);
if(err==NPERR_NO_ERROR&&supportsCG){
如果(错误==nperr_no_error&&supportscg){
/*Setstateflagsasneededto
/*设置所需的国旗
tellyourowncodetouseCoreGraphics
告诉你自己的代码使用核心图形
drawinginthefuture.*/
在未来。
*/
}
}
}
}
Afteryouhavedonethesetwothings,thebrowserfillsthewindowfieldoftheNPWindowstructurewithanNP_CGContextstructure.Thisstructurecontainstwofields,contextandwindow,whicharedefinedasfollows:
当你做到这两点,浏览器填充窗口领域的npwindow结构与np_cgcontext结构。
这个结构包含2个领域,背景和窗口,其定义如下:
typedefstructNP_CGContext
np_cgcontext树
{
{
CGContextRefcontext;
cgcontextref背景;
WindowRefwindow;
windowref窗口;
}NP_CGContext;
}np_cgcontext;
ThecontextvalueisaCoreGraphicsdrawingcontextsuitableforQuartz2Ddrawing.Formoreinformationonhowtousethiscontext,readQuartz2DProgrammingGuide.ThewindowisareferencetoanNSWindowobject.
背景值是一个核心图形绘制背景适合石英二维绘图。
更多信息关于如何使用这方面,读石英二维编程指南。
窗口是参考一个nswindow对象。
Toobtaintheboundsforyourplug-in’sdrawingregion,dothefollowinginyourNPP_SetWindowcallback:
获得跨越你的插件的绘图区域,做以下的npp_setwindow回调:
NPErrorsetwindow_cb(NPPinstance,NPWindow*npw){
nperrorsetwindow_cb(核电站的实例,npwindow*负压波){
...
…
NP_CGContext*npcontext=npw.window;
np_cgcontext*npcontext=npw.window;
CGContextRefcontext=npcontext.context;
cgcontextref背景=npcontext.context;
CGRectboundingBox=CGContextGetClipBoundingBox(context);
cgrectboundingbox=cgcontextgetclipboundingbox(背景);
...
…
TheCoreAnimationmodelissimilar,butreversed.IfyousetyousetNPNVpluginDrawingModeltoNPDrawingModelCoreAnimation,yourNPN_GetValuecallbackmustprovidearetainedCoreAnimationlayertothebrowserwhenitqueriestheNPPVpluginCoreAnimationLayervariable.
核心动画模型类似,但扭转。
如果你设置你npnvplugindrawingmodel到npdrawingmodelcoreanimationnpn_getvalue回调,你必须提供一个保留核心动画层的浏览器时,它查询nppvplugincoreanimationlayer变量。
AswiththeCoregraphicsmodel,youcanfindoutifthebrowsersupportstheCoreAnimationdrawingmodelbycheckingthevalueoftheNPNVsupportsCoreAnimationBoolvariable.Forexample:
与核心图形模型,你可以找到浏览器是否支持核心动画绘制模型通过检查价值的npnvsupportscoreanimationbool变量。
比如说呢。
NPBoolsupportsCA=false;
npboolsupportsca=假;
NPErrorerror=browser->getvalue(instance,
nperror误差=->Value(实例,
NPNVsupportsCoreAnimationBool,
npnvsupportscoreanimationbool,
&supportsCA);
与supportsca);
Out-of-ProcessPlug-Ins
进程外插件
BeginninginMacOSXv10.6,on64-bit-capablecomputers,Netscape-styleplug-insexecuteinanout-of-processfashion.ThismeansthateachNetscape-styleplug-ingetsitsownprocessseparatefromtheapplicati