Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'jb51379' into 'master'
[embedlite-components] Update user-agent request header on http-on-modify-request. JB#51379

See merge request mer-core/embedlite-components!91
  • Loading branch information
rainemak committed Oct 26, 2020
2 parents d8d0224 + 616d5e2 commit 698504e
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions jscomps/UserAgentOverrideHelper.js
Expand Up @@ -35,10 +35,6 @@ UserAgentOverrideHelper.prototype = {
Services.obs.addObserver(this, VIEW_CREATED, true);
Services.obs.addObserver(this, XPCOM_SHUTDOWN, false);
Services.prefs.addObserver(PREF_OVERRIDE, this, false);
// Do not initialize UserAgent here because then UserAgentOverrides.jsm
// will receive desktop ua as a default user agent <= initialized too ealy.
// If initialized too early regular expression based user agent overrides will
// not work.
break;
}
case "nsPref:changed": {
Expand All @@ -63,17 +59,13 @@ UserAgentOverrideHelper.prototype = {
}
},

// From nsISiteSpecificUserAgent
getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) {
return UserAgent.getUserAgentForWindow(aURI)
},

QueryInterface: XPCOMUtils.generateQI([Ci.nsISiteSpecificUserAgent, Ci.nsIObserver,
Ci.nsISupportsWeakReference, Ci.nsIFormSubmitObserver])
};

var UserAgent = {
_desktopMode: false,
_debug: false,
_customUA: null,
overrideMap: new Map,
initilized: false,
Expand All @@ -89,6 +81,8 @@ var UserAgent = {
}

Services.obs.addObserver(this, "DesktopMode:Change", false);
Services.obs.addObserver(this.onModifyRequest.bind(this),
"http-on-modify-request");
Services.prefs.addObserver(PREF_OVERRIDE, this, false);
this._customUA = this.getCustomUserAgent();
Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
Expand All @@ -102,6 +96,14 @@ var UserAgent = {
this.initilized = true;
},

onModifyRequest(aSubject, aTopic, aData) {
if (aTopic === "http-on-modify-request") {
let channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
let ua = this.onRequest(channel, this.getDefaultUserAgent());
channel.setRequestHeader("User-Agent", ua, false);
}
},

getCustomUserAgent: function() {
if (Services.prefs.prefHasUserValue(PREF_OVERRIDE)) {
let ua = Services.prefs.getCharPref(PREF_OVERRIDE);
Expand Down Expand Up @@ -129,35 +131,30 @@ var UserAgent = {
onRequest: function(channel, defaultUA) {
let ua = "";
let uri = channel.URI;
let loadingPrincipalURI = null;

// Prefer current uri over the loading principal's uri in case both have overrides.
ua = uri && UserAgentOverrides.getOverrideForURI(uri)

if (ua) {
// Requires also Logger to be enabled
if (this._debug) {
Logger.debug("Uri:", uri.asciiHost, "UA:", ua)
}

return ua
} else {
let loadInfo = channel.loadInfo;
let loadingPrincipalURI = loadInfo && loadInfo.loadingPrincipal && loadInfo.loadingPrincipal.URI;
if (loadingPrincipalURI && loadingPrincipalURI.asciiHost) {
uri = loadingPrincipalURI;
}
loadingPrincipalURI = loadInfo && loadInfo.loadingPrincipal && loadInfo.loadingPrincipal.URI;
}

return this.getUserAgentForWindow(uri);
},

getUserAgentForWindow: function ua_getUserAgentForWindow(aUri, aWindow) {
// Try to pick 'general.useragent.override.*'
let ua = null;

if (aUri) {
ua = UserAgentOverrides.getOverrideForURI(aUri);
}

if (ua) {
if (loadingPrincipalURI && loadingPrincipalURI.asciiHost) {
ua = UserAgentOverrides.getOverrideForURI(loadingPrincipalURI);
if (this._debug) {
Logger.debug("Loading principal uri:", loadingPrincipalURI.asciiHost, "Uri:", uri.asciiHost, "UA:", ua);
}
return ua;
}

return this.getDefaultUserAgent();
},

Expand Down

0 comments on commit 698504e

Please sign in to comment.