Discussion:
get current content document inside Firefox cpp code
DONG Xinshu
2009-11-09 14:10:32 UTC
Permalink
Hi,

I've been struggled with this problem for a while. In Firefox
extensions, we can easily get the current content document in the
selected tab by:

content.document or gBrowser.contentDocument

However, if I want to get this info inside Firefox code base
(nsHttpChannel::Init), what shall I do?

I tried with the following piece of code, but GetContentDOMWindow always
failed. And I debugged into the function body of GetContentDOMWindow,
the variable mDocShell was null. As in extensions we can get content
document in any scope of code, so in Firefox native cpp code can I also
get it in nsHttpChannel::Init or some other functions? My purpose is
just to get the original URI of a page before it's changed. Thanks.

Regards,
Xinshu
Boris Zbarsky
2009-11-09 14:39:31 UTC
Permalink
Post by DONG Xinshu
However, if I want to get this info inside Firefox code base
(nsHttpChannel::Init), what shall I do?
You can't get the document from that code: necko can't depend on content.
Post by DONG Xinshu
I tried with the following piece of code,
Which you didn't include....
Post by DONG Xinshu
My purpose is just to get the original URI of a page before it's changed
Meaning what?

-Boris
DONG Xinshu
2009-11-10 01:52:34 UTC
Permalink
Thanks, Boris. Sorry I forgot to append the code.

Basically I just want to do something in Firefox code similar as the
following in extensions:

When 'http-on-modify-request' notifies the observer, get current
top-level URI by
content.document.location.href. (At that time, new HTTP request is about
to be sent)

I tried in nsHttpChannel::Init with

