Possible to share object instances between windows?

42 views
Skip to first unread message

Thomas Hyldgaard

unread,
Nov 24, 2007, 4:43:39 PM11/24/07
to Google Web Toolkit
Hi,

I like to be able to share object instances across windows, eg. by
using a singleton pattern:

public class Singleton
{
private static Singleton instance = new Singleton();
private Singleton getInstance() { return instance; }
}

The first window opens the second window using the Window.open(url,
name, features) method.
And I want the EntryPoint class creates in the two windows to get
acess to the same singleton instance.

Is this possible when using GWT?

Best regards
Thomas

Reinier Zwitserloot

unread,
Nov 24, 2007, 9:38:25 PM11/24/07
to Google Web Toolkit
Nope. Different browser windows are analogous to different JVMs -
static stuff doesn't get shared.

You can have shared data, more or less, though - assuming Window
'foobar', which is the main window, opens a new window 'xyzzy', which
is hosted on THE SAME SERVER (according to the same origin policy -
wikipedia that if that's a new term for you), then foobar can access
the window object returned upon creating xyzzy. With this, it could
pass itself (foobar) to xyzzy, which now gives us the situation that
foobar has a ref to xyzzy, and xyzzy a ref to foobar.

However, AFAIK, GWT doesn't support any of this, so you'll have to
transport the low level stuff across windows by using a few JSNI
methods. You can definitely pass JSNI safe types across the barrier
(all primitives, arrays of same, Elements, and Events, and
JavascriptObjects, though passing elements around is fraught with
subtle problems). You might be able to get away with passing java
objects into JSNI across the barrier as well. I am 100% certain that
that won't work if the two windows run different GWT apps, or even
different compilation runs of the same app. It might work if they are
the exact same GWT app, though.

If you give this a try, please do tell us how it went, and good luck.

L Frohman

unread,
Nov 24, 2007, 9:56:40 PM11/24/07
to Google-We...@googlegroups.com
I went through this myself, and the way I passed objects between windows
was to serialize / deserialize them using xml. The sending window converts
the object to a String - the object has a toXml() method that creates a
string
of xml representing the object. The receiving window gets the string and
uses
static fromXml() to create the object from the xml string. The object source
code
is shared between the two windows.

Ian Petersen

unread,
Nov 24, 2007, 11:24:44 PM11/24/07
to Google-We...@googlegroups.com
On Nov 24, 2007 9:38 PM, Reinier Zwitserloot <rein...@gmail.com> wrote:
> You can definitely pass JSNI safe types across the barrier
> (all primitives, arrays of same, Elements, and Events, and
> JavascriptObjects, though passing elements around is fraught with
> subtle problems). You might be able to get away with passing java
> objects into JSNI across the barrier as well. I am 100% certain that
> that won't work if the two windows run different GWT apps, or even
> different compilation runs of the same app. It might work if they are
> the exact same GWT app, though.

Passing objects is likely to be a big problem. For one thing,
instanceof will break, even if it's exactly the same GWT app in both
windows, for reasons similar to the fact that, in Java, class
foo.bar.Class loaded by classloader foo is not an instance of
foo.bar.Class loaded by classloader bar, even if the two classes were
loaded from the same sequence of bytes.

You may also run into the problem that a function defined in one
window may not be a callable object in another window in IE. For
example:

// in window one
function bar() {
}

var w = window.open(....)
w.useFunc(bar);

// in window two
function useFunc(f) {
f();
}

calling f() in useFunc should work because you're passing it a
function object, but, for some reason, IE sometimes doesn't like it
and will tell you that f is not a function.

As Reinier says, though, primitives should be fine.

Ian

--
Tired of pop-ups, security holes, and spyware?
Try Firefox: http://www.getfirefox.com

Thomas Hyldgaard

unread,
Nov 26, 2007, 10:07:52 AM11/26/07
to Google Web Toolkit
Hi everybody,

Thanks for your input!

Right now I use a class like:

public class SharedVariable
{
public static native void setSharedVariable(Object sharedVariable) /
*-{
$wnd.sharedVariable = sharedVariable;
}-*/;

public static native Object tryGetSharedVariable() /*-{
if ($wnd.sharedVariable == null)
{
if ($wnd.opener == null)
return null;
else
{
if ($wnd.opener.sharedVariable == null)
return null;
else
return $wnd.opener.sharedVariable;
}
}
else
return $wnd.sharedVariable;
}-*/;
}

This seems to work with Internet Explorer (but it does not work in
hosted browser mode).
BTW: It is the same GWT application and the shared variable is just a
data structure (which is readonly). The source window must use
SharedVariable.setSharedVariable before another window is opened.

Best regards,
Thomas


On Nov 25, 5:24 am, "Ian Petersen" <ispet...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages