DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (adf7d49a44f8)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsObserverList.h"

#include "mozilla/ResultExtensions.h"
#include "nsAutoPtr.h"
#include "nsCOMArray.h"
#include "xpcpublic.h"

nsresult nsObserverList::AddObserver(nsIObserver* anObserver, bool ownsWeak) {
  NS_ASSERTION(anObserver, "Null input");

  MOZ_TRY(mObservers.AppendWeakElement(anObserver, ownsWeak));
  return NS_OK;
}

nsresult nsObserverList::RemoveObserver(nsIObserver* anObserver) {
  NS_ASSERTION(anObserver, "Null input");

  MOZ_TRY(mObservers.RemoveWeakElement(anObserver));
  return NS_OK;
}

void nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator) {
  RefPtr<nsObserverEnumerator> e(new nsObserverEnumerator(this));
  e.forget(anEnumerator);
}

void nsObserverList::FillObserverArray(nsCOMArray<nsIObserver>& aArray) {
  aArray.SetCapacity(mObservers.Length());

  nsMaybeWeakPtrArray<nsIObserver> observers(mObservers);

  for (int32_t i = observers.Length() - 1; i >= 0; --i) {
    nsCOMPtr<nsIObserver> observer = observers[i].GetValue();
    if (observer) {
      aArray.AppendObject(observer);
    } else {
      // the object has gone away, remove the weakref
      mObservers.RemoveElementAt(i);
    }
  }
}

void nsObserverList::AppendStrongObservers(nsCOMArray<nsIObserver>& aArray) {
  aArray.SetCapacity(aArray.Length() + mObservers.Length());

  for (int32_t i = mObservers.Length() - 1; i >= 0; --i) {
    if (!mObservers[i].IsWeak()) {
      nsCOMPtr<nsIObserver> observer = mObservers[i].GetValue();
      aArray.AppendObject(observer);
    }
  }
}

void nsObserverList::NotifyObservers(nsISupports* aSubject, const char* aTopic,
                                     const char16_t* someData) {
  nsCOMArray<nsIObserver> observers;
  FillObserverArray(observers);

  for (int32_t i = 0; i < observers.Count(); ++i) {
    observers[i]->Observe(aSubject, aTopic, someData);
  }
}

nsObserverEnumerator::nsObserverEnumerator(nsObserverList* aObserverList)
    : mIndex(0) {
  aObserverList->FillObserverArray(mObservers);
}

NS_IMETHODIMP
nsObserverEnumerator::HasMoreElements(bool* aResult) {
  *aResult = (mIndex < mObservers.Count());
  return NS_OK;
}

NS_IMETHODIMP
nsObserverEnumerator::GetNext(nsISupports** aResult) {
  if (mIndex == mObservers.Count()) {
    return NS_ERROR_FAILURE;
  }

  NS_ADDREF(*aResult = mObservers[mIndex]);
  ++mIndex;
  return NS_OK;
}