xpcom/glue/Observer.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef mozilla_Observer_h
michael@0 7 #define mozilla_Observer_h
michael@0 8
michael@0 9 #include "nsTArray.h"
michael@0 10
michael@0 11 namespace mozilla {
michael@0 12
michael@0 13 /**
michael@0 14 * Observer<T> provides a way for a class to observe something.
michael@0 15 * When an event has to be broadcasted to all Observer<T>, Notify() method
michael@0 16 * is called.
michael@0 17 * T represents the type of the object passed in argument to Notify().
michael@0 18 *
michael@0 19 * @see ObserverList.
michael@0 20 */
michael@0 21 template <class T>
michael@0 22 class Observer
michael@0 23 {
michael@0 24 public:
michael@0 25 virtual ~Observer() { }
michael@0 26 virtual void Notify(const T& aParam) = 0;
michael@0 27 };
michael@0 28
michael@0 29 /**
michael@0 30 * ObserverList<T> tracks Observer<T> and can notify them when Broadcast() is
michael@0 31 * called.
michael@0 32 * T represents the type of the object passed in argument to Broadcast() and
michael@0 33 * sent to Observer<T> objects through Notify().
michael@0 34 *
michael@0 35 * @see Observer.
michael@0 36 */
michael@0 37 template <class T>
michael@0 38 class ObserverList
michael@0 39 {
michael@0 40 public:
michael@0 41 /**
michael@0 42 * Note: When calling AddObserver, it's up to the caller to make sure the
michael@0 43 * object isn't going to be release as long as RemoveObserver hasn't been
michael@0 44 * called.
michael@0 45 *
michael@0 46 * @see RemoveObserver()
michael@0 47 */
michael@0 48 void AddObserver(Observer<T>* aObserver) {
michael@0 49 mObservers.AppendElement(aObserver);
michael@0 50 }
michael@0 51
michael@0 52 /**
michael@0 53 * Remove the observer from the observer list.
michael@0 54 * @return Whether the observer has been found in the list.
michael@0 55 */
michael@0 56 bool RemoveObserver(Observer<T>* aObserver) {
michael@0 57 return mObservers.RemoveElement(aObserver);
michael@0 58 }
michael@0 59
michael@0 60 uint32_t Length() {
michael@0 61 return mObservers.Length();
michael@0 62 }
michael@0 63
michael@0 64 void Broadcast(const T& aParam) {
michael@0 65 uint32_t size = mObservers.Length();
michael@0 66 for (uint32_t i=0; i<size; ++i) {
michael@0 67 mObservers[i]->Notify(aParam);
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 protected:
michael@0 72 nsTArray<Observer<T>*> mObservers;
michael@0 73 };
michael@0 74
michael@0 75 } // namespace mozilla
michael@0 76
michael@0 77 #endif // mozilla_Observer_h

mercurial