Bug 1739145 - Create the DownloadSpamProtection module when needed. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D132178
This commit is contained in:
Micah Tigley 2021-11-26 22:58:01 +00:00
Родитель 2516d45fce
Коммит 0732a24662
5 изменённых файлов: 54 добавлений и 59 удалений

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

@ -30,8 +30,6 @@ XPCOMUtils.defineLazyModuleGetters(this, {
* downloads UI with this information.
*/
class DownloadSpamProtection {
static TOPIC = "blocked-automatic-download";
constructor() {
/**
* Tracks URLs we have detected download spam for.
@ -47,13 +45,9 @@ class DownloadSpamProtection {
return this.list;
}
async observe(aSubject, aTopic, URL) {
if (aTopic != DownloadSpamProtection.TOPIC) {
return;
}
if (this._blockedURLToDownloadSpam.has(URL)) {
let downloadSpam = this._blockedURLToDownloadSpam.get(URL);
update(url) {
if (this._blockedURLToDownloadSpam.has(url)) {
let downloadSpam = this._blockedURLToDownloadSpam.get(url);
this.spamList.remove(downloadSpam);
downloadSpam.blockedDownloadsCount += 1;
this.spamList.add(downloadSpam);
@ -61,9 +55,9 @@ class DownloadSpamProtection {
return;
}
let downloadSpam = new DownloadSpam(URL);
let downloadSpam = new DownloadSpam(url);
this.spamList.add(downloadSpam);
this._blockedURLToDownloadSpam.set(URL, downloadSpam);
this._blockedURLToDownloadSpam.set(url, downloadSpam);
let hasActiveDownloads = DownloadsCommon.summarizeDownloads(
this._indicator._activeDownloads()
).numDownloading;

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

@ -139,6 +139,20 @@ var DownloadsPanel = {
DownloadsCommon.log(
"Attempting to initialize DownloadsPanel for a window."
);
// Allow the download spam protection module to notify DownloadsView
// if it's been created.
if (
DownloadIntegration.downloadSpamProtection &&
!DownloadIntegration.downloadSpamProtection.spamList._views.has(
DownloadsView
)
) {
DownloadIntegration.downloadSpamProtection.spamList.addView(
DownloadsView
);
}
if (this._state != this.kStateUninitialized) {
DownloadsCommon.log("DownloadsPanel is already initialized.");
return;
@ -162,10 +176,6 @@ var DownloadsPanel = {
DownloadsSummary
);
DownloadIntegration.getDownloadSpamProtection().spamList.addView(
DownloadsView
);
DownloadsCommon.log(
"DownloadsView attached - the panel for this window",
"should now see download items come in."
@ -200,9 +210,13 @@ var DownloadsPanel = {
DownloadsView.kItemCountLimit
).removeView(DownloadsSummary);
this._unattachEventListeners();
DownloadIntegration.getDownloadSpamProtection().spamList.removeView(
DownloadsView
);
if (DownloadIntegration.downloadSpamProtection) {
DownloadIntegration.downloadSpamProtection.spamList.removeView(
DownloadsView
);
}
this._state = this.kStateUninitialized;
DownloadsSummary.active = false;

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

@ -54,43 +54,38 @@ add_task(async function setup() {
});
add_task(async function check_download_spam_ui() {
let spamProtection = DownloadIntegration.getDownloadSpamProtection();
let oldFunction = spamProtection.observe.bind(spamProtection);
let blockedDownloads = PromiseUtils.defer();
let counter = 0;
let newFunction = async (aSubject, aTopic, URL) => {
await oldFunction(aSubject, aTopic, URL);
counter++;
if (counter == 99) {
blockedDownloads.resolve();
await task_resetState();
registerCleanupFunction(async () => {
BrowserWindowTracker.getTopWindow().DownloadsPanel.hidePanel();
DownloadIntegration.downloadSpamProtection.clearDownloadSpam(TEST_URI);
let publicList = await Downloads.getList(Downloads.PUBLIC);
await publicList.removeFinished();
BrowserTestUtils.removeTab(newTab);
});
let observedBlockedDownloads = 0;
let gotAllBlockedDownloads = TestUtils.topicObserved(
"blocked-automatic-download",
() => {
return ++observedBlockedDownloads >= 99;
}
};
spamProtection.observe = newFunction;
);
let newTab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
TEST_PATH + "test_spammy_page.html"
);
registerCleanupFunction(async () => {
spamProtection.observe = oldFunction;
BrowserWindowTracker.getTopWindow().DownloadsPanel.hidePanel();
DownloadIntegration.getDownloadSpamProtection().clearDownloadSpam(TEST_URI);
let publicList = await Downloads.getList(Downloads.PUBLIC);
await publicList.removeFinished();
BrowserTestUtils.removeTab(newTab);
});
await BrowserTestUtils.synthesizeMouseAtCenter(
"body",
{},
newTab.linkedBrowser
);
await blockedDownloads.promise;
info("Waiting on all blocked downloads.");
await gotAllBlockedDownloads;
let spamProtection = DownloadIntegration.downloadSpamProtection;
let spamList = spamProtection.spamList;
is(
spamList._downloads[0].blockedDownloadsCount,

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

@ -167,6 +167,7 @@ const kObserverTopics = [
"network:offline-about-to-go-offline",
"network:offline-status-changed",
"xpcom-will-shutdown",
"blocked-automatic-download",
];
/**
@ -967,14 +968,11 @@ var DownloadIntegration = {
return Services.dirsvc.get(name, Ci.nsIFile).path;
},
/**
* Returns the DownloadSpamProtection instance.
* Initializes the DownloadSpamProtection instance.
* This is used to observe and group multiple automatic downloads.
*/
getDownloadSpamProtection() {
if (!this._downloadSpamProtection) {
this._downloadSpamProtection = new DownloadSpamProtection();
}
return this._downloadSpamProtection;
_initializeDownloadSpamProtection() {
this.downloadSpamProtection = new DownloadSpamProtection();
},
/**
@ -995,12 +993,6 @@ var DownloadIntegration = {
for (let topic of kObserverTopics) {
Services.obs.addObserver(DownloadObserver, topic);
}
if (AppConstants.MOZ_BUILD_APP == "browser") {
Services.obs.addObserver(
this.getDownloadSpamProtection(),
DownloadSpamProtection.TOPIC
);
}
}
return Promise.resolve();
},
@ -1230,15 +1222,15 @@ var DownloadObserver = {
for (let topic of kObserverTopics) {
Services.obs.removeObserver(this, topic);
}
break;
case "blocked-automatic-download":
if (
AppConstants.MOZ_BUILD_APP == "browser" &&
this._downloadSpamProtection
!DownloadIntegration.downloadSpamProtection
) {
Services.obs.removeObserver(
this._downloadSpamProtection,
DownloadSpamProtection.TOPIC
);
DownloadIntegration._initializeDownloadSpamProtection();
}
DownloadIntegration.downloadSpamProtection.update(aData);
break;
}
},

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

@ -77,7 +77,7 @@ add_task(async function check_download_spam_permissions() {
TEST_PATH + "test_spammy_page.html"
);
registerCleanupFunction(async () => {
DownloadIntegration.getDownloadSpamProtection().clearDownloadSpam(TEST_URI);
DownloadIntegration.downloadSpamProtection.clearDownloadSpam(TEST_URI);
DownloadsPanel.hidePanel();
await publicList.removeFinished();
BrowserTestUtils.removeTab(newTab);