Bug 1651868, convert async prefs module to use JSProcessActor, r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D83017
This commit is contained in:
Neil Deakin 2020-07-10 12:14:05 +00:00
Родитель e3312ff425
Коммит 5826a0e77e
3 изменённых файлов: 93 добавлений и 164 удалений

Просмотреть файл

@ -800,13 +800,6 @@ const listeners = {
"plugin-crashed": ["PluginManager"],
},
ppmm: {
// PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN AsyncPrefs.init
"AsyncPrefs:SetPref": ["AsyncPrefs"],
"AsyncPrefs:ResetPref": ["AsyncPrefs"],
// PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN AsyncPrefs.init
},
observe(subject, topic, data) {
for (let module of this.observers[topic]) {
try {
@ -817,27 +810,10 @@ const listeners = {
}
},
receiveMessage(modules, data) {
let val;
for (let module of modules[data.name]) {
try {
val = global[module].receiveMessage(data) || val;
} catch (e) {
Cu.reportError(e);
}
}
return val;
},
init() {
for (let observer of Object.keys(this.observers)) {
Services.obs.addObserver(this, observer);
}
let receiveMessagePPMM = this.receiveMessage.bind(this, this.ppmm);
for (let message of Object.keys(this.ppmm)) {
Services.ppmm.addMessageListener(message, receiveMessagePPMM);
}
},
};

Просмотреть файл

@ -34,6 +34,15 @@ const { DefaultMap } = ExtensionUtils;
* available at https://firefox-source-docs.mozilla.org/dom/Fission.html#jsprocessactor
*/
let JSPROCESSACTORS = {
AsyncPrefs: {
parent: {
moduleURI: "resource://gre/modules/AsyncPrefs.jsm",
},
child: {
moduleURI: "resource://gre/modules/AsyncPrefs.jsm",
},
},
ContentPrefs: {
parent: {
moduleURI: "resource://gre/modules/ContentPrefServiceParent.jsm",

Просмотреть файл

@ -4,7 +4,7 @@
"use strict";
var EXPORTED_SYMBOLS = ["AsyncPrefs"];
var EXPORTED_SYMBOLS = ["AsyncPrefs", "AsyncPrefsChild", "AsyncPrefsParent"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
@ -93,146 +93,90 @@ function maybeReturnErrorForSet(pref, value) {
return false;
}
var AsyncPrefs;
if (kInChildProcess) {
let gUniqueId = 0;
let gMsgMap = new Map();
class AsyncPrefsChild extends JSProcessActorChild {
set(pref, value) {
let error = maybeReturnErrorForSet(pref, value);
if (error) {
return Promise.reject(error);
}
AsyncPrefs = {
set(pref, value) {
let error = maybeReturnErrorForSet(pref, value);
if (error) {
return Promise.reject(error);
}
return this.sendQuery("AsyncPrefs:SetPref", {
pref,
value,
});
}
let msgId = ++gUniqueId;
return new Promise((resolve, reject) => {
gMsgMap.set(msgId, { resolve, reject });
Services.cpmm.sendAsyncMessage("AsyncPrefs:SetPref", {
pref,
value,
msgId,
});
});
},
reset(pref) {
let error = maybeReturnErrorForReset(pref);
if (error) {
return Promise.reject(error);
}
reset(pref) {
let error = maybeReturnErrorForReset(pref);
if (error) {
return Promise.reject(error);
}
let msgId = ++gUniqueId;
return new Promise((resolve, reject) => {
gMsgMap.set(msgId, { resolve, reject });
Services.cpmm.sendAsyncMessage("AsyncPrefs:ResetPref", { pref, msgId });
});
},
receiveMessage(msg) {
let promiseRef = gMsgMap.get(msg.data.msgId);
if (promiseRef) {
gMsgMap.delete(msg.data.msgId);
if (msg.data.success) {
promiseRef.resolve();
} else {
promiseRef.reject(msg.data.message);
}
}
},
};
Services.cpmm.addMessageListener("AsyncPrefs:PrefSetFinished", AsyncPrefs);
Services.cpmm.addMessageListener("AsyncPrefs:PrefResetFinished", AsyncPrefs);
} else {
AsyncPrefs = {
methodForType: {
number: "setIntPref",
boolean: "setBoolPref",
string: "setCharPref",
},
set(pref, value) {
let error = maybeReturnErrorForSet(pref, value);
if (error) {
return Promise.reject(error);
}
let methodToUse = this.methodForType[typeof value];
try {
Services.prefs[methodToUse](pref, value);
return Promise.resolve(value);
} catch (ex) {
Cu.reportError(ex);
return Promise.reject(ex.message);
}
},
reset(pref) {
let error = maybeReturnErrorForReset(pref);
if (error) {
return Promise.reject(error);
}
try {
Services.prefs.clearUserPref(pref);
return Promise.resolve();
} catch (ex) {
Cu.reportError(ex);
return Promise.reject(ex.message);
}
},
receiveMessage(msg) {
if (msg.name == "AsyncPrefs:SetPref") {
this.onPrefSet(msg);
} else {
this.onPrefReset(msg);
}
},
onPrefReset(msg) {
let { pref, msgId } = msg.data;
this.reset(pref).then(
function() {
msg.target.sendAsyncMessage("AsyncPrefs:PrefResetFinished", {
msgId,
success: true,
});
},
function(msg) {
msg.target.sendAsyncMessage("AsyncPrefs:PrefResetFinished", {
msgId,
success: false,
message: msg,
});
}
);
},
onPrefSet(msg) {
let { pref, value, msgId } = msg.data;
this.set(pref, value).then(
function() {
msg.target.sendAsyncMessage("AsyncPrefs:PrefSetFinished", {
msgId,
success: true,
});
},
function(msg) {
msg.target.sendAsyncMessage("AsyncPrefs:PrefSetFinished", {
msgId,
success: false,
message: msg,
});
}
);
},
init() {
// PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN nsBrowserGlue
Services.ppmm.addMessageListener("AsyncPrefs:SetPref", this);
Services.ppmm.addMessageListener("AsyncPrefs:ResetPref", this);
// PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN nsBrowserGlue
},
};
return this.sendQuery("AsyncPrefs:ResetPref", { pref });
}
}
var AsyncPrefs = {
set(pref, value) {
if (kInChildProcess) {
return ChromeUtils.domProcessChild
.getActor("AsyncPrefs")
.set(pref, value);
}
return AsyncPrefsParent.set(pref, value);
},
reset(pref, value) {
if (kInChildProcess) {
return ChromeUtils.domProcessChild.getActor("AsyncPrefs").reset(pref);
}
return AsyncPrefsParent.reset(pref);
},
};
const methodForType = {
number: "setIntPref",
boolean: "setBoolPref",
string: "setCharPref",
};
class AsyncPrefsParent extends JSProcessActorParent {
static set(pref, value) {
let error = maybeReturnErrorForSet(pref, value);
if (error) {
return Promise.reject(error);
}
let methodToUse = methodForType[typeof value];
try {
Services.prefs[methodToUse](pref, value);
} catch (ex) {
Cu.reportError(ex);
return Promise.reject(ex.message);
}
return Promise.resolve(value);
}
static reset(pref) {
let error = maybeReturnErrorForReset(pref);
if (error) {
return Promise.reject(error);
}
try {
Services.prefs.clearUserPref(pref);
} catch (ex) {
Cu.reportError(ex);
return Promise.reject(ex.message);
}
return Promise.resolve();
}
receiveMessage(msg) {
if (msg.name == "AsyncPrefs:SetPref") {
return AsyncPrefsParent.set(msg.data.pref, msg.data.value);
}
return AsyncPrefsParent.reset(msg.data.pref);
}
}