• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Add-ons
  1. MDN
  2. Mozilla
  3. Add-ons
  4. Legacy extensions for Firefox for Android
  5. Firefox Hub Walkthrough

Firefox Hub Walkthrough

In This Article
  1. Overview
  2. Creating a home panel
  3. Storing data
  4. View types
    1. List
      1. List dataset
    2. Grid
      1. Grid dataset
    3. Speed dial
      1. Speed dial dataset
    4. Headers
  5. Best practices
  6. Example add-ons

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

Starting from Firefox 53, no new legacy add-ons will be accepted on addons.mozilla.org (AMO) for desktop Firefox and Firefox for Android.

Starting from Firefox 57, WebExtensions will be the only supported extension type. Desktop Firefox and Firefox for Android will not load other extension types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more information.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

This guide walks you through developing a Firefox Hub add-on for Firefox for Android. The Firefox Hub APIs allow add-ons to add new content to the Firefox for Android home page. These APIs are new in Firefox 30, and increased capabilities are planned for future releases. The two main APIs that this guide covers are the Home.panels API and the HomeProvider API. For some example code to get started, you can copy the hub boilerplate add-on from github.

This guide assumes you understand the basics of building a restartless add-on for Firefox for Android. Please see the basic Firefox for Android add-on Walkthrough guide for more information about getting started.

Overview

There are two main parts to building a Firefox Hub add-on: creating a home panel, and storing data to show in that panel. Home panels consist of different views, each of which displays data from a given dataset.

Creating a home panel

To create a home panel, first use the Home.panels API to register a panel. The register API takes a panel id and an options callback function as parameters. This options callback is called to dynamically generate an options object whenever a panel is installed or updated, which allows for dynamic locale changes.

Cu.import("resource://gre/modules/Home.jsm");
const PANEL_ID = "test.panel@mydomain.com";
const DATASET_ID = "test.dataset@mydomain.com";
function panelOptionsCallback() {
  return {
    title: "My Panel",
    views: [{
      type: Home.panels.View.LIST,
      dataset: DATASET_ID
    }]
  };
}
Home.panels.register(PANEL_ID, panelOptionsCallback);

