Search completed in 1.41 seconds.
nsIObserverService
xpcom/ds/
nsiobserverservice.idlscriptable this interface provides methods to add, remove, notify, and enumerate observers of various notifications.
... implemented by @mozilla.org/observer-service;1 as a service: var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); method overview void addobserver( in nsiobserver anobserver, in string atopic, in boolean ownsweak); nsisimpleenumerator enumerateobservers( in string atopic ); void notifyobservers( in nsisupports asubject, in string atopic, in wstring somedata ); void removeobserver( in nsiobserver anobserver, in string atopic ); methods addobserver() registers a given listener for a notif...
... ownsweak if set to false, the
nsiobserverservice will hold a strong reference to anobserver.
... components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice) .notifyobservers(null, "mytopicid", "someadditionalinformationpassedas'data'parameter"); see also nsobserverservice observer notifications provides an overview of observers and a list of built-in notifications fired by mozilla.
Monitoring plugins - Archive of obsolete content
use this feature: registration to register for runtime notifications with the observer service you must create a class with an observe method which receives 3 parameters (subject, topic and data) as well as a register method that contains the following code: var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice (components.interfaces.
nsiobserverservice); observerservice.addobserver(this, "experimental-notify-plugin-call", false); observing as discussed above, to specify what you want done when a notification arrives your class must have an observe method, receiving 3 parameters (subject, topic and data).
... clean up to unregister your class with the observer service - when you no longer want to be listening to runtime notifications - your class must include an unregister method that contains the following code: var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.removeobserver(this, "experimental-notify-plugin-call"); skeleton observer class below is a skeleton class that you may use to listen to runtime notifications: function pluginobserver() { this.registered = false; this.register(); //takes care of registering this class with observer services as an observer for plugin runtime notifications } pluginwatcherobserv...
... var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.addobserver(this, "experimental-notify-plugin-call", false); this.registered = true; } }, //unregisters from the observer services unregister: function() { if (this.registered == true) { var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.nsiob...
...serverservice); observerservice.removeobserver(this, "experimental-notify-plugin-call"); this.registered = false; } } } additional resources more information on the observer service:
nsiobserverservice nsiobserver ...
Starting WebLock
once you do this, the observer service implementing
nsiobserverservice can notify your object of registered events by means of this interface, as in the figure below.
...when the notification is made, the
nsiobserverservice broadcasts the notification from the caller of the notifyobserver() to the nsiobserver object's observe() method.
...since the component hasn't been created yet, there are no instantiated nsiobserver objects that can be passed into the
nsiobserverservice, nor can the component code do anything until it is loaded.
... registering for notifications the
nsiobserverservice interface has methods for registering and unregistering an nsiobserver object.
Index
unless otherwise noted you register for the topics using the
nsiobserverservice.
... 807
nsiobserverservice interfaces, interfaces:scriptable, xpcom, xpcom api reference, xpcom interface reference the xpcom nsobserverservice implements this interface to provide global notifications for a variety of subsystems.
... 810 notifyobservers this content is now available at
nsiobserverservice.notifyobservers().
... 811 removeobserver this content is now available at
nsiobserverservice.removeobserver().
nsIObserver
these notifications are often, though not always, broadcast via the
nsiobserverservice.
... the most common implementation of
nsiobserverservice is the xpcom observer service.
... }, register: function() { var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.addobserver(this, "mytopicid", false); }, unregister: function() { var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.removeobserver(this, "mytopicid"); } } instantiation - this should be fired once you're ready to start observing (for e...
... observer.register("*"); see also
nsiobserverservice observer_notifications ...
Setting HTTP request headers
var observerservice = cc["@mozilla.org/observer-service;1"] .getservice(ci.
nsiobserverservice); observerservice.addobserver(httprequestobserver, "http-on-modify-request", false); the first statement gets the object that will let us register with topics that we want to get notified about.
...to unregister the observer use
nsiobserverservice.removeobserver as follows: observerservice.removeobserver(httprequestobserver, "http-on-modify-request"); all-in-one example here is a slightly different version of our httprequestobserver object.
...var httprequestobserver = { observe: function(subject, topic, data) { if (topic == "http-on-modify-request") { var httpchannel = subject.queryinterface(ci.nsihttpchannel); httpchannel.setrequestheader("x-hello", "world", false); } }, get observerservice() { return cc["@mozilla.org/observer-service;1"] .getservice(ci.
nsiobserverservice); }, register: function() { this.observerservice.addobserver(this, "http-on-modify-request", false); }, unregister: function() { this.observerservice.removeobserver(this, "http-on-modify-request"); } }; this object has a convenience register() and unregister() methods, so in order to activate it you just need to call: httprequestobserver.register(); you should also remem...
...nterface(components.interfaces.nsihttpchannel); httpchannel.setrequestheader(headername, headervalue, false); return; } if (topic == "profile-after-change") { log("----------------------------> profile-after-change"); var os = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); os.addobserver(this, "http-on-modify-request", false); return; } }, queryinterface: function (iid) { if (iid.equals(components.interfaces.nsiobserver) || iid.equals(components.interfaces.nsisupports)) return this; components.returncode = components.results.ns_error_no_interface; return null; }, }; var mymodule...
Observer Notifications - Archive of obsolete content
getservice(components.interfaces.
nsiobserverservice); observerservice.addobserver(testobserver, "xulschoolhello-test-topic", false); you should come up with a notification topic that is unique so you know it will not conflict with firefox or other extensions topics.
... getservice(components.interfaces.
nsiobserverservice); let subject = components.classes["@mozilla.org/supports-string;1"].
... getservice(components.interfaces.
nsiobserverservice); this._observerservice.addobserver(this, "xulschoolhello-test-topic", false); }, /** * unitializes this object.
CommandLine - Archive of obsolete content
s(nsicommandlinehandler) || aiid.equals(nsifactory) || aiid.equals(nsisupports)) return this; throw components.results.ns_error_no_interface; }, /* nsicommandlinehandler */ handle : function clh_handle(acmdline) { var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.notifyobservers(acmdline, "commandline-args-changed", null); }, helpinfo : " -test <value> a test attribute\n", /* nsifactory */ createinstance : function mdh_ci(aouter, aiid) { if (aouter != null) { throw components.results.ns_error_no_aggregation; } return this.queryinterface(aiid); }, lockfactory : function mdh_lock(alock) { ...
... observe: function(asubject, atopic, adata) { var cmdline = asubject.queryinterface(components.interfaces.nsicommandline); var test = cmdline.handleflagwithparam("test", false); alert("test = " + test); }, register: function() { var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.addobserver(this, "commandline-args-changed", false); }, unregister: function() { var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.removeobserver(this, "commandline-args-changed"); } } var observer = new commandlineobserver(); ...
...var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.notifyobservers(window.arguments[0], "commandline-args-changed", null); addeventlistener("unload", observer.unregister, false); finally, add a reference in your application window to the observer: chrome/content/window.xul <?xml version="1.0"?> <?xml-stylesheet href="chrome://global/skin/" type="text/css"?> <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="main" title="&window.title;" windowtype="xulmine" style="width: 300px; height: 3...
Common causes of memory leaks in extensions - Extensions
to avoid this problem explicitly call
nsiobserverservice.removeobserver() in an unload event listener.
... you may also specify ownsweak = true in the call to
nsiobserverservice.addobserver(), but that might require you to properly implement weak references as well.
... == "xpcom-shutdown") { services.obs.removeobserver(myobserver, "private-browsing"); services.obs.removeobserver(myobserver, "xpcom-shutdown"); } else { // do something with "private-browsing" } } }; services.obs.addobserver(myobserver, "private-browsing", false); services.obs.addobserver(myobserver, "xpcom-shutdown", false); finally, a lot of services other than
nsiobserverservice accept nsiobserver parameters or other interfaces and will keep strong references around.
nsIMemory
a client that wishes to be notified of low memory situations (for example, because the client maintains a large memory cache that could be released when memory is tight) should register with the observer service (see
nsiobserverservice) using the topic "memory-pressure".
... when a nsimemory instance notifies memory pressure observers, it passes itself as the asubject parameter in the call to
nsiobserverservice.notifyobservers().
... see also ns_getmemorymanager
nsiobserverservice.notifyobservers() nsiobserver
nsiobserverservice ...
JavaScript Object Management - Archive of obsolete content
*/ init : function() { this.obsservice = cc["@mozilla.org/observer-service;1"].getservice(ci.
nsiobserverservice); } }; 〈namespace〉.init(); }; js objects can also be treated as string-indexed arrays: // equivalent.
...we frequently need to use xpcom components in our code, so instead of doing this: this.obsservice = components.classes["@mozilla.org/observer-service;1"].getservice(components.interfaces.
nsiobserverservice); it's better to do this: this.obsservice = cc["@mozilla.org/observer-service;1"].getservice(ci.
nsiobserverservice); these 2 constants don't need to be defined in the overlay because they are already defined globally in the browser.js file in firefox.
Supporting per-window private browsing - Archive of obsolete content
function pbobserver() { /* clear private data */ } var os = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); os.addobserver(pbobserver, "last-pb-context-exited", false); preventing a private session from ending if there are unfinished transactions involving private data, where the transactions will be terminated by the ending of a private session, an add-on can vote to prevent the session from ending (prompting the user is recommended).
...var os = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); os.addobserver(function (asubject, atopic, adata) { asubject.queryinterface(components.interfaces.nsisupportsprbool); // if another extension has not already canceled entering the private mode if (!asubject.data) { /* you should display some user interface here */ asubject.data = true; // cancel the operation } }, "last-pb-context-exiting", false); forcing a channel into private mode usually, network channels inherit the privacy status of the document that created them, which means that they work correctly most of the tim...
Supporting private browsing mode - Archive of obsolete content
function privatebrowsinglistener() { this.init(); } privatebrowsinglistener.prototype = { _os: null, _inprivatebrowsing: false, // whether we are in private browsing mode _watcher: null, // the watcher object init : function () { this._inited = true; this._os = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); this._os.addobserver(this, "private-browsing", false); this._os.addobserver(this, "quit-application", false); try { var pbs = components.classes["@mozilla.org/privatebrowsing;1"] .getservice(components.interfaces.nsiprivatebrowsingservice); this._inprivatebrowsing = pbs.privatebrowsingenabled; } catch(ex) { // ignore exceptions in olde...
... to do this, simply watch for the private browsing private-browsing-cancel-vote notification with the subject "exit", and set its data field to true to cancel the operation, like this: var os = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); os.addobserver(function (asubject, atopic, adata) { asubject.queryinterface(components.interfaces.nsisupportsprbool); // if another extension has not already canceled entering the private mode if (!asubject.data) { if (adata == "exit") { // if we are leaving the private mode /* you should display some user interface here */ asubject.data = true; // cancel the o...
Creating Sandboxed HTTP Connections
the observer service (
nsiobserverservice) is used to send general notifications, including the two cookie ones.
...var observerservice = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); observerservice.addobserver(listener, "http-on-modify-request", false); observerservice.addobserver(listener, "http-on-examine-response", false); the final piece is to manipulate the cookies.
Components.classes
to access a service (a singleton component, only single instance of which exists at any time), you should use getservice instead of createinstance: var os = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); the first time anyone accesses a service, the corresponding component is created under the hood.
... shortcuts it's a common practice to abbreviate components.classes and components.interfaces by storing a reference to the object as a constant: const cc = components.classes, ci = components.interfaces; var os = cc["@mozilla.org/observer-service;1"] .getservice(ci.
nsiobserverservice); a less known trick, useful when creating multiple instances of the same component, is to use the new operator on the class object: var clazz = components.classes["@mozilla.org/supports-array;1"]; var inst = new clazz(components.interfaces.nsisupportsarray); this implicitly calls the createinstance() method for you.
nsIPlacesImportExportService
three
nsiobserverservice notifications are fired as a result of the import.
... three
nsiobserverservice notifications are fired as a result of the import.
nsIPushService
you can use the global
nsiobserverservice to listen for these notifications.
... example const obsservice = cc["@mozilla.org/observer-service;1"] .getservice(ci.
nsiobserverservice); let pushobserver = { scope: "chrome://my-module/push", observe(subject, topic, data) { // the scope is passed as the `data` argument for `push-message` and // `push-subscription-change` observers.
/loader - Archive of obsolete content
getservice(ci.
nsiobserverservice); let observer = { observe: function onunload(subject, topic, reason) { // if this module is unloaded then `subject.wrappedjsobject` will be // `unloadsubject`.
platform/xpcom - Archive of obsolete content
getservice(ci.
nsiobserverservice); var starobserver = class({ extends: unknown, interfaces: [ 'nsiobserver' ], topic: '*', register: function register() { observerservice.addobserver(this, this.topic, false); }, unregister: function() { observerservice.removeobserver(this, this.topic); }, observe: function observe(subject, topic, data) { console.log('star observer:', subject, topic, data); } }); ...
Miscellaneous - Archive of obsolete content
put this code in the components/certsservice.js file: const cc = components.classes; const ci = components.interfaces; components.utils.import("resource://gre/modules/xpcomutils.jsm"); const gobserver = cc['@mozilla.org/observer-service;1'].getservice(ci.
nsiobserverservice); const gioservice = cc["@mozilla.org/network/io-service;1"].getservice(ci.nsiioservice); function certsservice() {} certsservice.prototype = { observe: function(asubject, atopic, adata) { switch(atopic) { case "app-startup": gobserver.addobserver(this,"xpcom-shutdown",false); gobserver.addobserver(this,"final-ui-startup",false); ...
XPCOM Objects - Archive of obsolete content
this.obsservice = cc["@mozilla.org/observer-service;1"].getservice(ci.
nsiobserverservice); the cc object (components.classes) is an index to static objects and class definitions available through xpcom.
Communicating with frame scripts
unction myobserver() { } myobserver.prototype = { observe: function(subject, topic, data) { var index = messagemanagers.indexof(subject); if (index != -1) { console.log("one of our message managers disconnected"); mms.splice(index, 1); } }, register: function() { var observerservice = cc["@mozilla.org/observer-service;1"] .getservice(ci.
nsiobserverservice); observerservice.addobserver(this, "message-manager-disconnect", false); console.log("listening"); }, unregister: function() { var observerservice = cc["@mozilla.org/observer-service;1"] .getservice(ci.
nsiobserverservice); observerservice.removeobserver(this, "message-manager-disconnect"); } } var observer = new myobserver(); observer.register()...
Services.jsm
ffectivetld service focus nsifocusmanager focus manager io nsiioservice nsiioservice2 i/o service locale nsilocaleservice locale service logins nsiloginmanager password manager service metro nsiwinmetroutils 2 mm nsimessagebroadcaster nsiframescriptloader global frame message manager3 obs
nsiobserverservice observer service perms nsipermissionmanager permission manager service ppmm nsimessagebroadcaster nsiprocessscriptloader global parent process message manager3 prefs nsiprefbranch nsiprefbranch2 nsiprefservice preferences service prompt nsipromptservice prompt service scriptloader mozijssubscriptloader javas...
Avoiding leaks in JavaScript XPCOM components
consider the example of bug 170022 (which also demonstrates a leak via a global variable, fixed later in bug 231266): const observer = { observe: function(subject, topic, data) { if (topic != "open-new-tab-request" || subject != window) return; delayedopentab(data); } }; const service = components.classes["@mozilla.org/observer-service;1"] .getservice(components.interfaces.
nsiobserverservice); service.addobserver(observer, "open-new-tab-request", false); in this example, there is a similar cycle between observe and service.
mozilla::services namespace
for example, to obtain a reference to the ioservice: nscomptr<nsiioservice> ioservice = mozilla::services::getioservice(); provided service getters service accessor service interface service name getchromeregistryservice nsichromeregistryservice chrome registry service getioservice nsiioservice io service getobserverservice
nsiobserverservice observer service getstringbundleservice nsistringbundleservice string bundle service gettoolkitchromeregistryservice nsitoolkitchromeregistry toolkit chrome registry service getxulchromeregistryservice nsixulchromeregistry xul chrome registry service getxuloverlayproviderservice nsixuloverlayprovider xul overlay provider service more services will be added as needed.
nsObserverService
class id d07f5195-e3d1-11d2-8acd-00105a1b8860 contractid @mozilla.org/observer-service;1 supported interfaces
nsiobserverservice remarks this component is a singleton and should therefore be accessed via the xpcom service manager.
nsIAccessibleEvent
example to listen to in-process accessibility invents, make your object an nsiobserver, and listen for accessible-event by using code something like this: nscomptr<
nsiobserverservice> observerservice = do_getservice("@mozilla.org/observer-service;1", &rv); if (ns_succeeded(rv)) { rv = observerservice->addobserver(this, "accessible-event", pr_true); } ...
nsIHttpHeaderVisitor
; if ( avisitor.isflash( ) ) { uri = subject.uri; subject.cancel( this.aborted ); alert( "found flash!" ); //handle flash file here } } }; myhttprequestobserver.prototype.register = function ( ) { var observerservice = components.classes[ "@mozilla.org/observer-service;1" ].getservice( components.interfaces.
nsiobserverservice ); observerservice.addobserver(this, "http-on-examine-response", false); observerservice.addobserver(this, "http-on-examine-cached-response", false); }; myhttprequestobserver.prototype.unregister = function ( ) { var observerservice = components.classes[ "@mozilla.org/observer-service;1" ].getservice( components.interfaces.
nsiobserverservice ); observerservice.rem...
addObserver
this content is now available at
nsiobserverservice.addobserver().
enumerateObservers
this content is now available at
nsiobserverservice.enumerateobservers().
notifyObservers
this content is now available at
nsiobserverservice.notifyobservers().
removeObserver
this content is now available at
nsiobserverservice.removeobserver().
nsIWindowWatcher
note: this method may dispatch a "toplevel-window-ready" notification via
nsiobserverservice if the window did not already exist.
XPCOM Interface Reference
chcallbacknsinavhistorycontainerresultnodensinavhistoryfullvisitresultnodensinavhistoryobservernsinavhistoryquerynsinavhistoryqueryoptionsnsinavhistoryqueryresultnodensinavhistoryresultnsinavhistoryresultnodensinavhistoryresultobservernsinavhistoryresulttreeviewernsinavhistoryresultviewobservernsinavhistoryresultviewernsinavhistoryservicensinavhistoryvisitresultnodensinetworklinkservicensiobserver
nsiobserverservicensioutputstreamnsioutputstreamcallbacknsiparentalcontrolsservicensiparserutilsnsipasswordnsipasswordmanagernsipermissionnsipermissionmanagernsipipensiplacesimportexportservicensiplacesviewnsipluginhostnsiprefbranch2nsipreflocalizedstringnsiprefservicensiprincipalnsiprinterenumeratornsiprintingpromptnsiprivatebrowsingservicensiprocessnsiprocess2nsiprocessscriptloadernsiprofilensiprofilelocknsiprofi...
XPCOM Interface Reference by grouping
nsithreadmanager nsithreadobserver nsithreadpool nsithreadpoollistener nsitoolkit util nsiversioncomparator nsiweakreference nsifactory nsiinterfacerequestor nsijscid nsijsid nsijsiid nsimodule nsiobserver
nsiobserverservice nsiproperties nsiproperty nsipropertybag nsipropertybag2 nsipropertyelement nsiserversocket nsiserversocketlistener nsiservicemanager nsisocketprovider nsisocketproviderservice nsisockettransport nsisockettransportservice nsisupports nsiuuidgenerator debug ns...
XPCOM
unless otherwise noted you register for the topics using the
nsiobserverservice.setting http request headershttp is one of the core technologies behind the web.
Creating a Custom Column
window.addeventlistener("load", doonceloaded, false); function doonceloaded() { var observerservice = components.classes["@mozilla.org/observer-service;1"].getservice(components.interfaces.
nsiobserverservice); observerservice.addobserver(createdbobserver, "msgcreatedbview", false); } var createdbobserver = { // components.interfaces.nsiobserver observe: function(amsgfolder, atopic, adata) { addcustomcolumnhandler(); } } in this example we have a function addcustomcolumnhandler() that is called whenever the event is fired.
customDBHeaders Preference
lumnhandler); dump("column handler being added: " + columnhandler + "\n"); } var createdbobserver = { // components.interfaces.nsiobserver observe: function(amsgfolder, atopic, adata) { dump("here here!"); addcustomcolumnhandler(); } } function doonceloaded(){ var observerservice = components.classes["@mozilla.org/observer-service;1"].getservice(components.interfaces.
nsiobserverservice); observerservice.addobserver(createdbobserver, "msgcreatedbview", false); window.document.getelementbyid('foldertree').addeventlistener("select",addcustomcolumnhandler,false); } window.addeventlistener("load",doonceloaded,false); dump(" ~ ~ ~ ~ end superfluous ~ ~ ~ ~ \n"); important be aware that only messages that are added to the .msf database after the customdbheaders pref is set w...
Web Console remoting - Firefox Developer Tools
ivate": false, "arguments": [ "error omg aloha ", { "type": "object", "classname": "htmlbodyelement", "actor": "conn0.consoleobj20" }, " 960 739 3.141592653589793 %a", "zuzu", { "type": "null" }, { "type": "undefined" } ] } } similar to how we send the page errors, here we send the actual console event received from the
nsiobserverservice.