1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
14 # The Original Code is Google Safe Browsing.
16 # The Initial Developer of the Original Code is Google Inc.
17 # Portions created by the Initial Developer are Copyright (C) 2006
18 # the Initial Developer. All Rights Reserved.
21 # Fritz Schneider <fritz@google.com> (original author)
23 # Alternatively, the contents of this file may be used under the terms of
24 # either the GNU General Public License Version 2 or later (the "GPL"), or
25 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 # in which case the provisions of the GPL or the LGPL are applicable instead
27 # of those above. If you wish to allow use of your version of this file only
28 # under the terms of either the GPL or the LGPL, and not to allow others to
29 # use your version of this file under the terms of the MPL, indicate your
30 # decision by deleting the provisions above and replace them with the notice
31 # and other provisions required by the GPL or the LGPL. If you do not delete
32 # the provisions above, a recipient may use your version of this file under
33 # the terms of any one of the MPL, the GPL or the LGPL.
35 # ***** END LICENSE BLOCK *****
38 // A couple of classes to simplify creating observers.
42 // function doSomething() { ... }
43 // var observer = new G_ObserverWrapper(topic, doSomething);
44 // someObj.addObserver(topic, observer);
48 // function doSomething() { ... }
49 // new G_ObserverServiceObserver("profile-after-change",
51 // true /* run only once */);
55 * This class abstracts the admittedly simple boilerplate required of
56 * an nsIObserver. It saves you the trouble of implementing the
57 * indirection of your own observe() function.
59 * @param topic String containing the topic the observer will filter for
61 * @param observeFunction Reference to the function to call when the
66 function G_ObserverWrapper(topic, observeFunction) {
67 this.debugZone = "observer";
69 this.observeFunction_ = observeFunction;
75 G_ObserverWrapper.prototype.QueryInterface = function(iid) {
76 if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserver))
78 throw Components.results.NS_ERROR_NO_INTERFACE;
82 * Invoked by the thingy being observed
84 G_ObserverWrapper.prototype.observe = function(subject, topic, data) {
85 if (topic == this.topic_)
86 this.observeFunction_(subject, topic, data);
91 * This class abstracts the admittedly simple boilerplate required of
92 * observing an observerservice topic. It implements the indirection
93 * required, and automatically registers to hear the topic.
95 * @param topic String containing the topic the observer will filter for
97 * @param observeFunction Reference to the function to call when the
100 * @param opt_onlyOnce Boolean indicating if the observer should unregister
105 function G_ObserverServiceObserver(topic, observeFunction, opt_onlyOnce) {
106 this.debugZone = "observerserviceobserver";
108 this.observeFunction_ = observeFunction;
109 this.onlyOnce_ = !!opt_onlyOnce;
111 this.observer_ = new G_ObserverWrapper(this.topic_,
112 BindToObject(this.observe_, this));
113 this.observerService_ = Cc["@mozilla.org/observer-service;1"]
114 .getService(Ci.nsIObserverService);
115 this.observerService_.addObserver(this.observer_, this.topic_, false);
119 * Unregister the observer from the observerservice
121 G_ObserverServiceObserver.prototype.unregister = function() {
122 this.observerService_.removeObserver(this.observer_, this.topic_);
123 this.observerService_ = null;
127 * Invoked by the observerservice
129 G_ObserverServiceObserver.prototype.observe_ = function(subject, topic, data) {
130 this.observeFunction_(subject, topic, data);
136 function TEST_G_Observer() {
139 var z = "observer UNITTEST";
140 G_debugService.enableZone(z);
142 G_Debug(z, "Starting");
144 var regularObserverRan = 0;
145 var observerServiceObserverRan = 0;
147 function regularObserver() {
148 regularObserverRan++;
151 function observerServiceObserver() {
152 observerServiceObserverRan++;
155 var service = Cc["@mozilla.org/observer-service;1"]
156 .getService(Ci.nsIObserverService);
157 var topic = "google-observer-test";
159 var o1 = new G_ObserverWrapper(topic, regularObserver);
160 service.addObserver(o1, topic, false);
162 new G_ObserverServiceObserver(topic,
163 observerServiceObserver, true /* once */);
165 // Notifications happen synchronously, so this is easy
166 service.notifyObservers(null, topic, null);
167 service.notifyObservers(null, topic, null);
169 G_Assert(z, regularObserverRan == 2, "Regular observer broken");
170 G_Assert(z, observerServiceObserverRan == 1, "ObsServObs broken");
172 service.removeObserver(o1, topic);
173 G_Debug(z, "PASSED");