(FFOS Gecko) - several ways of registering a XPCOM Component

1. JavaScript Component

  (1) add a CustomComponent.manifest

# The {classID} here must match the classID in CustomComponent.js
component {e6b866e3-41b2-4f05-a4d2-3d4bde0f7ef8} components/CustomComponent.js
contract @foobar/customcomponent;1 {e6b866e3-41b2-4f05-a4d2-3d4bde0f7ef8}
category profile-after-change CustomComponent @foobar/customcomponent;1

  (2)  export a NSGetFactory() function

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function CustomComponent() {
}
CustomComponent.prototype = {
  // this must match whatever is in chrome.manifest!
  classID: Components.ID("{e6b866e3-41b2-4f05-a4d2-3d4bde0f7ef8}"),
  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsICustomComponent]),
  /* nsICustomComponent implementation goes here */
  ...
};

// The following line is what XPCOM uses to create components. Each component prototype
// must have a .classID which is used to create it.
if (XPCOMUtils.generateNSGetFactory) {
    const NSGetFactory = XPCOMUtils.generateNSGetFactory([CustomComponent]);
    this.NSGetFactory = XPCOMUtils.generateNSGetFactory([CustomComponent]);
} else {
    // for Mozilla 1.9.2 (Firefox 3.6)
    var NSGetModule = XPCOMUtils.generateNSGetModule([CustomComponent]);
}

  (3) add to moz.build

# EXTRA_COMPONENTS installs components written JavaScript to
# dist/bin/components
EXTRA_COMPONENTS += [
    ‘CustomComponent.js‘,
    ‘CustomComponent.manifest‘,
]

reference: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/XPCOMUtils.jsm

2. C++ Component

copy from gecko/xpcom/sample/nsSampleModule.cpp

////////////////////////////////////////////////////////////////////////
// Define the contructor function for the object nsSampleImpl
//
// What this does is defines a function nsSampleImplConstructor which we
// will specific in the nsModuleComponentInfo table. This function will
// be used by the generic factory to create an instance of nsSampleImpl.
//
// NOTE: This creates an instance of nsSampleImpl by using the default
//         constructor nsSampleImpl::nsSampleImpl()
//
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSampleImpl)

// The following line defines a kNS_SAMPLE_CID CID variable.
NS_DEFINE_NAMED_CID(NS_SAMPLE_CID);

// Build a table of ClassIDs (CIDs) which are implemented by this module. CIDs
// should be completely unique UUIDs.
// each entry has the form { CID, service, factoryproc, constructorproc }
// where factoryproc is usually nullptr.
static const mozilla::Module::CIDEntry kSampleCIDs[] = {
  { &kNS_SAMPLE_CID, false, nullptr, nsSampleImplConstructor },
  { nullptr }
};

// Build a table which maps contract IDs to CIDs.
// A contract is a string which identifies a particular set of functionality. In some
// cases an extension component may override the contract ID of a builtin gecko component
// to modify or extend functionality.
static const mozilla::Module::ContractIDEntry kSampleContracts[] = {
  { NS_SAMPLE_CONTRACTID, &kNS_SAMPLE_CID },
  { nullptr }
};

// Category entries are category/key/value triples which can be used
// to register contract ID as content handlers or to observe certain
// notifications. Most modules do not need to register any category
// entries: this is just a sample of how you‘d do it.
// @see nsICategoryManager for information on retrieving category data.
static const mozilla::Module::CategoryEntry kSampleCategories[] = {
  { "my-category", "my-key", NS_SAMPLE_CONTRACTID },
  { nullptr }
};

static const mozilla::Module kSampleModule = {
  mozilla::Module::kVersion,
  kSampleCIDs,
  kSampleContracts,
  kSampleCategories
};

// The following line implements the one-and-only "NSModule" symbol exported from this
// shared library.
NSMODULE_DEFN(nsSampleModule) = &kSampleModule;

