Skip to content

Commit

Permalink
Bug 1831498 - Stop hardcoding the actor name in ActivityStreamMessage…
Browse files Browse the repository at this point in the history
…Channel. r=mconley

Differential Revision: https://phabricator.services.mozilla.com/D177233
  • Loading branch information
l-hedgehog committed May 8, 2023
1 parent ed35510 commit 73c3d48
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 50 deletions.
6 changes: 4 additions & 2 deletions browser/actors/AboutNewTabParent.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ XPCOMUtils.defineLazyModuleGetters(lazy, {
});

// A mapping of loaded new tab pages, where the mapping is:
// browser -> { browser, browsingContext, portID, url, loaded }
// browser -> { actor, browser, browsingContext, portID, url, loaded }
let gLoadedTabs = new Map();

export class AboutNewTabParent extends JSWindowActorParent {
Expand All @@ -39,13 +39,15 @@ export class AboutNewTabParent extends JSWindowActorParent {
break;

case "Init": {
let browsingContext = message.target.browsingContext;
let actor = message.target;
let browsingContext = actor.browsingContext;
let browser = browsingContext.top.embedderElement;
if (!browser) {
return;
}

let tabDetails = {
actor,
browser,
browsingContext,
portID: message.data.portID,
Expand Down
57 changes: 25 additions & 32 deletions browser/components/newtab/lib/ActivityStreamMessageChannel.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ class ActivityStreamMessageChannel {
// that its likely time to refresh the cache.
lazy.AboutHomeStartupCache.onPreloadedNewTabMessage();

for (let browser of this.loadedTabs.keys()) {
browser.sendMessageToActor(
this.outgoingMessageName,
action,
"AboutNewTab"
);
for (let { actor } of this.loadedTabs.values()) {
try {
actor.sendAsyncMessage(this.outgoingMessageName, action);
} catch (e) {
// The target page is closed/closing by the user or test, so just ignore.
}
}
}

Expand All @@ -126,11 +126,7 @@ class ActivityStreamMessageChannel {
const targetId = action.meta && action.meta.toTarget;
const target = this.getTargetById(targetId);
try {
target.sendMessageToActor(
this.outgoingMessageName,
action,
"AboutNewTab"
);
target.sendAsyncMessage(this.outgoingMessageName, action);
} catch (e) {
// The target page is closed/closing by the user or test, so just ignore.
}
Expand All @@ -149,17 +145,17 @@ class ActivityStreamMessageChannel {
}

/**
* getIdByTarget - Retrieve the id of a message target, if it exists in this.targets
* getTargetById - Retrieve the message target by portID, if it exists
*
* @param {obj} targetObj A message target
* @return {string|null} The unique id of the target, if it exists.
* @param {string} id A portID
* @return {obj|null} The message target, if it exists.
*/
getTargetById(id) {
this.validatePortID(id);

for (let { portID, browser } of this.loadedTabs.values()) {
for (let { portID, actor } of this.loadedTabs.values()) {
if (portID === id) {
return browser;
return actor;
}
}
return null;
Expand All @@ -176,15 +172,11 @@ class ActivityStreamMessageChannel {
// the cache.
lazy.AboutHomeStartupCache.onPreloadedNewTabMessage();

const preloadedBrowsers = this.getPreloadedBrowsers();
if (preloadedBrowsers && action.data) {
for (let preloadedBrowser of preloadedBrowsers) {
const preloadedActors = this.getPreloadedActors();
if (preloadedActors && action.data) {
for (let preloadedActor of preloadedActors) {
try {
preloadedBrowser.sendMessageToActor(
this.outgoingMessageName,
action,
"AboutNewTab"
);
preloadedActor.sendAsyncMessage(this.outgoingMessageName, action);
} catch (e) {
// The preloaded page is no longer available, so just ignore.
}
Expand All @@ -193,19 +185,19 @@ class ActivityStreamMessageChannel {
}

/**
* getPreloadedBrowsers - Retrieve the preloaded browsers
* getPreloadedActors - Retrieve the preloaded actors
*
* @return {Array|null} An array of browsers belonging to the preloaded browsers, or null
* @return {Array|null} An array of actors belonging to the preloaded browsers, or null
* if there aren't any preloaded browsers
*/
getPreloadedBrowsers() {
let preloadedBrowsers = [];
for (let browser of this.loadedTabs.keys()) {
getPreloadedActors() {
let preloadedActors = [];
for (let { actor, browser } of this.loadedTabs.values()) {
if (this.isPreloadedBrowser(browser)) {
preloadedBrowsers.push(browser);
preloadedActors.push(actor);
}
}
return preloadedBrowsers.length ? preloadedBrowsers : null;
return preloadedActors.length ? preloadedActors : null;
}

/**
Expand All @@ -224,6 +216,7 @@ class ActivityStreamMessageChannel {
// Some pages might have already loaded, so we won't get the usual message
for (const loadedTab of this.loadedTabs.values()) {
let simulatedDetails = {
actor: loadedTab.actor,
browser: loadedTab.browser,
browsingContext: loadedTab.browsingContext,
portID: loadedTab.portID,
Expand Down Expand Up @@ -253,7 +246,7 @@ class ActivityStreamMessageChannel {
* @param {obj} tabDetails details about a loaded tab
*
* tabDetails contains:
* browser, browsingContext, portID, url
* actor, browser, browsingContext, portID, url
*/
onNewTabInit(msg, tabDetails) {
this.onActionFromContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ const OPTIONS = [
// Create an object containing details about a tab as expected within
// the loaded tabs map in ActivityStreamMessageChannel.jsm.
function getTabDetails(portID, url = "about:newtab", extraArgs = {}) {
let actor = {
portID,
sendAsyncMessage: sinon.spy(),
};
let browser = {
getAttribute: () => (extraArgs.preloaded ? "preloaded" : ""),
ownerGlobal: {},
sendMessageToActor: sinon.spy(),
portID,
};
let browsingContext = {
top: {
Expand All @@ -32,6 +34,7 @@ function getTabDetails(portID, url = "about:newtab", extraArgs = {}) {

let data = {
data: {
actor,
browser,
browsingContext,
portID,
Expand Down Expand Up @@ -179,31 +182,31 @@ describe("ActivityStreamMessageChannel", () => {
it("should get an id if it exists", () => {
let msg = getTabDetails("foo:1");
mm.loadedTabs.set(msg.data.browser, msg.data);
assert.equal(mm.getTargetById("foo:1"), msg.data.browser);
assert.equal(mm.getTargetById("foo:1"), msg.data.actor);
});
it("should return null if the target doesn't exist", () => {
let msg = getTabDetails("foo:2");
mm.loadedTabs.set(msg.data.browser, msg.data);
assert.equal(mm.getTargetById("bar:3"), null);
});
});
describe("#getPreloadedBrowsers", () => {
it("should get a preloaded browser if it exists", () => {
describe("#getPreloadedActors", () => {
it("should get a preloaded actor if it exists", () => {
let msg = getTabDetails("foo:3", null, { preloaded: true });
mm.loadedTabs.set(msg.data.browser, msg.data);
assert.equal(mm.getPreloadedBrowsers()[0].portID, "foo:3");
assert.equal(mm.getPreloadedActors()[0].portID, "foo:3");
});
it("should get all the preloaded browsers across windows if they exist", () => {
it("should get all the preloaded actors across windows if they exist", () => {
let msg = getTabDetails("foo:4a", null, { preloaded: true });
mm.loadedTabs.set(msg.data.browser, msg.data);
msg = getTabDetails("foo:4b", null, { preloaded: true });
mm.loadedTabs.set(msg.data.browser, msg.data);
assert.equal(mm.getPreloadedBrowsers().length, 2);
assert.equal(mm.getPreloadedActors().length, 2);
});
it("should return null if there is no preloaded browser", () => {
it("should return null if there is no preloaded actor", () => {
let msg = getTabDetails("foo:5");
mm.loadedTabs.set(msg.data.browser, msg.data);
assert.equal(mm.getPreloadedBrowsers(), null);
assert.equal(mm.getPreloadedActors(), null);
});
});
describe("#onNewTabInit", () => {
Expand Down Expand Up @@ -299,7 +302,7 @@ describe("ActivityStreamMessageChannel", () => {
const action = ac.AlsoToOneContent({ type: "HELLO" }, "foo:6");
mm.send(action);
assert.calledWith(
msg.data.browser.sendMessageToActor,
msg.data.actor.sendAsyncMessage,
DEFAULT_OPTIONS.outgoingMessageName,
action
);
Expand All @@ -318,7 +321,7 @@ describe("ActivityStreamMessageChannel", () => {
const action = ac.BroadcastToContent({ type: "HELLO" });
mm.broadcast(action);
assert.calledWith(
msg.data.browser.sendMessageToActor,
msg.data.actor.sendAsyncMessage,
DEFAULT_OPTIONS.outgoingMessageName,
action
);
Expand All @@ -331,7 +334,7 @@ describe("ActivityStreamMessageChannel", () => {
const action = ac.AlsoToPreloaded({ type: "HELLO", data: 10 });
mm.sendToPreloaded(action);
assert.calledWith(
msg.data.browser.sendMessageToActor,
msg.data.actor.sendAsyncMessage,
DEFAULT_OPTIONS.outgoingMessageName,
action
);
Expand All @@ -344,15 +347,15 @@ describe("ActivityStreamMessageChannel", () => {
mm.loadedTabs.set(msg2.data.browser, msg2.data);

mm.sendToPreloaded(ac.AlsoToPreloaded({ type: "HELLO", data: 10 }));
assert.calledOnce(msg1.data.browser.sendMessageToActor);
assert.calledOnce(msg2.data.browser.sendMessageToActor);
assert.calledOnce(msg1.data.actor.sendAsyncMessage);
assert.calledOnce(msg2.data.actor.sendAsyncMessage);
});
it("should not send the message to the preloaded browser if there's no data and a preloaded browser does not exists", () => {
let msg = getTabDetails("foo:11");
mm.loadedTabs.set(msg.data.browser, msg.data);
const action = ac.AlsoToPreloaded({ type: "HELLO" });
mm.sendToPreloaded(action);
assert.notCalled(msg.data.browser.sendMessageToActor);
assert.notCalled(msg.data.actor.sendAsyncMessage);
});
});
});
Expand Down

0 comments on commit 73c3d48

Please sign in to comment.