content-about-page-utils.js (2141B)
1 /* eslint-env mozilla/process-script */ 2 3 "use strict"; 4 5 var Cm = Components.manager; 6 7 function AboutPage(aboutHost, chromeURL, uriFlags) { 8 this.chromeURL = chromeURL; 9 this.aboutHost = aboutHost; 10 this.classID = Components.ID(Services.uuid.generateUUID().number); 11 this.description = "BrowserTestUtils: " + aboutHost; 12 this.uriFlags = uriFlags; 13 } 14 15 AboutPage.prototype = { 16 QueryInterface: ChromeUtils.generateQI(["nsIAboutModule"]), 17 getURIFlags() { 18 // eslint-disable-line no-unused-vars 19 return this.uriFlags; 20 }, 21 22 newChannel(aURI, aLoadInfo) { 23 let newURI = Services.io.newURI(this.chromeURL); 24 let channel = Services.io.newChannelFromURIWithLoadInfo(newURI, aLoadInfo); 25 channel.originalURI = aURI; 26 27 if (this.uriFlags & Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT) { 28 channel.owner = null; 29 } 30 return channel; 31 }, 32 33 createInstance(iid) { 34 return this.QueryInterface(iid); 35 }, 36 37 register() { 38 Cm.QueryInterface(Ci.nsIComponentRegistrar).registerFactory( 39 this.classID, 40 this.description, 41 "@mozilla.org/network/protocol/about;1?what=" + this.aboutHost, 42 this 43 ); 44 }, 45 46 unregister() { 47 Cm.QueryInterface(Ci.nsIComponentRegistrar).unregisterFactory( 48 this.classID, 49 this 50 ); 51 }, 52 }; 53 54 const gRegisteredPages = new Map(); 55 56 addMessageListener("browser-test-utils:about-registration:register", msg => { 57 let { aboutModule, pageURI, flags } = msg.data; 58 if (gRegisteredPages.has(aboutModule)) { 59 gRegisteredPages.get(aboutModule).unregister(); 60 } 61 let moduleObj = new AboutPage(aboutModule, pageURI, flags); 62 moduleObj.register(); 63 gRegisteredPages.set(aboutModule, moduleObj); 64 sendAsyncMessage( 65 "browser-test-utils:about-registration:registered", 66 aboutModule 67 ); 68 }); 69 70 addMessageListener("browser-test-utils:about-registration:unregister", msg => { 71 let aboutModule = msg.data; 72 let moduleObj = gRegisteredPages.get(aboutModule); 73 if (moduleObj) { 74 moduleObj.unregister(); 75 gRegisteredPages.delete(aboutModule); 76 } 77 sendAsyncMessage( 78 "browser-test-utils:about-registration:unregistered", 79 aboutModule 80 ); 81 });