Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose metrics to Ghostery extension via extension messaging. #30

Merged
merged 1 commit into from Nov 20, 2020
Merged
Changes from all commits
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -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();
}
});
@@ -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();
}
}
}
@@ -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": [
@@ -11,7 +11,9 @@
"token-pool.js",
"sjcl.js",
"background.js",
"choice-screen.js"
"choice-screen.js",
"metrics.js",
"api.js"
]
},
"content_scripts": [{
@@ -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;
}
ProTip! Use n and p to navigate between commits in a pull request.