您的位置:首页 > 移动开发 > Android开发

【Android浏览器插件开发准备工作之NPAPI】使用Visual Studio 2008 运行Firefox浏览器插件实例npruntime

2011-05-10 09:46 941 查看
最近在研究Android上浏览器插件..资料比较少..基本都是从研究NPAPI开始的..那么我也从这开始吧...

由于这篇文章是边做实验边记录,加上本身对VS不太了解,可能有些地方不对,欢迎大家指正!!





npruntime
参照下述步骤一步一步来:

Build

1.CreateanewprojectinVisual
StudioforaWin32GUIlibrary(DLL)(in.NET2003:Win32template,thenswitchtoDLLinApplicationSettingsinthefollowingdialog,export
symbolstoo?)(inVisualStudio2008,itisVisualc++|Win32|Win32Project,then
checkDLLinthewizard).
2.Ifawizardgivesyoua
checkboxtocreateanemptyproject,then
checkit.Otherwiseyou'lldeletefileslater.
3.Againnotethattheresulting
DLLfilenamemuststartwith"np",
soeithercallyourprojectlikethisorrenamethefilelater
4.Deletethe.cppand.handReadMefilesfromtheprojectanddisk(if
youdidnotcreateanemptyproject)
5.Copythenpruntimesample
pluginsourcecodeintothedirofthenewVSprojectandaddthefilestothe
projectusingtheVSGUI(.cppfilesto"SourceFiles",.hfilesto"HeaderFiles",.rcfileto"ResourceFiles").
Samplescanbeobtainedfrom:https://developer.mozilla.org/en/Plugins/Samples_and_Test_Cases
6.DownloadtheGecko
SDK
(aka
XULRunnerSDK)frommozilla.orgreleaseFTPandextractit.Youcandownloadit
fromhere:http://developer.mozilla.org/en/docs/Gecko_SDK
7.AddtheGecko
SDKincludepath
(example:
C:/xulrunner-sdk/sdk/include)toProjectProperties|(all
configurations)|C++|General|AdditionalIncludeDirectories
.Note:
Ifyourprojectisstillempty,theC++treemightnotbevisible.So,add
somefilesfirst.
8.Addthefollowingpreprocessor
definitionstoProjectProperties|(all
configurations)|C++|Preprocessor|PreprocessorDefinitions:
WIN32;_WINDOWS;XP_WIN32;MOZILLA_STRICT_API;XPCOM_GLUE;XP_WIN;_X86_;NPSIMPLE_EXPORTS

9.Disableprecompiledheaders
usingProjectProperties|(all
configurations)|C++|Precompiledheaders|Create/Useprecompiledheader
.
Theymaybealreadydisabled.
10.Definethefunctionexportsby
addingthe.deffilename(e.g.nprt.def)toProject
Properties|(allconfigurations)|Linker|Input|ModuleDefinitionFile
.
Itcouldbeeitherthefullpathorthepathrelativetotheprojectdirectory.
11.Optional:Opentheabove.deffileandchange"NPRT"to
thefilenameofyourdllasVSseesit(without"np",ifyoudecided
torenamelater)
12.Optional:Editthe.rcfileandandthetopofnpp_gate.cppforthedescription,mimetype,file
extensionetc.toreflectyourplugin
13.Removethefunction
NPP_GetJavaClass
fromnpp_gate.cpp
14.Build
15.RenametheresultingDLLso
thatthefilenamestartswith"np"
andendswith".dll"(or"32.dll"?8.3?)andcopyitin
Mozilla's"plugins"folder
16.StartMozillaandopenabout:pluginstoverifythepluginisdetected
17.Openthefile
"test.html"andbegintesting.Makesurethemimetypesofyourhtml
embedtagsmatchthemimetypespecifiedinyournprt.rcfileandthetopofyournpp_gate.cppfile
第一步:



第二步:



(因为选择的空项目,第三、第四步省略)
第五步:导入源码
npruntime源码结构:



下载地址:http://mxr.mozilla.org/seamonkey/source/modules/plugin/samples/npruntime/

