Skip to content

Commit d18ec80

Browse files
committed
Bug 1869678 - Use more idiomatic code patterns for better type inference r=robwu
These fall under 5 main categories: 1) Declare and/or initialize all class fiels in the constructor. (general good practise) 2) Use real getters and redefineGetter instead of defineLazyGetter. (also keeps related code closer together) 3) When subclassing, don't override class fields with getters (or vice versa). microsoft/TypeScript#33509 4) Declare and assign object literals at the same time, not separatelly. (don't use `let foo;` at the top of the file, use `var foo = {`) 5) Don't re-use local variables unnecesarily with different types. (general good practise, local variables are "free") Differential Revision: https://phabricator.services.mozilla.com/D196386 UltraBlame original commit: 8e768446e17cc306729e3b0f705b0285c69321cf
1 parent 85eef6c commit d18ec80

20 files changed

+403
-354
lines changed

toolkit/components/extensions/.eslintrc.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,21 @@ module.exports = {
5757
],
5858

5959

60-
"no-use-before-define": "error",
60+
"no-use-before-define": [
61+
"error",
62+
{
63+
allowNamedExports: true,
64+
classes: true,
65+
66+
67+
functions: false,
68+
69+
70+
71+
72+
variables: false,
73+
},
74+
],
6175

6276

6377

toolkit/components/extensions/ConduitsParent.sys.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,21 +452,21 @@ export class ConduitsParent extends JSWindowActorParent {
452452
return Hub.recvConduitOpened(arg, this);
453453
}
454454

455-
sender = Hub.remotes.get(sender);
456-
if (!sender || sender.actor !== this) {
455+
let remote = Hub.remotes.get(sender);
456+
if (!remote || remote.actor !== this) {
457457
throw new Error(`Unknown sender or wrong actor for recv${name}`);
458458
}
459459

460460
if (name === "ConduitClosed") {
461-
return Hub.recvConduitClosed(sender);
461+
return Hub.recvConduitClosed(remote);
462462
}
463463

464464
let conduit = Hub.byMethod.get(name);
465465
if (!conduit) {
466466
throw new Error(`Parent conduit for recv${name} not found`);
467467
}
468468

469-
return conduit._recv(name, arg, { actor: this, query, sender });
469+
return conduit._recv(name, arg, { actor: this, query, sender: remote });
470470
}
471471

