How to get URL from XPCOM/C++ and plugin

200 views
Skip to first unread message

yekerui

unread,
Mar 24, 2006, 5:28:36 PM3/24/06
to
Hi, All:
I have a big trouble to get URL from XPCOM/C++/plugin. It is very simple
to get URL from IE with interface IWebBrowser2. I want to do samething
within a mozilla browser like firefox and netscape, but I can't.
Here is some code I wrote to get
nsresult rv;

nsIComponentManager *pIComponentManager = NULL;

nsIDOMDocument *pIDOMDocument = NULL;

nsISupports *pISupports = NULL;

nsIURL *pIURL = NULL;

do

{

rv = NS_GetComponentManager(&pIComponentManager);

if(pIComponentManager == NULL)

break;

rv =
pIComponentManager->GetClassObjectByContractID("@mozilla.org/network/standard-url;1",

NS_GET_IID(nsISupports), (void**)&pISupports);

if(pISupports == NULL)

{

rv = pISupports->QueryInterface(NS_GET_IID(nsIURL), (void**)&pIURL); //<---
fails here with no interface available

}

if(pISupports != NULL)

{

pISupports->Release();

pISupports = NULL;

}

rv =
pIComponentManager->GetClassObjectByContractID("@mozilla.org/xul/xul-document;1",

NS_GET_IID(nsISupports), (void**)&pISupports);

if(pISupports)

rv = pISupports->QueryInterface(NS_GET_IID(nsIDOMDocument),
(void**)&pIDOMDocument); //<--- fails here with no interface available

}while(false);

if(pIDOMDocument != NULL)

pIDOMDocument->Release();

if(pIURL != NULL)

pIURL->Release();

if(pISupports != NULL)

pISupports->Release();

if(pIComponentManager != NULL)

pIComponentManager->Release();

With the above code, I can get the components nsISupports and but can
not get expected interfaces. What is wrong with the above code? Why does my
code fail? I can not find any clues from Internet by doing Yahoo and Google
searches.

Please give me a help!


Benjamin Smedberg

unread,
Mar 24, 2006, 9:14:52 PM3/24/06
to
yekerui wrote:
> Hi, All:
> I have a big trouble to get URL from XPCOM/C++/plugin. It is very simple
> to get URL from IE with interface IWebBrowser2. I want to do samething

What URL are you trying to get? From your sample code it looks like you're
trying to create a new URL.

> rv =
> pIComponentManager->GetClassObjectByContractID("@mozilla.org/network/standard-url;1",
> NS_GET_IID(nsISupports), (void**)&pISupports);

The class object is typically the object that implements nsIFactory, and it
is hardly ever necessary to call this method. You typically create new XPCOM
objects by calling CreateInstanceByContractID or GetServiceByContractID.


Furthermore, I can guarantee that you don't actually want to be creating a
new standardurl by hand. If you want to make a new URL, you should do so by
asking the IOService to create one using the correct protocol handler
(nsIIOService->NewURI).

--BDS

yekerui

unread,
Mar 27, 2006, 10:20:04 AM3/27/06
to
Hi, Benjamin:
Thanks a lot for your response!
All I want is to query URL on a mozilla browser without creating a new
URL. However, I can not find a way to query URL after looking through all of
interface definitions in the directory ..\gecko_sdk\idl\. Could you give me
a hint how to get an URL address like http://www.firefox.com displayed on
browser. I did google search, but I can get any clues about getting URL from
XPCOM/C++.
Again, thanks a lot for your help.


Regards

"Benjamin Smedberg" <benj...@smedbergs.us> wrote in message
news:VZSdnTF8Ip0GOrnZ...@mozilla.org...

yekerui

unread,
Mar 27, 2006, 10:29:54 AM3/27/06
to
One correction:
I did google search, but I can NOT get any clues about getting URL from
XPCOM/C++.


"yekerui" <nos...@yahoo.com> wrote in message
news:wO6dnfRtqrmpn7XZ...@mozilla.org...

Benjamin Smedberg

unread,
Mar 27, 2006, 10:52:18 AM3/27/06
to
yekerui wrote:
> Hi, Benjamin:
> Thanks a lot for your response!
> All I want is to query URL on a mozilla browser without creating a new
> URL. However, I can not find a way to query URL after looking through all of
> interface definitions in the directory ..\gecko_sdk\idl\. Could you give me
> a hint how to get an URL address like http://www.firefox.com displayed on
> browser. I did google search, but I can get any clues about getting URL from
> XPCOM/C++.
> Again, thanks a lot for your help.

What kind of browser? What object(s) do you have a reference to? XPCOM
doesn't really have any concept of a browser, you have to have a reference
to the browser object that you want to query, and then you ask it what
location is loaded.

--BDS

yekerui

unread,
Mar 27, 2006, 11:11:04 AM3/27/06
to
Hi, Benjamin Smedberg:
I am porting an activeX control for IE onto mozilla plugin. With IE, it
is very simple to get the interface IWebBrowser2 at run time without
requiring javascript coding. Afterwards, I can use the interface
IWebBrowser2 to get URL address. At the end, my ActiveX control can restrict
domains for the sake of security. I want to do similar work with mozilla
browsers (firefox and netscape at least) from XPCOM/C++. It seems to me that
it is impossible to do such a work with mozilla browsers because of mozilla
limitations in integration.
I am looking foward to someone suggestions or hints how to solve such a
problem.

