summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/geckoview/GeckoViewContentBlocking.jsm
blob: 2b556b24e6c375a60a5bd729d15a5b12b59bce6d (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
/* 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";

var EXPORTED_SYMBOLS = ["GeckoViewContentBlocking"];

const { GeckoViewModule } = ChromeUtils.import(
  "resource://gre/modules/GeckoViewModule.jsm"
);

class GeckoViewContentBlocking extends GeckoViewModule {
  onEnable() {
    const flags = Ci.nsIWebProgress.NOTIFY_CONTENT_BLOCKING;
    this.progressFilter = Cc[
      "@mozilla.org/appshell/component/browser-status-filter;1"
    ].createInstance(Ci.nsIWebProgress);
    this.progressFilter.addProgressListener(this, flags);
    this.browser.addProgressListener(this.progressFilter, flags);

    this.registerListener(["ContentBlocking:RequestLog"]);
  }

  onDisable() {
    if (this.progressFilter) {
      this.progressFilter.removeProgressListener(this);
      this.browser.removeProgressListener(this.progressFilter);
      delete this.progressFilter;
    }

    this.unregisterListener(["ContentBlocking:RequestLog"]);
  }

  // Bundle event handler.
  onEvent(aEvent, aData, aCallback) {
    debug`onEvent: event=${aEvent}, data=${aData}`;

    switch (aEvent) {
      case "ContentBlocking:RequestLog": {
        let bc = this.browser.browsingContext;

        if (!bc) {
          warn`Failed to export content blocking log.`;
          break;
        }

        // Get the top-level browsingContext. The ContentBlockingLog is located
        // in its current window global.
        bc = bc.top;

        const topWindowGlobal = bc.currentWindowGlobal;

        if (!topWindowGlobal) {
          warn`Failed to export content blocking log.`;
          break;
        }

        const log = JSON.parse(topWindowGlobal.contentBlockingLog);
        const res = Object.keys(log).map(key => {
          const blockData = log[key].map(data => {
            return {
              category: data[0],
              blocked: data[1],
              count: data[2],
            };
          });
          return {
            origin: key,
            blockData,
          };
        });

        aCallback.onSuccess({ log: res });
        break;
      }
    }
  }

  onContentBlockingEvent(aWebProgress, aRequest, aEvent) {
    debug`onContentBlockingEvent ${aEvent.toString(16)}`;

    if (!(aRequest instanceof Ci.nsIClassifiedChannel)) {
      return;
    }

    const channel = aRequest.QueryInterface(Ci.nsIChannel);
    const uri = channel.URI && channel.URI.spec;

    if (!uri) {
      return;
    }

    const classChannel = aRequest.QueryInterface(Ci.nsIClassifiedChannel);
    const blockedList = classChannel.matchedList || null;
    let loadedLists = [];

    if (aRequest instanceof Ci.nsIHttpChannel) {
      loadedLists = classChannel.matchedTrackingLists || [];
    }

    debug`onContentBlockingEvent matchedList: ${blockedList}`;
    debug`onContentBlockingEvent matchedTrackingLists: ${loadedLists}`;

    const message = {
      type: "GeckoView:ContentBlockingEvent",
      uri,
      category: aEvent,
      blockedList,
      loadedLists,
    };

    this.eventDispatcher.sendRequest(message);
  }
}

const { debug, warn } = GeckoViewContentBlocking.initLogging(
  "GeckoViewContentBlocking"
);