Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
fix(addon): #698 Don't send session pings for inactive tabs when clos…
Browse files Browse the repository at this point in the history
…ing the broswer window
  • Loading branch information
ncloudioj committed May 19, 2016
1 parent 55ee35c commit dd0207a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/TabTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ TabTracker.prototype = {
this._tabData.session_duration = (Date.now() - this._tabData.start_time);
delete this._tabData.start_time;
}
delete this._tabData.active;
Services.obs.notifyObservers(null, COMPLETE_NOTIF, JSON.stringify(this._tabData));
this._clearTabData();
},
Expand Down Expand Up @@ -177,6 +178,7 @@ TabTracker.prototype = {
// URL stored in this._openTabs object keeps the previous URL after the tab.url
// is replaced with a different page URL, as in click action of page reload
this._openTabs[tab.id].url = tab.url;
this._openTabs[tab.id].active = true;
}
},

Expand All @@ -188,19 +190,27 @@ TabTracker.prototype = {
}
if (this.isActivityStreamsURL(tab.url)) {
this.navigateAwayFromPage(tab, "unfocus");
this._openTabs[tab.id].active = false;
}
},

logClose(tab) {
if (this.isActivityStreamsURL(tab.url)) {
// check whether this tab is inactive or not, don't send the close ping
// if it's inactive as an "unfocus" one has already been sent by logDeactivate.
// Note that the test !tabs.activeTab won't work here when the user closes
// the window
if (!this._openTabs[tab.id].active) {
return;
}
this.navigateAwayFromPage(tab, "close");
}
// get rid of that tab reference
delete this._openTabs[tab.id];
},

onOpen(tab) {
this._openTabs[tab.id] = {tab: tab, url: tab.url};
this._openTabs[tab.id] = {tab: tab, url: tab.url, active: true};

this.logReady = this.logReady.bind(this);
this.logActivate = this.logActivate.bind(this);
Expand Down
46 changes: 46 additions & 0 deletions test/test-TabTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,52 @@ exports.test_TabTracker_reactivating = function*(assert) {
checkLoadUnloadReasons(assert, pingData, loadReasons, unloadReasons);
};

exports.test_TabTracker_close_window_with_multitabs = function*(assert) {
let pingData = [];
let openTabs = [];

let pingsSentPromise = createPingSentPromise(pingData, 2);

let tabsOpenedPromise = new Promise(resolve => {
let onOpen = function(tab) {
openTabs.push(tab);
if (openTabs.length === 2) {
tabs.removeListener("ready", onOpen);
resolve();
}
};

tabs.on("ready", onOpen);
});
assert.deepEqual(app.tabData, {}, "tabData starts out empty");

tabs.open(ACTIVITY_STREAMS_URL);
tabs.open(ACTIVITY_STREAMS_URL);

yield tabsOpenedPromise;

openTabs[1].activate();
openTabs[0].activate();

yield pingsSentPromise;
// close both tabs
let tabClosedPromise = new Promise(resolve => {
for (let i in openTabs) {
openTabs[i].close(() => {
if (Number(i) === openTabs.length - 1) {
// We've closed the last tab
resolve();
}
});
}
});

yield tabClosedPromise;
let loadReasons = ["newtab", "focus"];
let unloadReasons = ["unfocus", "unfocus"];
checkLoadUnloadReasons(assert, pingData, loadReasons, unloadReasons, false);
};

exports.test_TabTracker_refresh = function*(assert) {
let openTab;
let numLoads = 0;
Expand Down

0 comments on commit dd0207a

Please sign in to comment.