1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/url-classifier/content/moz/observer.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +# This Source Code Form is subject to the terms of the Mozilla Public 1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + 1.8 + 1.9 +// A couple of classes to simplify creating observers. 1.10 +// 1.11 +// // Example1: 1.12 +// 1.13 +// function doSomething() { ... } 1.14 +// var observer = new G_ObserverWrapper(topic, doSomething); 1.15 +// someObj.addObserver(topic, observer); 1.16 +// 1.17 +// // Example2: 1.18 +// 1.19 +// function doSomething() { ... } 1.20 +// new G_ObserverServiceObserver("profile-after-change", 1.21 +// doSomething, 1.22 +// true /* run only once */); 1.23 + 1.24 + 1.25 +/** 1.26 + * This class abstracts the admittedly simple boilerplate required of 1.27 + * an nsIObserver. It saves you the trouble of implementing the 1.28 + * indirection of your own observe() function. 1.29 + * 1.30 + * @param topic String containing the topic the observer will filter for 1.31 + * 1.32 + * @param observeFunction Reference to the function to call when the 1.33 + * observer fires 1.34 + * 1.35 + * @constructor 1.36 + */ 1.37 +function G_ObserverWrapper(topic, observeFunction) { 1.38 + this.debugZone = "observer"; 1.39 + this.topic_ = topic; 1.40 + this.observeFunction_ = observeFunction; 1.41 +} 1.42 + 1.43 +/** 1.44 + * XPCOM 1.45 + */ 1.46 +G_ObserverWrapper.prototype.QueryInterface = function(iid) { 1.47 + if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserver)) 1.48 + return this; 1.49 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.50 +} 1.51 + 1.52 +/** 1.53 + * Invoked by the thingy being observed 1.54 + */ 1.55 +G_ObserverWrapper.prototype.observe = function(subject, topic, data) { 1.56 + if (topic == this.topic_) 1.57 + this.observeFunction_(subject, topic, data); 1.58 +} 1.59 + 1.60 + 1.61 +/** 1.62 + * This class abstracts the admittedly simple boilerplate required of 1.63 + * observing an observerservice topic. It implements the indirection 1.64 + * required, and automatically registers to hear the topic. 1.65 + * 1.66 + * @param topic String containing the topic the observer will filter for 1.67 + * 1.68 + * @param observeFunction Reference to the function to call when the 1.69 + * observer fires 1.70 + * 1.71 + * @param opt_onlyOnce Boolean indicating if the observer should unregister 1.72 + * after it has fired 1.73 + * 1.74 + * @constructor 1.75 + */ 1.76 +function G_ObserverServiceObserver(topic, observeFunction, opt_onlyOnce) { 1.77 + this.debugZone = "observerserviceobserver"; 1.78 + this.topic_ = topic; 1.79 + this.observeFunction_ = observeFunction; 1.80 + this.onlyOnce_ = !!opt_onlyOnce; 1.81 + 1.82 + this.observer_ = new G_ObserverWrapper(this.topic_, 1.83 + BindToObject(this.observe_, this)); 1.84 + this.observerService_ = Cc["@mozilla.org/observer-service;1"] 1.85 + .getService(Ci.nsIObserverService); 1.86 + this.observerService_.addObserver(this.observer_, this.topic_, false); 1.87 +} 1.88 + 1.89 +/** 1.90 + * Unregister the observer from the observerservice 1.91 + */ 1.92 +G_ObserverServiceObserver.prototype.unregister = function() { 1.93 + this.observerService_.removeObserver(this.observer_, this.topic_); 1.94 + this.observerService_ = null; 1.95 +} 1.96 + 1.97 +/** 1.98 + * Invoked by the observerservice 1.99 + */ 1.100 +G_ObserverServiceObserver.prototype.observe_ = function(subject, topic, data) { 1.101 + this.observeFunction_(subject, topic, data); 1.102 + if (this.onlyOnce_) 1.103 + this.unregister(); 1.104 +} 1.105 + 1.106 +#ifdef DEBUG 1.107 +function TEST_G_Observer() { 1.108 + if (G_GDEBUG) { 1.109 + 1.110 + var z = "observer UNITTEST"; 1.111 + G_debugService.enableZone(z); 1.112 + 1.113 + G_Debug(z, "Starting"); 1.114 + 1.115 + var regularObserverRan = 0; 1.116 + var observerServiceObserverRan = 0; 1.117 + 1.118 + function regularObserver() { 1.119 + regularObserverRan++; 1.120 + }; 1.121 + 1.122 + function observerServiceObserver() { 1.123 + observerServiceObserverRan++; 1.124 + }; 1.125 + 1.126 + var service = Cc["@mozilla.org/observer-service;1"] 1.127 + .getService(Ci.nsIObserverService); 1.128 + var topic = "google-observer-test"; 1.129 + 1.130 + var o1 = new G_ObserverWrapper(topic, regularObserver); 1.131 + service.addObserver(o1, topic, false); 1.132 + 1.133 + new G_ObserverServiceObserver(topic, 1.134 + observerServiceObserver, true /* once */); 1.135 + 1.136 + // Notifications happen synchronously, so this is easy 1.137 + service.notifyObservers(null, topic, null); 1.138 + service.notifyObservers(null, topic, null); 1.139 + 1.140 + G_Assert(z, regularObserverRan == 2, "Regular observer broken"); 1.141 + G_Assert(z, observerServiceObserverRan == 1, "ObsServObs broken"); 1.142 + 1.143 + service.removeObserver(o1, topic); 1.144 + G_Debug(z, "PASSED"); 1.145 + } 1.146 +} 1.147 +#endif