Hooking app exit in Firefox extensions

Posted by chetan on February 10, 2010

I’ve spent the last few days since joining Better Advertising working on a new feature for a Firefox extension called Ghostery. We’ll be announcing the new feature soon, but until then I thought I’d share some of what I’ve learned so far.

I’ve never worked on an extension before but as it turns out, it’s really quite easy to pick up; some fairly simple XML (aka XUL) for composing the UI and JavaScript for the rest. One of the trickier bits has to do with scope. After doing some testing I figured out that the entry point into an extension is via the browser window; that is, your extension code will be executed each time you open a new window and that means that all your code is basically scoped to a single window.

In developing the new Ghostery feature I needed a way to run some code when the user quits Firefox. Luckily, the extension architecture is extremely flexible (if poorly documented at times) and I didn’t have to jump through any hoops to do it. Almost anything, it seems can be either chained or hooked in some way. In this case, the nsIObserverService gives us access to the necessary shutdown event to which we can attach an observer using a simple interface.

The problem, then, was that since our code is run every time a new window is created, I needed a way to register the hook only once to avoid firing multiple times on exit. My first thought was to try to register the hook outside of the window scope (e.g. using a different chrome overlay) but that appeared to be a dead end. Using a globally scoped variable as a lock was also a dead end. In the end I settled on something I already knew how to use: preferences. Essentially, I created a simple lock around a preference variable which, while I don’t need to store it between sessions, is in fact a global storage area that can be accessed from different windows.

Check out the code below for an example implementation. I left out the actual preferences code since it’s not crucial to understanding the solution.

Writing command line interfaces for Spring apps

Posted by chetan on June 29, 2009

I recently needed to script some tasks for a Spring-based app at work so we could shove it into a crontab. It proved to be much easier than I thought.

You can use your spring.xml config file for wiring up your beans as usual, but rather than deal with various property files you can easily override properties on the fly using system properties. See the following example:

In your spring.xml make sure you have this line:

1
2
3
4
5
6
7
8
9
<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- Allow properties to be overriden via 
         system properties -->
    <property name="systemPropertiesMode" value="2" />
    <property name="locations">
        <list> [...snip...] </list>
    </property>
</bean>

