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...


_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to