Bug 1386807 - Part 2: Add embedded webextension to enable CSS and JS overrides r=kmag,mconley

MozReview-Commit-ID: JdSUZYHkuiV

--HG--
extra : rebase_source : 66d5d0c6c7b386ca70b6abfeb50090d8cf00f7b6
This commit is contained in:
Dennis Schubert 2018-02-09 18:52:29 +01:00
Родитель bb40cfc765
Коммит 14e4acd6cb
8 изменённых файлов: 153 добавлений и 45 удалений

45
browser/extensions/webcompat/bootstrap.js поставляемый
Просмотреть файл

@ -6,7 +6,14 @@ ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
const PREF_BRANCH = "extensions.webcompat.";
const PREF_DEFAULTS = {perform_ua_overrides: true};
const PREF_DEFAULTS = {
perform_injections: true,
perform_ua_overrides: true
};
const INJECTIONS_ENABLE_PREF_NAME = "extensions.webcompat.perform_injections";
const BROWSER_STARTUP_FINISHED_TOPIC = "browser-delayed-startup-finished";
const UA_OVERRIDES_INIT_TOPIC = "useragentoverrides-initialized";
const UA_ENABLE_PREF_NAME = "extensions.webcompat.perform_ua_overrides";
@ -15,6 +22,15 @@ ChromeUtils.defineModuleGetter(this, "UAOverrider", "chrome://webcompat/content/
ChromeUtils.defineModuleGetter(this, "UAOverrides", "chrome://webcompat/content/data/ua_overrides.jsm");
let overrider;
let webextensionPort;
function InjectionsEnablePrefObserver() {
let isEnabled = Services.prefs.getBoolPref(INJECTIONS_ENABLE_PREF_NAME);
webextensionPort.postMessage({
type: "injection-pref-changed",
prefState: isEnabled
});
}
function UAEnablePrefObserver() {
let isEnabled = Services.prefs.getBoolPref(UA_ENABLE_PREF_NAME);
@ -57,6 +73,9 @@ this.startup = function({webExtension}) {
// Intentionally reset the preference on every browser restart to avoid site
// breakage by accidentally toggled preferences or by leaving it off after
// debugging a site.
Services.prefs.clearUserPref(INJECTIONS_ENABLE_PREF_NAME);
Services.prefs.addObserver(INJECTIONS_ENABLE_PREF_NAME, InjectionsEnablePrefObserver);
Services.prefs.clearUserPref(UA_ENABLE_PREF_NAME);
Services.prefs.addObserver(UA_ENABLE_PREF_NAME, UAEnablePrefObserver);
@ -64,7 +83,7 @@ this.startup = function({webExtension}) {
// initialize our overrider there. This is done to avoid slowing down the
// apparent startup proces, since we avoid loading anything before the first
// window is visible to the user. See bug 1371442 for details.
let startupWatcher = {
let uaStartupObserver = {
observe(aSubject, aTopic, aData) {
if (aTopic !== UA_OVERRIDES_INIT_TOPIC) {
return;
@ -75,9 +94,29 @@ this.startup = function({webExtension}) {
overrider.init();
}
};
Services.obs.addObserver(startupWatcher, UA_OVERRIDES_INIT_TOPIC);
Services.obs.addObserver(uaStartupObserver, UA_OVERRIDES_INIT_TOPIC);
// Observe browser-delayed-startup-finished and only initialize our embedded
// WebExtension after that. Otherwise, we'd try to initialize as soon as the
// browser starts up, which adds a heavy startup penalty.
let appStartupObserver = {
observe(aSubject, aTopic, aData) {
webExtension.startup().then((api) => {
api.browser.runtime.onConnect.addListener((port) => {
webextensionPort = port;
});
return Promise.resolve();
}).catch((ex) => {
console.error(ex);
});
Services.obs.removeObserver(this, BROWSER_STARTUP_FINISHED_TOPIC);
}
};
Services.obs.addObserver(appStartupObserver, BROWSER_STARTUP_FINISHED_TOPIC);
};
this.shutdown = function() {
Services.prefs.removeObserver(INJECTIONS_ENABLE_PREF_NAME, InjectionsEnablePrefObserver);
Services.prefs.removeObserver(UA_ENABLE_PREF_NAME, UAEnablePrefObserver);
};

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

@ -3,48 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* This is an array of objects that specify user agent overrides. Each object
* can have three attributes:
* For detailed information on our policies, and a documention on this format
* and its possibilites, please check the Mozilla-Wiki at
*
* * `baseDomain`, required: The base domain that further checks and user
* agents override are applied to. This does not include subdomains.
* * `applications`: Array of applications this override is valid in.
* Defaults to ["firefox"], can be one or more of:
* * `firefox`: Firefox Desktop (regardless of the operating system)
* * `fennec`: Firefox for Android
* * `uriMatcher`: Function that gets the requested URI passed in the first
* argument and needs to return boolean whether or not the override should
* be applied. If not provided, the user agent override will be applied
* every time.
* * `uaTransformer`, required: Function that gets the original Firefox user
* agent passed as its first argument and needs to return a string that
* will be used as the the user agent for this URI.
*
* Examples:
*
* Gets applied for all requests to mozilla.org and subdomains made on
* Firefox Desktop:
*
* ```
* {
* baseDomain: "mozilla.org",
* uriMatcher: (uri) => uri.includes("/app/"),
* uaTransformer: (originalUA) => `Ohai Mozilla, it's me, ${originalUA}`
* }
* ```
*
* Applies to *.example.com/app/* on Firefox for Android:
*
* ```
* {
* baseDomain: "example.com",
* applications: ["fennec"],
* uriMatcher: (uri) => uri.includes("/app/"),
* uaTransformer: (originalUA) => originalUA.replace("Firefox", "Otherfox")
* }
* ```
* https://wiki.mozilla.org/Compatibility/Go_Faster_Addon/Override_Policies_and_Workflows#User_Agent_overrides
*/
const UAOverrides = [
/*
@ -57,7 +20,7 @@ const UAOverrides = [
{
baseDomain: "schub.io",
applications: ["firefox", "fennec"],
uriMatcher: (uri) => uri.includes("webcompat-ua-dummy.schub.io"),
uriMatcher: (uri) => uri.includes("webcompat-addon-testcases.schub.io"),
uaTransformer: (originalUA) => {
let prefix = originalUA.substr(0, originalUA.indexOf(")") + 1);
return `${prefix} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36`;

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

@ -10,10 +10,11 @@
<Description about="urn:mozilla:install-manifest">
<em:id>webcompat@mozilla.org</em:id>
<em:version>1.1</em:version>
<em:version>2.0</em:version>
<em:type>2</em:type>
<em:bootstrap>true</em:bootstrap>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
<em:hasEmbeddedWebExtension>true</em:hasEmbeddedWebExtension>
<!-- Firefox Desktop -->
<em:targetApplication>

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

@ -11,6 +11,19 @@ FINAL_TARGET_FILES.features['webcompat@mozilla.org'] += [
'bootstrap.js'
]
FINAL_TARGET_FILES.features['webcompat@mozilla.org']['webextension'] += [
'webextension/background.js',
'webextension/manifest.json'
]
FINAL_TARGET_FILES.features['webcompat@mozilla.org']['webextension']['injections']['css'] += [
'webextension/injections/css/bug0000000-dummy-css-injection.css'
]
FINAL_TARGET_FILES.features['webcompat@mozilla.org']['webextension']['injections']['js'] += [
'webextension/injections/js/bug0000000-dummy-js-injection.js'
]
FINAL_TARGET_PP_FILES.features['webcompat@mozilla.org'] += [
'install.rdf.in'
]

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

@ -0,0 +1,55 @@
/**
* For detailed information on our policies, and a documention on this format
* and its possibilites, please check the Mozilla-Wiki at
*
* https://wiki.mozilla.org/Compatibility/Go_Faster_Addon/Override_Policies_and_Workflows#User_Agent_overrides
*/
const contentScripts = [
{
matches: ["*://webcompat-addon-testcases.schub.io/*"],
css: [{file: "injections/css/bug0000000-dummy-css-injection.css"}],
js: [{file: "injections/js/bug0000000-dummy-js-injection.js"}],
runAt: "document_start"
}
];
/* globals browser */
let port = browser.runtime.connect();
let registeredContentScripts = [];
function registerContentScripts() {
contentScripts.forEach(async (contentScript) => {
try {
let handle = await browser.contentScripts.register(contentScript);
registeredContentScripts.push(handle);
} catch (ex) {
console.error("Registering WebCompat GoFaster content scripts failed: ", ex);
}
});
}
function unregisterContentScripts() {
registeredContentScripts.forEach((contentScript) => {
contentScript.unregister();
});
}
port.onMessage.addListener((message) => {
switch (message.type) {
case "injection-pref-changed":
if (message.prefState) {
registerContentScripts();
} else {
unregisterContentScripts();
}
break;
}
});
/**
* Note that we reset all preferences on extension startup, so the injections will
* never be disabled when this loads up. Because of that, we can simply register
* right away.
*/
registerContentScripts();

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

@ -0,0 +1,3 @@
#css-injection.red {
background-color: #0f0;
}

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

@ -0,0 +1,11 @@
"use strict";
/* globals exportFunction */
Object.defineProperty(window.wrappedJSObject, "isTestFeatureSupported", {
get: exportFunction(function() {
return true;
}, window),
set: exportFunction(function() {}, window)
});

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

@ -0,0 +1,23 @@
{
"manifest_version": 2,
"name": "Web Compat",
"description": "Urgent post-release fixes for web compatibility.",
"version": "2.0",
"applications": {
"gecko": {
"id": "webcompat@mozilla.org",
"strict_min_version": "59.0b5"
}
},
"permissions": [
"<all_urls>"
],
"background": {
"scripts": [
"background.js"
]
}
}