Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem deploying nsIObserver in C++ and js

16 views
Skip to first unread message

Eric

unread,
Jul 31, 2006, 11:02:52 PM7/31/06
to
dear all:
I'm trying to use nsIObserver in C++ and JS ,in order to make callbacks just
through observerservice .everything goes fine except for this :
1.in C++:
I defined a new interface IMyComponent which inherits from nsIObserver,not
directly from nsISupports,because i want to implement my observe().when exec
xpidl.exe,it doesn't emit Observe() into IMyComponent.h .so i have to copy
it from nsIObserver.h .and then i defined a new class
MyComponent:IMyComponent .
/////////////////////////////////////////////
IMyComponent.idl
////////////////////////////////////////////
#include "nsISupports.idl"
#include "nsIObserver.idl"

[scriptable, uuid(90758A97-A6F3-4ea4-8953-16BD2EE3A977)]
interface IMyComponent : nsIObserver //nsISupports
{
void NotifyMe();
};

/////////////////////////////////////////////
MyComponent.CPP

///////////////////////////////////////////////////
#include "StdAfx.h"
#include "MyComponent.h"

NS_IMPL_ISUPPORTS1(MyComponent, IMyComponent)

MyComponent::MyComponent()
{
NS_GetServiceManager(getter_AddRefs(m_pServiceManager));
m_pServiceManager->GetServiceByContractID("@mozilla.org/observer-service;1",
NS_GET_IID(nsIObserverService), getter_AddRefs(m_pObserverService));

m_pObserverService->AddObserver(this,SM_TOPIC_TEST,false);
m_pObserverService->NotifyObservers(NULL, SM_TOPIC_TEST, L"SM-XPCOM:
notify all");
}

MyComponent::~MyComponent()
{
/* destructor code */
m_pObserverService->RemoveObserver(this,SM_TOPIC_TEST);
}

/* void NotifyMe (); */
NS_IMETHODIMP MyComponent::NotifyMe()
{
MessageBox(0,"You must have pushed notify button on xul","from
MyComponent::NotifyMe()",0);
if(m_pObserverService){
m_pObserverService->NotifyObservers(NULL,SM_TOPIC_TEST,L"from dll");
m_pObserverService->NotifyObservers(this,SM_TOPIC_TEST,L"from
MyComponent::this");
MyComponent* x = new MyComponent();
m_pObserverService->AddObserver(x,SM_TOPIC_TEST,false);
m_pObserverService->NotifyObservers(x,SM_TOPIC_TEST,L"from MyComponent's
child: m_pObServer");
}
else
MessageBox(0,"null observer","from dll",0);
return NS_OK;
}

/* void observe (in nsISupports aSubject, in string aTopic, in wstring
aData); */
NS_IMETHODIMP MyComponent::Observe(nsISupports *aSubject, const char
*aTopic, const PRUnichar *aData)
{
MessageBox(0,"","data",0);
return NS_OK;
}
//////////////////////////////////////////////////////

well ,compiling and linking are all ok ,when executes ,the result is : only
my JS observers get the topic ,moreover ,
JS received all the topics which are soppused to be sent to MyComponent and
x :

1. m_pObserverService->NotifyObservers(NULL,SM_TOPIC_TEST,L"from dll");
2. m_pObserverService->NotifyObservers(x,SM_TOPIC_TEST,L"from
MyComponent's child: m_pObServer");
3. m_pObserverService->NotifyObservers(this,SM_TOPIC_TEST,L"from
MyComponent::this");
above : x and this are inherited from nsIObserver ,why cannot they behave as
Observer ? why can JS's observer take over the topics that are sent to (x
and this ) ?

Thank you all for reading and answer me :P I'm puzzled...


Boris Zbarsky

unread,
Aug 1, 2006, 1:57:30 AM8/1/06
to
Eric wrote:
> NS_IMPL_ISUPPORTS1(MyComponent, IMyComponent)

This should probably be NS_IMPL_ISUPPORTS2(MyComponent, IMyComponent,
nsIObserver). Otherwise your component doesn't QI to nsIObserver.

Also, I strongly recommend NOT inheriting your own interfaces from interfaces
you do not control. It causes problems later if mozilla.org creates an
interface inheriting from nsIObserver and you want to implement both that and
IMyComponent. There's no technical reason to have IMyComponent inherit from
nsIObserver that I can see....

-Boris

Eric

unread,
Aug 1, 2006, 3:12:58 AM8/1/06
to
I made the changes according to your advice and now it works fine ! thank
you very much for the big help out !
as for your suggestion ,i don't fully understand ,cause my goal is to
implement an independant application based on xpcom ,with xulrunner
and i want my xpcom --the MyComponent.dll also be able to response to the
notifications both from dll and from javascript .
if i don't inherit from nsIObserver ,then i don't know if there is another
way to implement this ? (I already used the callbacks ,which is too
unconvenient ,because everytime you want to add a new callback you have to
modify your codes ,so i switched to nsIObserver )

Benjamin Smedberg

unread,
Aug 1, 2006, 9:22:12 AM8/1/06
to
Eric wrote:

> if i don't inherit from nsIObserver ,then i don't know if there is another
> way to implement this ? (I already used the callbacks ,which is too
> unconvenient ,because everytime you want to add a new callback you have to
> modify your codes ,so i switched to nsIObserver )

Your component can implement multiple independent interfaces:

class MyComponent : public nsIObserver, public IMyComponent
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
NS_DECL_IMYCOMPONENT
};

Boris Zbarsky

unread,
Aug 1, 2006, 10:55:53 AM8/1/06
to
Eric wrote:
> as for your suggestion ,i don't fully understand ,cause my goal is to
> implement an independant application based on xpcom ,with xulrunner
> and i want my xpcom --the MyComponent.dll also be able to response to the
> notifications both from dll and from javascript .
> if i don't inherit from nsIObserver

Your _component_ should inherit from nsIObserver. Your _interface_ should not.
C++ supports multiple inheritance; use it here. ;)

-Boris

Eric

unread,
Aug 1, 2006, 11:34:07 AM8/1/06
to
now i see...lol :p ,thanks for the explainations .

"Boris Zbarsky" <bzba...@mit.edu> wrote in message
news:I-SdnePwDMrk9lLZ...@mozilla.org...

0 new messages