重要:
调查到最后发现,上面不知道是对应什么版本的。与我下载的SDK1.9.2不匹配。所以在下载npruntime源代码的时候,同时注意选择SDK的版本。
对应GeocSDK1.9.2的npruntime源码位置:
http://mxr.mozilla.org/mozilla1.9.2/source/modules/plugin/sdk/samples/npruntime/

Moliza1.9.2版本的npruntime源码结构:(结构是一样的,关键里面代码不一样。郁闷死我了!)



导入后的项目目录结构:





其中resource.h和npxx1.rc和源码中的可能有点不一样的。

resource.h是创建npxx1.rc后自动产生的。


第六步&第七步:
给项目配置Gecko
SDKincludepath
(example:
C:/xulrunner-sdk/sdk/include)。
配置方法:ProjectProperties|(all
configurations)|C++|General|AdditionalIncludeDirectories

注意:如果项目还是空的,那么就无法看到C++这个选项,所以先加几个文件吧。

空项目时的[b]ProjectProperties:[/b]




追加完Source文件后的[b]ProjectProperties:[/b]




追加路径的窗口:





第八步:
添加预处理器定义,把你没有的添加进去即可!




第九步:
不使用预编译头。(一般默认就是不使用了)




第十步:模块定义文件配置
Npruntime中有个def文件,之前没有用,现在派上用场了。我把它暂时放在了E盘。使用绝对路径来配置。




第十一步:编辑上面的def文件,重命名DLL.




注意,这边的library名字最好要和resoiurce文件一样!这边我懒得重新截图了!

第十二步:修改.rc文件和
npp_gate.cpp文件
(
description,
mimetype,fileextension)
第十三步:npp_gate.cpp文件删除
NPP_GetJavaClass
方法

第十四步:
编译

第十五步:重命名
DLL
,一定要以
np
开头,以
.dll
结尾



最后附上注意事项,编译时如果有错,也请看下面的介绍:


Version
Issues


1.Ifyou
areusingGeckoSDKv1.9andhigher,you'llprobablyneedtoaddfolders/plugin,/nspr,and/javaasincludeddirectories(asseenabove,gotoProjectProperties|(all
configurations)|C++|General|AdditionalIncludeDirectories
).ThesedirectoriesarecontainedintheGeckoSDK
includepaththatyoupreviouslyadded.
2.IfVC++
compilerthrowsyouerrorC2664on'DrawText'functioncall,youmayreplaceitby
'DrawTextA'.Infact,allwin32APIfunctionsdealingwithcharacterstrings
canbeaddedan'A'totheendtoavoidunicodecasterrors.
3.Visual
C++2008Expressdon'tsupportC99standardaboutint32_t,uint32_t.Youhavetoadd#include"nptypes.h"intopofplugin.hfile.Forxulrunner1.9.0.1SDK,npapi.hfileusesint32,uint32whichisdifferentfromint32_t,uint32_tdefinedinnptypes.h,anditmaycauseproblemsinlinkstage.
4.Forxulrunner1.9.0.1SDK,youmaynotfindnpfunctions.hinincludedirectories.Youhavetoreplaceallnpfunctions.hbynpnpp.hin#includeline.
5.Feelfree
toappendhereyourissuesfixesiftheaboveguidehelpedyou.

第二条:在SDK1.9.2版本编译时遇到了。

第三条补充:
xulrunner1.9.0.1SDK以上,nptypes.h中定义的都是int32_t,uint32_t这样,而不是int32,uint32。所以npruntime代码中需要相应的修改。
(如果下载的源码与SDK的版本相对应,应该不存在这个问题)

第五条补充:
xulrunner1.9.0.1SDK中不存在npfunctions.h,但是在xulrunner1.9.2SDK中又包含了该文件,反而没有npnpp.h。

总结:(造成打开资源文件时RC1004错误,编译时会造成Error
PRJ002
等错误,花了我不少时间)
拷贝npruntime这一步非常重要,我直接Ctrl+cCtrl+v来拷贝,最后发现错误一堆。
所以自己一个一个创建文件吧,然后把代码贴过去。

另外里面的资源文件nprt.rc最好要跟你的项目名相同。千万注意这边不是单纯的把这个源文件拷贝过去。
另外resource.h会由VS2008自动生成,不需要你修改(也不要去拷贝源码中的文件)。






