Saturday, November 24, 2007

Week 4: Source Code Reading Lab

Bookmarks > Bookmark This Page...

Search Using LXR:
I selected the "Bookmarks > Bookmark This Page..." top-level browser action to search and trace through the code to better understand where the code is located and how it works. I started with a text search at http://lxr.mozilla.org/mozilla/ . My search produced the following resuls:

/browser/locales/en-US/chrome/browser/browser.dtd, line 58
<!ENTITY addCurPageAsCmd.label "Bookmark This Page...">

/browser/locales/en-US/chrome/browser/browser.dtd, line 234
<!ENTITY bookmarkPageCmd.label "Bookmark This Page...">

/browser/locales/en-US/chrome/help/menu_reference.xhtml, line 303
<h3 id="add_to_bookmarks">Bookmark This Page...</h3>

/browser/locales/en-US/chrome/help/firebird-toc.rdf, line 360
<rdf:li><rdf:Description ID="menu-bookmarks-add-to-bookmarks" nc:name="Bookmark This Page..." nc:link="menu_reference.xhtml#add_to_bookmarks"/> </rdf:li>

The first two results are entity references within the browser.dtd file of the Mozilla browser and appear to be more relevant to the "Bookmark This Page.." functionality of the browser. The third result is an XHTML file containing help information from the browser's help menu. The fourth result appears to be more relevant to Firebird.

Next, I searched using the term "bookmarkPageCmd.label". One of the results was in the browser directory:

/browser/base/content/browser-context.inc, line 127
label="&bookmarkPageCmd.label;"

I found that the oncommand attribute of the <menuitem> called a function named bookmarkThisPage(), so I did a search of this function. I found a JavaScript file (/browser/base/content/nsContextMenu.js, line 1142) with the following code:

bookmarkThisPage: function CM_bookmarkThisPage() {
window.top.PlacesCommandHook.bookmarkPage(this.browser,
PlacesUtils.bookmarksMenuFolderId, true);
},

I saw that this function called another function named bookmarkPage() so I searched for that function and I found a JavaScript file (/browser/base/content/browser-places.js, line 173) with the following function definition:

/**
* Adds a bookmark to the page loaded in the given browser.
*
* @param aBrowser
* a element.
* @param [optional] aParent
* The folder in which to create a new bookmark if the page loaded in
* aBrowser isn't bookmarked yet, defaults to the unfiled root.
* @param [optional] aShowEditUI
* whether or not to show the edit-bookmark UI for the bookmark item
*/
bookmarkPage: function PCH_bookmarkPage(aBrowser, aParent, aShowEditUI) {
var uri = aBrowser.currentURI;
var itemId = PlacesUtils.getMostRecentBookmarkForURI(uri);
if (itemId == -1) {
// Copied over from addBookmarkForBrowser:
// Bug 52536: We obtain the URL and title from the nsIWebNavigation
// associated with a rather than from a DOMWindow.
// This is because when a full page plugin is loaded, there is
// no DOMWindow (?) but information about the loaded document
// may still be obtained from the webNavigation.
var webNav = aBrowser.webNavigation;
var url = webNav.currentURI;
var title;
var description;
try {
title = webNav.document.title;
description = PlacesUtils.getDescriptionFromDocument(webNav.document);
}
catch (e) { }

var parent = aParent != undefined ?
aParent : PlacesUtils.unfiledBookmarksFolderId;
var descAnno = { name: DESCRIPTION_ANNO, value: description };
var txn = PlacesUtils.ptm.createItem(uri, parent, -1,
title, null, [descAnno]);
PlacesUtils.ptm.commitTransaction(txn);
if (aShowEditUI)
itemId = PlacesUtils.getMostRecentBookmarkForURI(uri);
}

if (aShowEditUI) {
// dock the panel to the star icon when possible, otherwise dock
// it to the content area
if (aBrowser.contentWindow == window.content) {
var starIcon = aBrowser.ownerDocument.getElementById("star-button");
if (starIcon && isElementVisible(starIcon)) {
this.showEditBookmarkPopup(itemId, starIcon, "after_start");
return;
}
}
this.showEditBookmarkPopup(itemId, aBrowser, "overlap");
}
},

Note about LXR: It provides an option for regular expression searches.

Search Using MXR:
I conducted a similar text search for the text "Bookmark This Page..." using the Firefox branch at MXR (http://mxr.mozilla.org/firefox/). I followed a similar progression during my search through files that were the same as that of my previous search. I found this search mechanism to be more helpful than LXR because it showed a summary of where the bookmarkPage() function is defined and in what files it is referenced:

Defined as a function in: Referenced (in 3 files total) in:
Like LXR, MXR provides an option for regular expression searches and it allows for case sensitive searches. MXR also enables users to limit the search results to certain types of files, specific output patterns and to specify what tree to search.

Search Using MXR-test:
This search yielded very similar results as the MXR search. I used the Mozilla starting point (http://mxr-test.landfill.bugzilla.org/mozilla/).

Defined as a function in:Referenced in: MXR-test enables users to narrow their search results similarly to MXR but it provides more file-types to select for refining the search and it also allows limiting the number of matches per file.

No comments: