Firefox Plugin : add Observer xpcom-startup

    技术2025-05-25  18

    1.

    static NS_METHOD ModuleRegister(nsIComponentManager *aCompMgr,nsIFile *aPath, const char *lpRegisterLocation        , const char *lpComponentType, const nsModuleComponentInfo * aInfo)

    {

     nsresult rv = NS_ERROR_FAILURE;

     nsCOMPtr<nsIServiceManager> spSvrMan = do_QueryInterface((nsISupports*)aCompMgr, &rv); if(NS_FAILED(rv)) {  return rv; }

     nsCOMPtr<nsICategoryManager> spCatMan; spSvrMan->GetServiceByContractID(NS_CATEGORYMANAGER_CONTRACTID, NS_GET_IID(nsICategoryManager)  , getter_AddRefs(spCatMan)); if(NS_FAILED(rv)) {  return rv; }

     char *lpPrevious = nsnull;

     rv = spCatMan->AddCategoryEntry("xpcom-startup", HW_COMPONENT_CLASSNAME, HW_COMPONENT_CONTRACTID  , PR_TRUE, PR_TRUE, &lpPrevious); if(lpPrevious != NULL) {  nsMemory::Free(lpPrevious);  lpPrevious = nsnull; }

    }

     

    2.

    NS_IMPL_ISUPPORTSn(CSSOFfPlugin, nsIObserver...)

     

    3.

    class CSSOFfPlugin : public nsIObserver...

    {public: CSSOFfPlugin(); ~CSSOFfPlugin();

    public: NS_DECL_ISUPPORTS NS_DECL_NSIOBSERVER};

    4.

    NS_IMETHODIMP CSSOFfPlugin::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData){ OutputDebugStringA("CSSOFfPlugin::Observe"); OutputDebugStringA(aTopic);

     if(strcmp(aTopic, "xpcom-startup") == 0) {  do   {   nsCOMPtr<nsIServiceManager> spSvrMan;   NS_GetServiceManager(getter_AddRefs(spSvrMan));   if(spSvrMan == NULL)    break;   nsCOMPtr<nsIObserverService> spObsvSvr;   spSvrMan->GetServiceByContractID(NS_OBSERVERSERVICE_CONTRACTID, NS_GET_IID(nsIObserverService)    , getter_AddRefs(spObsvSvr));   if(spObsvSvr == NULL)    break;   spObsvSvr->AddObserver((nsIObserver*)this, "quit-application", PR_FALSE);

      } while (0); }else if(strcmp(aTopic, "quit-application") == 0) {  do   {   nsCOMPtr<nsIServiceManager> spSvrMan;   NS_GetServiceManager(getter_AddRefs(spSvrMan));   if(spSvrMan == NULL)    break;   nsCOMPtr<nsIObserverService> spObsvSvr;   spSvrMan->GetServiceByContractID(NS_OBSERVERSERVICE_CONTRACTID, NS_GET_IID(nsIObserverService)    , getter_AddRefs(spObsvSvr));   if(spObsvSvr == NULL)    break;   spObsvSvr->RemoveObserver((nsIObserver*)this, "quit-application");

      } while (0); }

     return NS_OK;}

    最新回复(0)