Introduction

This article is to show you how to create a new protocol for Mozilla Firefox browser. Mozilla Firefox supports the generic web protocols, such as HTTP and FTP and it also support protocols like Chrome of its own. It is sometime required to make a custom protocol for your project which will be plugged with the existing Firefox installation.

To create a new protocol, you’ll need to implement an XPCOM component. XPCOM is very similar to MS COM (and in this article basics of the XPCOM will not be covered, you can find so many good articles and online books on XPCOM). You can use JavaScript or C++ to implement XPCOM. I’ve found a very good article written by Doron Rosenberg which tells you how to build a new protocol for Mozilla by using JavaScript.

In this article we’ll use C++ to build the XPCOM for the protocol. The intension of this article is to give you a very simple sample of custom protocol written in C++ when ever you are in hurry to create a custom protocol for Mozilla Firefox and you can reuse most of the code and can concentrate on your logic about the functionality of your protocol rather than searching here and there about the basic framework of a Mozilla protocol.

One other place you can find implementation of protocol in C++ is in Mozilla’s site and it is a little bit complex and less documented.

What does this sample protocol do?

The sample for this article builds will be “find:” protocol which will use the user's default search engine to initiate a search, same as the “x-search:” protocol which Doron Rosenberg created in JavaScript.

When this protocol is installed in Mozilla Firefox, it can be called by typing

find:<search term>

Or

find://<search term>

into the URL bar and pressing enter.

How to install this protocol?

Just download the file “findprotocol.xpi” and open it with Mozilla Firefox. Firefox will install this as a new protocol. .xpi file is nothing but a .zip file renamed as .xpi.

Alternatively, you can copy the XPCOM which is a DLL file to “components” folder of existing Mozilla Firefox installation and then delete the “Compreg.dat” file from “C:\Documents and Settings\<user>\application Data\Mozilla\Firefox\Profiles\<wtimmm.default>" and restart Firefox, Firefox will register the new XPCOM DLL.

How to compile the source?

This sample is compiled with VC++ 6 with service pack 5 installed. You need to build Mozilla Firefox because after building the Firefox you’ll get all the headers and libs you need to create XPCOM. Building Mozilla is not a trivial task, you can find many guidelines on net to build Mozilla. After you build Mozilla it’ll product a folder called “Dist” which will contain all the libs and headers which are to be linked with your code to build this.

Note:- This code compiles cleanly without any warnings at the Warning level 3. It flashes warning at the Warning level 4.

Using the code

To create a new protocol, you have to implement nsIProtocolHandler interface. The protocol handler is a class which implements nsIProtocolHandler. It is responsible for returning basic information about the protocol to the network library (such as the default port and URI format) and for creating the appropriate type of channel to handle the request.

There are four areas you may need to customize to use this for your protocol:

GetScheme

Change this to return the prefix that your protocol uses.

GetDefaultPort

Change this to return the default port that your protocol runs on. For protocols that do not use a port, you can return -1. This example doesn’t use any port so it is given -1.

NewURI

Here NS_SIMPLEURI_CID is used, which parses simple URLs, such as those without a path. This function gets the whole typed URL. When ever user types “find://<anything>”, this function will be called by Firefox and the string will be on the first parameter of this function. Here is the code:

You can also implement nsIChannel like the finger protocol example. There is another class used here in this sample, CBuildSearchString, to extract the search term from the total URL and to build the final URL string.

Conclusion

I hope you guys find this sample helpful when you are going to prepare a protocol for Mozilla Firefox. You can contact me, if you have anything to ask or whatever.

Credits/Acknowledgements

Mr. Doron Rosenberg, for the simple implementation of a protocol in JavaScript.

Mr. Prasadarao and Mr. Dileep C. George for building Mozilla Firefox.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

2. Then I figured out that deleting compreg.dat and xpti.dat from D:\<your doc folder>\..Data\Mozilla\Firefox\Profiles\ and restarting mozilla 2.0.0.x and upwill auto-register the components, (*.dll and *.xpt) previously copied in <mozilla>/components/ folder

3. Then I get from crap-zilla the following message box 'Components.classes[cid] has no properties'

Instead of returning new channel with (new URL) i just want to launch an application from the current client machine by providing local application path (say eg path to caclulator.exe) is it Possible scenario ??????

Gecko--the open source web browser toolkit--can be used to create XPCOM components.It provides all the header file, idl file and library files that are needed to create anXPCOM component. It is easy than compliling Firfox to get the files needed.

You should not get those errors, if you are able to build firefox cleanly, then add its libs and header to the VC++ 6.0 projects. What I found from the compilation errors most of them are string related.Please See the latest mozila strings APIs and classes if the existing code needs to be modified.The source code I wrote in VC++ 6.0 supplied in the article.The code supplied there is now about 1 year old, just verify if some chenges has happened in the Mozilla string functions.Regards

"7 You shall have no other gods before me. 8 You shall not make an images in order to bow down to them or serve them. 11 You shall not take the name of the LORD your God in vain. 12 Observe the sabbath day 16 Honor your father and your mother, that your days may be prolonged. 17 You shall not kill. 18 Neither shall you commit adultery. 19 Neither shall you steal. 20 Neither shall you bear false witness against your neighbor. 21 Neither shall you covet anything that's your neighbor's." Your God

I think you can’t compile the source unless you build Firefox source, I also tried with Gecko, and you will get those headers when you’ll compile the IDLs. The best way is to compile the Firefox source and you’ll get all your library, headers in Dist folder.