summaryrefslogtreecommitdiffstats
path: root/browser/components/reportbrokensite/test/browser/send.js
blob: a8599741acf335f1057c268efc1ff9179ce23c9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/* Helper methods for testing sending reports with
 * the Report Broken Site feature.
 */

/* import-globals-from head.js */

"use strict";

const { Troubleshoot } = ChromeUtils.importESModule(
  "resource://gre/modules/Troubleshoot.sys.mjs"
);

function getSysinfoProperty(propertyName, defaultValue) {
  try {
    return Services.sysinfo.getProperty(propertyName);
  } catch (e) {}
  return defaultValue;
}

function securityStringToArray(str) {
  return str ? str.split(";") : null;
}

function getExpectedGraphicsDevices(snapshot) {
  const { graphics } = snapshot;
  return [
    graphics.adapterDeviceID,
    graphics.adapterVendorID,
    graphics.adapterDeviceID2,
    graphics.adapterVendorID2,
  ]
    .filter(i => i)
    .sort();
}

function compareGraphicsDevices(expected, rawActual) {
  const actual = rawActual
    .map(({ deviceID, vendorID }) => [deviceID, vendorID])
    .flat()
    .filter(i => i)
    .sort();
  return areObjectsEqual(actual, expected);
}

function getExpectedGraphicsDrivers(snapshot) {
  const { graphics } = snapshot;
  const expected = [];
  for (let i = 1; i < 3; ++i) {
    const version = graphics[`webgl${i}Version`];
    if (version && version != "-") {
      expected.push(graphics[`webgl${i}Renderer`]);
      expected.push(version);
    }
  }
  return expected.filter(i => i).sort();
}

function compareGraphicsDrivers(expected, rawActual) {
  const actual = rawActual
    .map(({ renderer, version }) => [renderer, version])
    .flat()
    .filter(i => i)
    .sort();
  return areObjectsEqual(actual, expected);
}

function getExpectedGraphicsFeatures(snapshot) {
  const expected = {};
  for (let { name, log, status } of snapshot.graphics.featureLog.features) {
    for (const item of log?.reverse() ?? []) {
      if (item.failureId && item.status == status) {
        status = `${status} (${item.message || item.failureId})`;
      }
    }
    expected[name] = status;
  }
  return expected;
}

async function getExpectedWebCompatInfo(tab, snapshot, fullAppData = false) {
  const gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo);

  const { application, graphics, intl, securitySoftware } = snapshot;

  const { fissionAutoStart, memorySizeBytes, updateChannel, userAgent } =
    application;

  const app = {
    defaultLocales: intl.localeService.available,
    defaultUseragentString: userAgent,
    fissionEnabled: fissionAutoStart,
  };
  if (fullAppData) {
    app.applicationName = application.name;
    app.osArchitecture = getSysinfoProperty("arch", null);
    app.osName = getSysinfoProperty("name", null);
    app.osVersion = getSysinfoProperty("version", null);
    app.updateChannel = updateChannel;
    app.version = application.version;
  }

  const hasTouchScreen = graphics.info.ApzTouchInput == 1;

  const { registeredAntiVirus, registeredAntiSpyware, registeredFirewall } =
    securitySoftware;

  const browserInfo = {
    app,
    graphics: {
      devicesJson(actualStr) {
        const expected = getExpectedGraphicsDevices(snapshot);
        // If undefined is saved to the Glean value here, we'll get the string "undefined" (invalid JSON).
        // We should stop using JSON like this in bug 1875185.
        if (!actualStr || actualStr == "undefined") {
          return !expected.length;
        }
        return compareGraphicsDevices(expected, JSON.parse(actualStr));
      },
      driversJson(actualStr) {
        const expected = getExpectedGraphicsDrivers(snapshot);
        // If undefined is saved to the Glean value here, we'll get the string "undefined" (invalid JSON).
        // We should stop using JSON like this in bug 1875185.
        if (!actualStr || actualStr == "undefined") {
          return !expected.length;
        }
        return compareGraphicsDrivers(expected, JSON.parse(actualStr));
      },
      featuresJson(actualStr) {
        const expected = getExpectedGraphicsFeatures(snapshot);
        // If undefined is saved to the Glean value here, we'll get the string "undefined" (invalid JSON).
        // We should stop using JSON like this in bug 1875185.
        if (!actualStr || actualStr == "undefined") {
          return !expected.length;
        }
        return areObjectsEqual(JSON.parse(actualStr), expected);
      },
      hasTouchScreen,
      monitorsJson(actualStr) {
        // We don't care about monitor data on Android right now.
        if (AppConstants.platform == "android") {
          return actualStr == "undefined";
        }
        return actualStr == JSON.stringify(gfxInfo.getMonitors());
      },
    },
    prefs: {
      cookieBehavior: Services.prefs.getIntPref(
        "network.cookie.cookieBehavior",
        -1
      ),
      forcedAcceleratedLayers: Services.prefs.getBoolPref(
        "layers.acceleration.force-enabled",
        false
      ),
      globalPrivacyControlEnabled: Services.prefs.getBoolPref(
        "privacy.globalprivacycontrol.enabled",
        false
      ),
      installtriggerEnabled: Services.prefs.getBoolPref(
        "extensions.InstallTrigger.enabled",
        false
      ),
      opaqueResponseBlocking: Services.prefs.getBoolPref(
        "browser.opaqueResponseBlocking",
        false
      ),
      resistFingerprintingEnabled: Services.prefs.getBoolPref(
        "privacy.resistFingerprinting",
        false
      ),
      softwareWebrender: Services.prefs.getBoolPref(
        "gfx.webrender.software",
        false
      ),
    },
    security: {
      antispyware: securityStringToArray(registeredAntiSpyware),
      antivirus: securityStringToArray(registeredAntiVirus),
      firewall: securityStringToArray(registeredFirewall),
    },
    system: {
      isTablet: getSysinfoProperty("tablet", false),
      memory: Math.round(memorySizeBytes / 1024 / 1024),
    },
  };

  const tabInfo = await tab.linkedBrowser.ownerGlobal.SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    async function () {
      return {
        devicePixelRatio: `${content.devicePixelRatio}`,
        antitracking: {
          blockList: "basic",
          isPrivateBrowsing: false,
          hasTrackingContentBlocked: false,
          hasMixedActiveContentBlocked: false,
          hasMixedDisplayContentBlocked: false,
        },
        frameworks: {
          fastclick: false,
          marfeel: false,
          mobify: false,
        },
        languages: content.navigator.languages,
        useragentString: content.navigator.userAgent,
      };
    }
  );

  browserInfo.graphics.devicePixelRatio = tabInfo.devicePixelRatio;
  delete tabInfo.devicePixelRatio;

  return { browserInfo, tabInfo };
}