You must always register your panel on startup, but when your add-on is installed (or whenever you want to actually add the panel to the user's home page), you must also install it.

Home.panels.install(PANEL_ID);

See the Home.panels API documentation for details about how to add more features to your panels, such as authentication and empty views. You can also register to listen for user actions, such as installing or refreshing a panel.

Storing data

Use the HomeProvider API to store data to display in your home panel. The HomeProvider API gives you access to HomeStorage objects, which allow you to asynchronously save and delete data for a given dataset. The save and deleteAll APIs return promises that are resolved when the transaction is complete. You can use Task.jsm to execute these transactions within a task.

Cu.import("resource://gre/modules/HomeProvider.jsm");
Cu.import("resource://gre/modules/Task.jsm");
let items = [
 { url: "http://example.com/1", title: "Example 1" },
 { url: "http://example.com/2", title: "Example 2" }
];
let storage = HomeProvider.getStorage(DATASET_ID);
Task.spawn(function() {
  // Delete any existing items.
  yield storage.deleteAll();
  // Save the new items.
  yield storage.save(items);
  // New way to replace items in Firefox 31+
  // yield storage.save(items, { replace: true });
}).then(null, Cu.reportError);

In practice, if you are going to use a network request to fetch your data, you should use the requestSync API to allow HomeProvider to decide if it's a good time to sync.

function syncCallback() {
  // Fetch new data and use HomeProvider APIs to store data.
}
HomeProvider.requestSync(DATASET_ID, syncCallback);

You can also use the addPeriodicSync API to request data syncing at a regular interval.

// Calls syncData callback once every 3600 seconds (1 hour)
HomeProvider.addPeriodicSync(DATASET_ID, 3600, syncCallback);

View types

Each panel you create with the Hub APIs can have a number of views. For each view, you can choose one of three different layouts: list, grid, or speed dial.

List

The list view displays items in a list, from top to bottom:

To create a list view, specify the view's type as LIST:

Cu.import("resource://gre/modules/Home.jsm");
function optionsCallback() {
  return {
    title: "My list view",
    views: [{
      dataset: DATASET_ID,
      type: Home.panels.View.LIST
    }]
  };
}
Home.panels.register(PANEL_ID, optionsCallback);

List dataset

For each item, the list view will display the following attributes from the view's dataset, if they are present:

  • title
  • description
  • the image referenced by image_url

In the dataset, you must supply at least one of these attributes for each item.

Grid

A grid view displays items in a grid, as images:

To create a grid view, specify the view's type as GRID:

Cu.import("resource://gre/modules/Home.jsm");
function optionsCallback() {
  return {
    title: "My grid view",
    views: [{
      dataset: DATASET_ID,
      type: Home.panels.View.GRID
    }]
  };
}
Home.panels.register(PANEL_ID, optionsCallback);

Grid dataset

For each item, the grid view will display the following attributes from the view's dataset, if they are present:

  • title
  • description
  • the image referenced by image_url

The grid view is primarily designed to display the image referenced by image_url.

In the dataset, you must supply at least one of these attributes for each item.

Speed dial

Starting in Firefox 41, there's a special type of grid view called a speed dial. In this view each item is presented as a background image or a background color, and optionally a title or an icon:

To create a speed dial view, specify GRID as the view's type and ICON as the view's itemType:

Cu.import("resource://gre/modules/Home.jsm");
function optionsCallback() {
  return {
    title: "My speed dial view",
    views: [{
      dataset: DATASET_ID,
      type: Home.panels.View.GRID,
      itemType: Home.panels.Item.ICON
    }]
  };
}
Home.panels.register(PANEL_ID, optionsCallback); 

Speed dial dataset

For each item, the speed dial view will display the following attributes from the view's dataset, if they are present:

  • background_color
  • the image referenced by background_url, as a background image
  • the image referenced by image_url, as an icon
  • title

In the dataset, you must supply at least one of title, description, or image_url for each item.

Headers

From Firefox 42 onwards, you can specify a header image for a grid view using the header view option:

Cu.import("resource://gre/modules/Home.jsm");
function optionsCallback() {
  return {
    title: "My speed dial view",
    views: [{
      dataset: DATASET_ID,
      type: Home.panels.View.GRID,
      itemType: Home.panels.Item.ICON,
      header: {
        image_url: "http://path/to/pumpkin.png",
        url: "http://www.mozilla.org"
      }
    }]
  };
}
Home.panels.register(PANEL_ID, optionsCallback);

Header images are placed at the top of the page and extend right across the page:

Best practices

Here are some tips for developing hub add-ons:

  • Use unique ids for panels and datasets.
  • Register panels in your add-on's startup() function.
  • Unregister panels in your add-on's shutdown() function.
  • Delete panel data when a panel is uninstalled.

Example add-ons

Here are some open source add-ons that use these hub APIs:

  • https://github.com/leibovic/hub-kitchen-sink
  • https://github.com/leibovic/pocket-panel
  • https://github.com/leibovic/fennec_rss

Document Tags and Contributors

 Contributors to this page: rebloor, andrewtruongmoz, wbamberg, leibovic, diegocr
 Last updated by: rebloor, Jun 19, 2017, 7:43:41 PM
See also
  1. WebExtensions
  2. Getting started
    1. What are WebExtensions?
    2. Your first WebExtension
    3. Your second WebExtension
    4. Anatomy of a WebExtension
    5. Example WebExtensions
  3. How to
    1. Intercept HTTP requests
    2. Modify a web page
    3. Add a button to the toolbar
    4. Implement a settings page
  4. User interface
    1. Introduction
    2. Toolbar button
    3. Address bar button
    4. Sidebar
    5. Context menu items
    6. Options page
    7. Bundled web pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. Concepts
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Internationalization
    5. Content Security Policy
    6. Native messaging
  6. Porting
    1. Porting a Google Chrome extension
    2. Porting a legacy Firefox add-on
    3. Embedded WebExtensions
    4. Comparison with the Add-on SDK
    5. Comparison with XUL/XPCOM extensions
    6. Chrome incompatibilities
    7. Differences between desktop and Android
  7. Firefox workflow
    1. Temporary Installation in Firefox
    2. Debugging
    3. Developing for Firefox for Android
    4. Getting started with web-ext
    5. web-ext command reference
    6. WebExtensions and the Add-on ID
    7. Publishing your WebExtension
  8. JavaScript APIs
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browsingData
    6. commands
    7. contextMenus
    8. contextualIdentities
    9. cookies
    10. devtools.inspectedWindow
    11. devtools.network
    12. devtools.panels
    13. downloads
    14. events
    15. extension
    16. extensionTypes
    17. history
    18. i18n
    19. identity
    20. idle
    21. management
    22. notifications
    23. omnibox
    24. pageAction
    25. permissions
    26. privacy
    27. proxy
    28. runtime
    29. sessions
    30. sidebarAction
    31. storage
    32. tabs
    33. topSites
    34. types
    35. webNavigation
    36. webRequest
    37. windows
  9. Manifest keys
    1. applications
    2. author
    3. background
    4. browser_action
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. commands
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. homepage_url
    15. icons
    16. incognito
    17. manifest_version
    18. name
    19. omnibox
    20. optional_permissions
    21. options_ui
    22. page_action
    23. permissions
    24. protocol_handlers
    25. short_name
    26. sidebar_action
    27. version
    28. web_accessible_resources
  10. Add-on SDK
  11. Getting started
    1. Installation
    2. Getting started
    3. Troubleshooting
  12. High-Level APIs
    1. addon-page
    2. base64
    3. clipboard
    4. context-menu
    5. hotkeys
    6. indexed-db
    7. l10n
    8. notifications
    9. page-mod
    10. page-worker
    11. panel
    12. passwords
    13. private-browsing
    14. querystring
    15. request
    16. selection
    17. self
    18. simple-prefs
    19. simple-storage
    20. system
    21. tabs
    22. timers
    23. ui
    24. url
    25. webextension
    26. widget
    27. windows
  13. Low-Level APIs
    1. /loader
    2. chrome
    3. console/plain-text
    4. console/traceback
    5. content/content
    6. content/loader
    7. content/mod
    8. content/symbiont
    9. content/worker
    10. core/heritage
    11. core/namespace
    12. core/promise
    13. dev/panel
    14. event/core
    15. event/target
    16. frame/hidden-frame
    17. frame/utils
    18. fs/path
    19. io/byte-streams
    20. io/file
    21. io/text-streams
    22. lang/functional
    23. lang/type
    24. loader/cuddlefish
    25. loader/sandbox
    26. net/url
    27. net/xhr
    28. places/bookmarks
    29. places/favicon
    30. places/history
    31. platform/xpcom
    32. preferences/event-target
    33. preferences/service
    34. remote/child
    35. remote/parent
    36. stylesheet/style
    37. stylesheet/utils
    38. system/child_process
    39. system/environment
    40. system/events
    41. system/runtime
    42. system/unload
    43. system/xul-app
    44. tabs/utils
    45. test/assert
    46. test/harness
    47. test/httpd
    48. test/runner
    49. test/utils
    50. ui/button/action
    51. ui/button/toggle
    52. ui/frame
    53. ui/id
    54. ui/sidebar
    55. ui/toolbar
    56. util/array
    57. util/collection
    58. util/deprecate
    59. util/list
    60. util/match-pattern
    61. util/object
    62. util/uuid
    63. window/utils
  14. Firefox for Android
  15. Getting started
    1. Walkthrough
    2. Debugging
    3. Code snippets
  16. APIs
    1. Accounts.jsm
    2. BrowserApp
    3. HelperApps.jsm
    4. Home.jsm
    5. HomeProvider.jsm
    6. NativeWindow
    7. Notifications.jsm
    8. PageActions.jsm
    9. Prompt.jsm
    10. RuntimePermissions.jsm
    11. Snackbars.jsm
    12. Sound.jsm
    13. Tab
  17. Legacy
  18. Restartless extensions
    1. Overview
  19. Overlay extensions
    1. Overview
  20. Themes
  21. Publishing add-ons
  22. Guides
    1. Signing and distribution overview
    2. Submit an add-on
    3. Creating an appealing listing
    4. Review policies
    5. Developer agreement
    6. Featured add-ons
    7. Contact addons.mozilla.org
  23. Community and support
  24. Channels
    1. Add-ons blog
    2. Add-on forums
    3. Stack Overflow
    4. Development newsgroup
    5. IRC Channel