好了,到此所有操作都结束了,运行后会得到一个dll文件。我这边resource文件名为npxx.rc在def文件中配置的library名也是npxx,所以我得到的是npxx.dll..(这两句话感觉好二...)



把这个dll文件拷贝到Firefox的安装目录下的plugins目录下即可。



安装完后,在Firefox的地址栏中输入:about:plugins,就可以看到这个插件了。







下面打开测试网页,就可以看到效果了。







最后附上测试网页代码:

<HTML>
<HEAD>
<TITLE>ScriptablePlug-inTest</TITLE>
</HEAD>
<BODYid="bodyId">

<center>
<h1>SampleScriptablePlug-in</h1>
</center>

Thispagecontainsatestcasewhichdemonstratestheworkof
scriptable4.xstyleNavigatorplug-inwithMozilla.Theexample
plug-inoccupiesthearearightbelowthistext,andyoushouldseea
frametheplug-indrawsarounditswindow.Belowtheplug-inwindow
therearetwobuttons.Clickingonthebuttonswillresultincalling
nativepluginmethodsfromJavaScript.ShowVersionwillinstructthe
plug-intoretrievetheMozillauseragentstringanddisplayitin
theplug-inwindow,Clearbuttonwillcallplug-inmethodtoerasethe
window.

<br><br>

<center>

<mce:scripttype="text/javascript"><!--
functionbar(arg)
{
document.getElementById("result").innerHTML+="<p>"+"functionbar("+arg+")called!"+"</p>";

return4;
}

//--></mce:script>

<divid="result">
<p>resultsgohere:</p>
</div>

<embedid="embed1"type="application/mozilla-npruntime-scriptable-plugin"width=600height=40><br>

<embedtype="application/x-java-mozilla-npruntime-scriptable-plugin"style="display:block;width:50%;height:100px;"><br>

<mce:scripttype="text/javascript"><!--
varembed1=document.getElementById('embed1');

//--></mce:script>

<br>
<formname="formname">
<inputtype=buttonvalue="Callpluginobj.foo()"onclick='alert(pluginobj.foo())'>
<inputtype=buttonvalue="alert(pluginobj.bar)"onclick='alert(pluginobj.bar)'>
<inputtype=buttonvalue="alert(pluginobj('foo'))"onclick='alert(pluginobj("foo"))'>
<br>
<inputtype=buttonvalue="alert(embed1.bar)"onclick='alert(embed1.bar)'>
<inputtype=buttonvalue="alert(embed1.foo())"onclick='alert(embed1.foo())'>
<inputtype=buttonvalue="alert(embed1())"onclick='alert(embed1())'>
<inputtype=buttonvalue="embed1.bar='foo'"onclick='embed1.bar="foo";'>
</form>

</center>

</BODY>
</HTML>






参考文献(Mozila官方文档):
http://code.google.com/chrome/extensions/npapi.html
https://developer.mozilla.org/en/Plugins
https://developer.mozilla.org/en/Gecko_Plugin_API_Referencehttps://developer.mozilla.org/en/Gecko_Plugin_API_Reference/Preface#ThePlug-insoftwaredevelopmentkit

个人博客:
http://blog.csdn.net/qyqzj/archive/2010/06/14/5670816.aspx
http://blog.csdn.net/qyqzj/archive/2010/07/04/5712151.aspx
http://www.blogjava.net/anymobile/articles/328592.html?opt=admin
http://blog.csdn.net/qyqzj/archive/2010/05/30/5634532.aspx
http://blog.csdn.net/qyqzj/archive/2010/08/03/5786313.aspx
http://blog.sina.com.cn/s/blog_6d1656130100ml5t.html

NPAPI帮助理解:
http://blog.csdn.net/hanyuxinting/archive/2010/10/28/5972428.aspx
http://blog.csdn.net/hanyuxinting/archive/2010/10/29/5973630.aspx
http://blog.csdn.net/hanyuxinting/archive/2010/12/10/6068384.aspx
http://blog.csdn.net/vinilarain/archive/2010/03/03/5343170.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: