Bug 1509047 - Part 2: Add an API for measuring the number of unique origins visited in the past 24 hours r=johannh

Depends on D12862

Differential Revision: https://phabricator.services.mozilla.com/D12863

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Ehsan Akhgari 2018-11-28 23:01:36 +00:00
Родитель 7c2539e61d
Коммит 2f29272172
4 изменённых файлов: 93 добавлений и 2 удалений

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

@ -1795,3 +1795,5 @@ pref("prio.enabled", true);
pref("browser.discovery.enabled", false);
pref("browser.discovery.containers.enabled", true);
pref("browser.discovery.sites", "addons.mozilla.org");
pref("browser.engagement.recent_visited_origins.expiry", 86400); // 24 * 60 * 60 (24 hours in seconds)

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

@ -7,6 +7,7 @@
var EXPORTED_SYMBOLS = [
"BrowserUsageTelemetry",
"URICountListener",
"URLBAR_SELECTED_RESULT_TYPES",
"URLBAR_SELECTED_RESULT_METHODS",
"MINIMUM_TAB_COUNT_INTERVAL_MS",
@ -18,8 +19,14 @@ XPCOMUtils.defineLazyModuleGetters(this, {
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
SearchTelemetry: "resource:///modules/SearchTelemetry.jsm",
Services: "resource://gre/modules/Services.jsm",
setTimeout: "resource://gre/modules/Timer.jsm",
});
// This pref is in seconds!
XPCOMUtils.defineLazyPreferenceGetter(this,
"gRecentVisitedOriginsExpiry",
"browser.engagement.recent_visited_origins.expiry");
// The upper bound for the count of the visited unique domain names.
const MAX_UNIQUE_VISITED_DOMAINS = 100;
@ -128,6 +135,8 @@ function shouldRecordSearchCount(tabbrowser) {
let URICountListener = {
// A set containing the visited domains, see bug 1271310.
_domainSet: new Set(),
// A set containing the visited origins during the last 24 hours (similar to domains, but not quite the same)
_origin24hrSet: new Set(),
// A map to keep track of the URIs loaded from the restored tabs.
_restoredURIsMap: new WeakMap(),
@ -230,13 +239,26 @@ let URICountListener = {
// Unique domains should be aggregated by (eTLD + 1): x.test.com and y.test.com
// are counted once as test.com.
let baseDomain;
try {
// Even if only considering http(s) URIs, |getBaseDomain| could still throw
// due to the URI containing invalid characters or the domain actually being
// an ipv4 or ipv6 address.
this._domainSet.add(Services.eTLD.getBaseDomain(uri));
baseDomain = Services.eTLD.getBaseDomain(uri);
this._domainSet.add(baseDomain);
} catch (e) {
return;
baseDomain = uri.host;
}
// Record the origin, but with the base domain (eTLD + 1).
let baseDomainURI = uri.mutate()
.setHost(baseDomain)
.finalize();
this._origin24hrSet.add(baseDomainURI.prePath);
if (gRecentVisitedOriginsExpiry) {
setTimeout(() => {
this._origin24hrSet.delete(baseDomainURI.prePath);
}, gRecentVisitedOriginsExpiry * 1000);
}
Services.telemetry.scalarSet(UNIQUE_DOMAINS_COUNT_SCALAR_NAME, this._domainSet.size);
@ -249,6 +271,21 @@ let URICountListener = {
this._domainSet.clear();
},
/**
* Returns the number of unique origins visited in this session during the
* last 24 hours.
*/
get uniqueOriginsVisitedInPast24Hours() {
return this._origin24hrSet.size;
},
/**
* Resets the number of unique origins visited in this session.
*/
resetUniqueOriginsVisitedInPast24Hours() {
this._origin24hrSet.clear();
},
QueryInterface: ChromeUtils.generateQI([Ci.nsIWebProgressListener,
Ci.nsISupportsWeakReference]),
};

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

@ -39,6 +39,7 @@ run-if = crashreporter
[browser_UsageTelemetry_domains.js]
[browser_UsageTelemetry_private_and_restore.js]
skip-if = verify && debug
[browser_UsageTelemetry_uniqueOriginsVisitedInPast24Hours.js]
[browser_UsageTelemetry_urlbar.js]
support-files =
usageTelemetrySearchSuggestions.sjs

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

@ -0,0 +1,51 @@
/* eslint-disable mozilla/no-arbitrary-setTimeout */
/* 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";
ChromeUtils.defineModuleGetter(this, "URICountListener",
"resource:///modules/BrowserUsageTelemetry.jsm");
add_task(async function test_uniqueOriginsVisitedInPast24Hours() {
URICountListener.resetUniqueOriginsVisitedInPast24Hours();
let startingCount = URICountListener.uniqueOriginsVisitedInPast24Hours;
is(startingCount, 0, "We should have no origins recorded in the history right after resetting");
// Add a new window and then some tabs in it.
let win = await BrowserTestUtils.openNewBrowserWindow();
await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "http://example.com");
await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "http://test1.example.com");
is(URICountListener.uniqueOriginsVisitedInPast24Hours, startingCount + 1,
"test1.example.com should only count as a unique visit if example.com wasn't visited before");
// http://www.exämple.test
await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "http://xn--exmple-cua.test");
is(URICountListener.uniqueOriginsVisitedInPast24Hours, startingCount + 2,
"www.exämple.test should count as a unique visit");
// Set the expiry time to 1 second
SpecialPowers.setIntPref("browser.engagement.recent_visited_origins.expiry", 1);
await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "http://127.0.0.1");
is(URICountListener.uniqueOriginsVisitedInPast24Hours, startingCount + 3,
"127.0.0.1 should count as a unique visit");
let countBefore = URICountListener.uniqueOriginsVisitedInPast24Hours;
await new Promise(resolve => {
setTimeout(_ => {
let countAfter = URICountListener.uniqueOriginsVisitedInPast24Hours;
is(countAfter, countBefore - 1,
"The expiry should work correctly");
resolve();
}, 1100);
});
BrowserTestUtils.removeTab(win.gBrowser.selectedTab);
BrowserTestUtils.removeTab(win.gBrowser.selectedTab);
await BrowserTestUtils.closeWindow(win);
});