pete pete, why do you set yourself up for such abuse?
- Why the heck would you want to do this?
- Isn't creating an instance of a service a pretty major
non-obvious side effect?
- Why get the service over and over again in the loop rather than
getting it first and loop over calls to QI?
- What if the service implements many interfaces?
- What if the for-in loop happens to give you nsISupports before
any other match? i.e. if nsISupports happened to be the first
thing in the list then if would *always* be the result of this
call. No?
- Your function has paths that don't return a value.
- Could "for(list in i)" be any less intuitive? :)
John.
Pete Collins wrote:
>
> I wanted to run this by Jband.
>
> I am working on some xpconnect utilities.
> One in particular returns an interface name from a ProgID argument.
>
> I'd like to know is this a good way to implement it?
>
> test output:
>
> js> getInterface('@mozilla.org/network/local-file-channel;1');
> nsIChannel
> js> getInterface('@mozilla.org/network/file-transport-service;1');
> nsIFileTransportService
> js> getInterface('@mozilla.org/file/local;1');
> nsILocalFile
> js>
>
> function getInterface(aProgID)
> {
>
> var C=Components;
> var i=C.interfaces;
> var c=C.classes;
> var list;
>
> if(!aProgID)
> {
> throw C.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS;
> return null;
> }
>
> for(list in i)
> {
> try
> {
> if(typeof(i[list])!='undefined')
> if(typeof(c[aProgID].getService(i[list]))=='object')
> return list;
> }
>
> catch(e){}
> }
>
> }