OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 $console = window.console; | 5 var $console = window.console; |
6 | 6 |
7 /** | 7 /** |
8 * Returns a function that logs a 'not available' error to the console and | 8 * Returns a function that logs a 'not available' error to the console and |
9 * returns undefined. | 9 * returns undefined. |
10 * | 10 * |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 function disableMethods(object, objectName, methodNames, useThrowingStubs) { | 68 function disableMethods(object, objectName, methodNames, useThrowingStubs) { |
69 $Array.forEach(methodNames, function(methodName) { | 69 $Array.forEach(methodNames, function(methodName) { |
70 var messagePrefix = objectName + '.' + methodName + '()'; | 70 var messagePrefix = objectName + '.' + methodName + '()'; |
71 object[methodName] = useThrowingStubs ? | 71 object[methodName] = useThrowingStubs ? |
72 generateThrowingMethodStub(messagePrefix) : | 72 generateThrowingMethodStub(messagePrefix) : |
73 generateDisabledMethodStub(messagePrefix); | 73 generateDisabledMethodStub(messagePrefix); |
74 }); | 74 }); |
75 } | 75 } |
76 | 76 |
77 /** | 77 /** |
78 * Deletes the given methods from the passed in objext. | |
79 * | |
80 * This may be needed for feature detection as ('methodName' in object) will | |
81 * return false after this. | |
82 * | |
83 * @param {Object} object The object with methods to disable. The prototype is | |
84 * preferred. | |
85 * @param {Array.<string>} methodNames names of methods to delete. | |
86 */ | |
87 function deleteMethods(object, methodNames) { | |
88 $Array.forEach(methodNames, function(methodName) { | |
89 delete object[methodName]; | |
90 }); | |
91 } | |
92 | |
93 /** | |
78 * Replaces the given properties of the passed in object with stubs that log | 94 * Replaces the given properties of the passed in object with stubs that log |
79 * 'not available' warnings to the console and return undefined when gotten. If | 95 * 'not available' warnings to the console and return undefined when gotten. If |
80 * a property's setter is later invoked, the getter and setter are restored to | 96 * a property's setter is later invoked, the getter and setter are restored to |
81 * default behaviors. | 97 * default behaviors. |
82 * | 98 * |
83 * @param {Object} object The object with properties to disable. The prototype | 99 * @param {Object} object The object with properties to disable. The prototype |
84 * is preferred. | 100 * is preferred. |
85 * @param {string} objectName The display name to use in the error message | 101 * @param {string} objectName The display name to use in the error message |
86 * thrown by the getter stub (this is the name that the object is commonly | 102 * thrown by the getter stub (this is the name that the object is commonly |
87 * referred to by web developers, e.g. "document" instead of | 103 * referred to by web developers, e.g. "document" instead of |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 configurable: true, | 154 configurable: true, |
139 enumerable: false, | 155 enumerable: false, |
140 get: function() { | 156 get: function() { |
141 return; | 157 return; |
142 }, | 158 }, |
143 set: stub | 159 set: stub |
144 }); | 160 }); |
145 }); | 161 }); |
146 } | 162 } |
147 | 163 |
164 /** | |
165 * Deletes the given properties of the passed in object. | |
166 * | |
167 * This may be needed for feature detection as ('propertyName' in object) will | |
168 * return false after this. | |
169 * | |
170 * @param {Object} object The object with properties to disable. | |
171 * @param {Array.<string>} propertyNames names of properties to delete. | |
172 */ | |
173 function deleteProperties(object, propertyNames) { | |
174 $Array.forEach(propertyNames, function(propertyName) { | |
175 delete object[propertyName]; | |
176 }); | |
177 } | |
178 | |
148 // Disable benign Document methods. | 179 // Disable benign Document methods. |
149 disableMethods(HTMLDocument.prototype, 'document', ['open', 'clear', 'close']); | 180 disableMethods(HTMLDocument.prototype, 'document', ['open', 'clear', 'close']); |
150 | 181 |
151 // Replace evil Document methods with exception-throwing stubs. | 182 // Replace evil Document methods with exception-throwing stubs. |
152 disableMethods(HTMLDocument.prototype, 'document', ['write', 'writeln'], true); | 183 disableMethods(HTMLDocument.prototype, 'document', ['write', 'writeln'], true); |
153 | 184 |
154 // Disable history. | 185 // Disable history API. |
155 window.history = {}; | |
arv (Not doing code reviews)
2014/04/22 13:54:42
This one could have been replaced with a definePro
Inactive
2014/04/22 14:43:54
Would be much simpler, let me give this a try.
Inactive
2014/04/22 14:59:59
Done, thanks. Much simpler indeed.
| |
156 disableGetters(window.history, 'history', ['back', 'forward', 'go', 'length']); | 186 disableGetters(window.history, 'history', ['back', 'forward', 'go', 'length']); |
187 // These are part of the HTML5 History API that are feature detected, so we | |
188 // remove them altogether, allowing apps to have fallback behavior. | |
189 deleteProperties(window.history, ['state']); | |
190 deleteMethods(History.prototype, ['pushState', 'replaceState']); | |
157 | 191 |
158 // Disable find. | 192 // Disable find. |
159 disableMethods(Window.prototype, 'window', ['find']); | 193 disableMethods(Window.prototype, 'window', ['find']); |
160 | 194 |
161 // Disable modal dialogs. Shell windows disable these anyway, but it's nice to | 195 // Disable modal dialogs. Shell windows disable these anyway, but it's nice to |
162 // warn. | 196 // warn. |
163 disableMethods(Window.prototype, 'window', ['alert', 'confirm', 'prompt']); | 197 disableMethods(Window.prototype, 'window', ['alert', 'confirm', 'prompt']); |
164 | 198 |
165 // Disable window.*bar. | 199 // Disable window.*bar. |
166 disableGetters(window, 'window', | 200 disableGetters(window, 'window', |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 | 232 |
199 // Disable onunload, onbeforeunload. | 233 // Disable onunload, onbeforeunload. |
200 disableSetters(Window.prototype, 'window', ['onbeforeunload', 'onunload']); | 234 disableSetters(Window.prototype, 'window', ['onbeforeunload', 'onunload']); |
201 var windowAddEventListener = Window.prototype.addEventListener; | 235 var windowAddEventListener = Window.prototype.addEventListener; |
202 Window.prototype.addEventListener = function(type) { | 236 Window.prototype.addEventListener = function(type) { |
203 if (type === 'unload' || type === 'beforeunload') | 237 if (type === 'unload' || type === 'beforeunload') |
204 generateDisabledMethodStub(type)(); | 238 generateDisabledMethodStub(type)(); |
205 else | 239 else |
206 return $Function.apply(windowAddEventListener, window, arguments); | 240 return $Function.apply(windowAddEventListener, window, arguments); |
207 }; | 241 }; |
OLD | NEW |