dom/base/SiteSpecificUserAgent.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/SiteSpecificUserAgent.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Cu = Components.utils;
     1.9 +const Cc = Components.classes;
    1.10 +const Ci = Components.interfaces;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +
    1.14 +const MAX_CACHE_SIZE      = 250;
    1.15 +const PREF_UPDATE         = "general.useragent.updates.";
    1.16 +const PREF_OVERRIDE       = "general.useragent.override.";
    1.17 +const XPCOM_SHUTDOWN      = "xpcom-shutdown";
    1.18 +const HTTP_PROTO_HANDLER = Cc["@mozilla.org/network/protocol;1?name=http"]
    1.19 +                             .getService(Ci.nsIHttpProtocolHandler);
    1.20 +
    1.21 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    1.22 +  "@mozilla.org/childprocessmessagemanager;1",
    1.23 +  "nsISyncMessageSender");
    1.24 +
    1.25 +function SiteSpecificUserAgent() {
    1.26 +  this.inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
    1.27 +    .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
    1.28 +
    1.29 +  if (this.inParent) {
    1.30 +    Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
    1.31 +  } else {
    1.32 +    Cu.import("resource://gre/modules/Services.jsm");
    1.33 +    Services.prefs.addObserver(PREF_OVERRIDE, this, false);
    1.34 +    Services.prefs.addObserver(PREF_UPDATE, this, false);
    1.35 +    Services.obs.addObserver(this, XPCOM_SHUTDOWN, false);
    1.36 +    this.userAgentCache = new Map;
    1.37 +  }
    1.38 +}
    1.39 +
    1.40 +SiteSpecificUserAgent.prototype = {
    1.41 +  getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) {
    1.42 +    if (this.inParent) {
    1.43 +      return UserAgentOverrides.getOverrideForURI(aURI) || HTTP_PROTO_HANDLER.userAgent;
    1.44 +    }
    1.45 +
    1.46 +    let host = aURI.asciiHost;
    1.47 +    let cachedResult = this.userAgentCache.get(host);
    1.48 +    if (cachedResult) {
    1.49 +      return cachedResult;
    1.50 +    }
    1.51 +
    1.52 +    let data = { uri: aURI };
    1.53 +    let result = cpmm.sendSyncMessage("Useragent:GetOverride", data)[0] || HTTP_PROTO_HANDLER.userAgent;
    1.54 +
    1.55 +    if (this.userAgentCache.size >= MAX_CACHE_SIZE) {
    1.56 +      this.userAgentCache.clear();
    1.57 +    }
    1.58 +
    1.59 +    this.userAgentCache.set(host, result);
    1.60 +    return result;
    1.61 +  },
    1.62 +
    1.63 +  invalidateCache: function() {
    1.64 +    this.userAgentCache.clear();
    1.65 +  },
    1.66 +
    1.67 +  clean: function() {
    1.68 +    this.userAgentCache.clear();
    1.69 +    if (!this.inParent) {
    1.70 +      Services.obs.removeObserver(this, XPCOM_SHUTDOWN);
    1.71 +      Services.prefs.removeObserver(PREF_OVERRIDE, this);
    1.72 +      Services.prefs.removeObserver(PREF_UPDATE, this);
    1.73 +    }
    1.74 +  },
    1.75 +
    1.76 +  observe: function(subject, topic, data) {
    1.77 +    switch (topic) {
    1.78 +      case "nsPref:changed":
    1.79 +        this.invalidateCache();
    1.80 +        break;
    1.81 +      case XPCOM_SHUTDOWN:
    1.82 +        this.clean();
    1.83 +        break;
    1.84 +    }
    1.85 +  },
    1.86 +
    1.87 +  classID: Components.ID("{506c680f-3d1c-4954-b351-2c80afbc37d3}"),
    1.88 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsISiteSpecificUserAgent])
    1.89 +};
    1.90 +
    1.91 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SiteSpecificUserAgent]);

mercurial