dom/base/SiteSpecificUserAgent.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const Cu = Components.utils;
     6 const Cc = Components.classes;
     7 const Ci = Components.interfaces;
     9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    11 const MAX_CACHE_SIZE      = 250;
    12 const PREF_UPDATE         = "general.useragent.updates.";
    13 const PREF_OVERRIDE       = "general.useragent.override.";
    14 const XPCOM_SHUTDOWN      = "xpcom-shutdown";
    15 const HTTP_PROTO_HANDLER = Cc["@mozilla.org/network/protocol;1?name=http"]
    16                              .getService(Ci.nsIHttpProtocolHandler);
    18 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    19   "@mozilla.org/childprocessmessagemanager;1",
    20   "nsISyncMessageSender");
    22 function SiteSpecificUserAgent() {
    23   this.inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
    24     .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
    26   if (this.inParent) {
    27     Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
    28   } else {
    29     Cu.import("resource://gre/modules/Services.jsm");
    30     Services.prefs.addObserver(PREF_OVERRIDE, this, false);
    31     Services.prefs.addObserver(PREF_UPDATE, this, false);
    32     Services.obs.addObserver(this, XPCOM_SHUTDOWN, false);
    33     this.userAgentCache = new Map;
    34   }
    35 }
    37 SiteSpecificUserAgent.prototype = {
    38   getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) {
    39     if (this.inParent) {
    40       return UserAgentOverrides.getOverrideForURI(aURI) || HTTP_PROTO_HANDLER.userAgent;
    41     }
    43     let host = aURI.asciiHost;
    44     let cachedResult = this.userAgentCache.get(host);
    45     if (cachedResult) {
    46       return cachedResult;
    47     }
    49     let data = { uri: aURI };
    50     let result = cpmm.sendSyncMessage("Useragent:GetOverride", data)[0] || HTTP_PROTO_HANDLER.userAgent;
    52     if (this.userAgentCache.size >= MAX_CACHE_SIZE) {
    53       this.userAgentCache.clear();
    54     }
    56     this.userAgentCache.set(host, result);
    57     return result;
    58   },
    60   invalidateCache: function() {
    61     this.userAgentCache.clear();
    62   },
    64   clean: function() {
    65     this.userAgentCache.clear();
    66     if (!this.inParent) {
    67       Services.obs.removeObserver(this, XPCOM_SHUTDOWN);
    68       Services.prefs.removeObserver(PREF_OVERRIDE, this);
    69       Services.prefs.removeObserver(PREF_UPDATE, this);
    70     }
    71   },
    73   observe: function(subject, topic, data) {
    74     switch (topic) {
    75       case "nsPref:changed":
    76         this.invalidateCache();
    77         break;
    78       case XPCOM_SHUTDOWN:
    79         this.clean();
    80         break;
    81     }
    82   },
    84   classID: Components.ID("{506c680f-3d1c-4954-b351-2c80afbc37d3}"),
    85   QueryInterface: XPCOMUtils.generateQI([Ci.nsISiteSpecificUserAgent])
    86 };
    88 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SiteSpecificUserAgent]);

mercurial