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
|
/* -*- 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 } = ExtensionUtils;
this.webRequest = class extends ExtensionAPI {
STREAM_FILTER_INACTIVE_STATUSES = ["closed", "disconnected", "failed"];
hasActiveStreamFilter(filtersWeakSet) {
const iter = ChromeUtils.nondeterministicGetWeakSetKeys(filtersWeakSet);
for (let filter of iter) {
if (!this.STREAM_FILTER_INACTIVE_STATUSES.includes(filter.status)) {
return true;
}
}
return false;
}
watchStreamFilterSuspendCancel({
context,
filters,
onSuspend,
onSuspendCanceled,
}) {
if (
!context.isBackgroundContext ||
context.extension.persistentBackground !== false
) {
return;
}
const { extension } = context;
const cancelSuspendOnActiveStreamFilter = () =>
this.hasActiveStreamFilter(filters);
context.callOnClose({
close() {
extension.off(
"internal:stream-filter-suspend-cancel",
cancelSuspendOnActiveStreamFilter
);
extension.off("background-script-suspend", onSuspend);
extension.off("background-script-suspend-canceled", onSuspend);
},
});
extension.on(
"internal:stream-filter-suspend-cancel",
cancelSuspendOnActiveStreamFilter
);
extension.on("background-script-suspend", onSuspend);
extension.on("background-script-suspend-canceled", onSuspendCanceled);
}
getAPI(context) {
let filters = new WeakSet();
context.callOnClose({
close() {
for (let filter of ChromeUtils.nondeterministicGetWeakSetKeys(
filters
)) {
try {
filter.disconnect();
} catch (e) {
// Ignore.
}
}
},
});
let isSuspending = false;
this.watchStreamFilterSuspendCancel({
context,
filters,
onSuspend: () => (isSuspending = true),
onSuspendCanceled: () => (isSuspending = false),
});
function filterResponseData(requestId) {
if (isSuspending) {
throw new ExtensionError(
"filterResponseData method calls forbidden while background extension global is suspending"
);
}
requestId = parseInt(requestId, 10);
let streamFilter = context.cloneScope.StreamFilter.create(
requestId,
context.extension.id
);
filters.add(streamFilter);
return streamFilter;
}
const webRequest = {};
// For extensions with manifest_version >= 3, an additional webRequestFilterResponse permission
// is required to get access to the webRequest.filterResponseData API method.
if (
context.extension.manifestVersion < 3 ||
context.extension.hasPermission("webRequestFilterResponse")
) {
webRequest.filterResponseData = filterResponseData;
} else {
webRequest.filterResponseData = () => {
throw new ExtensionError(
'Missing required "webRequestFilterResponse" permission'
);
};
}
return { webRequest };
}
};
|