472472
/**

toolkit/components/extensions/Extension.sys.mjs

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export { Management };
172172

173173
const { getUniqueId, promiseTimeout } = ExtensionUtils;
174174

175-
const { EventEmitter, updateAllowedOrigins } = ExtensionCommon;
175+
const { EventEmitter, redefineGetter, updateAllowedOrigins } = ExtensionCommon;
176176

177177
ChromeUtils.defineLazyGetter(
178178
lazy,
@@ -869,6 +869,19 @@ const manifestTypes = new Map([
869869
* `loadManifest` has been called, and completed.
870870
*/
871871
export class ExtensionData {
872+
/**
873+
* Note: These fields are only available and meant to be used on Extension
874+
* instances, declared here because methods from this class reference them.
875+
*/
876+
/** @type {object} TODO: move to the Extension class, bug 1871094. */
877+
addonData;
878+
/** @type {nsIURI} */
879+
baseURI;
880+
/** @type {nsIPrincipal} */
881+
principal;
882+
/** @type {boolean} */
883+
temporarilyInstalled;
884+
872885
constructor(rootURI, isPrivileged = false) {
873886
this.rootURI = rootURI;
874887
this.resourceURL = rootURI.spec;
@@ -2676,7 +2689,7 @@ class BootstrapScope {
26762689
// APP_STARTED. In some situations, such as background and
26772690
// persisted listeners, we also need to know that the addon
26782691
// was updated.
2679-
this.updateReason = this.BOOTSTRAP_REASON_TO_STRING_MAP[reason];
2692+
this.updateReason = BootstrapScope.BOOTSTRAP_REASON_MAP[reason];
26802693
// Retain any previously granted permissions that may have migrated
26812694
// into the optional list.
26822695
if (data.oldPermissions) {
@@ -2703,39 +2716,35 @@ class BootstrapScope {
27032716
// eslint-disable-next-line no-use-before-define
27042717
this.extension = new Extension(
27052718
data,
2706-
this.BOOTSTRAP_REASON_TO_STRING_MAP[reason],
2719+
BootstrapScope.BOOTSTRAP_REASON_MAP[reason],
27072720
this.updateReason
27082721
);
27092722
return this.extension.startup();
27102723
}
27112724

27122725
async shutdown(data, reason) {
27132726
let result = await this.extension.shutdown(
2714-
this.BOOTSTRAP_REASON_TO_STRING_MAP[reason]
2727+
BootstrapScope.BOOTSTRAP_REASON_MAP[reason]
27152728
);
27162729
this.extension = null;
27172730
return result;
27182731
}
2719-
}
27202732

2721-
ChromeUtils.defineLazyGetter(
2722-
BootstrapScope.prototype,
2723-
"BOOTSTRAP_REASON_TO_STRING_MAP",
2724-
() => {
2725-
const { BOOTSTRAP_REASONS } = lazy.AddonManagerPrivate;
2726-
2727-
return Object.freeze({
2728-
[BOOTSTRAP_REASONS.APP_STARTUP]: "APP_STARTUP",
2729-
[BOOTSTRAP_REASONS.APP_SHUTDOWN]: "APP_SHUTDOWN",
2730-
[BOOTSTRAP_REASONS.ADDON_ENABLE]: "ADDON_ENABLE",
2731-
[BOOTSTRAP_REASONS.ADDON_DISABLE]: "ADDON_DISABLE",
2732-
[BOOTSTRAP_REASONS.ADDON_INSTALL]: "ADDON_INSTALL",
2733-
[BOOTSTRAP_REASONS.ADDON_UNINSTALL]: "ADDON_UNINSTALL",
2734-
[BOOTSTRAP_REASONS.ADDON_UPGRADE]: "ADDON_UPGRADE",
2735-
[BOOTSTRAP_REASONS.ADDON_DOWNGRADE]: "ADDON_DOWNGRADE",
2733+
static get BOOTSTRAP_REASON_MAP() {
2734+
const BR = lazy.AddonManagerPrivate.BOOTSTRAP_REASONS;
2735+
const value = Object.freeze({
2736+
[BR.APP_STARTUP]: "APP_STARTUP",
2737+
[BR.APP_SHUTDOWN]: "APP_SHUTDOWN",
2738+
[BR.ADDON_ENABLE]: "ADDON_ENABLE",
2739+
[BR.ADDON_DISABLE]: "ADDON_DISABLE",
2740+
[BR.ADDON_INSTALL]: "ADDON_INSTALL",
2741+
[BR.ADDON_UNINSTALL]: "ADDON_UNINSTALL",
2742+
[BR.ADDON_UPGRADE]: "ADDON_UPGRADE",
2743+
[BR.ADDON_DOWNGRADE]: "ADDON_DOWNGRADE",
27362744
});
2745+
return redefineGetter(this, "BOOTSTRAP_REASON_TO_STRING_MAP", value);
27372746
}
2738-
);
2747+
}
27392748

27402749
class DictionaryBootstrapScope extends BootstrapScope {
27412750
install(data, reason) {}
@@ -2744,28 +2753,28 @@ class DictionaryBootstrapScope extends BootstrapScope {
27442753
startup(data, reason) {
27452754
// eslint-disable-next-line no-use-before-define
27462755
this.dictionary = new Dictionary(data);
2747-
return this.dictionary.startup(this.BOOTSTRAP_REASON_TO_STRING_MAP[reason]);
2756+
return this.dictionary.startup(BootstrapScope.BOOTSTRAP_REASON_MAP[reason]);
27482757
}
27492758

2750-
shutdown(data, reason) {
2751-
this.dictionary.shutdown(this.BOOTSTRAP_REASON_TO_STRING_MAP[reason]);
2759+
async shutdown(data, reason) {
2760+
this.dictionary.shutdown(BootstrapScope.BOOTSTRAP_REASON_MAP[reason]);
27522761
this.dictionary = null;
27532762
}
27542763
}
27552764

27562765
class LangpackBootstrapScope extends BootstrapScope {
27572766
install(data, reason) {}
27582767
uninstall(data, reason) {}
2759-
update(data, reason) {}
2768+
async update(data, reason) {}
27602769

27612770
startup(data, reason) {
27622771
// eslint-disable-next-line no-use-before-define
27632772
this.langpack = new Langpack(data);
2764-
return this.langpack.startup(this.BOOTSTRAP_REASON_TO_STRING_MAP[reason]);
2773+
return this.langpack.startup(BootstrapScope.BOOTSTRAP_REASON_MAP[reason]);
27652774
}
27662775

2767-
shutdown(data, reason) {
2768-
this.langpack.shutdown(this.BOOTSTRAP_REASON_TO_STRING_MAP[reason]);
2776+
async shutdown(data, reason) {
2777+
this.langpack.shutdown(BootstrapScope.BOOTSTRAP_REASON_MAP[reason]);
27692778
this.langpack = null;
27702779
}
27712780
}
@@ -2779,12 +2788,12 @@ class SitePermissionBootstrapScope extends BootstrapScope {
27792788
// eslint-disable-next-line no-use-before-define
27802789
this.sitepermission = new SitePermission(data);
27812790
return this.sitepermission.startup(
2782-
this.BOOTSTRAP_REASON_TO_STRING_MAP[reason]
2791+
BootstrapScope.BOOTSTRAP_REASON_MAP[reason]
27832792
);
27842793
}
27852794

2786-
shutdown(data, reason) {
2787-
this.sitepermission.shutdown(this.BOOTSTRAP_REASON_TO_STRING_MAP[reason]);
2795+
async shutdown(data, reason) {
2796+
this.sitepermission.shutdown(BootstrapScope.BOOTSTRAP_REASON_MAP[reason]);
27882797
this.sitepermission = null;
27892798
}
27902799
}
@@ -2800,6 +2809,9 @@ let pendingExtensions = new Map();
28002809
* @augments ExtensionData
28012810
*/
28022811
export class Extension extends ExtensionData {
2812+
/** @type {Map<string, Map<string, any>>} */
2813+
persistentListeners;
2814+
28032815
constructor(addonData, startupReason, updateReason) {
28042816
super(addonData.resourceURI, addonData.isPrivileged);
28052817

@@ -2832,6 +2844,7 @@ export class Extension extends ExtensionData {
28322844
this.startupData = addonData.startupData || {};
28332845
this.startupReason = startupReason;
28342846
this.updateReason = updateReason;
2847+
this.temporarilyInstalled = !!addonData.temporarilyInstalled;
28352848

28362849
if (
28372850
updateReason ||
@@ -3077,10 +3090,6 @@ export class Extension extends ExtensionData {
30773090
return [this.id, this.version, Services.locale.appLocaleAsBCP47];
30783091
}
30793092

3080-
get temporarilyInstalled() {
3081-
return !!this.addonData.temporarilyInstalled;
3082-
}
3083-
30843093
saveStartupData() {
30853094
if (this.dontSaveStartupData) {
30863095
return;

toolkit/components/extensions/ExtensionActions.sys.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class PanelActionBase {
4343
enabled: true,
4444
title: options.default_title || extension.name,
4545
popup: options.default_popup || "",
46+
icon: null,
4647
};
4748
this.globals = Object.create(this.defaults);
4849

toolkit/components/extensions/ExtensionChild.sys.mjs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { ExtensionUtils } from "resource://gre/modules/ExtensionUtils.sys.mjs";
3939
const { DefaultMap, ExtensionError, LimitedSet, getUniqueId } = ExtensionUtils;
4040

4141
const {
42-
defineLazyGetter,
42+
redefineGetter,
4343
EventEmitter,
4444
EventManager,
4545
LocalAPIImplementation,
@@ -299,12 +299,13 @@ class Port {
299299
}
300300
throw new this.context.Error("Attempt to postMessage on disconnected port");
301301
}
302-
}
303302

304-
defineLazyGetter(Port.prototype, "api", function () {
305-
let api = this.getAPI();
306-
return Cu.cloneInto(api, this.context.cloneScope, { cloneFunctions: true });
307-
});
303+
get api() {
304+
const scope = this.context.cloneScope;
305+
const value = Cu.cloneInto(this.getAPI(), scope, { cloneFunctions: true });
306+
return redefineGetter(this, "api", value);
307+
}
308+
}
308309

309310
/**
310311
* Each extension context gets its own Messenger object. It handles the
@@ -522,7 +523,7 @@ class BrowserExtensionContent extends EventEmitter {
522523

523524
emit(event, ...args) {
524525
Services.cpmm.sendAsyncMessage(this.MESSAGE_EMIT_EVENT, { event, args });
525-
super.emit(event, ...args);
526+
return super.emit(event, ...args);
526527
}
527528

528529
// TODO(Bug 1768471): consider folding this back into emit if we will change it to
@@ -914,10 +915,10 @@ class ChildAPIManager {
914915
* hasListener methods. See SchemaAPIInterface for documentation.
915916
*/
916917
getParentEvent(path) {
917-
path = path.split(".");
918+
let parts = path.split(".");
918919

919-
let name = path.pop();
920-
let namespace = path.join(".");
920+
let name = parts.pop();
921+
let namespace = parts.join(".");
921922

922923
let impl = new ProxyAPIImplementation(namespace, name, this, true);
923924
return {

0 commit comments

Comments
 (0)