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

Side by Side Diff: assets/js/XMLHttpRequest.jsm

Issue 8482109: ABP/Android JavaScript code (Closed)
Patch Set: Created Oct. 5, 2012, 9:23 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1
2 XMLHttpRequest = function()
3 {
4 this.headers = {};
5 this.responseHeaders = {};
6 };
7
8 XMLHttpRequest.UNSENT = 0;
9 XMLHttpRequest.OPEN = 1;
10 XMLHttpRequest.HEADERS_RECEIVED = 2;
11 XMLHttpRequest.LOADING = 3;
12 XMLHttpRequest.DONE = 4;
13
14 XMLHttpRequest.prototype =
15 {
16 open: function(method, url, async, user, password)
17 {
18 this.async = (typeof async == "undefined" || async) ? true : false;
19 this.method = method || "GET";
20 this.url = url;
21 this.readyState = XMLHttpRequest.OPEN;
22 this.onreadystatechange();
23 },
24 setRequestHeader: function(header, value)
25 {
26 this.headers[header] = value;
27 },
28 send: function(data)
29 {
30 var self = this;
31
32 Android.httpSend(self.method, self.url, self.headers, self.async, handleResp onse);
33
34 function handleResponse(code, message, headers, text)
35 {
36 if (self.aborted)
37 return;
38 if (headers != null)
39 {
40 for (var i = 0; i < headers.length; i++)
41 {
42 var headerName = headers[i][0];
43 var headerValue = headers[i][1];
44 if (headerName)
45 self.responseHeaders[headerName] = headerValue;
46 }
47 }
48 self.readyState = XMLHttpRequest.HEADERS_RECEIVED;
49 self.onreadystatechange();
50
51 self.readyState = XMLHttpRequest.LOADING;
52 self.onreadystatechange();
53 self.status = parseInt(code) || undefined;
54 self.statusText = message || "";
55
56 self.responseText = text;
57
58 self.readyState = XMLHttpRequest.DONE;
59 self.onreadystatechange();
60 self.triggerListeners("load");
61 self.triggerListeners("loadend");
62 }
63 },
64 abort: function()
65 {
66 this.aborted = true;
67 this.triggerListeners("abort");
68 this.readyState = XMLHttpRequest.DONE;
69 this.onreadystatechange();
70 },
71 onreadystatechange: function(){},
72 getResponseHeader: function(header)
73 {
74 if (this.readyState < XMLHttpRequest.LOADING)
75 throw new Error("INVALID_STATE_ERR");
76 else
77 {
78 var returnedHeaders = [];
79 for (var rHeader in this.responseHeaders)
80 {
81 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.
82 returnedHeaders.push(this.responseHeaders[rHeader]);
83 }
84
85 if (returnedHeaders.length)
86 return returnedHeaders.join(", ");
87 }
88
89 return null;
90 },
91 getAllResponseHeaders: function(header)
92 {
93 if (this.readyState < 3)
94 throw new Error("INVALID_STATE_ERR");
95 else
96 {
97 var returnedHeaders = [];
98
99 for (var header in this.responseHeaders)
100 returnedHeaders.push(header + ": " + this.responseHeaders[header]);
101
102 return returnedHeaders.join("\r\n");
103 }
104 },
105 overrideMimeType: function(mime) {},
106 addEventListener: function(type, listener, useCapture)
107 {
108 var listeners = null;
109
110 if (type == "progress")
111 listeners = this.progressEventListeners;
112 else if (type == "loadend")
113 listeners = this.loadendEventListeners;
114 else if (type == "load")
115 listeners = this.loadEventListeners;
116 else if (type == "error")
117 listeners = this.errorEventListeners;
118 else if (type == "abort")
119 listeners = this.abortEventListeners;
120
121 if (listeners == null || listeners.indexOf(listener) >= 0)
122 return;
123 listeners.push(listener);
124 },
125 triggerListeners: function(type)
126 {
127 var listeners = null;
128
129 if (type == "progress")
130 listeners = this.progressEventListeners;
131 else if (type == "loadend")
132 listeners = this.loadendEventListeners;
133 else if (type == "load")
134 listeners = this.loadEventListeners;
135 else if (type == "error")
136 listeners = this.errorEventListeners;
137 else if (type == "abort")
138 listeners = this.abortEventListeners;
139
140 for (var i = 0; i < listeners.length; i++)
141 {
142 var listener = listeners[i];
143 listener();
144 }
145 },
146 progressEventListeners: [],
147 loadendEventListeners: [],
148 loadEventListeners: [],
149 errorEventListeners: [],
150 abortEventListeners: [],
151 aborted: false,
152 async: true,
153 readyState: XMLHttpRequest.UNSENT,
154 responseText: "",
155 status: 0
156 };
157
158 XMLHttpRequest.prototype.channel =
159 {
160 status: -1,
161 notificationCallbacks: {},
162 loadFlags: 0,
163 INHIBIT_CACHING: 0,
164 VALIDATE_ALWAYS: 0,
165 QueryInterface: function()
166 {
167 return this;
168 }
169 };
OLDNEW
« 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