summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/resources/storage/local-and-session-storage.js
blob: ba0f006d22a7ba15b0effa212dc9606127ef6de7 (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
/* 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 {
  BaseStorageActor,
  DEFAULT_VALUE,
} = require("resource://devtools/server/actors/resources/storage/index.js");
const {
  LongStringActor,
} = require("resource://devtools/server/actors/string.js");

class LocalOrSessionStorageActor extends BaseStorageActor {
  constructor(storageActor, typeName) {
    super(storageActor, typeName);

    Services.obs.addObserver(this, "dom-storage2-changed");
    Services.obs.addObserver(this, "dom-private-storage2-changed");
  }

  destroy() {
    if (this.isDestroyed()) {
      return;
    }
    Services.obs.removeObserver(this, "dom-storage2-changed");
    Services.obs.removeObserver(this, "dom-private-storage2-changed");

    super.destroy();
  }

  getNamesForHost(host) {
    const storage = this.hostVsStores.get(host);
    return storage ? Object.keys(storage) : [];
  }

  getValuesForHost(host, name) {
    const storage = this.hostVsStores.get(host);
    if (!storage) {
      return [];
    }
    if (name) {
      const value = storage ? storage.getItem(name) : null;
      return [{ name, value }];
    }
    if (!storage) {
      return [];
    }

    // local and session storage cannot be iterated over using Object.keys()
    // because it skips keys that are duplicated on the prototype
    // e.g. "key", "getKeys" so we need to gather the real keys using the
    // storage.key() function.
    const storageArray = [];
    for (let i = 0; i < storage.length; i++) {
      const key = storage.key(i);
      storageArray.push({
        name: key,
        value: storage.getItem(key),
      });
    }
    return storageArray;
  }

  // We need to override this method as populateStoresForHost expect the window object
  populateStoresForHosts() {
    this.hostVsStores = new Map();
    for (const window of this.windows) {
      const host = this.getHostName(window.location);
      if (host) {
        this.populateStoresForHost(host, window);
      }
    }
  }

  populateStoresForHost(host, window) {
    try {
      this.hostVsStores.set(host, window[this.typeName]);
    } catch (ex) {
      console.warn(
        `Failed to enumerate ${this.typeName} for host ${host}: ${ex}`
      );
    }
  }

  async getFields() {
    return [
      { name: "name", editable: true },
      { name: "value", editable: true },
    ];
  }

  async addItem(guid, host) {
    const storage = this.hostVsStores.get(host);
    if (!storage) {
      return;
    }
    storage.setItem(guid, DEFAULT_VALUE);
  }

  /**
   * Edit localStorage or sessionStorage fields.
   *
   * @param {Object} data
   *        See editCookie() for format details.
   */
  async editItem({ host, field, oldValue, items }) {
    const storage = this.hostVsStores.get(host);
    if (!storage) {
      return;
    }

    if (field === "name") {
      storage.removeItem(oldValue);
    }

    storage.setItem(items.name, items.value);
  }

  async removeItem(host, name) {
    const storage = this.hostVsStores.get(host);
    if (!storage) {
      return;
    }
    storage.removeItem(name);
  }

  async removeAll(host) {
    const storage = this.hostVsStores.get(host);
    if (!storage) {
      return;
    }
    storage.clear();
  }

  observe(subject, topic, data) {
    if (
      (topic != "dom-storage2-changed" &&
        topic != "dom-private-storage2-changed") ||
      data != this.typeName
    ) {
      return null;
    }

    const host = this.getSchemaAndHost(subject.url);

    if (!this.hostVsStores.has(host)) {
      return null;
    }

    let action = "changed";
    if (subject.key == null) {
      return this.storageActor.update("cleared", this.typeName, [host]);
    } else if (subject.oldValue == null) {
      action = "added";
    } else if (subject.newValue == null) {
      action = "deleted";
    }
    const updateData = {};
    updateData[host] = [subject.key];
    return this.storageActor.update(action, this.typeName, updateData);
  }

  /**
   * Given a url, correctly determine its protocol + hostname part.
   */
  getSchemaAndHost(url) {
    const uri = Services.io.newURI(url);
    if (!uri.host) {
      return uri.spec;
    }
    return uri.scheme + "://" + uri.hostPort;
  }

  toStoreObject(item) {
    if (!item) {
      return null;
    }

    return {
      name: item.name,
      value: new LongStringActor(this.conn, item.value || ""),
    };
  }
}

class LocalStorageActor extends LocalOrSessionStorageActor {
  constructor(storageActor) {
    super(storageActor, "localStorage");
  }
}
exports.LocalStorageActor = LocalStorageActor;

class SessionStorageActor extends LocalOrSessionStorageActor {
  constructor(storageActor) {
    super(storageActor, "sessionStorage");
  }
}
exports.SessionStorageActor = SessionStorageActor;