зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1376602
- Part 1: Import sources for WebCompat Go Faster 1.2. r=Felipe
MozReview-Commit-ID: cLuF81S7H4 --HG-- extra : rebase_source : 3a92adf155da360e42c8fddc59d131907124b7df
This commit is contained in:
Родитель
917c4fb30f
Коммит
6e5bf84c9f
|
@ -2,12 +2,15 @@
|
|||
* 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/. */
|
||||
|
||||
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Components.utils.import("resource://gre/modules/Services.jsm");
|
||||
const {utils: Cu} = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
const PREF_BRANCH = "extensions.webcompat.";
|
||||
const PREF_DEFAULTS = {perform_ua_overrides: true};
|
||||
|
||||
const UA_OVERRIDES_INIT_TOPIC = "useragentoverrides-initialized";
|
||||
const UA_ENABLE_PREF_NAME = "extensions.webcompat.perform_ua_overrides";
|
||||
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "UAOverrider", "chrome://webcompat/content/lib/ua_overrider.jsm");
|
||||
|
@ -21,7 +24,6 @@ function UAEnablePrefObserver() {
|
|||
overrider = new UAOverrider(UAOverrides);
|
||||
overrider.init();
|
||||
} else if (!isEnabled && overrider) {
|
||||
overrider.uninit();
|
||||
overrider = null;
|
||||
}
|
||||
}
|
||||
|
@ -60,14 +62,24 @@ this.startup = function({webExtension}) {
|
|||
Services.prefs.clearUserPref(UA_ENABLE_PREF_NAME);
|
||||
Services.prefs.addObserver(UA_ENABLE_PREF_NAME, UAEnablePrefObserver);
|
||||
|
||||
overrider = new UAOverrider(UAOverrides);
|
||||
overrider.init();
|
||||
// Listen to the useragentoverrides-initialized notification we get and
|
||||
// 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 = {
|
||||
observe(aSubject, aTopic, aData) {
|
||||
if (aTopic !== UA_OVERRIDES_INIT_TOPIC) {
|
||||
return;
|
||||
}
|
||||
|
||||
Services.obs.removeObserver(this, UA_OVERRIDES_INIT_TOPIC);
|
||||
overrider = new UAOverrider(UAOverrides);
|
||||
overrider.init();
|
||||
}
|
||||
};
|
||||
Services.obs.addObserver(startupWatcher, UA_OVERRIDES_INIT_TOPIC);
|
||||
};
|
||||
|
||||
this.shutdown = function() {
|
||||
Services.prefs.removeObserver(UA_ENABLE_PREF_NAME, UAEnablePrefObserver);
|
||||
|
||||
if (overrider) {
|
||||
overrider.uninit();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -7,10 +7,8 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/Console.jsm");
|
||||
|
||||
const DefaultUA = Cc["@mozilla.org/network/protocol;1?name=http"].getService(Ci.nsIHttpProtocolHandler).userAgent;
|
||||
const NS_HTTP_ON_USERAGENT_REQUEST_TOPIC = "http-on-useragent-request";
|
||||
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "Services", "resource://gre/modules/Services.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "UserAgentOverrides", "resource://gre/modules/UserAgentOverrides.jsm");
|
||||
XPCOMUtils.defineLazyServiceGetter(this, "eTLDService", "@mozilla.org/network/effective-tld-service;1", "nsIEffectiveTLDService");
|
||||
|
||||
class UAOverrider {
|
||||
|
@ -35,24 +33,14 @@ class UAOverrider {
|
|||
}
|
||||
|
||||
init() {
|
||||
Services.obs.addObserver(this, NS_HTTP_ON_USERAGENT_REQUEST_TOPIC);
|
||||
UserAgentOverrides.addComplexOverride(this.overrideCallback.bind(this));
|
||||
}
|
||||
|
||||
uninit() {
|
||||
Services.obs.removeObserver(this, NS_HTTP_ON_USERAGENT_REQUEST_TOPIC);
|
||||
}
|
||||
|
||||
observe(subject, topic) {
|
||||
if (topic !== NS_HTTP_ON_USERAGENT_REQUEST_TOPIC) {
|
||||
return;
|
||||
}
|
||||
|
||||
let channel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
|
||||
let uaOverride = this.lookupUAOverride(channel.URI);
|
||||
|
||||
overrideCallback(channel, defaultUA) {
|
||||
let uaOverride = this.lookupUAOverride(channel.URI, defaultUA);
|
||||
if (uaOverride) {
|
||||
console.log("The user agent has been overridden for compatibility reasons.");
|
||||
channel.setRequestHeader("User-Agent", uaOverride, false);
|
||||
return uaOverride;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,12 +76,12 @@ class UAOverrider {
|
|||
* If there are more than one possible overrides, that is if two or more
|
||||
* uriMatchers would return true, the first one gets applied.
|
||||
*/
|
||||
lookupUAOverride(uri) {
|
||||
lookupUAOverride(uri, defaultUA) {
|
||||
let baseDomain = this.getBaseDomainFromURI(uri);
|
||||
if (baseDomain && this._overrides[baseDomain]) {
|
||||
for (let uaOverride of this._overrides[baseDomain]) {
|
||||
if (uaOverride.uriMatcher(uri.specIgnoringRef)) {
|
||||
return uaOverride.uaTransformer(DefaultUA);
|
||||
return uaOverride.uaTransformer(defaultUA);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче