forked from mozilla/multi-account-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.js
187 lines (169 loc) · 5.17 KB
/
bootstrap.js
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
"use strict";
const PREFS = [
{
name: "privacy.userContext.enabled",
value: true,
type: "bool",
default: false
},
{
name: "privacy.userContext.longPressBehavior",
value: 2,
type: "int",
default: 0
},
{
name: "privacy.userContext.ui.enabled",
value: true, // Post web ext we will be setting this true
type: "bool",
default: true
},
{
name: "privacy.usercontext.about_newtab_segregation.enabled",
value: true,
type: "bool",
default: false
},
];
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cc = Components.classes;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.importGlobalProperties(["TextEncoder", "TextDecoder"]);
XPCOMUtils.defineLazyModuleGetter(this, "OS",
"resource://gre/modules/osfile.jsm");
const JETPACK_DIR_BASENAME = "jetpack";
const EXTENSION_ID = "@testpilot-containers";
function loadStyles(resourceURI) {
const styleSheetService = Cc["@mozilla.org/content/style-sheet-service;1"]
.getService(Ci.nsIStyleSheetService);
const styleURI = styleSheet(resourceURI);
const sheetType = styleSheetService.AGENT_SHEET;
styleSheetService.loadAndRegisterSheet(styleURI, sheetType);
}
function styleSheet(resourceURI) {
return Services.io.newURI("data/usercontext.css", null, resourceURI);
}
function unloadStyles(resourceURI) {
const styleURI = styleSheet(resourceURI);
const styleSheetService = Cc["@mozilla.org/content/style-sheet-service;1"]
.getService(Ci.nsIStyleSheetService);
const sheetType = styleSheetService.AGENT_SHEET;
if (styleSheetService.sheetRegistered(styleURI, sheetType)) {
styleSheetService.unregisterSheet(styleURI, sheetType);
}
}
function filename() {
const storeFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
storeFile.append(JETPACK_DIR_BASENAME);
storeFile.append(EXTENSION_ID);
storeFile.append("simple-storage");
storeFile.append("store.json");
return storeFile.path;
}
async function makeFilepath() {
const storeFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
storeFile.append(JETPACK_DIR_BASENAME);
await OS.File.makeDir(storeFile.path, { ignoreExisting: true });
storeFile.append(EXTENSION_ID);
await OS.File.makeDir(storeFile.path, { ignoreExisting: true });
storeFile.append("simple-storage");
await OS.File.makeDir(storeFile.path, { ignoreExisting: true });
}
async function getConfig() {
let savedConfig = {savedConfiguration: {}};
try {
const bytes = await OS.File.read(filename());
const raw = new TextDecoder().decode(bytes) || "";
if (raw) {
savedConfig = JSON.parse(raw);
}
} catch (e) {
// ignore file read errors, sometimes they happen and I'm not sure if we can fix
}
return savedConfig;
}
async function initConfig() {
const savedConfig = await getConfig();
savedConfig.savedConfiguration.version = 2;
if (!("prefs" in savedConfig.savedConfiguration)) {
savedConfig.savedConfiguration.prefs = {};
PREFS.forEach((pref) => {
if ("int" === pref.type) {
savedConfig.savedConfiguration.prefs[pref.name] = Services.prefs.getIntPref(pref.name);
} else {
savedConfig.savedConfiguration.prefs[pref.name] = Services.prefs.getBoolPref(pref.name);
}
});
}
const serialized = JSON.stringify(savedConfig);
const bytes = new TextEncoder().encode(serialized) || "";
await makeFilepath();
await OS.File.writeAtomic(filename(), bytes, { });
}
function setPrefs() {
PREFS.forEach((pref) => {
if ("int" === pref.type) {
Services.prefs.setIntPref(pref.name, pref.value);
} else {
Services.prefs.setBoolPref(pref.name, pref.value);
}
});
}
// eslint-disable-next-line no-unused-vars
async function install() {
await initConfig();
setPrefs();
}
// eslint-disable-next-line no-unused-vars
async function uninstall({resourceURI}, aReason) {
if (checkLegacyFirefox()) {
if (aReason === ADDON_UNINSTALL) {
unloadStyles(resourceURI);
await removeChanges();
}
}
}
async function removeChanges() {
const config = await getConfig();
const storedPrefs = config.savedConfiguration.prefs || {};
PREFS.forEach((pref) => {
let value = pref.default;
if (pref.name in storedPrefs) {
value = storedPrefs[pref.name];
}
if ("int" === pref.type) {
Services.prefs.setIntPref(pref.name, value);
} else {
Services.prefs.setBoolPref(pref.name, value);
}
});
}
function checkLegacyFirefox() {
const version = Services.appinfo.version;
const versionMatch = version.match(/^([0-9]+)\./)[1];
if (Number(versionMatch) <= 56) {
return true;
}
return false;
}
// eslint-disable-next-line no-unused-vars
function startup({webExtension, resourceURI}) {
if (checkLegacyFirefox()) {
loadStyles(resourceURI);
// Reset prefs that may have changed, or are legacy
install();
}
// Start the embedded webextension.
webExtension.startup();
}
// eslint-disable-next-line no-unused-vars
function shutdown({resourceURI}, aReason) {
if (checkLegacyFirefox()) {
unloadStyles(resourceURI);
if (aReason === ADDON_DISABLE) {
removeChanges();
}
}
}