summaryrefslogtreecommitdiffstats
path: root/browser/extensions/webcompat/experiment-apis/trackingProtection.js
blob: 557090e9742ecb992a7c02dbcee327b22a13a9b4 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* 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/. */

"use strict";

/* global ExtensionAPI, ExtensionCommon, ExtensionParent, Services, XPCOMUtils */

XPCOMUtils.defineLazyModuleGetters(this, {
  Services: "resource://gre/modules/Services.jsm",
});

XPCOMUtils.defineLazyGlobalGetters(this, ["URL", "ChannelWrapper"]);

class Manager {
  constructor() {
    this._allowLists = new Map();
  }

  _ensureStarted() {
    if (this._classifierObserver) {
      return;
    }

    this._unblockedChannelIds = new Set();
    this._channelClassifier = Cc[
      "@mozilla.org/url-classifier/channel-classifier-service;1"
    ].getService(Ci.nsIChannelClassifierService);
    this._classifierObserver = {};
    this._classifierObserver.observe = (subject, topic, data) => {
      switch (topic) {
        case "http-on-stop-request": {
          const { channelId } = subject.QueryInterface(Ci.nsIIdentChannel);
          this._unblockedChannelIds.delete(channelId);
          break;
        }
        case "urlclassifier-before-block-channel": {
          const channel = subject.QueryInterface(
            Ci.nsIUrlClassifierBlockedChannel
          );
          const { channelId, url } = channel;
          let topHost;
          try {
            topHost = new URL(channel.topLevelUrl).hostname;
          } catch (_) {
            return;
          }
          for (const allowList of this._allowLists.values()) {
            for (const entry of allowList.values()) {
              const { matcher, hosts, notHosts } = entry;
              if (matcher.matches(url)) {
                if (
                  !notHosts?.has(topHost) &&
                  (hosts === true || hosts.has(topHost))
                ) {
                  this._unblockedChannelIds.add(channelId);
                  channel.unblock();
                  return;
                }
              }
            }
          }
          break;
        }
      }
    };
    Services.obs.addObserver(this._classifierObserver, "http-on-stop-request");
    this._channelClassifier.addListener(this._classifierObserver);
  }

  stop() {
    if (!this._classifierObserver) {
      return;
    }

    Services.obs.removeObserver(
      this._classifierObserver,
      "http-on-stop-request"
    );
    this._channelClassifier.removeListener(this._classifierObserver);
    delete this._channelClassifier;
    delete this._classifierObserver;
  }

  wasChannelIdUnblocked(channelId) {
    return this._unblockedChannelIds.has(channelId);
  }

  allow(allowListId, patterns, { hosts, notHosts }) {
    this._ensureStarted();

    if (!this._allowLists.has(allowListId)) {
      this._allowLists.set(allowListId, new Map());
    }
    const allowList = this._allowLists.get(allowListId);
    for (const pattern of patterns) {
      if (!allowList.has(pattern)) {
        allowList.set(pattern, {
          matcher: new MatchPattern(pattern),
        });
      }
      const allowListPattern = allowList.get(pattern);
      if (!hosts) {
        allowListPattern.hosts = true;
      } else {
        if (!allowListPattern.hosts) {
          allowListPattern.hosts = new Set();
        }
        for (const host of hosts) {
          allowListPattern.hosts.add(host);
        }
      }
      if (notHosts) {
        if (!allowListPattern.notHosts) {
          allowListPattern.notHosts = new Set();
        }
        for (const notHost of notHosts) {
          allowListPattern.notHosts.add(notHost);
        }
      }
    }
  }

  revoke(allowListId) {
    this._allowLists.delete(allowListId);
  }
}
var manager = new Manager();

function getChannelId(context, requestId) {
  const wrapper = ChannelWrapper.getRegisteredChannel(
    requestId,
    context.extension.policy,
    context.xulBrowser.frameLoader.remoteTab
  );
  return wrapper?.channel?.QueryInterface(Ci.nsIIdentChannel)?.channelId;
}

this.trackingProtection = class extends ExtensionAPI {
  onShutdown(isAppShutdown) {
    if (manager) {
      manager.stop();
    }
  }

  getAPI(context) {
    return {
      trackingProtection: {
        async allow(allowListId, patterns, options) {
          manager.allow(allowListId, patterns, options);
        },
        async revoke(allowListId) {
          manager.revoke(allowListId);
        },
        async wasRequestUnblocked(requestId) {
          if (!manager) {
            return false;
          }
          const channelId = getChannelId(context, requestId);
          if (!channelId) {
            return false;
          }
          return manager.wasChannelIdUnblocked(channelId);
        },
      },
    };
  }
};