Monday, 28 November 2011

Let's have a look at some recently landed Web APIs for Firefox

So, without further ado (as there's been too much "to do" about it) let's take a look at what we presently have (landed or about to land) regarding our latest (Firefox) Web APIs.

You'll need the latest xul fennec builds to run these.  Technically, these also run on the latest birch nightly, but that one is presently undergoing a major facelift due to rendering and related issues.

I'd suggest using a tablet. (For the SMS app, you'll need a phone with simcard and a special build, but we'll get to that presently).

All example testpages to which we'll be referring are available under the directory at:

If you find a bug with any of the demos and/or APIs themselves (and we hope you will:) please write them here:

Make sure to include your device model & Android version, as well as the nightly fennec version you are using,  in the bug.

Battery API demo: 

This API tests the different charging states of the battery, along with charging level and discharging time.  Try launching the page with the device plugged in, and  then unplugging (and/or vice versa).  Note that the charging/discharging time presently shows Infinity  until the battery has charged/discharged about 2% of its total capacity.

Does the testpage show the correct battery state for the device?  Does it correctly generate an alert when the charging state changes?  Is the level correct (when compared against native battery app level)?  Does it give a numerical discharging/charging time after some time in the same charging state?

Camera API demo: 
This API presently tests the browser's ability to directly capture pictures from the device camera.

Point the device camera at the object you want to shoot  and click capture.

Is the picture rendered correctly (as opposed to  upside down, or sideways) both in the pre-capture and the preview?  Is the picture quality consistent with what your camera normally delivers?  Are there any other issues?  Please feel to play around with it a bit.

IndexedDB API demo: 

Since accessing the native file system from the web introduces a huge risk, there needs to be a way to handle offline storage directly on a device.  Enter IndexedDB.   Actually, this api has been around since Firefox 4 (and subsequently for quite a few Chrome and other browser versions since) but the W3C spec got a rewrite recently and it's been well overdue for an upgrade.

When the browser first tries to create a local database, a prompt requests permission:

Subsequently, once this is allowed, the db initializes, and after some time, reports back on the number of records:

Our current demo matches a hash of zipcodes against a city (you must have zipcodes.csv present in same dir to work - this demo does), doing all the database access operations.  (Note that we're presently missing the functionality to do the reverse!) Once the CSV is loaded into the device local database, the  lookup should be instantaneous (note that all the steps are not depicted here):

Note that I've been getting dramatically different results from device to device.   Sometimes it works, sometimes not.   To be sure the feature is still a work in progress, so let me know what your experiences are.

Vibrator API demo: 

Web apps, just like native ones,  need a way to send feedback to various sensors - including notification vibes.   The W3C spec lays a foundation, which we've implemented.  Note that you'll need a device with a vibrator to test this one:

You'll need a special build for this one also.  Try out each of the scenarios and verify the result.     Are we getting the correct number of buzzes, given the spec?  As with any of our APIs here, you can check the implementation by typing the string "view-source:" (without the quotes) before the "http://....".  Try it.

SMS API demo:
Note that you'll need an android phone with a working SIM card to try these demos.  Until this feature lands, you may also may also need a  special build with this feature.

The simplesend is a page that lets you send SMS messages to any phone.  Try it!  Is your message able to get through?  Any other issues?  Note that,  for testing purposes,  you can also try sending the messages directly to the same phone from where they originate.

This next example lets you do exactly that:  send and receive SMS messages from the same webpage instance:

This page is similar to the previous one, but also has an event handler for incoming messages.

Don't forget to study the page source, for each of these pages, to understand how these APIs are implemented.

And, once again, don't hesitate to file a bug if something doesn't appear to be working correctly, or if you have an idea about some features you'd like to see.  

Addendum:  The Web API test pages are now available in github:


  1. This comment has been removed by a blog administrator.

  2. This comment has been removed by the author.

  3. This is all very exciting, good job all!

    Just a data point: since you mention that the vibration functionality is specified over at W3C, I thought I'd mention that so is the battery stuff:

  4. Nice but when is the Camera API coming to PC version =D

  5. Is the correct interpretation of that Web SQL is not long to live and IndexedDB is the future? Is Firefox dropping Web SQL DB at some point?

  6. Hi all!

    Can't comment on the SQL because I don't know yet. But the Camera API stuff is supposed to be landing soon for PC if it hasn't landed already.

  7. This comment has been removed by the author.

  8. Just got word - it seems we have never supported WebSQL for firefox.

  9. Battery API page did not load for me!!!

  10. Sorry to hear that - or since we're bughunting, maybe not so sorry, really ;) - which Android version on which device do you have? And which FF version?

  11. Thanks for the review !
    excellent stuff !!

  12. Can we please change the name of the Vibrator API? In America a vibrator generally refers to a sex toy (try googling it).

  13. Caspy - I know. I have suggested, for example, the Buzzer API. I think we are following Android's lead.... :-/

  14. Looks like you need correct the bugzilla link, it points firefox core component?

    Files bug reports from Gallaxy SII runing Firefox mobile native UI trunk.

  15. Will this API be available for Laptops? Say, will I be able to use the Camera API for my MacBook Pro camera?

  16. There is currently an effort to make Camera API (and some of the others as well) available on Desktop Builds. I don't know exactly when it will land; I will make it a point to blog here when the API does land.

  17. how are webapi integrated? is it firefox sourcecode (is it written in C++ and packaged in the final exe of firefox) ?

    can these API be reused in other browsers or you have to do it all over again ?

  18. WebAPIs are currently "baked-into" the sourcecode for the native- and XUL-based Fennec (Android) Firefox versions. They will land in the Desktop Builds in the near future. Eventually the aim is to have them working in B2G (Boot To Gecko) as well.

  19. The IndexedDB API demo fails in Firefox nightly and SeaMonkey 2.9 with
    Error: uncaught exception: [Exception... "A mutation operation was attempted on a database that did not allow mutations." code: "6" nsresult: "0x80660006 (NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR)" location: " Line: 220"]

    I also see
    [17:09:52.178] The constant IDBTransaction.READ_ONLY has been deprecated. Use the string value "readonly" instead.

    It always reports "City: Unknown zip code"

  20. After you need pleasure and sensuality, together with relaxation, erotic male sizegenetics is exactly genital herpes should be looking for. If you happen to join a well designed website, you should pick from different professionals who advertise their services for male sizegenetics. You should look because of the pictures using the different gay sizegenetics therapists, and read descriptions of their technique and services. You're able to get an understanding of what to look for at the time you explore the profiles, additionally the photos will aid you to assess how well they fit into your reasoning behind a gay sizegenetics professional.

  21. It is really interesting post. I never read such kind of post. It impressed me. Thanks for sharing…