Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
[CB-6763] Fixes issue when multiple simultaneous requests are sent.
  • Loading branch information
Vladimir Kotikov committed Jun 4, 2014
1 parent 64fe2f6 commit efaf559ae846567e68e4d96446c8760c3ceed8fc
Showing 1 changed file with 34 additions and 28 deletions.
@@ -18,9 +18,28 @@ public class XHRHelper : IBrowserDecorator

public void InjectScript()
{
string script = @"(function(win, doc) {
var __XHRShimAliases = {};
string script = @"(function(win, doc) {
window.__onXHRLocalCallback = function (responseCode, responseText, reqId) {
if (__XHRShimAliases[reqId]){
var alias = __XHRShimAliases[reqId];
if (alias){
delete __XHRShimAliases[reqId];
if (responseCode == '200'){
alias.onResult && alias.onResult(responseText);
Object.defineProperty(alias, 'responseXML', {
get: function () {
return new DOMParser().parseFromString(this.responseText, 'text/xml');
}
});
} else {
alias.onError && alias.onError(responseText);
}
}
}
};
var docDomain = null;
try {
@@ -221,32 +240,17 @@ public void InjectScript()
resolvedUrl = basePath + resolvedUrl; // consider it relative
}
// Generate unique request ID
var reqId = new Date().getTime().toString() + Math.random();
var funk = function () {
window.__onXHRLocalCallback = function (responseCode, responseText) {
alias.status = responseCode;
if (responseCode == '200') {
alias.responseText = responseText;
Object.defineProperty(alias, 'responseXML', {
get: function () {
return new DOMParser().parseFromString(this.responseText, 'text/xml');
}
});
}
else {
alias.onerror && alias.onerror(responseCode);
}
__XHRShimAliases[reqId] = alias;
alias.changeReadyState(XHRShim.DONE);
}
alias.changeReadyState(XHRShim.LOADING);
window.external.Notify('XHRLOCAL/' + resolvedUrl);
}
if (this.isAsync) {
setTimeout(funk, 0);
}
else {
funk();
}
window.external.Notify('XHRLOCAL/' + reqId + '/' + resolvedUrl);
};
this.isAsync ? setTimeout(funk, 0) : funk();
}
},
status: 404
@@ -262,7 +266,9 @@ public bool HandleCommand(string commandStr)
{
if (commandStr.IndexOf("XHRLOCAL") == 0)
{
string url = commandStr.Replace("XHRLOCAL/", "");
var reqStr = commandStr.Replace("XHRLOCAL/", "").Split(new char[] {'/'}, 2);
string reqId = reqStr[0];
string url = reqStr[1];

Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);

@@ -273,7 +279,7 @@ public bool HandleCommand(string commandStr)
using (TextReader reader = new StreamReader(isoFile.OpenFile(uri.AbsolutePath, FileMode.Open, FileAccess.Read)))
{
string text = reader.ReadToEnd();
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text });
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text, reqId });
return true;
}
}
@@ -286,15 +292,15 @@ public bool HandleCommand(string commandStr)
if (resource == null)
{
// 404 ?
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "404" });
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "404", null, reqId });
return true;
}
else
{
using (StreamReader streamReader = new StreamReader(resource.Stream))
{
string text = streamReader.ReadToEnd();
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text });
Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text, reqId });
return true;
}
}

0 comments on commit efaf559

Please sign in to comment.