Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Merge branch 'CB-6763' of https://github.com/MSOpenTech/cordova-wp8
  • Loading branch information
purplecabbage committed Aug 14, 2014
2 parents 4d65cc8 + efaf559 commit 6ded156117dae97e5c336b9edec8342096b650ed
Showing 1 changed file with 34 additions and 28 deletions.
@@ -32,9 +32,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 {
@@ -235,32 +254,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
@@ -276,7 +280,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);

@@ -287,7 +293,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;
}
}
@@ -300,15 +306,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 6ded156

Please sign in to comment.