From 47813a6a87e20c884bb9de2e4c0ec4d173e68e90 Mon Sep 17 00:00:00 2001 From: Nihanth Subramanya Date: Thu, 28 Nov 2019 19:13:02 +0000 Subject: [PATCH] Bug 1598223 - Replace netChange WEE with networkStatus API. r=mixedpuppy,maxxcrawford Differential Revision: https://phabricator.services.mozilla.com/D54625 --HG-- extra : moz-landing-system : lando --- browser/extensions/doh-rollout/background.js | 64 +++++++++---------- .../doh-rollout/experiments/netChange/api.js | 64 ------------------- .../experiments/netChange/schema.json | 19 ------ browser/extensions/doh-rollout/manifest.json | 9 +-- browser/extensions/doh-rollout/moz.build | 5 -- 5 files changed, 31 insertions(+), 130 deletions(-) delete mode 100644 browser/extensions/doh-rollout/experiments/netChange/api.js delete mode 100644 browser/extensions/doh-rollout/experiments/netChange/schema.json diff --git a/browser/extensions/doh-rollout/background.js b/browser/extensions/doh-rollout/background.js index 1500bc2ab0c1..bfd486a727c8 100644 --- a/browser/extensions/doh-rollout/background.js +++ b/browser/extensions/doh-rollout/background.js @@ -100,7 +100,7 @@ const stateManager = { }, async rememberDoorhangerShown() { - // This will be shown on startup and netChange events until a user clicks + // This will be shown on startup and network changes until a user clicks // to confirm/disable DoH or presses the esc key (confirming) log("Remembering that doorhanger has been shown"); await rollout.setSetting(DOH_DOORHANGER_SHOWN_PREF, true); @@ -228,26 +228,6 @@ const rollout = { await stateManager.rememberDoorhangerShown(); }, - async netChangeListener() { - // Possible race condition between multiple notifications? - let curTime = new Date().getTime() / 1000; - let timePassed = curTime - this.lastNetworkChangeTime; - log("Time passed since last network change:", timePassed); - if (timePassed < 30) { - return; - } - - this.lastNetworkChangeTime = curTime; - - // Run heuristics to determine if DoH should be disabled - let decision = await rollout.heuristics("netChange"); - if (decision === "disable_doh") { - await stateManager.setState("disabled"); - } else { - await stateManager.setState("enabled"); - } - }, - async heuristics(evaluateReason) { // Run heuristics defined in heuristics.js and experiments/heuristics/api.js let results = await runHeuristics(); @@ -478,25 +458,41 @@ const rollout = { } // Listen for network change events to run heuristics again - browser.experiments.netChange.onConnectionChanged.addListener(async () => { + browser.networkStatus.onConnectionChanged.addListener(async () => { log("onConnectionChanged"); - // Only run the heuristics if user hasn't explicitly enabled/disabled DoH - let shouldRunHeuristics = await stateManager.shouldRunHeuristics(); - let shouldShowDoorhanger = await stateManager.shouldShowDoorhanger(); - if (!shouldRunHeuristics) { + let linkInfo = await browser.networkStatus.getLinkInfo(); + if (linkInfo.status !== "up") { + log("Link down."); + if (rollout.networkSettledTimeout) { + log("Canceling queued heuristics run."); + clearTimeout(rollout.networkSettledTimeout); + rollout.networkSettledTimeout = null; + } return; } - const netChangeDecision = await rollout.heuristics("netChange"); + log("Queing a heuristics run in 60s, will cancel if network fluctuates."); + rollout.networkSettledTimeout = setTimeout(async () => { + log("No network fluctuation for 60 seconds, running heuristics."); + // Only run the heuristics if user hasn't explicitly enabled/disabled DoH + let shouldRunHeuristics = await stateManager.shouldRunHeuristics(); + let shouldShowDoorhanger = await stateManager.shouldShowDoorhanger(); - if (netChangeDecision === "disable_doh") { - await stateManager.setState("disabled"); - } else if (shouldShowDoorhanger) { - await stateManager.showDoorHangerAndEnableDoH(); - } else { - await stateManager.setState("enabled"); - } + if (!shouldRunHeuristics) { + return; + } + + const netChangeDecision = await rollout.heuristics("netChange"); + + if (netChangeDecision === "disable_doh") { + await stateManager.setState("disabled"); + } else if (shouldShowDoorhanger) { + await stateManager.showDoorHangerAndEnableDoH(); + } else { + await stateManager.setState("enabled"); + } + }, 60000); }); }, diff --git a/browser/extensions/doh-rollout/experiments/netChange/api.js b/browser/extensions/doh-rollout/experiments/netChange/api.js deleted file mode 100644 index 7794fb61381a..000000000000 --- a/browser/extensions/doh-rollout/experiments/netChange/api.js +++ /dev/null @@ -1,64 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -/* global Cc, Ci, ExtensionAPI, ExtensionCommon */ - -ChromeUtils.import("resource://gre/modules/Services.jsm", this); - -const { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm"); - -let gNetworkLinkService = Cc[ - "@mozilla.org/network/network-link-service;1" -].getService(Ci.nsINetworkLinkService); - -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} - -let netChangeWaiting = false; - -this.netChange = class netChange extends ExtensionAPI { - getAPI(context) { - return { - experiments: { - netChange: { - onConnectionChanged: new ExtensionCommon.EventManager({ - context, - name: "netChange.onConnectionChanged", - register: fire => { - let observer = async (subject, topic, data) => { - if (netChangeWaiting) { - return; - } - if (data === "changed" || data === "up") { - // Trigger the netChangeWaiting switch, initiating 5sec timeout - netChangeWaiting = true; - await sleep(60000); - if ( - gNetworkLinkService.linkStatusKnown && - gNetworkLinkService.isLinkUp - ) { - fire.async(data); - } - // Reset the netChangeWaiting switch - netChangeWaiting = false; - } - }; - - Services.obs.addObserver(observer, "network:link-status-changed"); - return () => { - Services.obs.removeObserver( - observer, - "network:link-status-changed" - ); - }; - }, - }).api(), - }, - }, - }; - } -}; diff --git a/browser/extensions/doh-rollout/experiments/netChange/schema.json b/browser/extensions/doh-rollout/experiments/netChange/schema.json deleted file mode 100644 index 4e9fc37fa396..000000000000 --- a/browser/extensions/doh-rollout/experiments/netChange/schema.json +++ /dev/null @@ -1,19 +0,0 @@ -[ - { - "namespace": "experiments.netChange", - "events": [ - { - "name": "onConnectionChanged", - "type": "function", - "description": "Fired when the user has changed networks", - "parameters": [ - { - "type": "boolean", - "name": "connectivity", - "description": "True if we have network connectivity, false otherwise" - } - ] - } - ] - } -] diff --git a/browser/extensions/doh-rollout/manifest.json b/browser/extensions/doh-rollout/manifest.json index bf16ebfb865a..aca5a1527249 100644 --- a/browser/extensions/doh-rollout/manifest.json +++ b/browser/extensions/doh-rollout/manifest.json @@ -16,6 +16,7 @@ "permissions": [ "captivePortal", "dns", + "networkStatus", "storage" ], @@ -40,14 +41,6 @@ "paths": [["experiments", "heuristics"]] } }, - "netChange": { - "schema": "experiments/netChange/schema.json", - "parent": { - "scopes": ["addon_parent"], - "script": "experiments/netChange/api.js", - "paths": [["experiments", "netChange"]] - } - }, "doorhanger": { "schema": "experiments/doorhanger/schema.json", "parent": { diff --git a/browser/extensions/doh-rollout/moz.build b/browser/extensions/doh-rollout/moz.build index 5dfd71c0bde6..1dec8a1121bc 100644 --- a/browser/extensions/doh-rollout/moz.build +++ b/browser/extensions/doh-rollout/moz.build @@ -24,11 +24,6 @@ FINAL_TARGET_FILES.features['doh-rollout@mozilla.org']["experiments"]["heuristic 'experiments/heuristics/schema.json' ] -FINAL_TARGET_FILES.features['doh-rollout@mozilla.org']["experiments"]["netChange"] += [ - 'experiments/netChange/api.js', - 'experiments/netChange/schema.json' -] - FINAL_TARGET_FILES.features['doh-rollout@mozilla.org']["experiments"]["preferences"] += [ 'experiments/preferences/api.js', 'experiments/preferences/schema.json'