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
|
/* 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/. */
const port = browser.runtime.connectNative("browser");
const APIS = {
AddHistogram({ id, value }) {
browser.test.addHistogram(id, value);
},
Eval({ code }) {
// eslint-disable-next-line no-eval
return eval(`(async () => {
${code}
})()`);
},
SetScalar({ id, value }) {
browser.test.setScalar(id, value);
},
GetRequestedLocales() {
return browser.test.getRequestedLocales();
},
GetLinkColor({ tab, selector }) {
return browser.test.getLinkColor(tab.id, selector);
},
GetPidForTab({ tab }) {
return browser.test.getPidForTab(tab.id);
},
WaitForContentTransformsReceived({ tab }) {
return browser.test.waitForContentTransformsReceived(tab.id);
},
GetProfilePath() {
return browser.test.getProfilePath();
},
GetAllBrowserPids() {
return browser.test.getAllBrowserPids();
},
KillContentProcess({ pid }) {
return browser.test.killContentProcess(pid);
},
GetPrefs({ prefs }) {
return browser.test.getPrefs(prefs);
},
GetActive({ tab }) {
return browser.test.getActive(tab.id);
},
RemoveAllCertOverrides() {
browser.test.removeAllCertOverrides();
},
RestorePrefs({ oldPrefs }) {
return browser.test.restorePrefs(oldPrefs);
},
SetPrefs({ oldPrefs, newPrefs }) {
return browser.test.setPrefs(oldPrefs, newPrefs);
},
SetResolutionAndScaleTo({ tab, resolution }) {
return browser.test.setResolutionAndScaleTo(tab.id, resolution);
},
FlushApzRepaints({ tab }) {
return browser.test.flushApzRepaints(tab.id);
},
PromiseAllPaintsDone({ tab }) {
return browser.test.promiseAllPaintsDone(tab.id);
},
UsingGpuProcess() {
return browser.test.usingGpuProcess();
},
KillGpuProcess() {
return browser.test.killGpuProcess();
},
CrashGpuProcess() {
return browser.test.crashGpuProcess();
},
ClearHSTSState() {
return browser.test.clearHSTSState();
},
TriggerCookieBannerDetected({ tab }) {
return browser.test.triggerCookieBannerDetected(tab.id);
},
TriggerCookieBannerHandled({ tab }) {
return browser.test.triggerCookieBannerHandled(tab.id);
},
TriggerTranslationsOffer({ tab }) {
return browser.test.triggerTranslationsOffer(tab.id);
},
TriggerLanguageStateChange({ tab, languageState }) {
return browser.test.triggerLanguageStateChange(tab.id, languageState);
},
};
port.onMessage.addListener(async message => {
const impl = APIS[message.type];
apiCall(message, impl);
});
browser.runtime.onConnect.addListener(contentPort => {
contentPort.onMessage.addListener(message => {
message.args.tab = contentPort.sender.tab;
const impl = APIS[message.type];
apiCall(message, impl);
});
});
function apiCall(message, impl) {
const { id, args } = message;
try {
sendResponse(id, impl(args));
} catch (error) {
sendResponse(id, Promise.reject(error));
}
}
function sendResponse(id, response) {
Promise.resolve(response).then(
value => sendSyncResponse(id, value),
reason => sendSyncResponse(id, null, reason)
);
}
function sendSyncResponse(id, response, exception) {
port.postMessage({
id,
response: JSON.stringify(response),
exception: exception && exception.toString(),
});
}
|