jb’s open source blog

A look into my work and play in open source

XPCOM lab (FirstXpcom)

leave a comment »

First off, let me start by saying despite reading about it for 20 minutes, before starting I had no idea what the purpose of XPCOM was. Wikipedia’s articles on COM and CORBA gave me an idea of what problem it tries to solve: the ability to “export” functionality in a language neutral way through interfaces, or contracts. The XPCOM article paints a different picture, calling XPCOM the reason Mozilla is an “actual framework”, because it offers features such as file abstraction, memory management. The Zenit wiki’s definition is much more concise:

XPCOM is a framework for developing cross platform software that can be written in C, C++, JavaScript with extensions for Perl and Python.You can access the XPCOM component through the web or any Mozilla application (similar to the idea of accessing web services).

After diving into the lab I think I have a better feel for it. It appears (one possible use for) XPCOM is to expose C++/C methods to Javascript. This is what the lab covered.

The gory details:

  • Create an interface specification in XPIDL (.idl) format: one attribute (name), one method (add): IFirstXpcom.idl
  • Create a (bunch of) Makefile.in file(s) to make the build system automatically translate our .idl to .cpp/.h:

  • Create an install manifest (install.rdf)
  • Use make-makefile to create a Makefile inside objdir/extensions/firstxpcom
  • Run make from objdir/extensions/firstxpcom
  • Watch it fail. Look in objdir/dist/include/firstxpcom. A header file, IFirstXpcom.h, was generated.
  • Copy/paste implementation stub from IFirstXpcom.h into extensions/firstxpcom/src/FirstXpcom.cpp. Replace _MYCLASS_ with FirstXpcom.
  • Run make in objdir/extensions/firstxpcom. (It fails.)
  • Add #include "IFirstXpcom.h" at the top of FirstXpcom.cpp.
  • Run make in objdir/extensions/firstxpcom. (Success!)
  • Look at IFirstXpcom.h, make observations:
    • AString => nsAString
    • long => PRInt32
  • “Write a bit of code to get component registered”. Copy and paste from here. Read explanations here.
  • Run make. (It fails.)
  • Change line mName.Assign(L"FirstXpcom Component"); to mName.AssignLiteral("FirstXpcom Component");. (Thanks mmullin/gavin).
  • Run make. (Success!)
  • Run objdir/dist/bin/firefox -no-remote -profilemanager.
  • (!) Minefield could not install this item because “install.rdf” (provided by the item) is not well-formed or does not exist.
  • Open install.rdf in vi. Fix indentation (gg=G), save. No more install.rdf errors. (No idea how that fixed it, but it did.)
  • Use this tip from Rueen to install Extension Developer’s Extension in Minefield without security warnings.
  • Open the Javascript shell and access the FirstXpcom component.

FirstXpcom in the Addon Manager
FirstXpcom in the Addon Manager

FirstXpcom and the Javascript shell
FirstXpcom and the Javascript shell

firstxpcom_jb.tar.bz2 – all files from this lab

Advertisements

Written by jbopensrc

November 15, 2007 at 3:27 am

Posted in Open Source

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: