OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 if (!result.succeed) | 131 if (!result.succeed) |
132 throw result.error; | 132 throw result.error; |
133 }, | 133 }, |
134 deserializeResult: function(result) | 134 deserializeResult: function(result) |
135 { | 135 { |
136 this.checkResult(result); | 136 this.checkResult(result); |
137 return this.deserialize(result.result); | 137 return this.deserialize(result.result); |
138 }, | 138 }, |
139 serialize: function(obj, memo) | 139 serialize: function(obj, memo) |
140 { | 140 { |
141 var objectId = this.objects.indexOf(obj); | 141 if ((typeof obj == "object" || typeof obj == "function") && obj != null) |
142 if (objectId != -1) | 142 { |
143 return {type: "hosted", objectId: objectId}; | 143 if ("__proxyObjectId" in obj) |
| 144 return {type: "hosted", objectId: obj.__proxyObjectId}; |
144 | 145 |
145 if (typeof obj == "function") | 146 if (typeof obj == "function") |
146 { | 147 { |
147 var callbackId = this.callbacks.indexOf(obj); | 148 var callbackId; |
148 if (callbackId == -1) | 149 if ("__proxyCallbackId" in obj) |
149 callbackId = this.callbacks.push(obj) - 1; | 150 callbackId = obj.__proxyCallbackId; |
| 151 else |
| 152 { |
| 153 callbackId = this.callbacks.push(obj) - 1; |
| 154 Object.defineProperty(obj, "__proxyCallbackId", {value: callbackId})
; |
| 155 } |
150 | 156 |
151 return {type: "callback", callbackId: callbackId, frameId: documentInfo.
frameId}; | 157 return {type: "callback", callbackId: callbackId, frameId: documentInf
o.frameId}; |
152 } | |
153 | |
154 if (typeof obj == "object" && | |
155 obj != null && | |
156 obj.constructor != Date && | |
157 obj.constructor != RegExp) | |
158 { | |
159 if (!memo) | |
160 memo = {specs: [], objects: []}; | |
161 | |
162 var idx = memo.objects.indexOf(obj); | |
163 if (idx != -1) | |
164 return memo.specs[idx]; | |
165 | |
166 var spec = {}; | |
167 memo.specs.push(spec); | |
168 memo.objects.push(obj); | |
169 | |
170 if (obj.constructor == Array) | |
171 { | |
172 spec.type = "array"; | |
173 spec.items = []; | |
174 | |
175 for (var i = 0; i < obj.length; i++) | |
176 spec.items.push(this.serialize(obj[i], memo)); | |
177 } | |
178 else | |
179 { | |
180 spec.type = "object"; | |
181 spec.properties = {}; | |
182 | |
183 for (var k in obj) | |
184 spec.properties[k] = this.serialize(obj[k], memo); | |
185 } | 158 } |
186 | 159 |
187 return spec; | 160 if (obj.constructor != Date && obj.constructor != RegExp) |
| 161 { |
| 162 if (!memo) |
| 163 memo = {specs: [], objects: []}; |
| 164 |
| 165 var idx = memo.objects.indexOf(obj); |
| 166 if (idx != -1) |
| 167 return memo.specs[idx]; |
| 168 |
| 169 var spec = {}; |
| 170 memo.specs.push(spec); |
| 171 memo.objects.push(obj); |
| 172 |
| 173 if (obj.constructor == Array) |
| 174 { |
| 175 spec.type = "array"; |
| 176 spec.items = []; |
| 177 |
| 178 for (var i = 0; i < obj.length; i++) |
| 179 spec.items.push(this.serialize(obj[i], memo)); |
| 180 } |
| 181 else |
| 182 { |
| 183 spec.type = "object"; |
| 184 spec.properties = {}; |
| 185 |
| 186 for (var k in obj) |
| 187 spec.properties[k] = this.serialize(obj[k], memo); |
| 188 } |
| 189 |
| 190 return spec; |
| 191 } |
188 } | 192 } |
189 | 193 |
190 return {type: "value", value: obj}; | 194 return {type: "value", value: obj}; |
191 }, | 195 }, |
192 deserializeSequence: function(specs, array, memo) | 196 deserializeSequence: function(specs, array, memo) |
193 { | 197 { |
194 if (!array) | 198 if (!array) |
195 array = []; | 199 array = []; |
196 | 200 |
197 if (!memo) | 201 if (!memo) |
(...skipping 20 matching lines...) Expand all Loading... |
218 if (idx != -1) | 222 if (idx != -1) |
219 return memo.arrays[idx]; | 223 return memo.arrays[idx]; |
220 | 224 |
221 var array = []; | 225 var array = []; |
222 memo.specs.push(spec); | 226 memo.specs.push(spec); |
223 memo.arrays.push(array); | 227 memo.arrays.push(array); |
224 | 228 |
225 return this.deserializeSequence(spec.items, array, memo); | 229 return this.deserializeSequence(spec.items, array, memo); |
226 } | 230 } |
227 }, | 231 }, |
228 getObjectId: function(obj) | |
229 { | |
230 return this.objects.indexOf(obj); | |
231 }, | |
232 getProperty: function(objectId, property) | 232 getProperty: function(objectId, property) |
233 { | 233 { |
234 return this.deserializeResult( | 234 return this.deserializeResult( |
235 this.send( | 235 this.send( |
236 { | 236 { |
237 type: "getProperty", | 237 type: "getProperty", |
238 objectId: objectId, | 238 objectId: objectId, |
239 property: property | 239 property: property |
240 }) | 240 }) |
241 ); | 241 ); |
242 }, | 242 }, |
243 createProperty: function(property, enumerable) | 243 createProperty: function(property, enumerable) |
244 { | 244 { |
245 var proxy = this; | 245 var proxy = this; |
246 return { | 246 return { |
247 get: function() | 247 get: function() |
248 { | 248 { |
249 return proxy.getProperty(proxy.getObjectId(this), property); | 249 return proxy.getProperty(this.__proxyObjectId, property); |
250 }, | 250 }, |
251 set: function(value) | 251 set: function(value) |
252 { | 252 { |
253 proxy.checkResult( | 253 proxy.checkResult( |
254 proxy.send( | 254 proxy.send( |
255 { | 255 { |
256 type: "setProperty", | 256 type: "setProperty", |
257 objectId: proxy.getObjectId(this), | 257 objectId: this.__proxyObjectId, |
258 property: property, | 258 property: property, |
259 value: proxy.serialize(value) | 259 value: proxy.serialize(value) |
260 }) | 260 }) |
261 ); | 261 ); |
262 }, | 262 }, |
263 enumerable: enumerable, | 263 enumerable: enumerable, |
264 configurable: true | 264 configurable: true |
265 }; | 265 }; |
266 }, | 266 }, |
267 createFunction: function(objectId) | 267 createFunction: function(objectId) |
268 { | 268 { |
269 var proxy = this; | 269 var proxy = this; |
270 return function() | 270 return function() |
271 { | 271 { |
272 return proxy.deserializeResult( | 272 return proxy.deserializeResult( |
273 proxy.send( | 273 proxy.send( |
274 { | 274 { |
275 type: "callFunction", | 275 type: "callFunction", |
276 functionId: objectId, | 276 functionId: objectId, |
277 contextId: proxy.getObjectId(this), | 277 contextId: this.__proxyObjectId, |
278 args: Array.prototype.map.call( | 278 args: Array.prototype.map.call( |
279 arguments, | 279 arguments, |
280 proxy.serialize.bind(proxy) | 280 proxy.serialize.bind(proxy) |
281 ) | 281 ) |
282 }) | 282 }) |
283 ); | 283 ); |
284 }; | 284 }; |
285 }, | 285 }, |
286 handleCallback: function(message) | 286 handleCallback: function(message) |
287 { | 287 { |
(...skipping 13 matching lines...) Expand all Loading... |
301 if (obj) | 301 if (obj) |
302 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop
]; }); | 302 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop
]; }); |
303 else | 303 else |
304 { | 304 { |
305 if (objectInfo.isFunction) | 305 if (objectInfo.isFunction) |
306 obj = this.createFunction(objectId); | 306 obj = this.createFunction(objectId); |
307 else | 307 else |
308 obj = {}; | 308 obj = {}; |
309 | 309 |
310 this.objects[objectId] = obj; | 310 this.objects[objectId] = obj; |
| 311 Object.defineProperty(obj, "__proxyObjectId", {value: objectId}); |
311 } | 312 } |
312 | 313 |
313 var ignored = []; | 314 var ignored = []; |
314 if ("prototypeOf" in objectInfo) | 315 if ("prototypeOf" in objectInfo) |
315 { | 316 { |
316 var prototype = window[objectInfo.prototypeOf].prototype; | 317 var prototype = window[objectInfo.prototypeOf].prototype; |
317 | 318 |
318 ignored = Object.getOwnPropertyNames(prototype); | 319 ignored = Object.getOwnPropertyNames(prototype); |
319 ignored.splice(ignored.indexOf("constructor"), 1); | 320 ignored.splice(ignored.indexOf("constructor"), 1); |
320 | 321 |
321 obj.__proto__ = prototype; | 322 obj.__proto__ = prototype; |
322 } | 323 } |
323 else | 324 else |
324 { | 325 { |
325 if (objectInfo.isFunction) | 326 if (objectInfo.isFunction) |
326 ignored = Object.getOwnPropertyNames(function() {}); | 327 ignored = Object.getOwnPropertyNames(function() {}); |
327 else | 328 else |
328 ignored = []; | 329 ignored = []; |
329 | 330 |
330 if ("prototypeId" in objectInfo) | 331 if ("prototypeId" in objectInfo) |
331 obj.__proto__ = this.getObject(objectInfo.prototypeId); | 332 obj.__proto__ = this.getObject(objectInfo.prototypeId); |
332 else | 333 else |
333 obj.__proto__ = null; | 334 obj.__proto__ = null; |
334 } | 335 } |
335 | 336 |
336 for (var property in objectInfo.properties) | 337 for (var property in objectInfo.properties) |
| 338 { |
337 if (ignored.indexOf(property) == -1) | 339 if (ignored.indexOf(property) == -1) |
338 Object.defineProperty(obj, property, this.createProperty( | 340 { |
339 property, objectInfo.properties[property].enumerable | 341 var desc = Object.getOwnPropertyDescriptor(obj, property); |
340 )); | |
341 | 342 |
342 if (objectInfo.isFunction) | 343 if (!desc || desc.configurable) |
343 obj.prototype = this.getProperty(objectId, "prototype"); | 344 { |
| 345 Object.defineProperty(obj, property, this.createProperty( |
| 346 property, objectInfo.properties[property].enumerable |
| 347 )); |
| 348 } |
| 349 else if (desc.writable) |
| 350 obj[property] = this.getProperty(objectId, property); |
| 351 } |
| 352 } |
344 | 353 |
345 return obj; | 354 return obj; |
346 } | 355 } |
347 }; | 356 }; |
348 | 357 |
349 ext.backgroundPage = { | 358 ext.backgroundPage = { |
350 sendMessage: function(message, responseCallback) | 359 sendMessage: function(message, responseCallback) |
351 { | 360 { |
352 messageProxy.sendMessage(message, responseCallback, documentInfo); | 361 messageProxy.sendMessage(message, responseCallback, documentInfo); |
353 }, | 362 }, |
(...skipping 26 matching lines...) Expand all Loading... |
380 messageProxy.handleResponse(event.message); | 389 messageProxy.handleResponse(event.message); |
381 break; | 390 break; |
382 case "proxyCallback": | 391 case "proxyCallback": |
383 backgroundPageProxy.handleCallback(event.message); | 392 backgroundPageProxy.handleCallback(event.message); |
384 break; | 393 break; |
385 } | 394 } |
386 } | 395 } |
387 } | 396 } |
388 }); | 397 }); |
389 })(); | 398 })(); |
OLD | NEW |