Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Notification when windows are created

8 views
Skip to first unread message

Jonas Sicking

unread,
Mar 15, 2010, 4:32:25 PM3/15/10
to
Figured I should mention it here since this is a topic that comes up
every so often.

We now have a notification that is sent out every time a new window is
created. This notification is sent out before any scripts inside the
page has executed. And it's sent out for all new windows, including
iframes and similar.

The notification is an observer service notification. To be notified
when "web page" windows are created, observe the
"content-document-global-created" notification. To be notified when
chrome windows are created, listen to "chrome-document-global-created".

When notified, you'll be handed both a reference to the newly created
window, as well as the domain for the window, i.e. something like
"http://foo.com".

This was added in bug 549539. If you would have need for the
notification to be back ported to 1.9.2, feel free to request approval
on the patch.

/ Jonas

Boris Zbarsky

unread,
Mar 15, 2010, 5:35:34 PM3/15/10
to
On 3/15/10 4:32 PM, Jonas Sicking wrote:
> We now have a notification that is sent out every time a new window is
> created. This notification is sent out before any scripts inside the
> page has executed. And it's sent out for all new windows, including
> iframes and similar.

To be clear, this includes inner windows, so every navigation will
notify about the new script globa being created.

-Boris

John J Barton

unread,
Mar 15, 2010, 5:15:09 PM3/15/10
to

Well I thought it was clear until you said that!

Do we get 2n+1 events for a web page then? One for the outer
nsIDOMWindow, (for each load/reload) one for about:blank and one for the
web page? Will the window argument always be the outer window ( I guess
it has to be) ?

jjb

>
> -Boris

Jonas Sicking

unread,
Mar 15, 2010, 7:25:02 PM3/15/10
to

We only notify when we're creating a new inner window (specifically, we
notify when an outer window is getting a new inner window).

But yes, often we will send two notifications. Once while about:blank is
the current document, and once when we've successfully gotten far enough
in the load that we know which URL we're loading into the window.

Sometimes we're able to reuse the window between these two loads.

As always, it's the outer window that you'll be handed a reference to.

/ Jonas

johnjbarton

unread,
Apr 4, 2010, 12:35:01 AM4/4/10
to
On 3/15/2010 4:25 PM, Jonas Sicking wrote:
> On 3/15/2010 14:15, John J Barton wrote:
>> Boris Zbarsky wrote:
>>> On 3/15/10 4:32 PM, Jonas Sicking wrote:
>>>> We now have a notification that is sent out every time a new window is
>>>> created. This notification is sent out before any scripts inside the
>>>> page has executed. And it's sent out for all new windows, including
>>>> iframes and similar.
...

>
> As always, it's the outer window that you'll be handed a reference to.

Sorry I've gotten very confused on a subject related to "when windows
are created"

From the example in bug 549539:

observe: function observe_func(subj, topic, data) {
if (topic == "app-startup") {
var obs = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
obs.addObserver(this, "content-document-window-created", false);
}
else if (topic == "content-document-window-created") {
// Here we know that |subj| is an outer window.
}
}

The object |subj| arrives in our scope from somewhere. The API tells us
the object is an "outer window" and thus a nsIDOMWindow.

So I guess (subj instanceof Ci.nsIDOMWindow) == true. Why? I guess
because Ci.nsIDOMWindow is "super global"? That is, all JS scopes refer
to a single Ci.nsIDOMWindow back in BackStagePass?

But we have no such hope for (subj instanceof Window) correct? Window is
an object in the observer's scope, correct? If subj is a Window, then
sometimes (subj instanceof Window)==true, and then you indeed have a
Window. But if you get 'false' you cannot conclude it's not a Window. It
could be a Window if you tested in the scope it was created in.

But then Firebug could not work, it has plenty of instanceof tests. I
have to conclude that Window is somehow magical, as well as some but not
all other objects. (Bonus question: anyway to tell the magical from the
ordinary?)

jjb

Neil

unread,
Apr 4, 2010, 5:35:14 AM4/4/10
to
johnjbarton wrote:

> I have to conclude that Window is somehow magical, as well as some but
> not all other objects.

That's true. Array and Object, for instance, are non-magical; you can
only use instanceof with the Array or Object class from the scope in
which the given array or object were defined. (This wasn't always the
case.) Components.interfaces are magical; the object must be an XPCOM
object, and its QueryInterface method is called; if that succeeds then
instanceof returns true, otherwise it returns false. (So it's not quite
the same as function isInstance(aObject, aInterface) { try {
aObject.QueryInterface(aInterface); return true; } catch (e) { return
false; } }.) Names of DOM classes are magical, but in a slightly
different way: the first interface for the class info for that class is
used. Which leads to the bug that because ChromeWindow lists
nsIDOMWindow as its first interface, all windows evaluate as instanceof
ChromeWindow.

--
Warning: May contain traces of nuts.

0 new messages