storage.sync
============

Represents the `sync` storage area. Items in `sync` storage are synced by the browser. The data is then available on all instances of the browser the user is logged into (for example, when using a Mozilla account on desktop versions of Firefox or a Google account on Chrome) across different devices.

For desktop Firefox, a user must have `Add-ons` selected in the "Sync" section in `"about:preferences"`. Firefox for Android does not synchronize data with the user's account. See Firefox bug 1316442.

The implementation of `storage.sync` in Firefox relies on the Add-on ID. If you use `storage.sync`, you must set an ID for your extension using the `browser_specific_settings` manifest.json key.

The main use case of this API is to store preferences about your extension and allow the user to sync them to different profiles.

Storage quotas for sync data
----------------------------

The browser enforces limits on the amount of data each extension is allowed to store in the sync area:

| Name | Description | Value in bytes |
| --- | --- | --- |
| Maximum total size | 
 The maximum total amount of data that each extension is allowed to store
 in the sync storage area, as measured by the JSON stringification of
 every value plus every key's length.
  | 102400 |
| Maximum item size | 
 The maximum size of any one item that each extension is allowed to store
 in the sync storage area, as measured by the JSON stringification of the
 item's value plus the length of its key.
  | 8192 |
| Maximum number of items | 
 The maximum number of items that each extension can store in the sync storage
 area.
  | 512 |

If an extension attempts to store items that exceed these limits, calls to `storage.sync.set()` are rejected with an error. An extension can use `storage.sync.getBytesInUse()` to find out how much of its quota is in use.

Synchronization process
-----------------------

In Firefox, extension data is synced every 10 minutes or whenever the user selects **Sync Now** (in **Settings** > **Sync** or from the Mozilla account icon). When the browser performs a sync, for each key stored, it:

* compares the value on the server with the value at the last sync; if they are different, the value from the server is written to the key in the browser's sync storage.
* compares the browser's sync storage values with the value on the server; if they are different, writes the browser's key value to the server.

This means that, for each key, a change on the server takes precedence over a change in the browser's sync storage.

This mechanism is generally OK for data such as user preferences or other global settings changed by the user.

However, a key's value can be updated on one browser and synchronized then updated on a second browser before the second browser is synchronized, resulting in the local update being overwritten during sync. This mechanism is, therefore, not ideal for data aggregated across devices, such as a count of page views or how many times an option is used. To handle such cases, use `storage.sync.onChanged` to listen for sync updates from the server (for example, a count of page views on another browser instance). Then adjust the value locally to take the remote value into account (for example, update the total views based on the remote count and new local count).

Methods
-------

The `sync` object implements the methods defined on the `storage.StorageArea` type:

`storage.sync.get()`

Retrieves one or more items from the storage area.

`storage.sync.getBytesInUse()`

Gets the amount of storage space (in bytes) used for one or more items in the storage area.

`storage.sync.set()`

Stores one or more items in the storage area. If the item exists, its value is updated.

`storage.sync.remove()`

Removes one or more items from the storage area.

`storage.sync.clear()`

Removes all items from the storage area.

Events
------

The `sync` object implements the events defined on the `storage.StorageArea` type:

`storage.sync.onChanged`

Fires when one or more items in the storage area change.

### Example extensions

* favourite-colour
Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

**Note:** This API is based on Chromium's `chrome.storage` API. This documentation is derived from `storage.json` in the Chromium code.