http://www.cnblogs.com/phinecos/archive/2008/04/25/1171614.html
于Mozilla平台的扩展开发(续)----XPCOM组件篇
源代码下载:HelloWorld示例.rar
在《浅谈基于Mozilla ThunderBird的扩展开发》这篇入门文章中简单介绍了基于Mozllia平台进行扩展开发的基础知识,但仍然欠缺最为重要的一种武器---没错,XPCOM!这篇文章就是为它准备的。
XPCOM是什么?
这个问题不多做解释了,相信XPCOM对于了解COM技术的人来说很快就可以上手开发了,下列是Mozilla官方给出的一些XPCOM知识的入门资源:
Creating XPCOM Components written by Doug Turner and Ian Oeschger that introduces XPCOM and illustrates how to create XPCOM components for Gecko-based applications.
An introduction to XPCOM on IBM's website by Rick Parrish.
Presentation: Introduction To XPCOM, Alec Flett (Feb 4, 2002)
Presentation: The XPCOM Library, Alec Flett (Feb 11, 2002)
Standalone XPCOM : Document on building xpcom standalone.
个人尤其推荐IBM developerworks上那5篇文章。
使用已有的XPCOM
XPCOM的使用十分简单,Mozilla平台已经为我们提供了许多功能强大的XPCOM组件了,如果你需要某方面功能的组件,请先看看Mozilla平台下是不是已经有对应的了,别再“自己造轮子了“。
关于这方面也不打算再多说了,有兴趣的朋友可以阅读IBM developerworks下面这篇文章,《实战 Firefox 扩展开发》,相信通过这样一个图片批量下载工具的开发,就会对于Mozilla平台下已有的XPCOM组件的使用有所了解的。
So,what's next?
没错,自己如何开发XPCOM组件并在扩展中使用。网上对于这方面的资料不是很多,而且没有特别完整的示例,这就是我写这篇文章的目的所在,通过一个简单的XPCOM组件的开发全过程,展示XPCOM组件的内部细节。
项目目标:
组件要实现的功能非常简单,就只提供一个做加法的接口供客户调用。
long Add(in long a, in long b);
然后在扩展中调用这个加法接口。
准备工作
0,按照《浅谈基于Mozilla ThunderBird的扩展开发》这篇文章建立起开发扩展的基本环境。
1、下载Gecko SDK
http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/mozilla1.8b1/gecko-sdk-i586-pc-msvc-1.8b1.zip ,我们需要使用它来对IDL定义进行解释。
2、创建GUID
使用微软的的guidgen 生成GUID,例如b7b04070-45fc -4635- b219-7a172f806bee
4,从C:\mozilla-build\moztools-180compat\bin下拷贝libIDL-0.6.dll,glib-1.2.dll到\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\bin下,否则运行xpidl会报错.
开发XPCOM组件
1,创建接口文件定义
#include "nsISupports.idl"
[scriptable, uuid(b7b04070-45fc -4635- b219-7a172f806bee)]
interface IMyComponent : nsISupports
{
long Add(in long a, in long b);
};
2、使用Gecko SDK 的xpidl.exe
进入xpidl所在目录,在CMD中输入命令
xpidl -m header -I ..\idl IMyComponent.idl(这里应该是IDL定义文件的实际路径)
xpidl -m typelib -I ..\idl IMyComponent.idl
如果上面执行有问题的话,可以将
\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\bin;\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\idl;\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\include;
加入到环境变量的PATH里面去。
如果上述命令执行通过,在\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\bin就会得到IMyComponent.h,IMyComponent.xpt 这2个文件。
2、创建新文件
根据IMyComponent.h创建文件MyComponent.h,MyComponent.cpp,MyComponentModule.cpp。
/**//* MyComponent.h*/
#pragma once
#ifndef _MY_COMPONENT_H_
#define _MY_COMPONENT_H_
#include "IMyComponent.h"
#define MY_COMPONENT_CONTRACTID "@mydomain.com/XPCOMSample/MyComponent;1"
#define MY_COMPONENT_CLASSNAME "A Simple XPCOM Sample"
#define MY_COMPONENT_CID {0xb7b04070, 0x45fc, 0x4635,{ 0xb2, 0x19, 0x7a, 0x17, 0x2f, 0x80, 0x6b, 0xee } }
class MyComponent:public IMyComponent
{
public:
NS_DECL_ISUPPORTS
NS_DECL_IMYCOMPONENT
MyComponent(void);
~MyComponent(void);
};
#endif
/**//* MyComponent.cpp*/
#include "StdAfx.h"
#include "MyComponent.h"
NS_IMPL_ISUPPORTS1(MyComponent, IMyComponent)
MyComponent::MyComponent(void)
{
}
MyComponent::~MyComponent(void)
{
}
NS_IMETHODIMP MyComponent::Add(PRInt32 a, PRInt32 b, PRInt32 *_retval)
{
*_retval = a + b;
return NS_OK;
}
/**//* MyComponentModule.cpp*/
#include "StdAfx.h"
#include "nsIGenericFactory.h"
#include "MyComponent.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(MyComponent)
static nsModuleComponentInfo components[] =
{
{
MY_COMPONENT_CLASSNAME,
MY_COMPONENT_CID,
MY_COMPONENT_CONTRACTID,
MyComponentConstructor,
}
};
NS_IMPL_NSGETMODULE("MyComponentsModule", components)
编译XPCOM组件
1、创建工程
使用VC2005,创建新的DLL工程,将IMyComponent.h, MyComponent.h,MyComponent.cpp,MyComponentModule.cpp添加到工程中。
2、工程配置
1)c/c++ GeneralAdditional Include Directories 中设置为\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\include
2) c/c++Preprocessor Preprocessor Definitions中加入MYCOMPONENT_EXPORTS,XPCOM_GLUE
3)c/c++Code GenerationRuntime Library中设置为Multi-threaded DLL (/MD)
,这里非常重要,否则编译会报错的!!。
4)LinkerAdditional Liberary Directoryse设置为\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\lib
5)Linker Additional Depenendies加入nspr4.lib plds4.lib plc4.lib xpcomglue.lib
3、编译生成MyComponent.dll。
在扩展中使用XPCOM组件
对《浅谈基于Mozilla ThunderBird的扩展开发》中的helloworld项目进行修改,加入一个文件夹components和一个安装文件install.js。
关于这两个东西具体的含义这里就不多做介绍了,简单点说,intall.js就是把XPCOM组件注册到Mozilla平台中去,就类似于Windows的注册表一样,从而可以使用组件。
1)Install.js的内容:
// Install script for helloworld
var err;
const APP_VERSION="0.0.0.1";//版本号
//初始化安装
err = initInstall("helloworld"+APP_VERSION, // name for install UI
"/helloworld", // registered name
APP_VERSION); // package version
if(err!=0)
{//安装出错,取消安装
cancelInstall(err);
}
//标准目录
var fProgram = getFolder("Program");//程序根目录
var fChrome = getFolder("Chrome");//chrome目录
var fComponents = getFolder("Components");//components目录
// workaround for Mozilla 1.8a3 and newer, failing to register enigmime correctly
var delComps = [ "compreg.dat" ]; // Components registry
for (var j=0; j<delComps.length; j++)
{
var delFile = getFolder(fComponents, delComps[j]);
if (File.exists(delFile))
File.remove(delFile);
}
err = getLastError();
if (err == DOES_NOT_EXIST)
{
// error code: file does not exist
resetError();
}
else if (err != SUCCESS)
{
cancelInstall(err);
}
// addDirectory: blank, archive_dir, install_dir, install_subdir
addDirectory("", "components", fComponents, "");
addDirectory("", "chrome", fChrome, "");
err = getLastError();
if (err == ACCESS_DENIED)
{
alert("Unable to write to components directory "+fComponents+".\n You will need to restart the browser with administrator/root privileges to install this software. After installing as root (or administrator), you will need to restart the browser one more time, as a privileged user, to register the installed software.\n After the second restart, you can go back to running the browser without privileges!");
cancelInstall(ACCESS_DENIED);
}
else if (err != SUCCESS)
{
cancelInstall(err);
}
else
{
// Register chrome
registerChrome(PACKAGE | DELAYED_CHROME, getFolder("Chrome","helloworld.jar"), "content/helloworld/");
err = getLastError();
if (err != SUCCESS)
{
cancelInstall(err);
}
else
{
performInstall();
}
}
2)在Componts文件夹中加入MyComponent.dll和IMyComponent.xpt
3)修改overlay.js如下:
// This is the main function
const ENIG_C = Components;
const ENIG_ENIGMAIL_CONTRACTID = "@mydomain.com/XPCOMSample/MyComponent;1"
var gEnigmailSvc = null;
function helloWorld()
{
try
{
alert("准备创建组件");
gEnigmailSvc = ENIG_C.classes[ENIG_ENIGMAIL_CONTRACTID].createInstance(ENIG_C.interfaces.IMyComponent);//创建实例
if(gEnigmailSvc!=null)
{
alert("创建组件成功");
gEnigmailSvc = gEnigmailSvc.QueryInterface(ENIG_C.interfaces.IMyComponent);
}
else
{
alert("创建组件失败");
return;
}
var res = gEnigmailSvc.Add(3, 4);
alert('Performing 3+4. Returned ' + res + '.');
alert("创建结束");
}
catch(ex)
{
alert("error");
}
}
好了,到此就完成了这个最简单的XPCOM组件的开发了,enjoy it!
Reference
1,利用VC创建XPCOM组件
2, http://enigmail.mozdev.org/home/index.php
需要完整代码的,请发email至phinecos@163.com,也欢迎有兴趣的朋友们一起来交流Mozilla的扩展开发技术
分享到:
相关推荐
本分详细对Mozilla的结构和对其进行扩展的基本步骤。
XML语言语法 Javascript等在Mozilla的应用
基于 Mozilla firefox 的扩展开发教程。如果你对firefox插件感兴趣,或许你也可以成为开发中的一员。
介绍了Mozilla扩展开发的基本知识和开发过程。
非常适合入门级别的Mozilla开发人员,介绍详细,界面整洁,对Mozilla或者火狐开发的人员来说是非常好的
Mozext Mozilla Firefox和Mozilla Thunderbird的扩展/附加组件请检查我的专用网站以获取更多详细信息: → 目录WebExtensions / MailExtensions | 请检查每个文件夹中的特定自述文件以获取构建说明。雷鸟 火狐浏览器...
osbpm企业应用开发平台已加入openEA定向开源计划,平台由广州市联科软件股份有限公司自主研发,严格遵照开源协议Mozilla Public License(MPL) 2.0开放源代码。为保证质量,减少企业应用风险,在初期实施定向开源...
莫佐瓦尔概述mozoval 项目是一组由 Mozilla 开发的实验性 OVAL 安全处理模块。 正在积极开发的主要工具集是基于 Go 的 OVAL 库和相关的命令行处理器。 该处理器可以读取已发布的 OVAL 检查的某些元素并返回结果。...
丰富的扩展和出色的性能使这款软件变得非常优秀,由于完全专注于单独邮件客户端的开发,开发者可以摒弃其他那些不需要的组件,尽最大可能把这个客户端程序做完美。软件界面美观简洁、简单全面、实用方便,可快速上手...
Mozilla与展讯的合作将扩展至展讯全系列芯片组.pdf
Mozilla Firefox,通称Firefox,中文也通称火狐,是一个自由及开源的网页浏览器[14],由Mozilla基金会及其子公司Mozilla公司开发。Firefox支持Windows、macOS及Linux,其移动版支持Android及Firefox OS,这些版本的...
代笔0.2.1 适用于Chrome的CasperJS扩展程序(Mozilla **即将推出**)开发人员工具快速设定(Google Chrome) 从Chrome网上应用店下载的。 全局安装(是否需要本地插件安装?请参阅)。 npm install -g grunt-...
企业应用开发平台介绍osbpm企业应用开发平台已加入openEA定向开源计划,平台由广州市联科软件股份有限公司自主研发,严格遵照开源协议Mozilla Public License(MPL)2.0开放源。为保证质量,减少企业应用风险,在...
一个Mozilla Firefox扩展程序,允许快速访问您的Google-dorking结果gDork一个Mozilla Firefox扩展程序,允许快速访问您的google-dorking结果安装使用“发布”选项卡或* .xpi文件从“ web-ext”下载该扩展程序-...
Mozilla 3e 日历提供程序 (发音为“threee”)是一套客户端和服务器应用程序,它们共同提供...开发环境这个扩展依赖于 Mozilla Build System 和 Mozilla Thunderbird 以及 Mozilla Calendar 头文件。 不幸的是,Mozill
Firebug是网页浏览器 Mozilla Firefox 下的一款开发类扩展,现属于Firefox的五星级强力推荐扩展之一。它集HTML查看和编辑、Javascript控制台、网络状况监视器于一体,是开发JavaScript、CSS、HTML和Ajax的得力助手。...
入门运行以下命令以安装依赖项并开始开发yarn install yarn dev脚本yarn dev-在监视模式下运行webpack yarn storybook-运行Storybook服务器yarn build-构建可立即投入生产的扩展纱线测试-u-运行Jest +更新测试快照纱
不管是开发网页中的JavaScript部分,还是进行火狐扩展开发,Venkman都能够提供非常强大的调试功能。 当然现在,Firebug也提供了越来越强大的调试功能。 还有个比较老的Trick,可以在JavaScript程序中设置断点,...
Windows+VB.Net开发FireFox浏览器扩展(Extension)并实现sendNativeMessage消息示例。 需要在注册表[HKEY_CURRENT_USER\Software\Mozilla\NativeMessagingHosts]下增加一个demo项,默认值为Demo.json的完整路径。...
Mozilla Firefox- 谷歌浏览器-https: Brave-(运行Google Chrome扩展程序) 通过HTTP响应标头从PHP登录到浏览器开发人员工具。 支持的协议: FirePHP(基于Wildfire) 有关受支持的服务器库的列表,请参见 。 ...