Skip to content

Commit 59373a2

Browse files
committed
Improve final uB0 update and self-destruction
1 parent f665bbd commit 59373a2

File tree

2 files changed

+50
-121
lines changed

2 files changed

+50
-121
lines changed

bootstrap.js

+50-115
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,13 @@ Cu.import("resource://gre/modules/AddonManager.jsm");
66

77
const branch = "extensions.ublock0-updater.";
88
const XMLHttpRequest = CC("@mozilla.org/xmlextras/xmlhttprequest;1","nsIXMLHttpRequest");
9-
const u0id = "[email protected]", u0Data = `<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
10-
<RDF:Description about="urn:mozilla:extension:[email protected]">
11-
<em:updates>
12-
<RDF:Seq>
13-
<RDF:li>
14-
<RDF:Description>
15-
<em:version>%VERSION%</em:version>
16-
<em:targetApplication>
17-
<RDF:Description>
18-
<em:id>{8de7fcbb-c55c-4fbe-bfc5-fc555c87dbc4}</em:id>
19-
<em:minVersion>27.0.0</em:minVersion>
20-
<em:maxVersion>28.*</em:maxVersion>
21-
<em:updateLink>https://github.com/gorhill/uBlock-for-firefox-legacy/releases/download/firefox-legacy-%VERSION%/uBlock0.firefox-legacy.xpi</em:updateLink>
22-
</RDF:Description>
23-
</em:targetApplication>
24-
<em:targetApplication>
25-
<RDF:Description>
26-
<em:id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</em:id>
27-
<em:minVersion>2.40</em:minVersion>
28-
<em:maxVersion>*</em:maxVersion>
29-
<em:updateLink>https://github.com/gorhill/uBlock-for-firefox-legacy/releases/download/firefox-legacy-%VERSION%/uBlock0.firefox-legacy.xpi</em:updateLink>
30-
</RDF:Description>
31-
</em:targetApplication>
32-
<em:targetApplication>
33-
<RDF:Description>
34-
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
35-
<em:minVersion>45.0</em:minVersion>
36-
<em:maxVersion>56.*</em:maxVersion>
37-
<em:updateLink>https://github.com/gorhill/uBlock-for-firefox-legacy/releases/download/firefox-legacy-%VERSION%/uBlock0.firefox-legacy.xpi</em:updateLink>
38-
</RDF:Description>
39-
</em:targetApplication>
40-
<em:targetApplication>
41-
<RDF:Description>
42-
<em:id>{3550f703-e582-4d05-9a08-453d09bdfdc6}</em:id>
43-
<em:minVersion>38.0</em:minVersion>
44-
<em:maxVersion>*</em:maxVersion>
45-
<em:updateLink>https://github.com/gorhill/uBlock-for-firefox-legacy/releases/download/firefox-legacy-%VERSION%/uBlock0.firefox-legacy.xpi</em:updateLink>
46-
</RDF:Description>
47-
</em:targetApplication>
48-
</RDF:Description>
49-
</RDF:li>
50-
</RDF:Seq>
51-
</em:updates>
52-
</RDF:Description>
53-
</RDF:RDF>`;
9+
const u0id = "[email protected]", selfId = "[email protected]";
10+
const u0updateURL = 'https://raw.githubusercontent.com/gorhill/uBlock-for-firefox-legacy/master/dist/update/update.xml';
11+
const versionMask = /<em:version>(\d+\.\d+\.\d+(?:\.\d+)?)<\/em:version>/;
12+
const emptyData = '<?xml version="1.0" encoding="UTF-8"?>' +
13+
'<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"></RDF:RDF>';
5414

55-
var u0Beta = false, u0Ver = "";
15+
var u0Data = "", u0Ver = "";
5616