Then you can override your aliases either using standard java properties (-Djdbc.url=”mysql://…”) or via your program’s own command line arguments, which is the route I ended up going.

CommandLineParser parser = new GnuParser();
Options options = new Options();
Option brokerOption = new Option("b", "broker", true, "broker uri");
brokerOption.setRequired(true);
options.addOption(brokerOption);
CommandLine line = parser.parse(options, args);
System.setProperty("jms.broker.url", line.getOptionValue("b"));

Pointless rewrite? Probably. 3

Posted by chetan on August 06, 2008

Del.icio.us (sorry, it’s just plain old “Delicious” now) 2.0 finally launched a few days ago and the response so far has been mixed. But now that the dust has settled some, it’s time to think about just how we got here and if it was really worth all the trouble.

According to the official blog post, the new and improved Delicious brings us speed, usability, and oh so good looks among other features and it was a long time in the making. The Yahoo acquisition was announced on Dec 9, 2005 and the new site finally went live a little over two and a half years later on July 31, 2008. So why did it take them so long?

A key change as a result of the Yahoo! acquisition was their decision to rewrite the whole thing in PHP using the Symfony framework, for no other reason than that it’s the current corporate standard at Yahoo!. Oh, and, coincidentally, Yahoo! Bookmarks was also built on PHP+Symfony.

So now it starts to make a bit of sense: you take a system being actively used by millions of users around the world and you start over from scratch with the goal of building it bigger and better, toss in a couple of hot buzzwords to meet Web 2.0 compliance guidelines, and before you know it 2 years have gone by.

I find it very hard to believe that with all the talent and the thousands of man years combined software development experience over there, that no one understands the pros and cons of rewriting vs refactoring a code base, especially given the enormous success of the service and the relatively trouble-free history as compared to, say, Twitter.

At the same time, I understand it all too well. From where I sit, and having been involved in a similar situation in the past as well as with my current employer, the decision to move to PHP was clearly not based on a cost/benefit analysis of maintaing the current system. In fact, I wonder if they even understood what the real problems, if any, were with the existing system before deciding to not just rewrite it, but write it in another language.

Moving to another language is a pretty drastic step to take and will rarely solve your problem.

Suits suck 1

Posted by chetan on February 21, 2008

I still intend to finish the series of posts I started earlier, but this quote pretty much sums it up:

In this regard management is also to blame, especially when it comes to dysfunctional schedules, wrong incentives, poor hiring, and demoralizing policies.

If you’re not outraged, you’re not paying attention

Posted by chetan on February 14, 2008

Is just getting the job done always enough? One of my personal philosophies is that if you’re not learning, you’re not improving. If you’re not improving, then what have you really accomplished? In fact, there’s a pretty good chance that you’ve even gotten worse, if only because everyone else has gotten better. It’s like a television or movie series that comes back year after with the same old, tired formula. No better than previous iterations, and doesn’t even meet the expectations set by its predecessors. Think Bond, James Bond.

Continue reading…

Go to sleep

Posted by chetan on February 12, 2008

“Commitment means being prepared to do extra things, going the extra mile”

Without a commitment from all the concerned parties, how can one expect any enterprise to be successful? Be it business, government, military or otherwise.

My group has shown it’s commitment time and again, but we haven’t seen the same from others. How do you find the motivation to stay committed in a situation like that?

I think I’ll just go to sleep instead.

What we do

Posted by chetan on January 13, 2008

according to steve:

what we do

Aren’t you on vacation?

Posted by chetan on December 31, 2007

It’s strange. Just because I’m back in New York for a couple of weeks before I go back to India, everyone just assumes that I’m here on vacation. If it was any other time of the year, I’m almost positive that I wouldn’t get this reaction at all. Stranger still, most of the people asking the question aren’t off either. What gives?

And you may find yourself in another part of the world

Posted by chetan on December 16, 2007

Ever have a moment where you suddenly stop and ask yourself: how did I get here, to this exact moment in time?

For me it’s almost an out of body experience where I look [down] at myself in a particular instant and I try to work backwards to the events that led up to it. It seems to be happening a lot, of late, and it always comes back to the same question. Two weeks ago, I was sitting in the domestic terminal at the Bombay airport, reading a book of no consequence and listening to my iPod while waiting for my flight to Pune when I suddenly looked up and thought to myself, “what the fuck am I doing here? How the hell did I get here?”

It happened again yesterday. I was sitting in my hotel room, eating a fabulous lunch (brunch, really, but it was 3pm; scrambled eggs, french toast, hash browns, chicken sausage, and hot chocolate) and looking out the window, out over the gloomy sprawl of Chennai before me, when it came up again: “what the fuck am I doing here? How the hell did I get here?”

There’s an obvious answer (I’m here for Operative as their resident software ninja, blah, blah) but that’s not good enough. It doesn’t really answer the question as to why I’m here, in the here and now. Even more, I can think of lots of reasons that I shouldn’t be here; why I should be somewhere else, doing something else. And only a few reasons (e.g., conscious decisions I’ve made) for being here.

And yet here I am.

Nice shoes, but I still think you’re stupid

Posted by chetan on December 16, 2007

My boss asked me a question recently, which really got me thinking: would it make any difference if I [my boss] wore slacks and a shirt every day? That is, do outward appearances at the office really matter that much? Could dressing a little better mean being taken more seriously?

It took me a really long time to answer the question, because to me it doesn’t affect how I judge a person’s performance, and it was difficult for me to look at the question objectively and realize that not everyone thinks the way I do. Everyone has their own criteria for judging performance or value. Which got me thinking about meritocracies and why they don’t work in business.

Many large, successful Open Source projects are, or appear to be (I’ll get to this later), meritocracies. The Apache Software Foundation (ASF) is a popular example. So what’s so different about open source software projects that a meritocracy not only works but seems to come so naturally?

Clearly much of it has to do with the voluntary nature of open source; when faced with an adverse situation in the management of a project you have two clear options when all else fails: stop contributing or fork. Both options carry an equally low risk, from an individual standpoint. The same two options are available in the business world, however the risks are much greater when one’s career and livelihood lie in the balance but the rewards are potentially much greater for forking — e.g., quit and start a competing product or service.

Even in the latter case, can a meritocracy really work in the long run? The incubation phase of almost any startup is entirely merit-based — from acquiring funding to competing in the marketplace, both will be judged on the merits of the ideas and the products or services the business is able to produce. But how long can this be sustained? Once a company grows out of it’s core group of people into a full-fledged business, it becomes increasingly difficult to keep the same level of excellence and far more costly to replace people at the upper levels. The stakes are also much higher for everyone involved, as noted by the ASF:

“What is interesting to note is that the process scaled very well without creating friction, because unlike in other situations where power is a scarce and conservative resource, in the apache group newcomers were seen as volunteers that wanted to help, rather than people that wanted to steal a position.”

So it becomes an issue of power and the ability to wield it. Bill Gates commented on a recent survey, saying: “Communication skills and the ability to work well with different types of people are very important too,” he said. “Software innovation, like almost every other kind of innovation, requires the ability to collaborate and share ideas with other people, and to sit down and talk with customers and get their feedback and understand their needs.” In other words, to innovate you need more than just technical ability and great ideas. You need the ability to communicate those ideas and work with people who may not always be as strong technically.

And that’s where strong leadership comes in. The problem with any large organization is that it’s almost impossible for everyone to be in complete agreement all the time. Even in a merit based organization like the ASF, consensus is not a given and people have found ways to game the system. Without strong leadership at the top you’ll have a constant power struggle to fill that void at every level in the organization, as different groups vie for control. (See also: IRAQ)

The best ideas can win out, but sometimes it takes a dictator.