summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/ExtensionScriptingStore.sys.mjs
blob: b772e4dfb8dd8cac26a8ab6fdf9468377d485ad0 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { ExtensionUtils } from "resource://gre/modules/ExtensionUtils.sys.mjs";

import { ExtensionParent } from "resource://gre/modules/ExtensionParent.sys.mjs";

const { StartupCache } = ExtensionParent;

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
  KeyValueService: "resource://gre/modules/kvstore.sys.mjs",
});

class Store {
  async _init() {
    const { path: storePath } = lazy.FileUtils.getDir("ProfD", [
      "extension-store",
    ]);
    // Make sure the folder exists.
    await IOUtils.makeDirectory(storePath, { ignoreExisting: true });
    this._store = await lazy.KeyValueService.getOrCreate(
      storePath,
      "scripting-contentScripts"
    );
  }

  lazyInit() {
    if (!this._initPromise) {
      this._initPromise = this._init();
    }

    return this._initPromise;
  }

  /**
   * Returns all the stored scripts for a given extension (ID).
   *
   * @param {string} extensionId An extension ID
   * @returns {Array} An array of scripts
   */
  async getAll(extensionId) {
    await this.lazyInit();
    const pairs = await this.getByExtensionId(extensionId);

    return pairs.map(([_, script]) => script);
  }

  /**
   * Writes all the scripts provided for a given extension (ID) to the internal
   * store (which is eventually stored on disk).
   *
   * We store each script of an extension as a key/value pair where the key is
   * `<extensionId>/<scriptId>` and the value is the corresponding script
   * details as a JSON string.
   *
   * The format on disk should look like this one:
   *
   * ```
   * {
   *   "@extension-id/script-1": {"id: "script-1", <other props>},
   *   "@extension-id/script-2": {"id: "script-2", <other props>}
   * }
   * ```
   *
   * @param {string} extensionId An extension ID
   * @param {Array} scripts An array of scripts to store on disk
   */
  async writeMany(extensionId, scripts) {
    await this.lazyInit();

    return this._store.writeMany(
      scripts.map(script => [
        `${extensionId}/${script.id}`,
        JSON.stringify(script),
      ])
    );
  }

  /**
   * Deletes all the stored scripts for a given extension (ID).
   *
   * @param {string} extensionId An extension ID
   */
  async deleteAll(extensionId) {
    await this.lazyInit();
    const pairs = await this.getByExtensionId(extensionId);

    return Promise.all(pairs.map(([key, _]) => this._store.delete(key)));
  }

  /**
   * Returns an array of key/script pairs from the internal store belonging to
   * the given extension (ID).
   *
   * The data returned by this method should look like this (assuming we have
   * two scripts named `script-1` and `script-2` for the extension with ID
   * `@extension-id`):
   *
   * ```
   * [
   *   ["@extension-id/script-1", {"id: "script-1", <other props>}],
   *   ["@extension-id/script-2", {"id: "script-2", <other props>}]
   * ]
   * ```
   *
   * @param {string} extensionId An extension ID
   * @returns {Array} An array of key/script pairs
   */
  async getByExtensionId(extensionId) {
    await this.lazyInit();

    const entries = [];
    // Retrieve all the scripts registered for the given extension ID by
    // enumerating all keys that are stored in a lexical order.
    const enumerator = await this._store.enumerate(
      `${extensionId}/`, // from_key (inclusive)
      `${extensionId}0` // to_key (exclusive)
    );

    while (enumerator.hasMoreElements()) {
      const { key, value } = enumerator.getNext();
      entries.push([key, JSON.parse(value)]);
    }

    return entries;
  }
}

const store = new Store();

/**
 * Given an extension and some content script options, this function returns
 * the content script representation we use internally, which is an object with
 * a `scriptId` and a nested object containing `options`. These (internal)
 * objects are shared with all content processes using IPC/sharedData.
 *
 * This function can optionally prepend the extension's base URL to the CSS and
 * JS paths, which is needed when we load internal scripts from the scripting
 * store (because the UUID in the base URL changes).
 *
 * @param {Extension} extension
 *        The extension that owns the content script.
 * @param {object} options
 *        Content script options.
 * @param {boolean} prependBaseURL
 *        Whether to prepend JS and CSS paths with the extension's base URL.
 *
 * @returns {object}
 */
export const makeInternalContentScript = (
  extension,
  options,
  prependBaseURL = false
) => {
  let cssPaths = options.css || [];
  let jsPaths = options.js || [];

  if (prependBaseURL) {
    cssPaths = cssPaths.map(css => `${extension.baseURL}${css}`);
    jsPaths = jsPaths.map(js => `${extension.baseURL}${js}`);
  }

  return {
    scriptId: ExtensionUtils.getUniqueId(),
    options: {
      // We need to store the user-supplied script ID for persisted scripts.
      id: options.id,
      allFrames: options.allFrames || false,
      // Although this flag defaults to true with MV3, it is not with MV2.
      // Check permissions at runtime since we aren't checking permissions
      // upfront.
      checkPermissions: true,
      cssPaths,
      excludeMatches: options.excludeMatches,
      jsPaths,
      matchAboutBlank: true,
      matches: options.matches,
      originAttributesPatterns: null,
      persistAcrossSessions: options.persistAcrossSessions,
      runAt: options.runAt || "document_idle",
    },
  };
};

