Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: chrome/renderer/resources/extensions/json.js

Issue 12287011: Move the chromeHidden.toJSON paranoia out of event.js and into json.js, a new (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test fixup Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 };
OLDNEW
« no previous file with comments | « chrome/renderer/resources/extensions/event.js ('k') | chrome/renderer/resources/extensions/miscellaneous_bindings.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698