Regards,

Yuancai (Charlie) Ye

"Benjamin Smedberg" <benj...@smedbergs.us> wrote in message

news:8d-dnZ15Usqsl7XZ...@mozilla.org...

Christian Biesinger

unread,
Mar 27, 2006, 12:21:20 PM3/27/06
to
(setting followup-to mozilla.dev.tech.plugins)

yekerui wrote:
> Hi, Benjamin Smedberg:
> I am porting an activeX control for IE onto mozilla plugin.

This isn't really an XPCOM question. A valid from: address would also be
nice. Anyway...

If you use NPN_GetURL, you automatically get a security check
(checkLoadURI, not same-origin). You could also use the method mentioned
in
http://groups.google.com/group/netscape.public.mozilla.plugins/msg/93166efa554a191b
to get the URI of the page the plugin is in.

Benjamin Smedberg

unread,
Mar 27, 2006, 12:27:11 PM3/27/06
to yekerui
yekerui wrote:
> Hi, Benjamin Smedberg:
> I am porting an activeX control for IE onto mozilla plugin. With IE, it
> is very simple to get the interface IWebBrowser2 at run time without
> requiring javascript coding. Afterwards, I can use the interface
> IWebBrowser2 to get URL address. At the end, my ActiveX control can restrict
> domains for the sake of security. I want to do similar work with mozilla
> browsers (firefox and netscape at least) from XPCOM/C++. It seems to me that
> it is impossible to do such a work with mozilla browsers because of mozilla
> limitations in integration.
> I am looking foward to someone suggestions or hints how to solve such a
> problem.

As this is a plugin issue I've set followup-to mozilla.dev.tech.plugins
only, let's continue this discussion there.

The NPAPI gives you the ability to get access to the nsIDOMWindow object
which contains the current plugin via the "NPNVDOMElement" enum[1] passed to
NPN_GetValue. This returns an addrefed nsIDOMElement[2] interface pointer
from which you can call .ownerDocument (on nsIDOMNode [3]), QueryInterface
that object to nsIDOM3Document[4], and call .documentURI.

1) http://lxr.mozilla.org/mozilla/source/modules/plugin/base/public/npapi.h#420

2)
http://lxr.mozilla.org/mozilla/source/dom/public/idl/core/nsIDOMElement.idl

3)
http://lxr.mozilla.org/mozilla/source/dom/public/idl/core/nsIDOMNode.idl#82

4)
http://lxr.mozilla.org/mozilla/source/dom/public/idl/core/nsIDOM3Document.idl#63

--BDS

Christian Biesinger

unread,
Mar 27, 2006, 12:30:43 PM3/27/06
to
Benjamin Smedberg wrote:
> The NPAPI gives you the ability to get access to the nsIDOMWindow object
> which contains the current plugin via the "NPNVDOMElement" enum[1]
> passed to NPN_GetValue.

This way of getting the URI is also compatible with older versions of
the browser (compared to my suggestion), but has the downside of
requiring C++. (and is inherently incompatible with other browsers
implementing the NPAPI)

yekerui

unread,
Mar 27, 2006, 12:49:36 PM3/27/06
to
Hi, Christian Biesinger and Benjamin Smedberg:
Thanks a lot, a lot and a lot to you two! It seems to me that is
solution. I am a beginner with mozilla browser. I am going to experiment
these methods.

Regards,

"Christian Biesinger" <cbies...@web.de> wrote in message
news:NbKdnWtf9fXTvLXZ...@mozilla.org...

Christian Biesinger

unread,
Mar 27, 2006, 4:22:51 PM3/27/06
to
Christian Biesinger wrote:
> but has the downside of requiring C++.

(as opposed to C)

Braden McDaniel

unread,
Mar 27, 2006, 5:05:11 PM3/27/06
to
yekerui wrote:
> Hi, Christian Biesinger and Benjamin Smedberg:
> Thanks a lot, a lot and a lot to you two! It seems to me that is
> solution. I am a beginner with mozilla browser. I am going to experiment
> these methods.

If you want the URI of the resource for which the plug-in is invoked,
the most NPAPI-friendly way to do that is to get it from the NPStream
that is passed to NPP_NewStream.

Braden

yekerui

unread,
Mar 27, 2006, 6:02:46 PM3/27/06
to
Hi, All:
I tried the link
http://groups.google.com/group/netscape.public.mozilla.plugins/msg/93166efa554a191b,
but i got the link the problem for
NPN_GetProperty and NPN_GetStringIdentifier: Here is error information from
Visual C++:

error LNK2019: unresolved external symbol _NPN_GetProperty referenced in
function "public: virtual unsigned int __stdcall
CWindowExternalImp::GetWXcom(class nsIWXcom * *)"
(?GetWXcom@CWindowExternalImp@@UAGIPAPAVnsIWXcom@@@Z)

nsUpdateCenterPeer.obj : error LNK2019: unresolved external symbol
_NPN_GetStringIdentifier referenced in function "public: virtual unsigned
int __stdcall CWindowExternalImp::GetWXcom(class nsIWXcom * *)"
(?GetWXcom@CWindowExternalImp@@UAGIPAPAVnsIWXcom@@@Z)

What *.lib file do I need to link against?

"Christian Biesinger" <cbies...@web.de> wrote in message

news:GLmdnYlFqNC...@mozilla.org...

Reply all
Reply to author
Forward
0 new messages