no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / remote / cdp / observers / ChannelEventSink.sys.mjs
blob48e7c6ee6407c0b56a458abbc0b0cd9c64297a71
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;
9 /**
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)
16  */
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);
32   },
34   unregisterCollector(collector) {
35     this.collectors.delete(collector);
37     if (this.collectors.size == 0) {
38       ChannelEventSinkFactory.unregister();
39     }
40   },
42   // eslint-disable-next-line no-shadow
43   asyncOnChannelRedirect(oldChannel, newChannel, flags, callback) {
44     for (const collector of this.collectors) {
45       try {
46         collector._onChannelRedirect(oldChannel, newChannel, flags);
47       } catch (ex) {
48         console.error(
49           "StackTraceCollector.onChannelRedirect threw an exception",
50           ex
51         );
52       }
53     }
54     callback.onRedirectVerifyCallback(Cr.NS_OK);
55   },
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)) {
64     return;
65   }
67   registrar.registerFactory(
68     SINK_CLASS_ID,
69     SINK_CLASS_DESCRIPTION,
70     SINK_CONTRACT_ID,
71     ChannelEventSinkFactory
72   );
74   Services.catMan.addCategoryEntry(
75     SINK_CATEGORY_NAME,
76     SINK_CONTRACT_ID,
77     SINK_CONTRACT_ID,
78     false,
79     true
80   );
83 ChannelEventSinkFactory.unregister = function () {
84   const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
85   registrar.unregisterFactory(SINK_CLASS_ID, ChannelEventSinkFactory);
87   Services.catMan.deleteCategoryEntry(
88     SINK_CATEGORY_NAME,
89     SINK_CONTRACT_ID,
90     false
91   );
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)
99     .wrappedJSObject;