Identifying Tab in OnStateChange C++

Vaibhav <[email protected]> Mon, 19 Apr 2010 06:13:48 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.dom
Organization http://groups.google.com
Message-ID <0aba19c5-c579-4e40-adf0-f1b4ffa19a50@i37g2000yqn.googlegroups.com>
Hi,

I am writing Firefox extension using C++.
In OnStateChange method I want to determine the tab associated with
particular request.

Currently I compare DOM Document Pointers to do it.

Following is the Pseudocode:

OnStateChange()
{
	//==================================================
	//+
	// GET URL

	nsCString url;
	const char *pcszURL;
	aRequest->GetName(url);
	NS_CStringGetData(url, &pcszURL);

	//-
	//==================================================

	nsCOMPtr<nsIDOMWindow> DOMWin;
	rv = aWebProgress->GetDOMWindow(getter_AddRefs(DOMWin));
	nsCOMPtr<nsIDOMDocument> DOMDoc;
	rv = DOMWin->GetDocument(getter_AddRefs(DOMDoc));

	INT iTabIndex;
	nsCOMPtr<nsIWindowMediator> windowMediator =
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
	nsCOMPtr<nsIDOMWindowInternal> dwi;
	windowMediator->GetMostRecentWindow(L"navigator:browser",
getter_AddRefs(dwi));
	nsCOMPtr<nsIDOMDocument> doc;
	dwi->GetDocument(getter_AddRefs(doc));
	nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc));
	nsCOMPtr<nsIDOMElement> domEl;
	doc->GetElementById(NS_LITERAL_STRING("content"),
getter_AddRefs(domEl));
	nsCOMPtr<nsIDOMElement> pAnoEl;

	//
    // getting xul:tabpanels
	//
	xbl->GetAnonymousElementByAttribute(
										domEl,
										NS_LITERAL_STRING("anonid"),
NS_LITERAL_STRING("panelcontainer"),
										getter_AddRefs(pAnoEl)
										);
	nsString retval;
	PRBool bRet = 0;
	nsCOMPtr<nsIDOMNodeList> nodeList;
	pAnoEl->GetChildNodes(getter_AddRefs(nodeList));
	nsCOMPtr<nsIDOMNode> domNode;
	PRUint32 len = 0;
	rv = nodeList->GetLength(&len);
	for( PRUint32 i=0; i<len; i++ )
	{
		// getting the xul::notificationbox
		nsCOMPtr<nsIDOMNode> domNode;
		nodeList->Item(i, getter_AddRefs(domNode));
		// get the last child of the 'xul::notificationbox' which is the
'xul:browser'
		nsCOMPtr<nsIDOMNode> domXULBrowser;
		domNode->GetLastChild(getter_AddRefs(domXULBrowser));
		nsCOMPtr<nsIDOMXULElement> xulElement =
do_QueryInterface(domXULBrowser);
		nsCOMPtr<nsIBoxObject> boxObject;
		xulElement->GetBoxObject(getter_AddRefs(boxObject));
		nsCOMPtr<nsIBrowserBoxObject> browserboxObject =
do_QueryInterface(boxObject);
		nsCOMPtr<nsIDocShell> docShell;
		browserboxObject->GetDocShell(getter_AddRefs(docShell));
		nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(docShell);
		nsCOMPtr<nsIURI> uri;
		webNav->GetCurrentURI(getter_AddRefs(uri));
		nsCString path;
		uri->GetAsciiSpec(path);
		const CHAR *ppathspec = path.get();
		if (0 == _stricmp(ppathspec, pcszURL))
		{
			nsCOMPtr<nsIDOMDocument> tabdoc;
			webNav->GetDocument(getter_AddRefs(tabdoc));
			if (tabdoc.get() == DOMDoc.get())
			{
				//
				//	!!! This is the required tab. !!!
				//	!!! This is the tab associated with the request. !!!
				//
			}
		}
	}
}

In above code I am comparing nsIDOMDocument pointers to get currently
navigated tab.

Is there another sure way to do this?

With Best Regards,
Vaibhav.