Bug 1453691 - create a Fennec system addon to spoof the UA string as Chrome for Google Search and Facebook. r=rhelmer
MozReview-Commit-ID: FFwhvvAqffs
--- a/mobile/android/app.mozbuild
+++ b/mobile/android/app.mozbuild
@@ -1,11 +1,14 @@
# vim: set filetype=python:
# 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/.
include('/toolkit/toolkit.mozbuild')
+if CONFIG['MOZ_EXTENSIONS']:
+ DIRS += ['/extensions']
+
DIRS += [
'/%s' % CONFIG['MOZ_BRANDING_DIRECTORY'],
'/mobile/android',
]
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -43,17 +43,17 @@ include $(topsrcdir)/config/android-comm
$(MAKE) -C ../../../faster
$(MAKE) -C ../installer stage-package
$(MKDIR) -p $(@D)
rsync --update $(DIST)/fennec/$(notdir $(OMNIJAR_NAME)) $@
$(RM) $(DIST)/fennec/$(notdir $(OMNIJAR_NAME))
.PHONY: features
features::
- $(call py_action,generate_builtin_addons,chrome/chrome/content/built_in_addons.json)
+ $(call py_action,generate_builtin_addons,--features=features chrome/chrome/content/built_in_addons.json)
ifndef MOZILLA_OFFICIAL
# Targets built very early during a Gradle build. In automation,
# these are built before Gradle is invoked, and gradle-targets is not
# made at all. This is required to avoid building gradle-targets with
# AB_CD=multi during multi-l10n builds.
gradle-targets: $(generated_resources) $(generated_files)
new file mode 100644
--- /dev/null
+++ b/mobile/android/extensions/gws-and-facebook-chrome-spoof/bootstrap.js
@@ -0,0 +1,73 @@
+/* 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/. */
+
+/* globals Services */
+
+"use strict";
+
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+ChromeUtils.import("resource://gre/modules/Services.jsm");
+ChromeUtils.defineModuleGetter(this, "UserAgentOverrides", "resource://gre/modules/UserAgentOverrides.jsm");
+Cu.importGlobalProperties(["URL"]);
+
+const TLDsToSpoof = /(^(www|encrypted|maps)\.google\.)|((.*\.facebook|.*\.fbcdn|.*\.fbsbx)\.(com|net)$)/;
+
+const OverrideNotice = "The user agent string has been overridden to get the Chrome experience on this site.";
+
+const defaultUA = Cc["@mozilla.org/network/protocol;1?name=http"].
+ getService(Ci.nsIHttpProtocolHandler).userAgent;
+
+const RunningFirefoxVersion = (defaultUA.match(/Firefox\/([0-9.]+)/) || ["", "58.0"])[1];
+const RunningAndroidVersion = defaultUA.match(/Android\/[0-9.]+/) || "Android 6.0";
+
+const ChromeMajorVersionToMimic = `${parseInt(RunningFirefoxVersion) + 4}.0.0.0`;
+
+const ChromePhoneUA = `Mozilla/5.0 (Linux; ${RunningAndroidVersion}; Nexus 5 Build/MRA58N) FxQuantum/${RunningFirefoxVersion} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ChromeMajorVersionToMimic} Mobile Safari/537.36`;
+const ChromeTabletUA = `Mozilla/5.0 (Linux; ${RunningAndroidVersion}; Nexus 7 Build/JSS15Q) FxQuantum/${RunningFirefoxVersion} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ChromeMajorVersionToMimic} Safari/537.36`;
+
+const IsPhone = defaultUA.includes("Mobile");
+
+const TargetUA = IsPhone ? ChromePhoneUA : ChromeTabletUA;
+
+const EnabledPref = "extensions.gws-and-facebook-chrome-spoof.enabled";
+
+let Enabled = true;
+
+function checkIfEnabled() {
+ Enabled = Services.prefs.getBoolPref(EnabledPref, true);
+}
+
+this.install = () => {
+ Services.prefs.setBoolPref(EnabledPref, true);
+};
+
+this.uninstall = () => {
+ Services.prefs.clearUserPref(EnabledPref);
+};
+
+this.shutdown = () => {
+ Services.prefs.removeObserver(EnabledPref, checkIfEnabled);
+ Enabled = false;
+};
+
+this.startup = () => {
+ UserAgentOverrides.addComplexOverride((channel, defaultUA) => {
+ if (!Enabled) {
+ return false;
+ }
+
+ try {
+ let domain = new URL(channel.URI.spec).host;
+ if (domain.match(TLDsToSpoof)) {
+ console.info(OverrideNotice);
+ return TargetUA;
+ }
+ } catch (_) {
+ }
+ return false;
+ });
+
+ Services.prefs.addObserver(EnabledPref, checkIfEnabled);
+ checkIfEnabled();
+};
new file mode 100644
--- /dev/null
+++ b/mobile/android/extensions/gws-and-facebook-chrome-spoof/install.rdf.in
@@ -0,0 +1,29 @@
+<?xml version="1.0"?>
+<!-- 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/. -->
+
+#filter substitution
+
+<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:em="http://www.mozilla.org/2004/em-rdf#">
+ <Description about="urn:mozilla:install-manifest">
+ <em:id>gws-and-facebook-spoof@mozilla.org</em:id>
+ <em:type>2</em:type>
+ <em:bootstrap>true</em:bootstrap>
+ <em:multiprocessCompatible>true</em:multiprocessCompatible>
+
+ <em:name>Use Chrome mobile version of Google Search and Facebook</em:name>
+ <em:description>Enables the Chrome Mobile experience for Google Search and Facebook.</em:description>
+
+ <em:version>1.0.0</em:version>
+
+ <em:targetApplication>
+ <Description>
+ <em:id>{aa3c5121-dab2-40e2-81ca-7ea25febc110}</em:id>
+ <em:minVersion>@MOZ_APP_VERSION@</em:minVersion>
+ <em:maxVersion>@MOZ_APP_MAXVERSION@</em:maxVersion>
+ </Description>
+ </em:targetApplication>
+ </Description>
+</RDF>
new file mode 100644
--- /dev/null
+++ b/mobile/android/extensions/gws-and-facebook-chrome-spoof/moz.build
@@ -0,0 +1,19 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+DEFINES['MOZ_APP_VERSION'] = CONFIG['MOZ_APP_VERSION']
+DEFINES['MOZ_APP_MAXVERSION'] = CONFIG['MOZ_APP_MAXVERSION']
+
+with Files('**'):
+ BUG_COMPONENT = ('Web Compatibility Tools', 'General')
+
+FINAL_TARGET_FILES.features['gws-and-facebook-spoof@mozilla.org'] += [
+ 'bootstrap.js'
+]
+
+FINAL_TARGET_PP_FILES.features['gws-and-facebook-spoof@mozilla.org'] += [
+ 'install.rdf.in'
+]
new file mode 100644
--- /dev/null
+++ b/mobile/android/extensions/moz.build
@@ -0,0 +1,14 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+with Files('**'):
+ BUG_COMPONENT = ('Firefox for Android', 'General')
+
+# Only include the following system add-ons if building Nightly
+if CONFIG['NIGHTLY_BUILD']:
+ DIRS += [
+ 'gws-and-facebook-chrome-spoof',
+ ]
--- a/mobile/android/moz.build
+++ b/mobile/android/moz.build
@@ -47,16 +47,17 @@ DIRS += [
'../locales',
'locales',
]
DIRS += [
'base',
'chrome',
'components',
+ 'extensions',
'modules',
'themes/core',
'themes/geckoview',
'app',
'fonts',
]
TEST_DIRS += [