// The following line implements the one-and-only "NSGetModule" symbol
// for compatibility with mozilla 1.9.2. You should only use this
// if you need a binary which is backwards-compatible and if you use
// interfaces carefully across multiple versions.
NS_IMPL_MOZILLA192_NSGETMODULE(&kSampleModule)
时间: 2024-08-22 03:07:02

(FFOS Gecko) - several ways of registering a XPCOM Component的相关文章

(FFOS Gecko) - several ways of using a XPCOM Service

1. Using a service in C++ nsCOMPtr<nsIServiceManager> servManager; nsresult rv = NS_GetServiceManager(getter_AddRefs(servManager)); if (NS_FAILED(rv)) { // get ServiceManager error return -1; } // get real Service nsCOMPtr<nsICustomService> cu

(FFOS Gecko) - several ways of exposing a module to navigator

当我们实现了一个module,并且通过navigator.mozCustomModule访问 根据module的实现方式的不同,有几种不同的做法. 1. XPCOM component(Javascript实现): 在chrome.manifest中配置(加粗部分): component {ComponentID} CustomModule.js contract ContractID {ComponentID} category JavaScript-navigator-property mo

(FFOS Gecko &amp; Gaia) OTA - Do real check

虽然代码分析了很多,但是还没有真正做check的工作,下面就来找到真正的checker. 代码位置:gecko/toolkit/mozapps/update/nsUpdateService.js.参考之前的(FFOS Gecko & Gaia) OTA - 代码模块总览,nsUpdateService.js中的Checker对象,实现了nsIUpdateChecker这个interface.下面就来分析Checker对象的实现. 1.  checkForUpdates函数: 实现略长,在代码中添

(FFOS Gecko &amp; Gaia) OTA - 又回到UpdatePrompt

当download完成时,又回到了UpdatePrompt,因为此时需要提示user下载完成,确认apply. 1. UpdatePrompt.showUpdateDownloaded showUpdateDownloaded: function UP_showUpdateDownloaded(aUpdate, aBackground) { if (this._systemUpdateListener) { this._systemUpdateListener.onUpdateReady();

(FFOS Gecko &amp; Gaia) OTA - 进入Gecko层

代码位置:gecko/b2g/components/UpdatePrompt.js SystemApp发出'force-update-check'事件,也就从gaia进入到了gecko层. 1. 首先大概介绍一下UpdatePrompt.js. (a) UpdatePrompt.js里包含了2个对象,分别是UpdatePrompt和UpdateCheckListener. (b) UpdatePrompt实现了“@mozilla.org/updates/update-prompt;1”这个XPC

(FFOS Gecko &amp; Gaia) OTA - 代码模块总览

OTA整体框架里会涉及以下代码,从gaia到gecko都有,而且由于历史原因,复用了desktop browser的一些模块,还有一些冗余代码,分析时走了不少弯路. 1. Gaia部分 (a) gaia/apps/settings/js/panels/about/update_check.js 这个Settings中OTA功能的入口,通过AMD(Asynchronous Module Definition)规范,定义了一个模块UpdateCheck. (b) gaia/apps/system/j

(FFOS Gecko &amp; Gaia) OTA - 重回Gaia层

SystemApp中的UpdateManager作为gaia和gecko的通信桥梁,会接收gecko中UpdatePrompt发送的'update-available'事件. 1. UpdateManager.handleEvent var detail = evt.detail; if (detail.type && detail.type === 'update-available') { // systemUpdatable是在UpdateManager初始化时new出来的,Upda

(FFOS Gecko) - useful MACRO

NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS is used for classes that have JS references or a mix of JS and C++ references to report.  This participant has Trace, Traverse, and Unlink methods.

(FFOS Gecko &amp; Gaia) OTA - 关键的apply

这篇分析已经是尾声了,在UpdatePrompt中,调用了UpdateService的applyOsUpdate函数. 1. UpdateService.applyOsUpdate 这个函数很简单,就是获取到update.zip以后,调用recovery service去完成更新. applyOsUpdate: function AUS_applyOsUpdate(aUpdate) { if (!aUpdate.isOSUpdate || aUpdate.state != STATE_APPLI