Skip to content

Commit

Permalink
Bug 1723776 - Add SmartBlock section to about:compat; r=denschub,pbz,…
Browse files Browse the repository at this point in the history
…webcompat-reviewers,flod

Differential Revision: https://phabricator.services.mozilla.com/D130524
  • Loading branch information
wisniewskit committed Nov 19, 2021
1 parent cbf4b6c commit 08e5aab
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 38 deletions.
6 changes: 6 additions & 0 deletions browser/extensions/webcompat/about-compat/aboutCompat.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ <h2 class="tab" data-l10n-id="label-interventions"></h2>
<col/>
<col/>
</table>
<h2 class="tab" data-l10n-id="label-smartblock"></h2>
<table id="smartblock" class="shims">
<col/>
<col/>
<col/>
</table>
</body>
</html>
134 changes: 123 additions & 11 deletions browser/extensions/webcompat/about-compat/aboutCompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const DOMContentLoadedPromise = new Promise(resolve => {
});

Promise.all([
browser.runtime.sendMessage("getOverridesAndInterventions"),
browser.runtime.sendMessage("getAllInterventions"),
DOMContentLoadedPromise,
]).then(([info]) => {
document.body.addEventListener("click", async evt => {
Expand Down Expand Up @@ -76,12 +76,18 @@ Promise.all([
});

function onMessageFromAddon(msg) {
const alsoShowHidden = location.hash === "#all";

if ("interventionsChanged" in msg) {
redrawTable($("#interventions"), msg.interventionsChanged);
redrawTable($("#interventions"), msg.interventionsChanged, alsoShowHidden);
}

if ("overridesChanged" in msg) {
redrawTable($("#overrides"), msg.overridesChanged);
redrawTable($("#overrides"), msg.overridesChanged, alsoShowHidden);
}

if ("shimsChanged" in msg) {
updateShimTables(msg.shimsChanged, alsoShowHidden);
}

const id = msg.toggling || msg.toggled;
Expand All @@ -101,13 +107,120 @@ function redraw() {
if (!availablePatches) {
return;
}
const { overrides, interventions } = availablePatches;
const showHidden = location.hash === "#all";
redrawTable($("#overrides"), overrides, showHidden);
redrawTable($("#interventions"), interventions, showHidden);
const { overrides, interventions, shims } = availablePatches;
const alsoShowHidden = location.hash === "#all";
redrawTable($("#overrides"), overrides, alsoShowHidden);
redrawTable($("#interventions"), interventions, alsoShowHidden);
updateShimTables(shims, alsoShowHidden);
}

function clearTableAndAddMessage(table, msgId) {
table.querySelectorAll("tr").forEach(tr => {
tr.remove();
});

const tr = document.createElement("tr");
tr.className = "message";
tr.id = msgId;

const td = document.createElement("td");
td.setAttribute("colspan", "3");
document.l10n.setAttributes(td, msgId);
tr.appendChild(td);

table.appendChild(tr);
}

function hideMessagesOnTable(table) {
table.querySelectorAll("tr.message").forEach(tr => {
tr.remove();
});
}

function updateShimTables(shimsChanged, alsoShowHidden) {
const tables = document.querySelectorAll("table.shims");
if (!tables.length) {
return;
}

for (const { bug, disabledReason, hidden, id, name, type } of shimsChanged) {
// if any shim is disabled by global pref, all of them are. just show the
// "disabled in about:config" message on each shim table in that case.
if (disabledReason === "globalPref") {
for (const table of tables) {
clearTableAndAddMessage(table, "text-disabled-in-about-config");
}
return;
}

// otherwise, find which table the shim belongs in. if there is none,
// ignore the shim (we're not showing it on the UI for whatever reason).
const table = document.querySelector(`table.shims#${type}`);
if (!table) {
continue;
}

// similarly, skip shims hidden from the UI (only for testing, etc).
if (!alsoShowHidden && hidden) {
continue;
}

// also, hide the shim if it is disabled because it is not meant for this
// platform, release (etc) rather than being disabled by pref/about:compat
const notApplicable =
disabledReason &&
disabledReason !== "pref" &&
disabledReason !== "session";
if (!alsoShowHidden && notApplicable) {
continue;
}

// create an updated table-row for the shim
const tr = document.createElement("tr");
tr.setAttribute("data-id", id);

let td = document.createElement("td");
td.innerText = name;
tr.appendChild(td);

td = document.createElement("td");
const a = document.createElement("a");
a.href = `https://bugzilla.mozilla.org/show_bug.cgi?id=${bug}`;
document.l10n.setAttributes(a, "label-more-information", { bug });
a.target = "_blank";
td.appendChild(a);
tr.appendChild(td);

td = document.createElement("td");
tr.appendChild(td);
const button = document.createElement("button");
document.l10n.setAttributes(
button,
disabledReason ? "label-enable" : "label-disable"
);
td.appendChild(button);

// is it already in the table?
const row = table.querySelector(`tr[data-id="${id}"]`);
if (row) {
row.replaceWith(tr);
} else {
table.appendChild(tr);
}
}

for (const table of tables) {
if (!table.querySelector("tr:not(.message)")) {
// no shims? then add a message that none are available for this platform/config
clearTableAndAddMessage(table, `text-no-${table.id}`);
} else {
// otherwise hide any such message, since we have shims on the list
hideMessagesOnTable(table);
}
}
}

function redrawTable(table, data, showHidden = false) {
function redrawTable(table, data, alsoShowHidden) {
const df = document.createDocumentFragment();
table.querySelectorAll("tr").forEach(tr => {
tr.remove();
Expand All @@ -117,8 +230,7 @@ function redrawTable(table, data, showHidden = false) {
if (data === false) {
noEntriesMessage = "text-disabled-in-about-config";
} else if (data.length === 0) {
noEntriesMessage =
table.id === "overrides" ? "text-no-overrides" : "text-no-interventions";
noEntriesMessage = `text-no-${table.id}`;
}

if (noEntriesMessage) {
Expand All @@ -135,7 +247,7 @@ function redrawTable(table, data, showHidden = false) {
}

for (const row of data) {
if (row.hidden && !showHidden) {
if (row.hidden && !alsoShowHidden) {
continue;
}

Expand Down
11 changes: 9 additions & 2 deletions browser/extensions/webcompat/data/shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const AVAILABLE_SHIMS = [
{
hiddenInAboutCompat: true,
id: "LiveTestShim",
platform: "all",
name: "Live test shim",
Expand All @@ -17,6 +18,7 @@ const AVAILABLE_SHIMS = [
needsShimHelpers: ["getOptions", "optIn"],
},
{
hiddenInAboutCompat: true,
id: "MochitestShim",
platform: "all",
branch: ["all:ignoredOtherPlatform"],
Expand All @@ -36,6 +38,7 @@ const AVAILABLE_SHIMS = [
unblocksOnOptIn: ["*://trackertest.org/*"],
},
{
hiddenInAboutCompat: true,
disabled: true,
id: "MochitestShim2",
platform: "all",
Expand All @@ -55,6 +58,7 @@ const AVAILABLE_SHIMS = [
unblocksOnOptIn: ["*://trackertest.org/*"],
},
{
hiddenInAboutCompat: true,
id: "MochitestShim3",
platform: "all",
name: "Test shim for Mochitests (host)",
Expand All @@ -66,6 +70,7 @@ const AVAILABLE_SHIMS = [
],
},
{
hiddenInAboutCompat: true,
id: "MochitestShim4",
platform: "all",
name: "Test shim for Mochitests (notHost)",
Expand All @@ -77,6 +82,7 @@ const AVAILABLE_SHIMS = [
],
},
{
hiddenInAboutCompat: true,
id: "MochitestShim5",
platform: "all",
name: "Test shim for Mochitests (branch)",
Expand All @@ -88,6 +94,7 @@ const AVAILABLE_SHIMS = [
],
},
{
hiddenInAboutCompat: true,
id: "MochitestShim6",
platform: "never matches",
name: "Test shim for Mochitests (platform)",
Expand Down Expand Up @@ -313,7 +320,7 @@ const AVAILABLE_SHIMS = [
{
id: "GoogleAnalyticsLegacy",
platform: "all",
name: "Legacy Google Analytics",
name: "Google Analytics (legacy version)",
bug: "1487072",
file: "google-analytics-legacy.js",
matches: ["*://ssl.google-analytics.com/ga.js"],
Expand Down Expand Up @@ -373,7 +380,7 @@ const AVAILABLE_SHIMS = [
{
id: "GoogleTrends",
platform: "all",
name: "GoogleTrends",
name: "Google Trends",
bug: "1624914",
custom: "google-trends-dfpi-fix",
onlyIfDFPIActive: true,
Expand Down
35 changes: 24 additions & 11 deletions browser/extensions/webcompat/lib/about_compat_broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ class AboutCompatBroker {
constructor(bindings) {
this._injections = bindings.injections;
this._uaOverrides = bindings.uaOverrides;
this._shims = bindings.shims;

if (!this._injections && !this._uaOverrides) {
throw new Error(
"No injections or UA overrides; about:compat broker is not needed"
);
if (!this._injections && !this._uaOverrides && !this._shims) {
throw new Error("No interventions; about:compat broker is not needed");
}

this.portsToAboutCompatTabs = this.buildPorts();
this._injections?.bindAboutCompatBroker(this);
this._uaOverrides?.bindAboutCompatBroker(this);
this._shims?.bindAboutCompatBroker(this);
}

buildPorts() {
Expand Down Expand Up @@ -50,10 +50,11 @@ class AboutCompatBroker {
});
}

getOverrideOrInterventionById(id) {
getInterventionById(id) {
for (const [type, things] of Object.entries({
overrides: this._uaOverrides?.getAvailableOverrides() || [],
interventions: this._injections?.getAvailableInjections() || [],
shims: this._shims?.getAvailableShims() || [],
})) {
for (const what of things) {
if (what.id === id) {
Expand All @@ -69,41 +70,52 @@ class AboutCompatBroker {
switch (msg.command || msg) {
case "toggle": {
const id = msg.id;
const { type, what } = this.getOverrideOrInterventionById(id);
const { type, what } = this.getInterventionById(id);
if (!what) {
return Promise.reject(
`No such override or intervention to toggle: ${id}`
);
}
const active = type === "shims" ? !what.disabledReason : what.active;
this.portsToAboutCompatTabs
.broadcast({ toggling: id, active: what.active })
.broadcast({ toggling: id, active })
.then(async () => {
switch (type) {
case "interventions": {
if (what.active) {
if (active) {
await this._injections?.disableInjection(what);
} else {
await this._injections?.enableInjection(what);
}
break;
}
case "overrides": {
if (what.active) {
if (active) {
await this._uaOverrides?.disableOverride(what);
} else {
await this._uaOverrides?.enableOverride(what);
}
break;
}
case "shims": {
if (active) {
await this._shims?.disableShimForSession(id);
} else {
await this._shims?.enableShimForSession(id);
}
// no need to broadcast the "toggled" signal for shims, as
// they send a shimsUpdated message themselves instead
return;
}
}
this.portsToAboutCompatTabs.broadcast({
toggled: id,
active: what.active,
active: !active,
});
});
break;
}
case "getOverridesAndInterventions": {
case "getAllInterventions": {
return Promise.resolve({
overrides:
(this._uaOverrides?.isEnabled() &&
Expand All @@ -117,6 +129,7 @@ class AboutCompatBroker {
this._injections?.getAvailableInjections()
)) ||
false,
shims: this._shims?.getAvailableShims() || false,
});
}
}
Expand Down
Loading

0 comments on commit 08e5aab

Please sign in to comment.