A contributor recently asked a question in #introduction that I couldn’t answer immediately, but I suspected that I could find the answer without too much work. I also decided to write down the steps I took and the reasons I took them, with the intent of creating a worked example of finding answers in the Firefox code.
The question: how do I determine if a given URI pointing to an RSS feed is subscribed as a live bookmark?
My first instinct when looking for code I have never touched before is to start with the visible strings in the Firefox interface. In this case, I went to slashdot.org and clicked on the RSS feed button to take me to the RSS feed viewer. Under the options for possible subscriptions, there is the “Live Bookmarks” choice. I’m going to find the code that handles subscribing a live bookmark, which should lead me to the way to find out if a feed is currently subscribed.
So, off to MXR: http://mxr.mozilla.org/mozilla-central/search?string=Live+Bookmarks&find=&findi=&filter=^[^\0]*%24&hitlimit=&tree=mozilla-central
Strings are usually stored in special localized files, where the actual string is referred to by a unique name. In this case, we’re looking at subscribe.properties, which shows us that some code should be using the name liveBookmarks
to show the localized string in the feed viewer window.
Let’s look that up: http://mxr.mozilla.org/mozilla-central/search?string=livebookmarks&find=&findi=&filter=^[^\0]*%24&hitlimit=&tree=mozilla-central
Out of the results, subscribe.xml
sounds like something I want to read: http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/content/subscribe.xml#53
Unfortunately, that line turns out to be not very interesting. However, let’s see what we can find for liveBookmarksMenuItem
, which sounds like it might be the item for the live bookmarks option in the dropdown: http://mxr.mozilla.org/mozilla-central/ident?i=liveBookmarksMenuItem
Let’s look at FeedWriter.js from the results: http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/src/FeedWriter.js
Woah, that’s a lot of code! Let’s take a step back – we care about the subscribing action right now. Going back to subscribe.xml (http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/content/subscribe.xml), notice that the button is called subscribeButton
. That sounds really useful! Let’s look in feedwriter.js again for that (just a regular ctr+f operation): http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/src/FeedWriter.js#859
Now, let’s find the function subscribe (ctr+f for subscribe:
, as that’s commonly how member functions are defined in the Firefox source): http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/src/FeedWriter.js#1241
Reading the source of subscribe()
, we come to
http://mxr.mozilla.org/mozilla-central/ident?i=addToClientReader
We can see the definition of addToClientReader
at http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/src/FeedConverter.js#390. Let’s look at what else is defined here – this seems to be implementing the nsIFeedResultService interface, so we can look up the nsIFeedResultService.idl
file in MXR: http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/public/nsIFeedResultService.idl.
Unfortunately, the service seems to be dealing with actual feed contents, and it doesn’t look like there’s a direct function to help us – let’s step back and see how addToClientReader
is implemented.
http://mxr.mozilla.org/mozilla-central/source/browser/components/feeds/src/FeedConverter.js#448 is an important line – let’s look at addLiveBookmark
(I just clicked the name again): http://mxr.mozilla.org/mozilla-central/ident?i=addLiveBookmark
Take a look at http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser-places.js#457. This shows us how a bookmark dialog is created, but not the actual subscription process. However, the important part is line 471, which shows us the type "livemark"
. Let’s search for that string and see what other code uses it: http://mxr.mozilla.org/mozilla-central/search?string=livemark
Woah! There are a lot of results! Since I’m not certain what I’m looking for at this point, I find the best thing to do here is to scan all the results quickly, looking for interesting snippets or filenames before opening any particular file. Partway down, there’s a file called test_bug636917_isLivemark.js
(http://mxr.mozilla.org/mozilla-central/source/toolkit/components/places/PlacesUtils.jsm
Searching for livemarks
in this file, we find line 2234 which defines the livemarks
object as the nsILivemarkService
service. Let’s look that up!
First the idl definition, to see if this service will be useful: http://mxr.mozilla.org/mozilla-central/source/toolkit/components/places/nsILivemarkService.idl. There’s a method called getLivemarkIdForFeedURI
(http://mxr.mozilla.org/mozilla-central/source/toolkit/components/places/nsILivemarkService.idl#107), which looks to be exactly what we want – it gives us a -1 result for a non-registered feed and another number for a registered one.
So, to sum up – we have found a service that will take a URI and return a value that can be used to determine whether a given feed is a subscribed livemark, and we have learned that livemarks are the internal name for Live Bookmarks. The only bit remaining now is to get access to this service, but we can either use PlacesUtils.livemarks
for that (if we import PlacesUtils.jsm), or just use a normal getService
call. These are just details; the hard part of answering the original question is complete!
Posted on January 12th, 2012 by Josh Matthews
Filed under: mozilla | 1 Comment »