OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Local implementation of JSON.parse & JSON.stringify that protect us |
| 6 // from being clobbered by an extension. |
| 7 // |
| 8 // Make sure this is run before any extension code runs! |
| 9 // |
| 10 // TODO(aa): This makes me so sad. We shouldn't need it, as we can just pass |
| 11 // Values directly over IPC without serializing to strings and use |
| 12 // JSONValueConverter. |
| 13 var classes = [Object, Array, Date, String, Number, Boolean]; |
| 14 var realToJSON = classes.map(function(cls) { return cls.prototype.toJSON; }); |
| 15 var realStringify = JSON.stringify; |
| 16 var realParse = JSON.parse; |
| 17 |
| 18 exports.stringify = function stringify(thing) { |
| 19 // I guess if we're being this paranoid we shouldn't use any of the methods |
| 20 // on Object/Array/etc either (forEach, push, etc). |
| 21 var saved = []; |
| 22 for (var i = 0; i < classes.length; i++) { |
| 23 var prototype = classes[i].prototype; |
| 24 if (prototype.toJSON !== realToJSON[i]) { |
| 25 saved[i] = prototype.toJSON; |
| 26 prototype.toJSON = realToJSON[i]; |
| 27 } |
| 28 } |
| 29 try { |
| 30 return realStringify(thing); |
| 31 } finally { |
| 32 for (var i = 0; i < classes.length; i++) { |
| 33 if (saved.hasOwnProperty(i)) |
| 34 classes[i].prototype.toJSON = saved[i]; |
| 35 } |
| 36 } |
| 37 }; |
| 38 |
| 39 exports.parse = function parse(thing) { |
| 40 return realParse(thing); |
| 41 }; |
OLD | NEW |