From 665c832058bff885465036788d5fd18932880a9f Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Thu, 19 Nov 2020 15:23:47 +0100 Subject: [PATCH] Expose metrics to Ghostery extension via extension messaging. --- src/api.js | 14 ++++++++ src/experiment_apis/ghostery_parent.js | 5 +++ src/experiment_apis/ghostery_schema.json | 10 ++++++ src/manifest.json | 4 ++- src/metrics.js | 45 ++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/api.js create mode 100644 src/metrics.js diff --git a/src/api.js b/src/api.js new file mode 100644 index 0000000..ec7d3cd --- /dev/null +++ b/src/api.js @@ -0,0 +1,14 @@ +/** + * API for external communication from Ghostery browser extension + */ + +const ALLOWED_ADDON_IDS = new Set(["firefox@ghostery.com"]); + +browser.runtime.onMessageExternal.addListener(async (message, sender) => { + if (!ALLOWED_ADDON_IDS.has(sender.id)) { + return false; + } + if (message === "getMetrics") { + return getMetrics(); + } +}); diff --git a/src/experiment_apis/ghostery_parent.js b/src/experiment_apis/ghostery_parent.js index 43b9672..38ff6b6 100644 --- a/src/experiment_apis/ghostery_parent.js +++ b/src/experiment_apis/ghostery_parent.js @@ -34,6 +34,11 @@ global.ghostery = class extends ExtensionCommon.ExtensionAPI { const window = getWindow(windowId); window.gURLBar.value = query || ''; window.gURLBar.focus(); + }, + isDefaultBrowser() { + const shell = Components.classes["@mozilla.org/browser/shell-service;1"] + .getService(Components.interfaces.nsIShellService) + return shell.isDefaultBrowser(); } } } diff --git a/src/experiment_apis/ghostery_schema.json b/src/experiment_apis/ghostery_schema.json index de56e82..3c289b5 100644 --- a/src/experiment_apis/ghostery_schema.json +++ b/src/experiment_apis/ghostery_schema.json @@ -42,6 +42,16 @@ "name": "query", "optional": true }] + }, + { + "name": "isDefaultBrowser", + "type": "function", + "async": true, + "description": "Check if this is the OS's default browser", + "parameters": [], + "returns": { + "description": "true if this is the default browser" + } } ], "events": [ diff --git a/src/manifest.json b/src/manifest.json index 35ef0bf..0ff2fba 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -11,7 +11,9 @@ "token-pool.js", "sjcl.js", "background.js", - "choice-screen.js" + "choice-screen.js", + "metrics.js", + "api.js" ] }, "content_scripts": [{ diff --git a/src/metrics.js b/src/metrics.js new file mode 100644 index 0000000..02273c8 --- /dev/null +++ b/src/metrics.js @@ -0,0 +1,45 @@ +// limit engines we can send to the specific set we're interested in from search choice screen +const searchEngineMap = { + "Ghostery Search": "ghostery", + Google: "google", + Bing: "bing", + StartPage: "sp", + Yahoo: "yahoo", +}; + +async function getMetrics() { + return { + ds: await getDefaultSearchEngine(), + db: await getIsDefaultBrowser(), + }; +} + +/** + * Get the name of the current default search engine (if it is in the searchEngineMap list). + * Returns "other" if the engine is not in the shortlist, and empty string if the search API is not + * available. + */ +async function getDefaultSearchEngine() { + if (browser.search) { + const engines = await browser.search.get(); + const defaultSearchEngine = engines.find((s) => s.isDefault).name; + return searchEngineMap[defaultSearchEngine] || "other"; + } + return ""; +} + +/** + * Get default browser state: + * 0 = unknown - API to check is missing + * 1 = not default browser + * 2 = is default browser + */ +async function getIsDefaultBrowser() { + if (browser.ghostery) { + if (await browser.ghostery.isDefaultBrowser()) { + return 2; + } + return 1; + } + return 0; +}