|
| 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/. */ |
| 4 | + |
| 5 | +import { ComponentUtils } from "resource://gre/modules/ComponentUtils.sys.mjs"; |
| 6 | + |
| 7 | +const Cm = Components.manager; |
| 8 | + |
| 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"; |
| 21 | + |
| 22 | +function ChannelEventSink() { |
| 23 | + this.wrappedJSObject = this; |
| 24 | + this.collectors = new Set(); |
| 25 | +} |
| 26 | + |
| 27 | +ChannelEventSink.prototype = { |
| 28 | + QueryInterface: ChromeUtils.generateQI(["nsIChannelEventSink"]), |
| 29 | + |
| 30 | + registerCollector(collector) { |
| 31 | + this.collectors.add(collector); |
| 32 | + }, |
| 33 | + |
| 34 | + unregisterCollector(collector) { |
| 35 | + this.collectors.delete(collector); |
| 36 | + |
| 37 | + if (this.collectors.size == 0) { |
| 38 | + ChannelEventSinkFactory.unregister(); |
| 39 | + } |
| 40 | + }, |
| 41 | + |
| 42 | + asyncOnChannelRedirect(oldChannel, newChannel, flags, callback) { |
| 43 | + for (const collector of this.collectors) { |
| 44 | + try { |
| 45 | + collector._onChannelRedirect(oldChannel, newChannel, flags); |
| 46 | + } catch (ex) { |
| 47 | + console.error( |
| 48 | + "StackTraceCollector.onChannelRedirect threw an exception", |
| 49 | + ex |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + callback.onRedirectVerifyCallback(Cr.NS_OK); |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +export const ChannelEventSinkFactory = |
| 58 | + ComponentUtils.generateSingletonFactory(ChannelEventSink); |
| 59 | + |
| 60 | +ChannelEventSinkFactory.register = function () { |
| 61 | + const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); |
| 62 | + if (registrar.isCIDRegistered(SINK_CLASS_ID)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + registrar.registerFactory( |
| 67 | + SINK_CLASS_ID, |
| 68 | + SINK_CLASS_DESCRIPTION, |
| 69 | + SINK_CONTRACT_ID, |
| 70 | + ChannelEventSinkFactory |
| 71 | + ); |
| 72 | + |
| 73 | + Services.catMan.addCategoryEntry( |
| 74 | + SINK_CATEGORY_NAME, |
| 75 | + SINK_CONTRACT_ID, |
| 76 | + SINK_CONTRACT_ID, |
| 77 | + false, |
| 78 | + true |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +ChannelEventSinkFactory.unregister = function () { |
| 83 | + const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); |
| 84 | + registrar.unregisterFactory(SINK_CLASS_ID, ChannelEventSinkFactory); |
| 85 | + |
| 86 | + Services.catMan.deleteCategoryEntry( |
| 87 | + SINK_CATEGORY_NAME, |
| 88 | + SINK_CONTRACT_ID, |
| 89 | + false |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +ChannelEventSinkFactory.getService = function () { |
| 94 | + // Make sure the ChannelEventSink service is registered before accessing it |
| 95 | + ChannelEventSinkFactory.register(); |
| 96 | + |
| 97 | + return Cc[SINK_CONTRACT_ID].getService(Ci.nsIChannelEventSink) |
| 98 | + .wrappedJSObject; |
| 99 | +}; |
0 commit comments