summaryrefslogtreecommitdiffstats
path: root/dom/base/SiteSpecificUserAgent.js
blob: 7e1bf44f8b5670d238235bfa5e23a929adb6d7cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* 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/. */

const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

const MAX_CACHE_SIZE      = 250;
const PREF_UPDATE         = "general.useragent.updates.";
const PREF_OVERRIDE       = "general.useragent.override.";
const XPCOM_SHUTDOWN      = "xpcom-shutdown";
const HTTP_PROTO_HANDLER = Cc["@mozilla.org/network/protocol;1?name=http"]
                             .getService(Ci.nsIHttpProtocolHandler);

XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
  "@mozilla.org/childprocessmessagemanager;1",
  "nsISyncMessageSender");

function SiteSpecificUserAgent() {
  this.inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
    .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;

  if (this.inParent) {
    Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
  } else {
    Cu.import("resource://gre/modules/Services.jsm");
    Services.prefs.addObserver(PREF_OVERRIDE, this, false);
    Services.prefs.addObserver(PREF_UPDATE, this, false);
    Services.obs.addObserver(this, XPCOM_SHUTDOWN, false);
    this.userAgentCache = new Map;
  }
}

SiteSpecificUserAgent.prototype = {
  getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) {
    if (this.inParent) {
      return UserAgentOverrides.getOverrideForURI(aURI) || HTTP_PROTO_HANDLER.userAgent;
    }

    let host = aURI.asciiHost;
    let cachedResult = this.userAgentCache.get(host);
    if (cachedResult) {
      return cachedResult;
    }

    let data = { uri: aURI.spec };
    let result = cpmm.sendRpcMessage("Useragent:GetOverride", data)[0] || HTTP_PROTO_HANDLER.userAgent;

    if (this.userAgentCache.size >= MAX_CACHE_SIZE) {
      this.userAgentCache.clear();
    }

    this.userAgentCache.set(host, result);
    return result;
  },

  invalidateCache: function() {
    this.userAgentCache.clear();
  },

  clean: function() {
    this.userAgentCache.clear();
    if (!this.inParent) {
      Services.obs.removeObserver(this, XPCOM_SHUTDOWN);
      Services.prefs.removeObserver(PREF_OVERRIDE, this);
      Services.prefs.removeObserver(PREF_UPDATE, this);
    }
  },

  observe: function(subject, topic, data) {
    switch (topic) {
      case "nsPref:changed":
        this.invalidateCache();
        break;
      case XPCOM_SHUTDOWN:
        this.clean();
        break;
    }
  },

  classID: Components.ID("{506c680f-3d1c-4954-b351-2c80afbc37d3}"),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsISiteSpecificUserAgent])
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SiteSpecificUserAgent]);