Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: assets/js/XMLHttpRequest.jsm

Issue 8482109: ABP/Android JavaScript code (Closed)
Patch Set: Created Oct. 5, 2012, 9:23 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: assets/js/XMLHttpRequest.jsm
===================================================================
new file mode 100644
--- /dev/null
+++ b/assets/js/XMLHttpRequest.jsm
@@ -0,0 +1,169 @@
+
+XMLHttpRequest = function()
+{
+ this.headers = {};
+ this.responseHeaders = {};
+};
+
+XMLHttpRequest.UNSENT = 0;
+XMLHttpRequest.OPEN = 1;
+XMLHttpRequest.HEADERS_RECEIVED = 2;
+XMLHttpRequest.LOADING = 3;
+XMLHttpRequest.DONE = 4;
+
+XMLHttpRequest.prototype =
+{
+ open: function(method, url, async, user, password)
+ {
+ this.async = (typeof async == "undefined" || async) ? true : false;
+ this.method = method || "GET";
+ this.url = url;
+ this.readyState = XMLHttpRequest.OPEN;
+ this.onreadystatechange();
+ },
+ setRequestHeader: function(header, value)
+ {
+ this.headers[header] = value;
+ },
+ send: function(data)
+ {
+ var self = this;
+
+ Android.httpSend(self.method, self.url, self.headers, self.async, handleResponse);
+
+ function handleResponse(code, message, headers, text)
+ {
+ if (self.aborted)
+ return;
+ if (headers != null)
+ {
+ for (var i = 0; i < headers.length; i++)
+ {
+ var headerName = headers[i][0];
+ var headerValue = headers[i][1];
+ if (headerName)
+ self.responseHeaders[headerName] = headerValue;
+ }
+ }
+ self.readyState = XMLHttpRequest.HEADERS_RECEIVED;
+ self.onreadystatechange();
+
+ self.readyState = XMLHttpRequest.LOADING;
+ self.onreadystatechange();
+ self.status = parseInt(code) || undefined;
+ self.statusText = message || "";
+
+ self.responseText = text;
+
+ self.readyState = XMLHttpRequest.DONE;
+ self.onreadystatechange();
+ self.triggerListeners("load");
+ self.triggerListeners("loadend");
+ }
+ },
+ abort: function()
+ {
+ this.aborted = true;
+ this.triggerListeners("abort");
+ this.readyState = XMLHttpRequest.DONE;
+ this.onreadystatechange();
+ },
+ onreadystatechange: function(){},
+ getResponseHeader: function(header)
+ {
+ if (this.readyState < XMLHttpRequest.LOADING)
+ throw new Error("INVALID_STATE_ERR");
+ else
+ {
+ var returnedHeaders = [];
+ for (var rHeader in this.responseHeaders)
+ {
+ if (rHeader.match(new RegExp(header, "i")))
Felix Dahlke 2012/11/09 14:59:16 I think we need to quote the header to use it in a
Andrey Novikov 2012/11/12 09:33:12 Done.
+ returnedHeaders.push(this.responseHeaders[rHeader]);
+ }
+
+ if (returnedHeaders.length)
+ return returnedHeaders.join(", ");
+ }
+
+ return null;
+ },
+ getAllResponseHeaders: function(header)
+ {
+ if (this.readyState < 3)
+ throw new Error("INVALID_STATE_ERR");
+ else
+ {
+ var returnedHeaders = [];
+
+ for (var header in this.responseHeaders)
+ returnedHeaders.push(header + ": " + this.responseHeaders[header]);
+
+ return returnedHeaders.join("\r\n");
+ }
+ },
+ overrideMimeType: function(mime) {},
+ addEventListener: function(type, listener, useCapture)
+ {
+ var listeners = null;
+
+ if (type == "progress")
+ listeners = this.progressEventListeners;
+ else if (type == "loadend")
+ listeners = this.loadendEventListeners;
+ else if (type == "load")
+ listeners = this.loadEventListeners;
+ else if (type == "error")
+ listeners = this.errorEventListeners;
+ else if (type == "abort")
+ listeners = this.abortEventListeners;
+
+ if (listeners == null || listeners.indexOf(listener) >= 0)
+ return;
+ listeners.push(listener);
+ },
+ triggerListeners: function(type)
+ {
+ var listeners = null;
+
+ if (type == "progress")
+ listeners = this.progressEventListeners;
+ else if (type == "loadend")
+ listeners = this.loadendEventListeners;
+ else if (type == "load")
+ listeners = this.loadEventListeners;
+ else if (type == "error")
+ listeners = this.errorEventListeners;
+ else if (type == "abort")
+ listeners = this.abortEventListeners;
+
+ for (var i = 0; i < listeners.length; i++)
+ {
+ var listener = listeners[i];
+ listener();
+ }
+ },
+ progressEventListeners: [],
+ loadendEventListeners: [],
+ loadEventListeners: [],
+ errorEventListeners: [],
+ abortEventListeners: [],
+ aborted: false,
+ async: true,
+ readyState: XMLHttpRequest.UNSENT,
+ responseText: "",
+ status: 0
+};
+
+XMLHttpRequest.prototype.channel =
+{
+ status: -1,
+ notificationCallbacks: {},
+ loadFlags: 0,
+ INHIBIT_CACHING: 0,
+ VALIDATE_ALWAYS: 0,
+ QueryInterface: function()
+ {
+ return this;
+ }
+};
« no previous file with comments | « assets/js/Synchronizer.jsm ('k') | assets/js/basedomain.js » ('j') | assets/js/start.js » ('J')

Powered by Google App Engine
This is Rietveld