(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)

 

posted @ 2015-07-21 10:58  coding4范儿  阅读(278)  评论(0编辑  收藏  举报