toolkit/components/url-classifier/content/moz/observer.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4
michael@0 5
michael@0 6 // A couple of classes to simplify creating observers.
michael@0 7 //
michael@0 8 // // Example1:
michael@0 9 //
michael@0 10 // function doSomething() { ... }
michael@0 11 // var observer = new G_ObserverWrapper(topic, doSomething);
michael@0 12 // someObj.addObserver(topic, observer);
michael@0 13 //
michael@0 14 // // Example2:
michael@0 15 //
michael@0 16 // function doSomething() { ... }
michael@0 17 // new G_ObserverServiceObserver("profile-after-change",
michael@0 18 // doSomething,
michael@0 19 // true /* run only once */);
michael@0 20
michael@0 21
michael@0 22 /**
michael@0 23 * This class abstracts the admittedly simple boilerplate required of
michael@0 24 * an nsIObserver. It saves you the trouble of implementing the
michael@0 25 * indirection of your own observe() function.
michael@0 26 *
michael@0 27 * @param topic String containing the topic the observer will filter for
michael@0 28 *
michael@0 29 * @param observeFunction Reference to the function to call when the
michael@0 30 * observer fires
michael@0 31 *
michael@0 32 * @constructor
michael@0 33 */
michael@0 34 function G_ObserverWrapper(topic, observeFunction) {
michael@0 35 this.debugZone = "observer";
michael@0 36 this.topic_ = topic;
michael@0 37 this.observeFunction_ = observeFunction;
michael@0 38 }
michael@0 39
michael@0 40 /**
michael@0 41 * XPCOM
michael@0 42 */
michael@0 43 G_ObserverWrapper.prototype.QueryInterface = function(iid) {
michael@0 44 if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserver))
michael@0 45 return this;
michael@0 46 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 47 }
michael@0 48
michael@0 49 /**
michael@0 50 * Invoked by the thingy being observed
michael@0 51 */
michael@0 52 G_ObserverWrapper.prototype.observe = function(subject, topic, data) {
michael@0 53 if (topic == this.topic_)
michael@0 54 this.observeFunction_(subject, topic, data);
michael@0 55 }
michael@0 56
michael@0 57
michael@0 58 /**
michael@0 59 * This class abstracts the admittedly simple boilerplate required of
michael@0 60 * observing an observerservice topic. It implements the indirection
michael@0 61 * required, and automatically registers to hear the topic.
michael@0 62 *
michael@0 63 * @param topic String containing the topic the observer will filter for
michael@0 64 *
michael@0 65 * @param observeFunction Reference to the function to call when the
michael@0 66 * observer fires
michael@0 67 *
michael@0 68 * @param opt_onlyOnce Boolean indicating if the observer should unregister
michael@0 69 * after it has fired
michael@0 70 *
michael@0 71 * @constructor
michael@0 72 */
michael@0 73 function G_ObserverServiceObserver(topic, observeFunction, opt_onlyOnce) {
michael@0 74 this.debugZone = "observerserviceobserver";
michael@0 75 this.topic_ = topic;
michael@0 76 this.observeFunction_ = observeFunction;
michael@0 77 this.onlyOnce_ = !!opt_onlyOnce;
michael@0 78
michael@0 79 this.observer_ = new G_ObserverWrapper(this.topic_,
michael@0 80 BindToObject(this.observe_, this));
michael@0 81 this.observerService_ = Cc["@mozilla.org/observer-service;1"]
michael@0 82 .getService(Ci.nsIObserverService);
michael@0 83 this.observerService_.addObserver(this.observer_, this.topic_, false);
michael@0 84 }
michael@0 85
michael@0 86 /**
michael@0 87 * Unregister the observer from the observerservice
michael@0 88 */
michael@0 89 G_ObserverServiceObserver.prototype.unregister = function() {
michael@0 90 this.observerService_.removeObserver(this.observer_, this.topic_);
michael@0 91 this.observerService_ = null;
michael@0 92 }
michael@0 93
michael@0 94 /**
michael@0 95 * Invoked by the observerservice
michael@0 96 */
michael@0 97 G_ObserverServiceObserver.prototype.observe_ = function(subject, topic, data) {
michael@0 98 this.observeFunction_(subject, topic, data);
michael@0 99 if (this.onlyOnce_)
michael@0 100 this.unregister();
michael@0 101 }
michael@0 102
michael@0 103 #ifdef DEBUG
michael@0 104 function TEST_G_Observer() {
michael@0 105 if (G_GDEBUG) {
michael@0 106
michael@0 107 var z = "observer UNITTEST";
michael@0 108 G_debugService.enableZone(z);
michael@0 109
michael@0 110 G_Debug(z, "Starting");
michael@0 111
michael@0 112 var regularObserverRan = 0;
michael@0 113 var observerServiceObserverRan = 0;
michael@0 114
michael@0 115 function regularObserver() {
michael@0 116 regularObserverRan++;
michael@0 117 };
michael@0 118
michael@0 119 function observerServiceObserver() {
michael@0 120 observerServiceObserverRan++;
michael@0 121 };
michael@0 122
michael@0 123 var service = Cc["@mozilla.org/observer-service;1"]
michael@0 124 .getService(Ci.nsIObserverService);
michael@0 125 var topic = "google-observer-test";
michael@0 126
michael@0 127 var o1 = new G_ObserverWrapper(topic, regularObserver);
michael@0 128 service.addObserver(o1, topic, false);
michael@0 129
michael@0 130 new G_ObserverServiceObserver(topic,
michael@0 131 observerServiceObserver, true /* once */);
michael@0 132
michael@0 133 // Notifications happen synchronously, so this is easy
michael@0 134 service.notifyObservers(null, topic, null);
michael@0 135 service.notifyObservers(null, topic, null);
michael@0 136
michael@0 137 G_Assert(z, regularObserverRan == 2, "Regular observer broken");
michael@0 138 G_Assert(z, observerServiceObserverRan == 1, "ObsServObs broken");
michael@0 139
michael@0 140 service.removeObserver(o1, topic);
michael@0 141 G_Debug(z, "PASSED");
michael@0 142 }
michael@0 143 }
michael@0 144 #endif

mercurial