Curious Chrome: Cross Platform Extension Behaviors

Chrome is a curious browser. It’s extremely popular, cross platform, and has an incredible cadence for updates. I typically use Chrome when I am developing for it. I don’t mean a developer that uses Chrome, I mean a developer that develops for Chrome.

Chrome supports Browser Extensions. I’ve briefly discussed them in the past, and because they are pure HTML, JavaScript, and CSS they are readily cross platform, too. The extension requires extra coding whether you are writing it for Windows or OSX.

There’s one particular case where I wasn’t seeing a consistent behavior between Windows and OSX, and that has to do with the lifecycle of the application. To catch those up to the details of OSX, closing the window does not mean you closed the application. You just closed the last open window. The application is still running, it still receives events, and it can still do work.

Windows Chrome is a bit different. When you close the last window, the process is gone: kaput. This small difference in application lifecycle is important when dealing with the chrome.windows.onRemoved event. With the Windows lifecycle of Chrome, its impossible to react to the last window being closed because the process, and all extensions, are being unloaded too.

On a Mac, this behavior doesn’t exist. The window just closed, but the extension is still able to respond to that event.

The only way I was able to get this to work on Windows was to give my application “background” permission in the manifest:

{
	"optional_permissions": ["background"]
}

Fortunately, I could make this permission optional since the extension didn’t need it full time, only if certain features were enabled.

With this setting on, Chrome correctly fires off the onRemoved event, even if it is the last window open on Windows.