Inspecting Bookmarks data with nsIRDFObserver

Japanese version of this post is also available.

nsIRDFObserver enables us to observe various events occurred at nsIRDFDataSource. The Firefox’s Bookmarks is managed internally as nsIRDFDataSource, so we can inspect it with nsIRDFObserver described below and Error Console (formally known as JavaScript Console).

First, you should define nsIRDFObserver object which observes the datasource. The two methods, “_targetToString” and “_log” are defined as original private methods.

var rdfObserver = 
{
    onAssert : function(aData, aRes, aProp, aTarget)
    {
        this._log(["onAssert", aData.URI, aRes.Value, aProp.Value, this._targetToString(aTarget)].join("
"));
    },
    onBeginUpdateBatch : function(aData)
    {
        this._log(["onBeginUpdateBatch", aData.URI].join("
"));
    },
    onChange : function(aData, aRes, aProp, aOldTarget, aNewTarget)
    {
        this._log(["onChange", aData.URI, aRes.Value, aProp.Value, this._targetToString(aOldTarget), this._targetToString(aNewTarget)].join("
"));
    },
    onEndUpdateBatch : function(aData)
    {
        this._log(["onEndUpdateBatch", aData.URI].join("
"));
    },
    onMove : function(aData, aOldRes, aNewRes, aProp, aTarget)
    {
        this._log(["onMove", aData.URI, aOldRes.Value, aNewRes.Value, aProp.Value, this._targetToString(aTarget)].join("
"));
    },
    onUnassert : function(aData, aRes, aProp, aTarget)
    {
        this._log(["onUnassert", aData.URI, aRes.Value, aProp.Value, this._targetToString(aTarget)].join("
"));
    },
    /**
     * refer to an appropriate interface for nsIRDFNode and get the string-type value
     */
    _targetToString : function(aTarget)
    {
        const Ci = Components.interfaces;
        if ( aTarget instanceof Ci.nsIRDFLiteral )
            // String type
            return aTarget.QueryInterface(Ci.nsIRDFLiteral).Value;
        else if ( aTarget instanceof Ci.nsIRDFInt )
            // Number type
            return aTarget.QueryInterface(Ci.nsIRDFInt).Value;
        else if ( aTarget instanceof Ci.nsIRDFDate )
            // Date type
            return aTarget.QueryInterface(Ci.nsIRDFDate).Value;
        else
            // And more...?
            return "";
    },
    /**
     * output string to Error Console
     */
    _log : function(aMsg)
    {
        const CONSOLE_SERVICE = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
        CONSOLE_SERVICE.logStringMessage(aMsg);
    },
};

Then, the only thing you have to do is adding the observer to Bookmarks datasource.

// get the datasource of Bookmarks
const RDF_SERVICE = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService(Components.interfaces.nsIRDFService);
var BMDS = RDF_SERVICE.GetDataSource("rdf:bookmarks");
// add the observer to datasource
BMDS.RemoveObserver(rdfObserver);
BMDS.AddObserver(rdfObserver);

References:
Interface Reference – nsIRDFObserver
Interface Reference – nsIRDFDataSource

TOP