[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...
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
> 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
};
Your _component_ should inherit from nsIObserver. Your _interface_ should not.
C++ supports multiple inheritance; use it here. ;)
-Boris
"Boris Zbarsky" <bzba...@mit.edu> wrote in message
news:I-SdnePwDMrk9lLZ...@mozilla.org...