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

Side by Side Diff: safari/ext/content.js

Issue 5670052883857408: Fixed properties defined on prototype in Safari background page proxy (Closed)
Patch Set: Rebased and fixed issue with inheritance Created March 6, 2014, 3:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 if (!result.succeed) 140 if (!result.succeed)
141 throw result.error; 141 throw result.error;
142 }, 142 },
143 deserializeResult: function(result) 143 deserializeResult: function(result)
144 { 144 {
145 this.checkResult(result); 145 this.checkResult(result);
146 return this.deserialize(result.result); 146 return this.deserialize(result.result);
147 }, 147 },
148 serialize: function(obj, memo) 148 serialize: function(obj, memo)
149 { 149 {
150 var objectId = this.objects.indexOf(obj); 150 if (typeof obj == "object" && obj != null || typeof obj == "function")
151 if (objectId != -1) 151 {
152 return {type: "hosted", objectId: objectId}; 152 if ("__proxyObjectId" in obj)
153 return {type: "hosted", objectId: obj.__proxyObjectId};
153 154
154 if (typeof obj == "function") 155 if (typeof obj == "function")
155 { 156 {
156 var callbackId = this.callbacks.indexOf(obj); 157 var callbackId;
157 if (callbackId == -1) 158 if ("__proxyCallbackId" in obj)
158 callbackId = this.callbacks.push(obj) - 1; 159 callbackId = obj.__proxyCallbackId;
160 else
161 {
162 callbackId = this.callbacks.push(obj) - 1;
163 Object.defineProperty(obj, "__proxyCallbackId", {value: callbackId}) ;
164 }
159 165
160 return {type: "callback", callbackId: callbackId, frameId: documentInfo. frameId}; 166 return {type: "callback", callbackId: callbackId, frameId: documentInf o.frameId};
161 }
162
163 if (typeof obj == "object" &&
164 obj != null &&
165 obj.constructor != Date &&
166 obj.constructor != RegExp)
167 {
168 if (!memo)
169 memo = {specs: [], objects: []};
170
171 var idx = memo.objects.indexOf(obj);
172 if (idx != -1)
173 return memo.specs[idx];
174
175 var spec = {};
176 memo.specs.push(spec);
177 memo.objects.push(obj);
178
179 if (obj.constructor == Array)
180 {
181 spec.type = "array";
182 spec.items = [];
183
184 for (var i = 0; i < obj.length; i++)
185 spec.items.push(this.serialize(obj[i], memo));
186 }
187 else
188 {
189 spec.type = "object";
190 spec.properties = {};
191
192 for (var k in obj)
193 spec.properties[k] = this.serialize(obj[k], memo);
194 } 167 }
195 168
196 return spec; 169 if (obj.constructor != Date && obj.constructor != RegExp)
170 {
171 if (!memo)
172 memo = {specs: [], objects: []};
173
174 var idx = memo.objects.indexOf(obj);
175 if (idx != -1)
176 return memo.specs[idx];
177
178 var spec = {};
179 memo.specs.push(spec);
180 memo.objects.push(obj);
181
182 if (obj.constructor == Array)
183 {
184 spec.type = "array";
185 spec.items = [];
186
187 for (var i = 0; i < obj.length; i++)
188 spec.items.push(this.serialize(obj[i], memo));
189 }
190 else
191 {
192 spec.type = "object";
193 spec.properties = {};
194
195 for (var k in obj)
196 spec.properties[k] = this.serialize(obj[k], memo);
197 }
198
199 return spec;
200 }
197 } 201 }
198 202
199 return {type: "value", value: obj}; 203 return {type: "value", value: obj};
200 }, 204 },
201 deserializeSequence: function(specs, array, memo) 205 deserializeSequence: function(specs, array, memo)
202 { 206 {
203 if (!array) 207 if (!array)
204 array = []; 208 array = [];
205 209
206 if (!memo) 210 if (!memo)
(...skipping 20 matching lines...) Expand all
227 if (idx != -1) 231 if (idx != -1)
228 return memo.arrays[idx]; 232 return memo.arrays[idx];
229 233
230 var array = []; 234 var array = [];
231 memo.specs.push(spec); 235 memo.specs.push(spec);
232 memo.arrays.push(array); 236 memo.arrays.push(array);
233 237
234 return this.deserializeSequence(spec.items, array, memo); 238 return this.deserializeSequence(spec.items, array, memo);
235 } 239 }
236 }, 240 },
237 getObjectId: function(obj)
238 {
239 return this.objects.indexOf(obj);
240 },
241 getProperty: function(objectId, property) 241 getProperty: function(objectId, property)
242 { 242 {
243 return this.deserializeResult( 243 return this.deserializeResult(
244 this.send( 244 this.send(
245 { 245 {
246 type: "getProperty", 246 type: "getProperty",
247 objectId: objectId, 247 objectId: objectId,
248 property: property 248 property: property
249 }) 249 })
250 ); 250 );
251 }, 251 },
252 createProperty: function(property, enumerable) 252 createProperty: function(property, enumerable)
253 { 253 {
254 var proxy = this; 254 var proxy = this;
255 return { 255 return {
256 get: function() 256 get: function()
257 { 257 {
258 return proxy.getProperty(proxy.getObjectId(this), property); 258 return proxy.getProperty(this.__proxyObjectId, property);
259 }, 259 },
260 set: function(value) 260 set: function(value)
261 { 261 {
262 proxy.checkResult( 262 proxy.checkResult(
263 proxy.send( 263 proxy.send(
264 { 264 {
265 type: "setProperty", 265 type: "setProperty",
266 objectId: proxy.getObjectId(this), 266 objectId: this.__proxyObjectId,
267 property: property, 267 property: property,
268 value: proxy.serialize(value) 268 value: proxy.serialize(value)
269 }) 269 })
270 ); 270 );
271 }, 271 },
272 enumerable: enumerable, 272 enumerable: enumerable,
273 configurable: true 273 configurable: true
274 }; 274 };
275 }, 275 },
276 createFunction: function(objectId) 276 createFunction: function(objectId)
277 { 277 {
278 var proxy = this; 278 var proxy = this;
279 return function() 279 return function()
280 { 280 {
281 return proxy.deserializeResult( 281 return proxy.deserializeResult(
282 proxy.send( 282 proxy.send(
283 { 283 {
284 type: "callFunction", 284 type: "callFunction",
285 functionId: objectId, 285 functionId: objectId,
286 contextId: proxy.getObjectId(this), 286 contextId: this.__proxyObjectId,
287 args: Array.prototype.map.call( 287 args: Array.prototype.map.call(
288 arguments, 288 arguments,
289 proxy.serialize.bind(proxy) 289 proxy.serialize.bind(proxy)
290 ) 290 )
291 }) 291 })
292 ); 292 );
293 }; 293 };
294 }, 294 },
295 handleCallback: function(message) 295 handleCallback: function(message)
296 { 296 {
(...skipping 13 matching lines...) Expand all
310 if (obj) 310 if (obj)
311 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop ]; }); 311 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop ]; });
312 else 312 else
313 { 313 {
314 if (objectInfo.isFunction) 314 if (objectInfo.isFunction)
315 obj = this.createFunction(objectId); 315 obj = this.createFunction(objectId);
316 else 316 else
317 obj = {}; 317 obj = {};
318 318
319 this.objects[objectId] = obj; 319 this.objects[objectId] = obj;
320 Object.defineProperty(obj, "__proxyObjectId", {value: objectId});
320 } 321 }
321 322
322 var ignored = []; 323 var excluded = [];
324 var included = [];
323 if ("prototypeOf" in objectInfo) 325 if ("prototypeOf" in objectInfo)
324 { 326 {
325 var prototype = window[objectInfo.prototypeOf].prototype; 327 var prototype = window[objectInfo.prototypeOf].prototype;
326 328
327 ignored = Object.getOwnPropertyNames(prototype); 329 excluded = Object.getOwnPropertyNames(prototype);
328 ignored.splice(ignored.indexOf("constructor"), 1); 330 included = ["constructor"];
329 331
330 obj.__proto__ = prototype; 332 obj.__proto__ = prototype;
331 } 333 }
332 else 334 else
333 { 335 {
334 if (objectInfo.isFunction) 336 if (objectInfo.isFunction)
335 ignored = Object.getOwnPropertyNames(function() {}); 337 {
336 else 338 excluded = Object.getOwnPropertyNames(function() {});
337 ignored = []; 339 included = ["prototype"];
340 }
338 341
339 if ("prototypeId" in objectInfo) 342 if ("prototypeId" in objectInfo)
340 obj.__proto__ = this.getObject(objectInfo.prototypeId); 343 obj.__proto__ = this.getObject(objectInfo.prototypeId);
341 else 344 else
342 obj.__proto__ = null; 345 obj.__proto__ = null;
343 } 346 }
344 347
345 for (var property in objectInfo.properties) 348 for (var property in objectInfo.properties)
346 if (ignored.indexOf(property) == -1) 349 {
347 Object.defineProperty(obj, property, this.createProperty( 350 if (excluded.indexOf(property) == -1 || included.indexOf(property) != -1 )
348 property, objectInfo.properties[property].enumerable 351 {
349 )); 352 var desc = Object.getOwnPropertyDescriptor(obj, property);
350 353
351 if (objectInfo.isFunction) 354 if (!desc || desc.configurable)
352 obj.prototype = this.getProperty(objectId, "prototype"); 355 {
356 Object.defineProperty(obj, property, this.createProperty(
357 property, objectInfo.properties[property].enumerable
358 ));
359 }
360 else if (desc.writable)
361 obj[property] = this.getProperty(objectId, property);
362 }
363 }
353 364
354 return obj; 365 return obj;
355 } 366 }
356 }; 367 };
357 368
358 ext.backgroundPage = { 369 ext.backgroundPage = {
359 sendMessage: function(message, responseCallback) 370 sendMessage: function(message, responseCallback)
360 { 371 {
361 messageProxy.sendMessage(message, responseCallback, documentInfo); 372 messageProxy.sendMessage(message, responseCallback, documentInfo);
362 }, 373 },
(...skipping 26 matching lines...) Expand all
389 messageProxy.handleResponse(event.message); 400 messageProxy.handleResponse(event.message);
390 break; 401 break;
391 case "proxyCallback": 402 case "proxyCallback":
392 backgroundPageProxy.handleCallback(event.message); 403 backgroundPageProxy.handleCallback(event.message);
393 break; 404 break;
394 } 405 }
395 } 406 }
396 } 407 }
397 }); 408 });
398 })(); 409 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld