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 import { ComponentUtils } from "resource://gre/modules/ComponentUtils.sys.mjs";
7 const Cm = Components.manager;
10 * This is a nsIChannelEventSink implementation that monitors channel redirects.
11 * This has been forked from:
12 * https://searchfox.org/mozilla-central/source/devtools/server/actors/network-monitor/channel-event-sink.js
13 * The rest of this module is also more or less forking:
14 * https://searchfox.org/mozilla-central/source/devtools/server/actors/network-monitor/network-observer.js
15 * TODO(try to re-unify /remote/ with /devtools code)
17 const SINK_CLASS_DESCRIPTION = "NetworkMonitor Channel Event Sink";
18 const SINK_CLASS_ID = Components.ID("{c2b4c83e-607a-405a-beab-0ef5dbfb7617}");
19 const SINK_CONTRACT_ID = "@mozilla.org/network/monitor/channeleventsink;1";
20 const SINK_CATEGORY_NAME = "net-channel-event-sinks";
22 function ChannelEventSink() {
23 this.wrappedJSObject = this;
24 this.collectors = new Set();
27 ChannelEventSink.prototype = {
28 QueryInterface: ChromeUtils.generateQI(["nsIChannelEventSink"]),
30 registerCollector(collector) {
31 this.collectors.add(collector);
34 unregisterCollector(collector) {
35 this.collectors.delete(collector);
37 if (this.collectors.size == 0) {
38 ChannelEventSinkFactory.unregister();
42 // eslint-disable-next-line no-shadow
43 asyncOnChannelRedirect(oldChannel, newChannel, flags, callback) {
44 for (const collector of this.collectors) {
46 collector._onChannelRedirect(oldChannel, newChannel, flags);
49 "StackTraceCollector.onChannelRedirect threw an exception",
54 callback.onRedirectVerifyCallback(Cr.NS_OK);
58 export const ChannelEventSinkFactory =
59 ComponentUtils.generateSingletonFactory(ChannelEventSink);
61 ChannelEventSinkFactory.register = function () {
62 const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
63 if (registrar.isCIDRegistered(SINK_CLASS_ID)) {
67 registrar.registerFactory(
69 SINK_CLASS_DESCRIPTION,
71 ChannelEventSinkFactory
74 Services.catMan.addCategoryEntry(
83 ChannelEventSinkFactory.unregister = function () {
84 const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
85 registrar.unregisterFactory(SINK_CLASS_ID, ChannelEventSinkFactory);
87 Services.catMan.deleteCategoryEntry(
94 ChannelEventSinkFactory.getService = function () {
95 // Make sure the ChannelEventSink service is registered before accessing it
96 ChannelEventSinkFactory.register();
98 return Cc[SINK_CONTRACT_ID].getService(Ci.nsIChannelEventSink)