function extractPingData(branch) {
  const data = {};
  for (const [name, value] of Object.entries(branch)) {
    data[name] = value.testGetValue();
  }
  return data;
}

function extractBrokenSiteReportFromGleanPing(Glean) {
  const ping = extractPingData(Glean.brokenSiteReport);
  ping.tabInfo = extractPingData(Glean.brokenSiteReportTabInfo);
  ping.tabInfo.antitracking = extractPingData(
    Glean.brokenSiteReportTabInfoAntitracking
  );
  ping.tabInfo.frameworks = extractPingData(
    Glean.brokenSiteReportTabInfoFrameworks
  );
  ping.browserInfo = {
    app: extractPingData(Glean.brokenSiteReportBrowserInfoApp),
    graphics: extractPingData(Glean.brokenSiteReportBrowserInfoGraphics),
    prefs: extractPingData(Glean.brokenSiteReportBrowserInfoPrefs),
    security: extractPingData(Glean.brokenSiteReportBrowserInfoSecurity),
    system: extractPingData(Glean.brokenSiteReportBrowserInfoSystem),
  };
  return ping;
}

async function testSend(tab, menu, expectedOverrides = {}) {
  const url = expectedOverrides.url ?? menu.win.gBrowser.currentURI.spec;
  const description = expectedOverrides.description ?? "";
  const breakageCategory = expectedOverrides.breakageCategory ?? null;

  let rbs = await menu.openAndPrefillReportBrokenSite(url, description);

  const snapshot = await Troubleshoot.snapshot();
  const expected = await getExpectedWebCompatInfo(tab, snapshot);

  expected.url = url;
  expected.description = description;
  expected.breakageCategory = breakageCategory;

  if (expectedOverrides.antitracking) {
    expected.tabInfo.antitracking = expectedOverrides.antitracking;
  }

  if (expectedOverrides.frameworks) {
    expected.tabInfo.frameworks = expectedOverrides.frameworks;
  }

  if (breakageCategory) {
    rbs.chooseReason(breakageCategory);
  }

  const pingCheck = new Promise(resolve => {
    Services.fog.testResetFOG();
    GleanPings.brokenSiteReport.testBeforeNextSubmit(() => {
      const ping = extractBrokenSiteReportFromGleanPing(Glean);
      ok(areObjectsEqual(ping, expected), "ping matches expectations");
      resolve();
    });
  });

  await rbs.clickSend();
  await pingCheck;
  await rbs.clickOkay();

  // re-opening the panel, the url and description should be reset
  rbs = await menu.openReportBrokenSite();
  rbs.isMainViewResetToCurrentTab();
  rbs.close();
}