2

many times I have had an issue and post for help and so many times QueryInterface is the solution. I have been addon programming for a long time now but never understood QueryInterface. It seems like magic, like it holds the solution to everything.

like:

window.QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIWebNavigation)
                         .QueryInterface(Ci.nsIDocShellTreeItem)
                         .rootTreeItem
                         .QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIDOMWindow);

So my question is I see a QueryInterface chain, I don't get why the chain, and I definitely don't get how to create my own chain. I don't get how do you know to chain from nsiInterfaceRequester to nsiWebNavigation and not from nsiInterfaceRequester straight to nsiDOMWindow

Noitidart
  • 35,443
  • 37
  • 154
  • 323

1 Answers1

1

There is really nothing special here. DOM windows implement the nsIInterfaceRequestor interface that allows getting related objects. One such object is the docshell that is associated with the window - you get it by asking for the nsIWebNavigation interface but it also implements the nsIDocShell and nsIDocShellTreeItem interfaces - and nsIInterfaceRequestor. The docshell for the current window lets you get to the docshell for the top window, despite the security boundary between chrome and content. And there you can use the nsIInterfaceRequestor interface again to ask the docshell for the window associated with it.

You can simply implement the nsIInterfaceRequestor interface in your XPCOM component just like any other interface. I don't see any reason to ever do that however, it's an ugly hack to hide internal window-related interfaces from the DOM.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Thanks so much wlad for this reply I've been wanting one but I started to feel I was the most epxerienced ff guy around here hahah and I was like that that cant be true cuz i suck haha. What I mean by magic is like with QueryInterface is there a way to find the tab another tab was opend from? Like you know tabs are opened relative right, I saw somewhere but can't recall how they did it, it was like magic. – Noitidart Mar 01 '14 at 23:35
  • Also how do you now DOM windows implement ```nsIInterfaceRequestor`? because when i go ```Services.appShell.hiddenDOMWindow.console.log(window);``` and then click on that in the Browser Console and then in VariableViewer filter by nsI I don't see nsIInterfaceRequestor anywhere there image [HERE](http://img.photobucket.com/albums/v135/noitidart/i20dont20see20nsIInterfaceRequestor_zps7bfc9d43.png) – Noitidart Mar 01 '14 at 23:38
  • @Noitidart: To check whether the window implements `nsIInterfaceRequestor` you do `window instanceof Ci.nsIInterfaceRequestor` - this will call `QueryInterface()` implicitly, you cannot check supported interfaces just by looking at the properties of an object. For the entire list of supported interfaces you have to go to the source code. – Wladimir Palant Mar 01 '14 at 23:53
  • Ah I got it, thanks Wlad! But even if doesnt implement this people can do some tricks to make it use QueryInterface right? – Noitidart Mar 01 '14 at 23:58
  • @Noitidart: This is getting into XPCOM basics. Want to read [the docs](https://developer.mozilla.org/en/docs/How_to_Build_an_XPCOM_Component_in_Javascript) maybe? – Wladimir Palant Mar 02 '14 at 14:03
  • Ah I read docs a lot but never a page on how to build my own XPCOM component, I think it's a good idea as learning from ground up teaches stuff to you. I'll check it out thx Wladimir! – Noitidart Mar 02 '14 at 14:10