Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Bootstrapless addon #3456

Merged
merged 15 commits into from Apr 2, 2018

Conversation

meandavejustice
Copy link
Contributor

@meandavejustice meandavejustice commented Mar 30, 2018

Rewrite of addon as webextension

Functionality:

  • add button to toolbar which links to testpilot based on the currentEnv (local/prod etc...)
  • toolbar link passes correct utm params
  • show notification when new experiments or Major news updates are added to the site.
  • submit a daily ping with a list of currently installed experiments

Structure:

  • got rid of all preprocessing of files
  • everything is in addon/background.js or options/options.js
  • npm scripts in package.json

Issues:

dave justice added 4 commits March 30, 2018 12:16
- options page is not showing up yet, I have a hunch that this may
be because of the addon is still a bootstrap hyrid, something seems
to be blocking the loading of the options.html page. Going to keep
investigating, but wanted to commit to get these changes pushed to
remote.
});
}

async function setup() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the starting point for the webextension.

Here we:

  • fetch or generate a clientUUID(used to daily metric ping)
  • setupEnvironment(setup listener for changing of addon environement(local/dev/stage/prod)
  • setup our browser action button
  • populate resources (experiments and news feed)
  • detect our installed test pilot addons and add listeners to keep that updated on install/uninstall events.
  • setup our daily ping interval

});
}

function submitPing(object, event, addons) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Telemetry and ping centre are no longer called in the new webextension. We're only using google analytics now.

@@ -0,0 +1,206 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where everything happens for the webextension. Most of what is in there was pulled from src/webextension/lib/{browserAction, storage} and src/lib/pings.

var manifestJSON = JSON.stringify(manifest, null, ' ');
fs.writeFileSync(manifestFilename, manifestJSON);

var installFilename = __dirname + '/../build/install.rdf';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is virtually the same except for generation of install.rdf was removed.

