From aa77a57273d20586f8394eb203288f43b94b9e47 Mon Sep 17 00:00:00 2001 From: Andrei Oprea Date: Wed, 4 Aug 2021 20:22:05 +0000 Subject: [PATCH] Bug 1721178 - Add targeting trigger for successful captive portal login r=dmose Differential Revision: https://phabricator.services.mozilla.com/D120730 --- .../newtab/lib/ASRouterTriggerListeners.jsm | 38 +++++++++++++++++++ .../test/browser/browser_trigger_listeners.js | 19 ++++++++++ .../TriggerActionSchemas.json | 12 ++++++ .../schemas/TriggerActionSchemas/index.md | 5 +++ 4 files changed, 74 insertions(+) diff --git a/browser/components/newtab/lib/ASRouterTriggerListeners.jsm b/browser/components/newtab/lib/ASRouterTriggerListeners.jsm index d64ceb142414..ed6009397258 100644 --- a/browser/components/newtab/lib/ASRouterTriggerListeners.jsm +++ b/browser/components/newtab/lib/ASRouterTriggerListeners.jsm @@ -585,6 +585,44 @@ this.ASRouterTriggerListeners = new Map([ }, }, ], + + [ + "captivePortalLogin", + { + id: "captivePortalLogin", + _initialized: false, + _triggerHandler: null, + + init(triggerHandler) { + if (!this._initialized) { + Services.obs.addObserver(this, "captive-portal-login-success"); + this._initialized = true; + } + this._triggerHandler = triggerHandler; + }, + + observe(aSubject, aTopic, aData) { + switch (aTopic) { + case "captive-portal-login-success": + const browser = Services.wm.getMostRecentBrowserWindow(); + if (browser) { + this._triggerHandler(browser.gBrowser.selectedBrowser, { + id: this.id, + }); + } + break; + } + }, + + uninit() { + if (this._initialized) { + this._triggerHandler = null; + this._initialized = false; + Services.obs.removeObserver(this, "captive-portal-login-success"); + } + }, + }, + ], ]); const EXPORTED_SYMBOLS = ["ASRouterTriggerListeners"]; diff --git a/browser/components/newtab/test/browser/browser_trigger_listeners.js b/browser/components/newtab/test/browser/browser_trigger_listeners.js index c10eafa326ed..af61c8c7a189 100644 --- a/browser/components/newtab/test/browser/browser_trigger_listeners.js +++ b/browser/components/newtab/test/browser/browser_trigger_listeners.js @@ -63,3 +63,22 @@ add_task(async function test_openURL_visit_counter_withPattern() { "Third call should have count 2 for http://example.com" ); }); + +add_task(async function test_captivePortalLogin() { + const stub = sinon.stub(); + const captivePortalTrigger = ASRouterTriggerListeners.get( + "captivePortalLogin" + ); + + captivePortalTrigger.init(stub); + + Services.obs.notifyObservers(this, "captive-portal-login-success", {}); + + Assert.ok(stub.called, "Called after login event"); + + captivePortalTrigger.uninit(); + + Services.obs.notifyObservers(this, "captive-portal-login-success", {}); + + Assert.equal(stub.callCount, 1, "Not called after uninit"); +}); diff --git a/toolkit/components/messaging-system/schemas/TriggerActionSchemas/TriggerActionSchemas.json b/toolkit/components/messaging-system/schemas/TriggerActionSchemas/TriggerActionSchemas.json index 682603f82c9b..1d948c93fe70 100644 --- a/toolkit/components/messaging-system/schemas/TriggerActionSchemas/TriggerActionSchemas.json +++ b/toolkit/components/messaging-system/schemas/TriggerActionSchemas/TriggerActionSchemas.json @@ -185,6 +185,18 @@ "additionalProperties": false, "required": ["id"], "description": "Happens when starting the browser or navigating to about:home/newtab" + }, + { + "type": "object", + "properties": { + "id": { + "type": "string", + "enum": ["captivePortalLogin"] + } + }, + "additionalProperties": false, + "required": ["id"], + "description": "Happens when the user successfully goes through a captive portal authentication flow." } ] } diff --git a/toolkit/components/messaging-system/schemas/TriggerActionSchemas/index.md b/toolkit/components/messaging-system/schemas/TriggerActionSchemas/index.md index 11898c4ccd71..e934b7941bf3 100644 --- a/toolkit/components/messaging-system/schemas/TriggerActionSchemas/index.md +++ b/toolkit/components/messaging-system/schemas/TriggerActionSchemas/index.md @@ -119,3 +119,8 @@ On the newtab/homepage it reports the `source` as `newtab`. let source = "newtab" | undefined; let willShowDefaultPrompt = boolean; ``` + +### `captivePortalLogin` + +Happens when the user successfully goes through a captive portal authentication flow. +