2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-05-27 12:50:52 +04:00
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsISupports.h"
|
2001-09-07 01:13:11 +04:00
|
|
|
#include "nsIComponentManager.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIObserver.h"
|
2013-04-08 22:35:31 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2005-11-08 21:17:49 +03:00
|
|
|
#include "nsStringGlue.h"
|
1999-12-07 17:06:36 +03:00
|
|
|
#include "nsWeakReference.h"
|
2005-11-08 21:17:49 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2012-06-20 07:41:56 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2005-11-08 21:17:49 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2013-10-11 00:42:16 +04:00
|
|
|
static nsIObserverService *anObserverService = nullptr;
|
1999-05-29 04:51:17 +04:00
|
|
|
|
|
|
|
static void testResult( nsresult rv ) {
|
|
|
|
if ( NS_SUCCEEDED( rv ) ) {
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("...ok\n");
|
1999-05-29 04:51:17 +04:00
|
|
|
} else {
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("...failed, rv=0x%x\n", (int)rv);
|
1999-05-29 04:51:17 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
void printString(nsString &str) {
|
2007-08-08 05:47:40 +04:00
|
|
|
printf("%s", NS_ConvertUTF16toUTF8(str).get());
|
1999-05-29 04:51:17 +04:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TestObserver final : public nsIObserver,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsSupportsWeakReference
|
2012-06-20 07:41:56 +04:00
|
|
|
{
|
1999-05-29 04:51:17 +04:00
|
|
|
public:
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit TestObserver( const nsAString &name )
|
1999-05-29 04:51:17 +04:00
|
|
|
: mName( name ) {
|
|
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-08-22 13:18:49 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
2004-01-15 09:14:18 +03:00
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
nsString mName;
|
2004-01-15 09:14:18 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
~TestObserver() {}
|
1999-05-29 04:51:17 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS( TestObserver, nsIObserver, nsISupportsWeakReference )
|
1999-05-29 04:51:17 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestObserver::Observe( nsISupports *aSubject,
|
2001-10-20 00:52:59 +04:00
|
|
|
const char *aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *someData ) {
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCString topic( aTopic );
|
1999-05-29 04:51:17 +04:00
|
|
|
nsString data( someData );
|
1999-12-07 17:46:35 +03:00
|
|
|
/*
|
|
|
|
The annoying double-cast below is to work around an annoying bug in
|
|
|
|
the compiler currently used on wensleydale. This is a test.
|
|
|
|
*/
|
2004-02-07 05:41:10 +03:00
|
|
|
printString(mName);
|
|
|
|
printf(" has observed something: subject@%p", (void*)aSubject);
|
|
|
|
printf(" name=");
|
2007-07-08 11:08:04 +04:00
|
|
|
printString(reinterpret_cast<TestObserver*>(reinterpret_cast<void*>(aSubject))->mName);
|
2004-02-07 05:41:10 +03:00
|
|
|
printf(" aTopic=%s", topic.get());
|
|
|
|
printf(" someData=");
|
|
|
|
printString(data);
|
|
|
|
printf("\n");
|
1999-05-29 04:51:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCString topicA; topicA.Assign( "topic-A" );
|
|
|
|
nsCString topicB; topicB.Assign( "topic-B" );
|
1999-04-07 00:54:09 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2004-11-01 21:50:36 +03:00
|
|
|
nsresult res = CallCreateInstance("@mozilla.org/observer-service;1", &anObserverService);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-27 12:50:52 +04:00
|
|
|
if (res == NS_OK) {
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2001-11-14 04:33:42 +03:00
|
|
|
nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
|
1999-05-29 04:51:17 +04:00
|
|
|
aObserver->AddRef();
|
2001-11-14 04:33:42 +03:00
|
|
|
nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
|
1999-05-29 04:51:17 +04:00
|
|
|
bObserver->AddRef();
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Adding Observer-A as observer of topic-A...\n");
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = anObserverService->AddObserver(aObserver, topicA.get(), false);
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Adding Observer-B as observer of topic-A...\n");
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = anObserverService->AddObserver(bObserver, topicA.get(), false);
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Adding Observer-B as observer of topic-B...\n");
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = anObserverService->AddObserver(bObserver, topicB.get(), false);
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Testing Notify(observer-A, topic-A)...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->NotifyObservers( aObserver,
|
2001-06-30 15:02:25 +04:00
|
|
|
topicA.get(),
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("Testing Notify(observer-A, topic-A)") );
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Testing Notify(observer-B, topic-B)...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->NotifyObservers( bObserver,
|
2001-06-30 15:02:25 +04:00
|
|
|
topicB.get(),
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("Testing Notify(observer-B, topic-B)") );
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Testing EnumerateObserverList (for topic-A)...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
|
|
|
rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
|
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Enumerating observers of topic-A...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
if ( e ) {
|
|
|
|
nsCOMPtr<nsIObserver> observer;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool loop = true;
|
2001-10-20 00:52:59 +04:00
|
|
|
while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
|
|
|
|
{
|
2014-03-15 23:00:17 +04:00
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
e->GetNext(getter_AddRefs(supports));
|
|
|
|
observer = do_QueryInterface(supports);
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Calling observe on enumerated observer ");
|
2007-07-08 11:08:04 +04:00
|
|
|
printString(reinterpret_cast<TestObserver*>
|
|
|
|
(reinterpret_cast<void*>(observer.get()))->mName);
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = observer->Observe( observer,
|
|
|
|
topicA.get(),
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("during enumeration") );
|
2001-10-20 00:52:59 +04:00
|
|
|
testResult(rv);
|
|
|
|
}
|
1999-05-27 12:50:52 +04:00
|
|
|
}
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("...done enumerating observers of topic-A\n");
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Removing Observer-A...\n");
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = anObserverService->RemoveObserver(aObserver, topicA.get());
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
1999-04-20 18:50:47 +04:00
|
|
|
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Removing Observer-B (topic-A)...\n");
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = anObserverService->RemoveObserver(bObserver, topicB.get());
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Removing Observer-B (topic-B)...\n");
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = anObserverService->RemoveObserver(bObserver, topicA.get());
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
1999-05-27 12:50:52 +04:00
|
|
|
|
|
|
|
}
|
2012-08-07 19:11:35 +04:00
|
|
|
return 0;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|