summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/ext-storage.js
blob: 46d4fe13c9b7e3d96f76b6580fbf5a909b3ff5d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict";

var {classes: Cc, interfaces: Ci, utils: Cu} = Components;

XPCOMUtils.defineLazyModuleGetter(this, "ExtensionStorage",
                                  "resource://gre/modules/ExtensionStorage.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionStorageSync",
                                  "resource://gre/modules/ExtensionStorageSync.jsm");

Cu.import("resource://gre/modules/ExtensionUtils.jsm");
var {
  EventManager,
} = ExtensionUtils;

function storageApiFactory(context) {
  let {extension} = context;
  return {
    storage: {
      local: {
        get: function(spec) {
          return ExtensionStorage.get(extension.id, spec);
        },
        set: function(items) {
          return ExtensionStorage.set(extension.id, items, context);
        },
        remove: function(keys) {
          return ExtensionStorage.remove(extension.id, keys);
        },
        clear: function() {
          return ExtensionStorage.clear(extension.id);
        },
      },

      sync: {
        get: function(spec) {
          return ExtensionStorageSync.get(extension, spec, context);
        },
        set: function(items) {
          return ExtensionStorageSync.set(extension, items, context);
        },
        remove: function(keys) {
          return ExtensionStorageSync.remove(extension, keys, context);
        },
        clear: function() {
          return ExtensionStorageSync.clear(extension, context);
        },
      },

      onChanged: new EventManager(context, "storage.onChanged", fire => {
        let listenerLocal = changes => {
          fire(changes, "local");
        };
        let listenerSync = changes => {
          fire(changes, "sync");
        };

        ExtensionStorage.addOnChangedListener(extension.id, listenerLocal);
        ExtensionStorageSync.addOnChangedListener(extension, listenerSync, context);
        return () => {
          ExtensionStorage.removeOnChangedListener(extension.id, listenerLocal);
          ExtensionStorageSync.removeOnChangedListener(extension, listenerSync);
        };
      }).api(),
    },
  };
}
extensions.registerSchemaAPI("storage", "addon_parent", storageApiFactory);
extensions.registerSchemaAPI("storage", "content_parent", storageApiFactory);