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

Side by Side Diff: extensions/renderer/resources/utils.js

Issue 1903303002: Ensure that privates are private. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase for safe_builtins conflict Created 4 years, 8 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 var createClassWrapper = requireNative('utils').createClassWrapper;
6 var nativeDeepCopy = requireNative('utils').deepCopy; 5 var nativeDeepCopy = requireNative('utils').deepCopy;
7 var schemaRegistry = requireNative('schema_registry'); 6 var schemaRegistry = requireNative('schema_registry');
8 var CHECK = requireNative('logging').CHECK; 7 var CHECK = requireNative('logging').CHECK;
9 var DCHECK = requireNative('logging').DCHECK; 8 var DCHECK = requireNative('logging').DCHECK;
10 var WARNING = requireNative('logging').WARNING; 9 var WARNING = requireNative('logging').WARNING;
11 10
12 /** 11 /**
13 * An object forEach. Calls |f| with each (key, value) pair of |obj|, using 12 * An object forEach. Calls |f| with each (key, value) pair of |obj|, using
14 * |self| as the target. 13 * |self| as the target.
15 * @param {Object} obj The object to iterate over. 14 * @param {Object} obj The object to iterate over.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 var types = schemaRegistry.GetSchema(schemaName).types; 57 var types = schemaRegistry.GetSchema(schemaName).types;
59 } 58 }
60 for (var i = 0; i < types.length; ++i) { 59 for (var i = 0; i < types.length; ++i) {
61 if (types[i].id == typeName) 60 if (types[i].id == typeName)
62 return types[i]; 61 return types[i];
63 } 62 }
64 return null; 63 return null;
65 } 64 }
66 65
67 /** 66 /**
68 * Takes a private class implementation |cls| and exposes a subset of its 67 * Takes a private class implementation |privateClass| and exposes a subset of
69 * methods |functions| and properties |properties| and |readonly| in a public 68 * its methods |functions| and properties |properties| and |readonly| to a
70 * wrapper class that it returns. Within bindings code, you can access the 69 * public wrapper class that should be passed in. Within bindings code, you can
71 * implementation from an instance of the wrapper class using 70 * access the implementation from an instance of the wrapper class using
72 * privates(instance).impl, and from the implementation class you can access 71 * privates(instance).impl, and from the implementation class you can access
73 * the wrapper using this.wrapper (or implInstance.wrapper if you have another 72 * the wrapper using this.wrapper (or implInstance.wrapper if you have another
74 * instance of the implementation class). 73 * instance of the implementation class).
75 * @param {string} name The name of the exposed wrapper class. 74 *
76 * @param {Object} cls The class implementation. 75 * |publicClass| should be a constructor that calls constructPrivate() like so:
76 *
77 * privates(publicClass).constructPrivate(this, arguments);
78 *
79 * @param {function} publicClass The publicly exposed wrapper class. This must
80 * be a named function, and the name appears in stack traces.
Devlin 2016/04/22 18:52:46 nit: I think this should be +4 indentation from th
robwu 2016/04/22 20:33:43 Done.
81 * @param {Object} privateClass The class implementation.
77 * @param {{superclass: ?Function, 82 * @param {{superclass: ?Function,
78 * functions: ?Array<string>, 83 * functions: ?Array<string>,
79 * properties: ?Array<string>, 84 * properties: ?Array<string>,
80 * readonly: ?Array<string>}} exposed The names of properties on the 85 * readonly: ?Array<string>}} exposed The names of properties on the
81 * implementation class to be exposed. |superclass| represents the 86 * implementation class to be exposed. |superclass| represents the
82 * constructor of the class to be used as the superclass of the exposed 87 * constructor of the class to be used as the superclass of the exposed
83 * class; |functions| represents the names of functions which should be 88 * class; |functions| represents the names of functions which should be
84 * delegated to the implementation; |properties| are gettable/settable 89 * delegated to the implementation; |properties| are gettable/settable
85 * properties and |readonly| are read-only properties. 90 * properties and |readonly| are read-only properties.
86 */ 91 */
87 function expose(name, cls, exposed) { 92 function expose(publicClass, privateClass, exposed) {
88 var publicClass = createClassWrapper(name, cls, exposed.superclass); 93 DCHECK(!(privateClass instanceof $Object.self));
94
95 $Object.setPrototypeOf(exposed, null);
96
97 // This should be called by publicClass.
98 privates(publicClass).constructPrivate = function(self, args) {
99 if (!(self instanceof publicClass)) {
100 throw new Error('Please use "new ' + publicClass.name + '"');
101 }
102 // The "instanceof publicClass" check can easily be spoofed, so we check
103 // whether the private impl is already set before continuing.
104 var privateSelf = privates(self);
105 if ('impl' in privateSelf) {
106 throw new Error('Object ' + publicClass.name + ' is already constructed');
107 }
108 var privateObj = $Object.create(privateClass.prototype);
109 $Function.apply(privateClass, privateObj, args);
110 privateObj.wrapper = self;
111 privateSelf.impl = privateObj;
112 };
113
114 function getPrivateImpl(self) {
115 var impl = privates(self).impl;
116 if (!(impl instanceof privateClass)) {
117 // Either the object is not constructed, or the property descriptor is
118 // used on a target that is not an instance of publicClass.
119 throw new Error('impl is not an instance of ' + privateClass.name);
120 }
121 return impl;
122 }
123
124 var publicClassPrototype = {
125 // The final prototype will be assigned at the end of this method.
126 __proto__: null,
127 };
89 128
90 if ('functions' in exposed) { 129 if ('functions' in exposed) {
91 $Array.forEach(exposed.functions, function(func) { 130 $Array.forEach(exposed.functions, function(func) {
92 publicClass.prototype[func] = function() { 131 publicClassPrototype[func] = function() {
93 var impl = privates(this).impl; 132 var impl = getPrivateImpl(this);
94 return $Function.apply(impl[func], impl, arguments); 133 return $Function.apply(impl[func], impl, arguments);
95 }; 134 };
96 }); 135 });
97 } 136 }
98 137
99 if ('properties' in exposed) { 138 if ('properties' in exposed) {
100 $Array.forEach(exposed.properties, function(prop) { 139 $Array.forEach(exposed.properties, function(prop) {
101 $Object.defineProperty(publicClass.prototype, prop, { 140 $Object.defineProperty(publicClassPrototype, prop, {
141 __proto__: null,
102 enumerable: true, 142 enumerable: true,
103 get: function() { 143 get: function() {
104 return privates(this).impl[prop]; 144 return getPrivateImpl(this)[prop];
105 }, 145 },
106 set: function(value) { 146 set: function(value) {
107 var impl = privates(this).impl; 147 var impl = getPrivateImpl(this);
108 delete impl[prop]; 148 delete impl[prop];
109 impl[prop] = value; 149 impl[prop] = value;
110 } 150 }
111 }); 151 });
112 }); 152 });
113 } 153 }
114 154
115 if ('readonly' in exposed) { 155 if ('readonly' in exposed) {
116 $Array.forEach(exposed.readonly, function(readonly) { 156 $Array.forEach(exposed.readonly, function(readonly) {
117 $Object.defineProperty(publicClass.prototype, readonly, { 157 $Object.defineProperty(publicClassPrototype, readonly, {
158 __proto__: null,
118 enumerable: true, 159 enumerable: true,
119 get: function() { 160 get: function() {
120 return privates(this).impl[readonly]; 161 return getPrivateImpl(this)[readonly];
121 }, 162 },
122 }); 163 });
123 }); 164 });
124 } 165 }
125 166
167 // The prototype properties have been installed. Now we can safely assign an
168 // unsafe prototype and export the class to the public.
169 publicClassPrototype.__proto__ = exposed.superclass.prototype || $Object.self;
Devlin 2016/04/22 18:52:46 Won't this throw if exposed.superclass is undefine
robwu 2016/04/22 20:33:43 Fixed. And I should also have used $Object.self.pr
170 publicClass.prototype = publicClassPrototype;
171
126 return publicClass; 172 return publicClass;
127 } 173 }
128 174
129 /** 175 /**
130 * Returns a deep copy of |value|. The copy will have no references to nested 176 * Returns a deep copy of |value|. The copy will have no references to nested
131 * values of |value|. 177 * values of |value|.
132 */ 178 */
133 function deepCopy(value) { 179 function deepCopy(value) {
134 return nativeDeepCopy(value); 180 return nativeDeepCopy(value);
135 } 181 }
(...skipping 29 matching lines...) Expand all
165 $Function.apply(func, null, args); 211 $Function.apply(func, null, args);
166 }); 212 });
167 } 213 }
168 214
169 exports.$set('forEach', forEach); 215 exports.$set('forEach', forEach);
170 exports.$set('loadTypeSchema', loadTypeSchema); 216 exports.$set('loadTypeSchema', loadTypeSchema);
171 exports.$set('lookup', lookup); 217 exports.$set('lookup', lookup);
172 exports.$set('expose', expose); 218 exports.$set('expose', expose);
173 exports.$set('deepCopy', deepCopy); 219 exports.$set('deepCopy', deepCopy);
174 exports.$set('promise', promise); 220 exports.$set('promise', promise);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698