@@ -16,6 +17,7 @@
},
"default_locale": "en_US",
"permissions": [
"management",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used for tracking addon installs/uninstalls

@@ -0,0 +1,26 @@
/* global browser */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environments came from src/webextension/lib/environments.js. They are now part of the options page as we no longer have access to about:config prefs.

@@ -1,108 +0,0 @@
/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer use frame scripts for detecting test pilot addon on certain mozilla properties

@@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal

@@ -1,66 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now using the management api to fetch addons and determine which txp addons are installed at the time of the daily ping.

@@ -1,30 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal, sessionstore-windows-restored was a hack to speed up startup for hybrid bootstrap embedded webextensions.

@@ -1,176 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal, no longer provide metrics channel for experiments

@@ -1,87 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal, this was a bridge for talking to the embedded webextension

@@ -1,63 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -1,100 +0,0 @@
// Shared metrics ping formatting & logic for bootstrap and webextension, with
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ga ping snippet pulled for background.js. Everything else removed.

@@ -1,53 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bootstrap-removal, no longer using prefs

@@ -1,126 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal, no more telemetry

@@ -1,65 +0,0 @@
// Shared list of topics for PubSub usage in bootstrap and webextension
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal, part of pubsub bridge to webextension. no longer needed

@@ -1,17 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log is commented out for development use in background.js

@@ -1,83 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal, embedded webextension management and bridge

@@ -1,102 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost this whole file is now in background.js

@@ -1,36 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal

@@ -1,28 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal

@@ -1,148 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal, daily-ping was written fresh using browser.alarms in background.js

@@ -1,23 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now in background.js

@@ -1,14 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bootstrap-removal

@@ -1 +0,0 @@
// Global setup for all tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty file

// HACK: Only perform bundling if the second half of build has been
// performed, but we still want to rebuild & post the first half on file
// watching.
const buildPackagePath = './build/package.json';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved into npm scripts

@meandavejustice
Copy link
Contributor Author

meandavejustice commented Mar 30, 2018

I seem to be having an issue with the update-version script and web-ext.

It appears that npm is cacheing $npm_package_version which is breaking the build.

Building web extension from /home/circleci/testpilot/addon
Your web extension is ready: /home/circleci/testpilot/addon/web-ext-artifacts/test_pilot-3.0.0-dev-6951b1b.zip
mv: cannot stat ‘web-ext-artifacts/test_pilot-3.0.0-dev-1207ff1.zip’: No such file or directory

I'm going to remove update-version from the prepackage script in order to get this working. Filed an issue to check into it after I can talk to the author(les) #3459

browser.management.onEnabled.addListener(setInstalledTxpAddons);
browser.management.onDisabled.addListener(setInstalledTxpAddons);
browser.management.onInstalled.addListener(setInstalledTxpAddons);
browser.management.onUninstalled.addListener(setInstalledTxpAddons);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we only use installedTxpAddons in the daily ping. It might be more polite to just run setInstalledTxpAddons then. Not a blocker. Your call.

browser.storage.onChanged.addListener((changes) => {
Object.keys(changes).forEach((k) => {
if (k === "environment") {
currentEnvironment = changes[k];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be changes[k].newValue

const delayInMinutes = 5;
const periodInMinutes = 60 * 24; // daily

browser.alarms.create("daily-ping", {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will fire after 5 minutes every time I restart the browser, which might be ok? (not my call) If we really only want at most one ping per day we should save a timestamp to storage so we can check it on startup.

log("fetchResources");
return Promise.all(
Object.keys(resources).map(path =>
fetch(`${currentEnvironment.baseUrl}/api/${path}.json`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this indenting is hard to read

text: (newExperiments.length || newsUpdates.length) > 0 ? "!" : ""
});

browser.browserAction.setBadgeText({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk why this is here twice. bad merge?

<option value="https://testpilot.dev.mozaws.net">Development</option>
<option value="https://testpilot-l10n.dev.mozaws.net">L10n</option>
<option value="https://testpilot.stage.mozaws.net">Stage</option>
<option value="https://testpilot.firefox.com">Production</option>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since production is the default in background.js I think it should be the one initialized to selected here

const twoWeeksAgo = Date.now() - TWO_WEEKS;
const newsUpdates = (news_updates || []).filter((u) => u.major)
.filter((u) => new Date(u.published).getTime() >= twoWeeksAgo)
.filter((u) => new Date(u.published).getTime() >= new Date(lastViewed))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lastViewed shouldn't need a Date wrapper

async function setup() {
const data = await storage.get("clientUUID");
if (!data.clientUUID) {
await storage.set({ clientUUID: clientUUID = uuidv4() });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your let clientUUID above doesn't get initialized once it's already stored

@@ -16,6 +17,7 @@
},
"default_locale": "en_US",
"permissions": [
"management",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs "alarms" permission

}

const storage = browser.storage.local;
const RESOURCE_UPDATE_INTERVAL = 10000; // 4 hours

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10000 isn't actually 4 hours ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that was a testing mistake. forgot to change, thanks for catching that!

dave justice added 2 commits April 2, 2018 11:33
- remove unneccessary date wrapper
- fix daily ping to test date before sending request
- indent cleanup
- change default in options.html to local
- fix setting of environment from onchanged listener
@ghost ghost mentioned this pull request Apr 2, 2018
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
}

function setInstalledTxpAddons() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it might be better to call this getInstalledTxpAddons now

const ONE_DAY = 60 * 60 * 1000 * 24; /* ms */
const { lastPing } = await storage.get("lastPing");
if (initial || ((new Date) - lastPing) > ONE_DAY) {
setInstalledTxpAddons().then((installedTxpAddons) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: await instead of then

periodInMinutes
});

browser.alarms.onAlarm.addListener((alarmInfo) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs async on the callback

setupEnvironment();
setupBrowserAction();
await fetchResources();
setDailyPing();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should have await for consistency

const { clientUUID } = await storage.get("clientUUID");
if (!clientUUID) {
await storage.set({ clientUUID: uuidv4() });
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

putting all the clientUUID code in setDailyPing is more cohesive

if (initial || ((new Date) - lastPing) > ONE_DAY) {
setInstalledTxpAddons().then((installedTxpAddons) => {
const { clientUUID } = await storage.get({ clientUUID: uuidv4() });
submitPing(alarmInfo.name, "installed-addons", installedTxpAddons, clientUUID);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastPing needs to be set after one is sent

if (!lastPing) {
initial = true;
await storage.set({ lastPing: Date.now()});
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove initial and skip this if we default lastPing to yesterday

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woah clever 👍


async function sendPingOnAlarm(alarmInfo) {
if (alarmInfo.name === "daily-ping") {
const { lastPing } = await storage.get("lastPing");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more along the lines of storage.get({ lastPing: Date.now() - ONE_DAY })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this would ever return successfully 🤔

}
}
});
browser.alarms.onAlarm.addListener(sendPingOnAlarm);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@codecov-io
Copy link

codecov-io commented Apr 2, 2018

Codecov Report

Merging #3456 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #3456   +/-   ##
=======================================
  Coverage   83.48%   83.48%           
=======================================
  Files         113      113           
  Lines        3172     3172           
=======================================
  Hits         2648     2648           
  Misses        524      524

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e3da7f5...0f7af11. Read the comment docs.

@meandavejustice meandavejustice merged commit 6bf5cda into mozilla:master Apr 2, 2018
@meandavejustice meandavejustice deleted the bootstrapless-addon branch April 2, 2018 18:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants