summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/resources/storage/index.js
blob: 147f9056eafc163c68c1546ebe80f8a695d18e8e (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* 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/. */

"use strict";

const { Actor } = require("resource://devtools/shared/protocol.js");
const specs = require("resource://devtools/shared/specs/storage.js");

loader.lazyRequireGetter(
  this,
  "naturalSortCaseInsensitive",
  "resource://devtools/shared/natural-sort.js",
  true
);

// Maximum number of cookies/local storage key-value-pairs that can be sent
// over the wire to the client in one request.
const MAX_STORE_OBJECT_COUNT = 50;
exports.MAX_STORE_OBJECT_COUNT = MAX_STORE_OBJECT_COUNT;

const DEFAULT_VALUE = "value";
exports.DEFAULT_VALUE = DEFAULT_VALUE;

// GUID to be used as a separator in compound keys. This must match the same
// constant in devtools/client/storage/ui.js,
// devtools/client/storage/test/head.js and
// devtools/server/tests/browser/head.js
const SEPARATOR_GUID = "{9d414cc5-8319-0a04-0586-c0a6ae01670a}";
exports.SEPARATOR_GUID = SEPARATOR_GUID;

class BaseStorageActor extends Actor {
  /**
   * Base class with the common methods required by all storage actors.
   *
   * This base class is missing a couple of required methods that should be
   * implemented seperately for each actor. They are namely:
   *   - observe : Method which gets triggered on the notification of the watched
   *               topic.
   *   - getNamesForHost : Given a host, get list of all known store names.
   *   - getValuesForHost : Given a host (and optionally a name) get all known
   *                        store objects.
   *   - toStoreObject : Given a store object, convert it to the required format
   *                     so that it can be transferred over wire.
   *   - populateStoresForHost : Given a host, populate the map of all store
   *                             objects for it
   *   - getFields: Given a subType(optional), get an array of objects containing
   *                column field info. The info includes,
   *                "name" is name of colume key.
   *                "editable" is 1 means editable field; 0 means uneditable.
   *
   * @param {string} typeName
   *        The typeName of the actor.
   */
  constructor(storageActor, typeName) {
    super(storageActor.conn, specs.childSpecs[typeName]);

    this.storageActor = storageActor;

    // Map keyed by host name whose values are nested Maps.
    // Nested maps are keyed by store names and values are store values.
    // Store values are specific to each sub class.
    // Map(host name => stores <Map(name => values )>)
    // Map(string => stores <Map(string => any )>)
    this.hostVsStores = new Map();

    this.onWindowReady = this.onWindowReady.bind(this);
    this.onWindowDestroyed = this.onWindowDestroyed.bind(this);
    this.storageActor.on("window-ready", this.onWindowReady);
    this.storageActor.on("window-destroyed", this.onWindowDestroyed);
  }

  destroy() {
    if (!this.storageActor) {
      return;
    }

    this.storageActor.off("window-ready", this.onWindowReady);
    this.storageActor.off("window-destroyed", this.onWindowDestroyed);

    this.hostVsStores.clear();

    super.destroy();

    this.storageActor = null;
  }

  /**
   * Returns a list of currently known hosts for the target window. This list
   * contains unique hosts from the window + all inner windows. If
   * this._internalHosts is defined then these will also be added to the list.
   */
  get hosts() {
    const hosts = new Set();
    for (const { location } of this.storageActor.windows) {
      const host = this.getHostName(location);

      if (host) {
        hosts.add(host);
      }
    }
    if (this._internalHosts) {
      for (const host of this._internalHosts) {
        hosts.add(host);
      }
    }
    return hosts;
  }

  /**
   * Returns all the windows present on the page. Includes main window + inner
   * iframe windows.
   */
  get windows() {
    return this.storageActor.windows;
  }

  /**
   * Converts the window.location object into a URL (e.g. http://domain.com).
   */
  getHostName(location) {
    if (!location) {
      // Debugging a legacy Firefox extension... no hostname available and no
      // storage possible.
      return null;
    }

    if (this.storageActor.getHostName) {
      return this.storageActor.getHostName(location);
    }

    switch (location.protocol) {
      case "about:":
        return `${location.protocol}${location.pathname}`;
      case "chrome:":
        // chrome: URLs do not support storage of any type.
        return null;
      case "data:":
        // data: URLs do not support storage of any type.
        return null;
      case "file:":
        return `${location.protocol}//${location.pathname}`;
      case "javascript:":
        return location.href;
      case "moz-extension:":
        return location.origin;
      case "resource:":
        return `${location.origin}${location.pathname}`;
      default:
        // http: or unknown protocol.
        return `${location.protocol}//${location.host}`;
    }
  }

  /**
   * Populates a map of known hosts vs a map of stores vs value.
   */
  async populateStoresForHosts() {
    for (const host of this.hosts) {
      await this.populateStoresForHost(host);
    }
  }

  getNamesForHost(host) {
    return [...this.hostVsStores.get(host).keys()];
  }

  getValuesForHost(host, name) {
    if (name) {
      return [this.hostVsStores.get(host).get(name)];
    }
    return [...this.hostVsStores.get(host).values()];
  }

  getObjectsSize(host, names) {
    return names.length;
  }

  /**
   * When a new window is added to the page. This generally means that a new
   * iframe is created, or the current window is completely reloaded.
   *
   * @param {window} window
   *        The window which was added.
   */
  async onWindowReady(window) {
    if (!this.hostVsStores) {
      return;
    }
    const host = this.getHostName(window.location);
    if (host && !this.hostVsStores.has(host)) {
      await this.populateStoresForHost(host, window);
      if (!this.storageActor) {
        // The actor might be destroyed during populateStoresForHost.
        return;
      }

      const data = {};
      data[host] = this.getNamesForHost(host);
      this.storageActor.update("added", this.typeName, data);
    }
  }

  /**
   * When a window is removed from the page. This generally means that an
   * iframe was removed, or the current window reload is triggered.
   *
   * @param {window} window
   *        The window which was removed.
   * @param {Object} options
   * @param {Boolean} options.dontCheckHost
   *        If set to true, the function won't check if the host still is in this.hosts.
   *        This is helpful in the case of the StorageActorMock, as the `hosts` getter
   *        uses its `windows` getter, and at this point in time the window which is
   *        going to be destroyed still exists.
   */
  onWindowDestroyed(window, { dontCheckHost } = {}) {
    if (!this.hostVsStores) {
      return;
    }
    if (!window.location) {
      // Nothing can be done if location object is null
      return;
    }
    const host = this.getHostName(window.location);
    if (host && (!this.hosts.has(host) || dontCheckHost)) {
      this.hostVsStores.delete(host);
      const data = {};
      data[host] = [];
      this.storageActor.update("deleted", this.typeName, data);
    }
  }

  form() {
    const hosts = {};
    for (const host of this.hosts) {
      hosts[host] = [];
    }

    return {
      actor: this.actorID,
      hosts,
      traits: this._getTraits(),
    };
  }

  // Share getTraits for child classes overriding form()
  _getTraits() {
    return {
      // The supportsXXX traits are not related to backward compatibility
      // Different storage actor types implement different APIs, the traits
      // help the client to know what is supported or not.
      supportsAddItem: typeof this.addItem === "function",
      // Note: supportsRemoveItem and supportsRemoveAll are always defined
      // for all actors. See Bug 1655001.
      supportsRemoveItem: typeof this.removeItem === "function",
      supportsRemoveAll: typeof this.removeAll === "function",
      supportsRemoveAllSessionCookies:
        typeof this.removeAllSessionCookies === "function",
    };
  }

  /**
   * Returns a list of requested store objects. Maximum values returned are
   * MAX_STORE_OBJECT_COUNT. This method returns paginated values whose
   * starting index and total size can be controlled via the options object
   *
   * @param {string} host
   *        The host name for which the store values are required.
   * @param {array:string} names
   *        Array containing the names of required store objects. Empty if all
   *        items are required.
   * @param {object} options
   *        Additional options for the request containing following
   *        properties:
   *         - offset {number} : The begin index of the returned array amongst
   *                  the total values
   *         - size {number} : The number of values required.
   *         - sortOn {string} : The values should be sorted on this property.
   *         - index {string} : In case of indexed db, the IDBIndex to be used
   *                 for fetching the values.
   *         - sessionString {string} : Client-side value of storage-expires-session
   *                         l10n string. Since this function can be called from both
   *                         the client and the server, and given that client and
   *                         server might have different locales, we can't compute
   *                         the localized string directly from here.
   * @return {object} An object containing following properties:
   *          - offset - The actual offset of the returned array. This might
   *                     be different from the requested offset if that was
   *                     invalid
   *          - total - The total number of entries possible.
   *          - data - The requested values.
   */
  async getStoreObjects(host, names, options = {}) {
    const offset = options.offset || 0;
    let size = options.size || MAX_STORE_OBJECT_COUNT;
    if (size > MAX_STORE_OBJECT_COUNT) {
      size = MAX_STORE_OBJECT_COUNT;
    }
    const sortOn = options.sortOn || "name";

    const toReturn = {
      offset,
      total: 0,
      data: [],
    };

    let principal = null;
    if (this.typeName === "indexedDB") {
      // We only acquire principal when the type of the storage is indexedDB
      // because the principal only matters the indexedDB.
      const win = this.storageActor.getWindowFromHost(host);
      principal = this.getPrincipal(win);
    }

    if (names) {
      for (const name of names) {
        const values = await this.getValuesForHost(
          host,
          name,
          options,
          this.hostVsStores,
          principal
        );

        const { result, objectStores } = values;

        if (result && typeof result.objectsSize !== "undefined") {
          for (const { key, count } of result.objectsSize) {
            this.objectsSize[key] = count;
          }
        }

        if (result) {
          toReturn.data.push(...result.data);
        } else if (objectStores) {
          toReturn.data.push(...objectStores);
        } else {
          toReturn.data.push(...values);
        }
      }

      if (this.typeName === "Cache") {
        // Cache storage contains several items per name but misses a custom
        // `getObjectsSize` implementation, as implemented for IndexedDB.
        // See Bug 1745242.
        toReturn.total = toReturn.data.length;
      } else {
        toReturn.total = this.getObjectsSize(host, names, options);
      }
    } else {
      let obj = await this.getValuesForHost(
        host,
        undefined,
        undefined,
        this.hostVsStores,
        principal
      );
      if (obj.dbs) {
        obj = obj.dbs;
      }

      toReturn.total = obj.length;
      toReturn.data = obj;
    }

    if (offset > toReturn.total) {
      // In this case, toReturn.data is an empty array.
      toReturn.offset = toReturn.total;
      toReturn.data = [];
    } else {
      // We need to use natural sort before slicing.
      const sorted = toReturn.data.sort((a, b) => {
        return naturalSortCaseInsensitive(
          a[sortOn],
          b[sortOn],
          options.sessionString
        );
      });
      let sliced;
      if (this.typeName === "indexedDB") {
        // indexedDB's getValuesForHost never returns *all* values available but only
        // a slice, starting at the expected offset. Therefore the result is already
        // sliced as expected.
        sliced = sorted;
      } else {
        sliced = sorted.slice(offset, offset + size);
      }
      toReturn.data = sliced.map(a => this.toStoreObject(a));
    }

    return toReturn;
  }

  getPrincipal(win) {
    if (win) {
      return win.document.effectiveStoragePrincipal;
    }
    // We are running in the browser toolbox and viewing system DBs so we
    // need to use system principal.
    return Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal);
  }
}
exports.BaseStorageActor = BaseStorageActor;