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
|
/* -*- 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/. */
"use strict";
var { ExtensionError, promiseObserved } = ExtensionUtils;
ChromeUtils.defineESModuleGetters(this, {
AddonManagerPrivate: "resource://gre/modules/AddonManager.sys.mjs",
SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
});
const SS_ON_CLOSED_OBJECTS_CHANGED = "sessionstore-closed-objects-changed";
const getRecentlyClosed = (maxResults, extension) => {
let recentlyClosed = [];
// Get closed windows
// Closed private windows are not stored in sessionstore, we do
// not need to check access for that.
let closedWindowData = SessionStore.getClosedWindowData();
for (let window of closedWindowData) {
recentlyClosed.push({
lastModified: window.closedAt,
window: Window.convertFromSessionStoreClosedData(extension, window),
});
}
// Get closed tabs
// Private closed tabs are in sessionstore if the owning window is still open .
for (let window of windowTracker.browserWindows()) {
if (!extension.canAccessWindow(window)) {
continue;
}
let closedTabData = SessionStore.getClosedTabDataForWindow(window);
for (let tab of closedTabData) {
recentlyClosed.push({
lastModified: tab.closedAt,
tab: Tab.convertFromSessionStoreClosedData(extension, tab, window),
});
}
}
// Sort windows and tabs
recentlyClosed.sort((a, b) => b.lastModified - a.lastModified);
return recentlyClosed.slice(0, maxResults);
};
const createSession = async function createSession(
restored,
extension,
sessionId
) {
if (!restored) {
throw new ExtensionError(
`Could not restore object using sessionId ${sessionId}.`
);
}
let sessionObj = { lastModified: Date.now() };
if (restored.isChromeWindow) {
await promiseObserved(
"sessionstore-single-window-restored",
subject => subject == restored
);
sessionObj.window = extension.windowManager.convert(restored, {
populate: true,
});
return sessionObj;
}
sessionObj.tab = extension.tabManager.convert(restored);
return sessionObj;
};
const getEncodedKey = function getEncodedKey(extensionId, key) {
// Throw if using a temporary extension id.
if (AddonManagerPrivate.isTemporaryInstallID(extensionId)) {
let message =
"Sessions API storage methods will not work with a temporary addon ID. " +
"Please add an explicit addon ID to your manifest.";
throw new ExtensionError(message);
}
return `extension:${extensionId}:${key}`;
};
this.sessions = class extends ExtensionAPIPersistent {
PERSISTENT_EVENTS = {
onChanged({ fire }) {
let observer = () => {
fire.async();
};
Services.obs.addObserver(observer, SS_ON_CLOSED_OBJECTS_CHANGED);
return {
unregister() {
Services.obs.removeObserver(observer, SS_ON_CLOSED_OBJECTS_CHANGED);
},
convert(_fire) {
fire = _fire;
},
};
},
};
getAPI(context) {
let { extension } = context;
function getTabParams(key, id) {
let encodedKey = getEncodedKey(extension.id, key);
let tab = tabTracker.getTab(id);
if (!context.canAccessWindow(tab.ownerGlobal)) {
throw new ExtensionError(`Invalid tab ID: ${id}`);
}
return { encodedKey, tab };
}
function getWindowParams(key, id) {
let encodedKey = getEncodedKey(extension.id, key);
let win = windowTracker.getWindow(id, context);
return { encodedKey, win };
}
function getClosedIdFromSessionId(sessionId) {
// sessionId is a string, but internally closedId values are integers.
// convertFromSessionStoreClosedData in ext-browser.js does the opposite conversion.
let closedId = parseInt(sessionId, 10);
if (Number.isInteger(closedId)) {
return closedId;
}
throw new ExtensionError(`Invalid sessionId: ${sessionId}.`);
}
return {
sessions: {
async getRecentlyClosed(filter) {
await SessionStore.promiseInitialized;
let maxResults =
filter.maxResults == undefined
? this.MAX_SESSION_RESULTS
: filter.maxResults;
return getRecentlyClosed(maxResults, extension);
},
async forgetClosedTab(windowId, sessionId) {
await SessionStore.promiseInitialized;
let window = windowTracker.getWindow(windowId, context);
let closedTabData = SessionStore.getClosedTabDataForWindow(window);
let closedId = getClosedIdFromSessionId(sessionId);
let closedTabIndex = closedTabData.findIndex(closedTab => {
return closedTab.closedId === closedId;
});
if (closedTabIndex < 0) {
throw new ExtensionError(
`Could not find closed tab using sessionId ${sessionId}.`
);
}
SessionStore.forgetClosedTab(window, closedTabIndex);
},
async forgetClosedWindow(sessionId) {
await SessionStore.promiseInitialized;
let closedWindowData = SessionStore.getClosedWindowData();
let closedId = getClosedIdFromSessionId(sessionId);
let closedWindowIndex = closedWindowData.findIndex(closedWindow => {
return closedWindow.closedId === closedId;
});
if (closedWindowIndex < 0) {
throw new ExtensionError(
`Could not find closed window using sessionId ${sessionId}.`
);
}
SessionStore.forgetClosedWindow(closedWindowIndex);
},
async restore(sessionId) {
await SessionStore.promiseInitialized;
let session;
let closedId;
if (sessionId) {
closedId = getClosedIdFromSessionId(sessionId);
}
let targetWindow;
// closedId is internally represented as an integer and could be 0.
if (closedId !== undefined) {
if (SessionStore.getObjectTypeForClosedId(closedId) == "tab") {
// we want to restore the tab to the original window is was closed from
targetWindow = SessionStore.getWindowForTabClosedId(
closedId,
extension.privateBrowsingAllowed
);
}
session = SessionStore.undoCloseById(
closedId,
extension.privateBrowsingAllowed,
targetWindow // ignored if we are restoring a window
);
} else if (SessionStore.lastClosedObjectType == "window") {
// If the most recently closed object is a window, just undo closing the most recent window.
session = SessionStore.undoCloseWindow(0);
} else {
// It is a tab, and we cannot call SessionStore.undoCloseTab without a window,
// so we must find the tab in which case we can just use its closedId.
let recentlyClosedTabs = [];
for (let window of windowTracker.browserWindows()) {
let closedTabData =
SessionStore.getClosedTabDataForWindow(window);
for (let tab of closedTabData) {
recentlyClosedTabs.push(tab);
}
}
if (recentlyClosedTabs.length) {
// Sort the tabs.
recentlyClosedTabs.sort((a, b) => b.closedAt - a.closedAt);
// Use the closedId of the most recently closed tab to restore it.
closedId = recentlyClosedTabs[0].closedId;
// we want the tab to be re-opened into the same window it was closed from
targetWindow = SessionStore.getWindowForTabClosedId(
closedId,
extension.privateBrowsingAllowed
);
session = SessionStore.undoCloseById(
closedId,
extension.privateBrowsingAllowed,
targetWindow
);
}
}
return createSession(session, extension, closedId);
},
setTabValue(tabId, key, value) {
let { tab, encodedKey } = getTabParams(key, tabId);
SessionStore.setCustomTabValue(
tab,
encodedKey,
JSON.stringify(value)
);
},
async getTabValue(tabId, key) {
let { tab, encodedKey } = getTabParams(key, tabId);
let value = SessionStore.getCustomTabValue(tab, encodedKey);
if (value) {
return JSON.parse(value);
}
return undefined;
},
removeTabValue(tabId, key) {
let { tab, encodedKey } = getTabParams(key, tabId);
SessionStore.deleteCustomTabValue(tab, encodedKey);
},
setWindowValue(windowId, key, value) {
let { win, encodedKey } = getWindowParams(key, windowId);
SessionStore.setCustomWindowValue(
win,
encodedKey,
JSON.stringify(value)
);
},
async getWindowValue(windowId, key) {
let { win, encodedKey } = getWindowParams(key, windowId);
let value = SessionStore.getCustomWindowValue(win, encodedKey);
if (value) {
return JSON.parse(value);
}
return undefined;
},
removeWindowValue(windowId, key) {
let { win, encodedKey } = getWindowParams(key, windowId);
SessionStore.deleteCustomWindowValue(win, encodedKey);
},
onChanged: new EventManager({
context,
module: "sessions",
event: "onChanged",
extensionApi: this,
}).api(),
},
};
}
};
|