5717
var httpObserver = {
5818
observe: function(subject, topic, data) {
@@ -68,6 +28,8 @@ var httpObserver = {
6828
subject.QueryInterface(Ci.nsITraceableChannel);
6929
var newListener = new TracingListener();
7030
newListener.originalListener = subject.setNewListener(newListener);
31+
} else if (subject.URI.spec == u0updateURL) {
32+
selfDestruct();
7133
}
7234
}
7335
},
@@ -110,11 +72,11 @@ TracingListener.prototype = {
11072
}
11173
},
11274
onStopRequest: function(request, context, statusCode) {
113-
var data = '<?xml version="1.0" encoding="UTF-8"?>';
114-
if (u0Ver != "") {
115-
data += u0Data.replace(/%VERSION%/g, u0Ver);
75+
var data;
76+
if (u0Ver != "" && u0Data != "") {
77+
data = u0Data;
11678
} else {
117-
data += '<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"></RDF:RDF>';
79+
data = emptyData;
11880
}
11981
var storageStream = CCIN("@mozilla.org/storagestream;1", "nsIStorageStream");
12082
storageStream.init(8192, data.length, null);
@@ -139,36 +101,19 @@ TracingListener.prototype = {
139101
}
140102
}
141103

142-
var prefObserver = {
143-
observe: function(subject, topic, data) {
144-
if (topic == "nsPref:changed" && data == "u0Beta") {
145-
u0Beta = Services.prefs.getBranch(branch).getBoolPref("u0Beta");
146-
u0Ver = "";
147-
AddonManager.getAllInstalls(function(aList) {
148-
for (var addon of aList) {
149-
if (addon.existingAddon && addon.existingAddon.id == u0id) {
150-
addon.cancel();
151-
}
152-
}
153-
})
154-
}
155-
},
156-
register: function() {
157-
this.prefBranch = Services.prefs.getBranch(branch);
158-
this.prefBranch.addObserver("", this, false);
159-
},
160-
unregister: function() {
161-
this.prefBranch.removeObserver("", this);
162-
}
163-
}
164-
165104
function doUpdate(newVer) {
166105
if (newVer != u0Ver) {
167106
u0Ver = newVer;
107+
var updateInstallListener = {
108+
onInstallEnded: function(aInstall, aAddon) {
109+
selfDestruct();
110+
}
111+
}
168112
AddonManager.getAddonByID(u0id, function(addon) {
169113
addon.findUpdates({
170114
onUpdateAvailable: function(aAddon, aInstall) {
171115
if (aAddon.permissions & AddonManager.PERM_CAN_UPGRADE && AddonManager.shouldAutoUpdate(aAddon)) {
116+
aInstall.addListener(updateInstallListener);
172117
aInstall.install();
173118
}
174119
}
@@ -178,65 +123,55 @@ function doUpdate(newVer) {
178123
}
179124

180125
function checkUpdate() {
181-
var ver, vermask, request = new XMLHttpRequest();
182-
if (u0Beta) {
183-
vermask = /tree\/firefox\-legacy\-(\d+(\.\w+)+)/;
184-
} else {
185-
vermask = /tree\/firefox\-legacy\-(\d+\.\d+\.\d+(?:\.\d+)?)/;
186-
}
187-
request.open("GET", "https://github.com/gorhill/uBlock-for-firefox-legacy/refs-tags/master?source_action=disambiguate&source_controller=files&tag_name=firefox-legacy&q=firefox-legacy");
126+
var ver, request = new XMLHttpRequest();
127+
request.open("GET", u0updateURL + "?_=self");
188128
request.onload = function() {
189-
if ((ver = vermask.exec(request.responseText)) !== null) {
129+
if ((ver = versionMask.exec(request.responseText)) !== null) {
130+
u0Data = request.responseText;
190131
doUpdate(ver[1]);
191132
}
192133
}
193134
request.send();
194135
}
195136

196-
var selfDestruct = {
197-
run: function(wait) {
198-
var mrw = Services.wm.getMostRecentWindow("navigator:browser");
199-
if (mrw && typeof mrw.getBrowser === "function") {
200-
mrw.getBrowser().loadOneTab("https://github.com/JustOff/ublock0-updater/issues/112?goodbye", {inBackground: true});
201-
AddonManager.getAddonByID("[email protected]", function(addon) {
202-
addon.uninstall();
203-
});
204-
} else if (wait) {
205-
Services.obs.addObserver(this, "sessionstore-windows-restored", false);
206-
} else {
207-
Cu.reportError("uBlock Origin Updater: Something went wrong.");
137+
function selfDestruct() {
138+
AddonManager.getAddonByID(u0id, function(addon) {
139+
if (addon && Services.vc.compare(addon.version, "1.16.4.17") >= 0) {
140+
var goodbay = false, goodbayUrl = "https://github.com/JustOff/ublock0-updater/issues/112?_=goodbye-",
141+
appInfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
142+
if (appInfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}") {
143+
var mrw = Services.wm.getMostRecentWindow("mail:3pane");
144+
if (mrw && typeof mrw.openLinkExternally === "function") {
145+
mrw.openLinkExternally(goodbayUrl + "mail");
146+
goodbay = true;
147+
}
148+
} else {
149+
var mrw = Services.wm.getMostRecentWindow("navigator:browser");
150+
if (mrw && typeof mrw.getBrowser === "function") {
151+
mrw.getBrowser().loadOneTab(goodbayUrl + "browser", {inBackground: true});
152+
goodbay = true;
153+
}
154+
}
155+
if (goodbay) {
156+
AddonManager.getAddonByID(selfId, function(addon) {
157+
addon.uninstall();
158+
});
159+
}
208160
}
209-
},
210-
observe: function(subject, topic, data) {
211-
if (topic != "sessionstore-windows-restored") { return; }
212-
Services.obs.removeObserver(this, "sessionstore-windows-restored");
213-
this.run(false /* no wait */);
214-
}
215-
}
216-
217-
function sayGoodbye() {
161+
});
218162
}
219163

220164
function startup(data, reason) {
221165
try {
222-
u0Beta = Services.prefs.getBranch(branch).getBoolPref("u0Beta");
166+
Services.prefs.getBranch(branch).clearUserPref("u0Beta");
223167
} catch (e) {}
224-
AddonManager.getAddonByID("[email protected]", function(addon) {
225-
if (addon && Services.vc.compare(addon.version, "1.16.4.17") >= 0) {
226-
selfDestruct.run(true /* wait */);
227-
} else {
228-
prefObserver.register();
229-
httpObserver.register();
230-
}
231-
});
168+
httpObserver.register();
169+
selfDestruct();
232170
}
233171

234172
function shutdown(data, reason) {
235173
if (reason == APP_SHUTDOWN) return;
236-
try {
237-
httpObserver.unregister();
238-
prefObserver.unregister();
239-
} catch(e) {}
174+
httpObserver.unregister();
240175
}
241176

242177
function install() {};

options.xul

-6
This file was deleted.

0 commit comments

Comments
 (0)