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

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: Fix for failing tests 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.
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 // TODO(robwu): Fix callers and uncomment this assertion.
94 // DCHECK(!(privateClass instanceof $Object.self));
95
96 $Object.setPrototypeOf(exposed, null);
97
98 // This should be called by publicClass.
99 privates(publicClass).constructPrivate = function(self, args) {
100 if (!(self instanceof publicClass)) {
101 throw new Error('Please use "new ' + publicClass.name + '"');
102 }
103 // The "instanceof publicClass" check can easily be spoofed, so we check
104 // whether the private impl is already set before continuing.
105 var privateSelf = privates(self);
106 if ('impl' in privateSelf) {
107 throw new Error('Object ' + publicClass.name + ' is already constructed');
108 }
109 var privateObj = $Object.create(privateClass.prototype);
110 $Function.apply(privateClass, privateObj, args);
111 privateObj.wrapper = self;
112 privateSelf.impl = privateObj;
113 };
114
115 function getPrivateImpl(self) {
116 var impl = privates(self).impl;
117 if (!(impl instanceof privateClass)) {
118 // Either the object is not constructed, or the property descriptor is
119 // used on a target that is not an instance of publicClass.
120 throw new Error('impl is not an instance of ' + privateClass.name);
121 }
122 return impl;
123 }
124
125 var publicClassPrototype = {
126 // The final prototype will be assigned at the end of this method.
127 __proto__: null,
128 constructor: publicClass,
129 };
89 130
90 if ('functions' in exposed) { 131 if ('functions' in exposed) {
91 $Array.forEach(exposed.functions, function(func) { 132 $Array.forEach(exposed.functions, function(func) {
92 publicClass.prototype[func] = function() { 133 publicClassPrototype[func] = function() {
93 var impl = privates(this).impl; 134 var impl = getPrivateImpl(this);
94 return $Function.apply(impl[func], impl, arguments); 135 return $Function.apply(impl[func], impl, arguments);
95 }; 136 };
96 }); 137 });
97 } 138 }
98 139
99 if ('properties' in exposed) { 140 if ('properties' in exposed) {
100 $Array.forEach(exposed.properties, function(prop) { 141 $Array.forEach(exposed.properties, function(prop) {
101 $Object.defineProperty(publicClass.prototype, prop, { 142 $Object.defineProperty(publicClassPrototype, prop, {
143 __proto__: null,
102 enumerable: true, 144 enumerable: true,
103 get: function() { 145 get: function() {
104 return privates(this).impl[prop]; 146 return getPrivateImpl(this)[prop];
105 }, 147 },
106 set: function(value) { 148 set: function(value) {
107 var impl = privates(this).impl; 149 var impl = getPrivateImpl(this);
108 delete impl[prop]; 150 delete impl[prop];
109 impl[prop] = value; 151 impl[prop] = value;
110 } 152 }
111 }); 153 });
112 }); 154 });
113 } 155 }
114 156
115 if ('readonly' in exposed) { 157 if ('readonly' in exposed) {
116 $Array.forEach(exposed.readonly, function(readonly) { 158 $Array.forEach(exposed.readonly, function(readonly) {
117 $Object.defineProperty(publicClass.prototype, readonly, { 159 $Object.defineProperty(publicClassPrototype, readonly, {
160 __proto__: null,
118 enumerable: true, 161 enumerable: true,
119 get: function() { 162 get: function() {
120 return privates(this).impl[readonly]; 163 return getPrivateImpl(this)[readonly];
121 }, 164 },
122 }); 165 });
123 }); 166 });
124 } 167 }
125 168
169 // The prototype properties have been installed. Now we can safely assign an
170 // unsafe prototype and export the class to the public.
171 var superclass = exposed.superclass || $Object.self;
172 $Object.setPrototypeOf(publicClassPrototype, superclass.prototype);
173 publicClass.prototype = publicClassPrototype;
174
126 return publicClass; 175 return publicClass;
127 } 176 }
128 177
129 /** 178 /**
130 * Returns a deep copy of |value|. The copy will have no references to nested 179 * Returns a deep copy of |value|. The copy will have no references to nested
131 * values of |value|. 180 * values of |value|.
132 */ 181 */
133 function deepCopy(value) { 182 function deepCopy(value) {
134 return nativeDeepCopy(value); 183 return nativeDeepCopy(value);
135 } 184 }
(...skipping 29 matching lines...) Expand all
165 $Function.apply(func, null, args); 214 $Function.apply(func, null, args);
166 }); 215 });
167 } 216 }
168 217
169 exports.$set('forEach', forEach); 218 exports.$set('forEach', forEach);
170 exports.$set('loadTypeSchema', loadTypeSchema); 219 exports.$set('loadTypeSchema', loadTypeSchema);
171 exports.$set('lookup', lookup); 220 exports.$set('lookup', lookup);
172 exports.$set('expose', expose); 221 exports.$set('expose', expose);
173 exports.$set('deepCopy', deepCopy); 222 exports.$set('deepCopy', deepCopy);
174 exports.$set('promise', promise); 223 exports.$set('promise', promise);
OLDNEW
« no previous file with comments | « extensions/renderer/resources/messaging.js ('k') | extensions/renderer/resources/web_request_internal_custom_bindings.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698