rvx =
servMan->GetServiceByContractID("@mozilla.org/embedding/browser/nsWebBrowser;1",
NS_GET_IID(nsIWebBrowser),
getter_AddRefs(webBrowser));
nsCAutoString curHref;
if (!NS_FAILED(rvx)) {
nsCOMPtr<nsIDOMWindow> domWin;
rvx = webBrowser->GetContentDOMWindow(getter_AddRefs(domWin));

But webBrowser->GetContentDOMWindow(getter_AddRefs(domWin)) always
returned failure result.

Also in nsHttpChannel::Init, I tried

nsCOMPtr<nsIWindowMediator> winMd;
rvx =
servMan->GetServiceByContractID("@mozilla.org/appshell/window-mediator;1",
NS_GET_IID(nsIWindowMediator),
getter_AddRefs(winMd));
nsCOMPtr<nsIDOMWindowInternal> domWinInt;
rvx = winMd->GetMostRecentWindow(0, getter_AddRefs(domWinInt));
nsCOMPtr<nsIDOMWindow> domWin;
rvx = domWinInt->GetContent(getter_AddRefs(domWin));
nsCOMPtr<nsIDOMDocument> domDoc;
rvx = domWin->GetDocument(getter_AddRefs(domDoc));
nsCOMPtr<nsIDocument> curDoc = do_QueryInterface(domDoc);
nsCAutoString curHref;
curDoc->GetDocumentURI()->GetSpec(curHref);

For static web pages, 'curHref' could give the correct URI, but for AJAX
applications, 'curHref' was not correct (different from what I got from
content.document.location.href from 'http-on-modify-request' observer in
extension). So I guess GetMostRecentWindow is not entirely the same
thing as currently selected tab.

Could you please advise the correct way to do it in Firefox code? And
the correct interception function other than nsHttpChannel::Init? Thanks.

Regards,
Xinshu
Post by Boris Zbarsky
Post by DONG Xinshu
However, if I want to get this info inside Firefox code base
(nsHttpChannel::Init), what shall I do?
You can't get the document from that code: necko can't depend on content.
Post by DONG Xinshu
I tried with the following piece of code,
Which you didn't include....
Post by DONG Xinshu
My purpose is just to get the original URI of a page before it's changed
Meaning what?
-Boris
_______________________________________________ dev-tech-dom mailing
https://lists.mozilla.org/listinfo/dev-tech-dom
Boris Zbarsky
2009-11-10 02:39:50 UTC
Permalink
Post by DONG Xinshu
When 'http-on-modify-request' notifies the observer, get current
top-level URI by
content.document.location.href.
This URI might have nothing to do with the channel the
http-on-modify-request is for, of course....
Post by DONG Xinshu
I tried in nsHttpChannel::Init with
Like I said, you can't get a document in nsHttpChannel
Post by DONG Xinshu
rvx =
Uh... whatever gave you the idea that nsWebBrowser is a service? It's not.
Post by DONG Xinshu
nsCOMPtr<nsIDOMWindow> domWin;
rvx = webBrowser->GetContentDOMWindow(getter_AddRefs(domWin));
But webBrowser->GetContentDOMWindow(getter_AddRefs(domWin)) always
returned failure result.
Sure. nsWebBrowser is what you can use to instantiate a web browser.
If you then set it up correctly (which you didn't) and then browse
somewhere in it (which you also didn't), then GetContentDOMWindow will
hand back the window of the content loaded in that web browser. This is
used, e.g in Camino (one nsWebBrowser per tab).
Post by DONG Xinshu
nsCOMPtr<nsIWindowMediator> winMd;
rvx =
NS_GET_IID(nsIWindowMediator),
getter_AddRefs(winMd));
nsCOMPtr<nsIDOMWindowInternal> domWinInt;
rvx = winMd->GetMostRecentWindow(0, getter_AddRefs(domWinInt));
I'm surprised this compiled....
Post by DONG Xinshu
For static web pages, 'curHref' could give the correct URI, but for AJAX
applications, 'curHref' was not correct (different from what I got from
content.document.location.href from 'http-on-modify-request' observer in
extension
In the case when you have multiple windows they don't have to match by
any means.
Post by DONG Xinshu
So I guess GetMostRecentWindow is not entirely the same
thing as currently selected tab.
It's a window, not a tab...
Post by DONG Xinshu
Could you please advise the correct way to do it in Firefox code?
You can't do what you want to do. Not only that, but whatever you're
trying to do doesn't make sense to me so far. So what are you
_actually_ trying to do. Not on the "I want a document" level, but on
the "explain it to your grandmother" level.

-Boris
DONG Xinshu
2009-11-10 03:49:02 UTC
Permalink
Dear Boris,

Sorry if I confused you. Thanks for your kind help.

My intention is to monitor the change of URI of current content document
in the selected tab.

For example in Gmail, after you login, the URI is
"http://mail.google.com/mail/?shva=1#inbox", after you click "Compose
Mail", it becomes "http://mail.google.com/mail/?shva=1#compose" as
displayed in the Address Bar. So whenever this change happens, I want to
get notified.

Hope this time I made it clear to you. So do you have some idea how to
do this? Thanks.

Regards,
Xinshu
Post by Boris Zbarsky
Post by DONG Xinshu
When 'http-on-modify-request' notifies the observer, get current
top-level URI by
content.document.location.href.
This URI might have nothing to do with the channel the
http-on-modify-request is for, of course....
Post by DONG Xinshu
I tried in nsHttpChannel::Init with
Like I said, you can't get a document in nsHttpChannel
Post by DONG Xinshu
rvx =
Uh... whatever gave you the idea that nsWebBrowser is a service? It's not.
Post by DONG Xinshu
nsCOMPtr<nsIDOMWindow> domWin;
rvx = webBrowser->GetContentDOMWindow(getter_AddRefs(domWin));
But webBrowser->GetContentDOMWindow(getter_AddRefs(domWin)) always
returned failure result.
Sure. nsWebBrowser is what you can use to instantiate a web browser.
If you then set it up correctly (which you didn't) and then browse
somewhere in it (which you also didn't), then GetContentDOMWindow will
hand back the window of the content loaded in that web browser. This
is used, e.g in Camino (one nsWebBrowser per tab).
Post by DONG Xinshu
nsCOMPtr<nsIWindowMediator> winMd;
rvx =
NS_GET_IID(nsIWindowMediator),
getter_AddRefs(winMd));
nsCOMPtr<nsIDOMWindowInternal> domWinInt;
rvx = winMd->GetMostRecentWindow(0, getter_AddRefs(domWinInt));
I'm surprised this compiled....
Post by DONG Xinshu
For static web pages, 'curHref' could give the correct URI, but for AJAX
applications, 'curHref' was not correct (different from what I got from
content.document.location.href from 'http-on-modify-request' observer in
extension
In the case when you have multiple windows they don't have to match by
any means.
Post by DONG Xinshu
So I guess GetMostRecentWindow is not entirely the same
thing as currently selected tab.
It's a window, not a tab...
Post by DONG Xinshu
Could you please advise the correct way to do it in Firefox code?
You can't do what you want to do. Not only that, but whatever you're
trying to do doesn't make sense to me so far. So what are you
_actually_ trying to do. Not on the "I want a document" level, but on
the "explain it to your grandmother" level.
-Boris
_______________________________________________
dev-tech-dom mailing list
https://lists.mozilla.org/listinfo/dev-tech-dom
Boris Zbarsky
2009-11-10 14:14:17 UTC
Permalink
Post by DONG Xinshu
My intention is to monitor the change of URI of current content document
in the selected tab.
Is there a reason the onLocationChange notification is not sufficient
for this purpose?

-Boris
DONG Xinshu
2009-11-11 09:19:16 UTC
Permalink
Thanks, Boris. According to your hint, I found nsDocShell::SetCurrentURI
is what I want. But nsDocShell::OnLocationChange is not triggered when
document location changes.

Regards,
Xinshu
Post by Boris Zbarsky
Post by DONG Xinshu
My intention is to monitor the change of URI of current content document
in the selected tab.
Is there a reason the onLocationChange notification is not sufficient
for this purpose?
-Boris
_______________________________________________
dev-tech-dom mailing list
https://lists.mozilla.org/listinfo/dev-tech-dom
Boris Zbarsky
2009-11-11 16:48:12 UTC
Permalink
Post by DONG Xinshu
Thanks, Boris. According to your hint, I found nsDocShell::SetCurrentURI
is what I want. But nsDocShell::OnLocationChange is not triggered when
document location changes.
Why are you poking in the C++ code instead of just registering an
onLocationChange observer on the browser you want to observe?

-Boris
DONG XINSHU
2009-11-12 01:07:55 UTC
Permalink
I want to know more about browser internals, so I don't want to stay at
the level of scriptable interfaces. But it seems much harder than
working in JavaScript extensions.

Regards,
Xinshu
Post by Boris Zbarsky
Post by DONG Xinshu
Thanks, Boris. According to your hint, I found nsDocShell::SetCurrentURI
is what I want. But nsDocShell::OnLocationChange is not triggered when
document location changes.
Why are you poking in the C++ code instead of just registering an
onLocationChange observer on the browser you want to observe?
-Boris
_______________________________________________
dev-tech-dom mailing list
https://lists.mozilla.org/listinfo/dev-tech-dom
Loading...