/**
 * Given an internal content script registered with the "scripting" API (and an
 * extension), this function returns a new object that matches the public
 * "scripting" API.
 *
 * This function is primarily in `scripting.getRegisteredContentScripts()`.
 *
 * @param {Extension} extension
 *        The extension that owns the content script.
 * @param {object} internalScript
 *        An internal script (see also: `makeInternalContentScript()`).
 *
 * @returns {object}
 */
export const makePublicContentScript = (extension, internalScript) => {
  let script = {
    id: internalScript.id,
    allFrames: internalScript.allFrames,
    matches: internalScript.matches,
    runAt: internalScript.runAt,
    persistAcrossSessions: internalScript.persistAcrossSessions,
  };

  if (internalScript.cssPaths.length) {
    script.css = internalScript.cssPaths.map(cssPath =>
      cssPath.replace(extension.baseURL, "")
    );
  }

  if (internalScript.excludeMatches?.length) {
    script.excludeMatches = internalScript.excludeMatches;
  }

  if (internalScript.jsPaths.length) {
    script.js = internalScript.jsPaths.map(jsPath =>
      jsPath.replace(extension.baseURL, "")
    );
  }

  return script;
};

export const ExtensionScriptingStore = {
  async initExtension(extension) {
    let scripts;

    // On downgrades/upgrades (and re-installation on top of an existing one),
    // we do clear any previously stored scripts and return earlier.
    switch (extension.startupReason) {
      case "ADDON_INSTALL":
      case "ADDON_UPGRADE":
      case "ADDON_DOWNGRADE":
        // On extension upgrades/downgrades the StartupCache data for the
        // extension would already be cleared, and so we set the hasPersistedScripts
        // flag here just to avoid having to check that (by loading the rkv store data)
        // on the next startup.
        StartupCache.general.set(
          [extension.id, extension.version, "scripting", "hasPersistedScripts"],
          false
        );
        store.deleteAll(extension.id);
        return;
    }

    const hasPersistedScripts = await StartupCache.get(
      extension,
      ["scripting", "hasPersistedScripts"],
      async () => {
        scripts = await store.getAll(extension.id);
        return !!scripts.length;
      }
    );

    if (!hasPersistedScripts) {
      return;
    }

    // Load the scripts from the storage, then convert them to their internal
    // representation and add them to the extension's registered scripts.
    scripts ??= await store.getAll(extension.id);

    scripts.forEach(script => {
      const { scriptId, options } = makeInternalContentScript(
        extension,
        script,
        true /* prepend the css/js paths with the extension base URL */
      );
      extension.registeredContentScripts.set(scriptId, options);
    });
  },

  getInitialScriptIdsMap(extension) {
    // This returns the current map of public script IDs to internal IDs.
    // `extension.registeredContentScripts` is initialized in `initExtension`,
    // which may be updated later via the scripting API. In practice, the map
    // of script IDs is retrieved before any scripting API method is exposed,
    // so the return value always matches the initial result from
    // `initExtension`.
    return new Map(
      Array.from(
        extension.registeredContentScripts.entries(),
        ([scriptId, options]) => [options.id, scriptId]
      )
    );
  },

  async persistAll(extension) {
    // We only persist the scripts that should be persisted and we convert each
    // script to their "public" representation before storing them. This is
    // because we don't want to deal with data migrations if we ever want to
    // change the internal representation (the "public" representation is less
    // likely to change because it is bound to the public scripting API).
    const scripts = Array.from(extension.registeredContentScripts.values())
      .filter(options => options.persistAcrossSessions)
      .map(options => makePublicContentScript(extension, options));

    // We want to replace all the scripts for the extension so we should delete
    // the existing ones first, and then write the new ones.
    //
    // TODO: Bug 1783131 - Implement individual updates without requiring all
    // data to be erased and written.
    await store.deleteAll(extension.id);
    await store.writeMany(extension.id, scripts);
    StartupCache.general.set(
      [extension.id, extension.version, "scripting", "hasPersistedScripts"],
      !!scripts.length
    );
  },

  // Delete all the persisted scripts for the given extension (id).
  //
  // NOTE: to be only used on addon uninstall, the extension entry in the StartupCache
  // is expected to also be fully cleared as part of handling the addon uninstall.
  async clearOnUninstall(extensionId) {
    await store.deleteAll(extensionId);
  },

  // As its name implies, don't use this method for anything but an easy access
  // to the internal store for testing purposes.
  _getStoreForTesting() {
    return store;
  },
};