My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.dotspots.rpcplus.client.transport.impl;

import com.dotspots.rpcplus.client.jsonrpc.RpcException;
import com.dotspots.rpcplus.client.transport.TransportLogger;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.FormElement;
import com.google.gwt.dom.client.IFrameElement;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FormPanel;

/**
* Base class for the two cross-domain frame request types: window.name and postMessage.
*/
abstract class CrossDomainFrameTransportRequest {
private final String arguments;
protected final AsyncCallback<String> callback;

private static final int NO_TIMER = -1;

protected static final String SEND_PREFIX = "wnt-";
protected static final String RECEIVE_PREFIX = "wnr-";

private final int timeout;
protected final Document document;
private final String url;

protected boolean running;

/**
* The document holding the iframe. Pinned here so IE doesn't GC it.
*/
private Document iframeDocument;

/**
* The iframe that we're posting into.
*/
protected IFrameElement iframe;

/**
* The form that we're posting to the iframe.
*/
private FormElement form;

/**
* A timer that tracks when we should consider this request timed out.
*/
private int timeoutTimerId = NO_TIMER;

protected String serial;
protected String iframeName;
protected String responseName;

public CrossDomainFrameTransportRequest(String arguments, final AsyncCallback<String> callback, Document document, String url,
int timeout) {
this.arguments = arguments;
this.callback = callback;
this.document = document;
this.url = url;
this.timeout = timeout;
}

/**
* Cancels a running request. Safe to call more than once.
*/
public void cancel() {
running = false;

if (timeoutTimerId != NO_TIMER) {
clearTimeout(timeoutTimerId);
timeoutTimerId = NO_TIMER;
}

if (iframe != null) {
detachIFrameListeners();

// TODO: GWT 2.0
// iframe.removeFromParent();
// form.removeFromParent();

removeFromParent(iframe);
removeFromParent(form);

iframeDocument = null;
iframe = null;
form = null;

// Allow IE to reclaim the htmlfile document
collectGarbage();
}
}

public void start() {
serial = UniqueRequestGenerator.createUniqueId();
iframeName = SEND_PREFIX + serial;
responseName = RECEIVE_PREFIX + serial;

try {
timeoutTimerId = createTimeoutTimer(timeout, this);

// Create the attached iframe
createAttachedIFrame();
setIFrameContentWindowName(iframe, iframeName);
attachIFrameListeners();

// Create and populate the form
createForm();
populateForm();

TransportLogger.INSTANCE.logSend(arguments);

form.submit();
running = true;
} catch (Throwable t) {
callback.onFailure(new RpcException("Unexpected error while submitting request", t));
cancel();
}
}

@SuppressWarnings("unused")
private void error() {
if (!running) {
return;
}

cancel();
callback.onFailure(new RpcException("RPC failed"));
}

@SuppressWarnings("unused")
private void timeout() {
cancel();
callback.onFailure(new RpcException("RPC timed out"));
}

private native void clearTimeout(int timeoutId) /*-{
$wnd.clearTimeout(timeoutId);
}-*/;

/**
* Creates a raw timeout on the window to avoid issues with the GWT Timer class.
*/
private native int createTimeoutTimer(int timeout, CrossDomainFrameTransportRequest self) /*-{
return $wnd.setTimeout(function() { self.@com.dotspots.rpcplus.client.transport.impl.CrossDomainFrameTransportRequest::timeout()() }, timeout);
}-*/;

private void removeFromParent(Element elem) {
elem.getParentElement().removeChild(elem);
}

private void makeInvisible(final Element elem) {
// TODO: GWT 2.0
// elem.getStyle().setVisibility(Visibility.HIDDEN);
// elem.getStyle().setHeight(10, Unit.PX);
// elem.getStyle().setWidth(10, Unit.PX);
// elem.getStyle().setPosition(Position.ABSOLUTE);

elem.getStyle().setProperty("visibility", "hidden");
elem.getStyle().setPropertyPx("height", 10);
elem.getStyle().setPropertyPx("width", 10);
elem.getStyle().setProperty("position", "absolute");
}

private void createAttachedIFrame() {
if (isActiveXSupported()) {
iframeDocument = createHtmlFile();
iframe = iframeDocument.getElementById("iframe").cast();
} else {
// Create and attach the iframe
iframe = document.createIFrameElement();
makeInvisible(iframe);
document.getBody().appendChild(iframe);
iframeDocument = iframe.getOwnerDocument();
}
}

private native Document createHtmlFile() /*-{
var htmlfile = new ActiveXObject("htmlfile");
htmlfile.open();
htmlfile.write("<html><body><iframe id='iframe'></iframe></body></html>");
htmlfile.close();
return htmlfile;
}-*/;

private void createForm() {
form = iframeDocument.createFormElement();
iframeDocument.getBody().appendChild(form);
form.setAction(url);
form.setMethod(FormPanel.METHOD_POST);
form.setTarget(iframeName);
form.setEnctype(FormPanel.ENCODING_URLENCODED);

// If we are using IE, we don't need to hide the form (it's running under the already-invisible ActiveX
// htmlfile)
if (!isActiveXSupported()) {
makeInvisible(form);
}
}

protected void populateForm() {
appendHiddenInput("serial", serial);
appendHiddenInput("data", arguments);
appendHiddenInput("type", getRequestType());
}

protected void appendHiddenInput(String name, String value) {
InputElement input = iframeDocument.createHiddenInputElement();
input.setName(name);
input.setValue(value);
form.appendChild(input);
}

protected native boolean isActiveXSupported() /*-{
return ("ActiveXObject" in $wnd);
}-*/;

private static native void collectGarbage() /*-{
if ("CollectGarbage" in $wnd)
CollectGarbage();
}-*/;

private static native void setIFrameContentWindowName(IFrameElement iframe, String name) /*-{
// For IE
iframe.contentWindow.name = name;

// For safari
iframe.setAttribute('name', name);
}-*/;

protected void receive(String response) {
TransportLogger.INSTANCE.logReceive(response);

/*
* Defer removing the iframe - works around a Firefox bug where the throbber keeps spinning, similar to this GWT
* bug: (http://code.google.com/p/google-web-toolkit/issues/detail?id=916). The running flag is set to false to
* make sure we don't try to re-send the response.
*/
running = false;
DeferredCommand.addCommand(new Command() {
public void execute() {
cancel();
}
});

try {
callback.onSuccess(response);
} catch (Throwable t) {
callback.onFailure(t);
}
}

protected abstract String getRequestType();

protected abstract void detachIFrameListeners();

protected abstract void attachIFrameListeners();
}

Change log

r143 by mmastrac on Jan 11, 2010   Diff
Refactor out the request cancellation code
from the window.name transport to reuse in
the postMessage transport. This fixes the
timeout error that gets thrown in the
success case.
Go to: 
Sign in to write a code review

Older revisions

r137 by mmastrac on Dec 11, 2009   Diff
Don't use the ActiveXObject window
while postMessage is running.
r136 by mmastrac on Dec 11, 2009   Diff
Some cleanups for the new cross-domain
code.
r131 by mmastrac on Dec 11, 2009   Diff
Initial checkin for the new cross-
domain frame transport that uses
postMessage where appropriate. Passes
local tests, but needs to run on the
testing cluster.
All revisions of this file

File info

Size: 6971 bytes, 260 lines
Powered by Google Project Hosting