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

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

@ -139,6 +139,20 @@ var DownloadsPanel = {
DownloadsCommon.log( DownloadsCommon.log(
"Attempting to initialize DownloadsPanel for a window." "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) { if (this._state != this.kStateUninitialized) {
DownloadsCommon.log("DownloadsPanel is already initialized."); DownloadsCommon.log("DownloadsPanel is already initialized.");
return; return;
@ -162,10 +176,6 @@ var DownloadsPanel = {
DownloadsSummary DownloadsSummary
); );
DownloadIntegration.getDownloadSpamProtection().spamList.addView(
DownloadsView
);
DownloadsCommon.log( DownloadsCommon.log(
"DownloadsView attached - the panel for this window", "DownloadsView attached - the panel for this window",
"should now see download items come in." "should now see download items come in."
@ -200,9 +210,13 @@ var DownloadsPanel = {
DownloadsView.kItemCountLimit DownloadsView.kItemCountLimit
).removeView(DownloadsSummary); ).removeView(DownloadsSummary);
this._unattachEventListeners(); this._unattachEventListeners();
DownloadIntegration.getDownloadSpamProtection().spamList.removeView(
if (DownloadIntegration.downloadSpamProtection) {
DownloadIntegration.downloadSpamProtection.spamList.removeView(
DownloadsView DownloadsView
); );
}
this._state = this.kStateUninitialized; this._state = this.kStateUninitialized;
DownloadsSummary.active = false; DownloadsSummary.active = false;

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

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

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

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

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

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