OLD | NEW |
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 $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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 * conjunction with feature detection. A great example is document.write(), as | 54 * conjunction with feature detection. A great example is document.write(), as |
55 * the HTML5 specification recommends against using it, and says that its | 55 * the HTML5 specification recommends against using it, and says that its |
56 * behavior is unreliable. No reasonable library code should ever use it. | 56 * behavior is unreliable. No reasonable library code should ever use it. |
57 * HTML5 spec: http://www.w3.org/TR/html5/dom.html#dom-document-write | 57 * HTML5 spec: http://www.w3.org/TR/html5/dom.html#dom-document-write |
58 * | 58 * |
59 * @param {Object} object The object with methods to disable. The prototype is | 59 * @param {Object} object The object with methods to disable. The prototype is |
60 * preferred. | 60 * preferred. |
61 * @param {string} objectName The display name to use in the error message | 61 * @param {string} objectName The display name to use in the error message |
62 * thrown by the stub (this is the name that the object is commonly referred | 62 * thrown by the stub (this is the name that the object is commonly referred |
63 * to by web developers, e.g. "document" instead of "HTMLDocument"). | 63 * to by web developers, e.g. "document" instead of "HTMLDocument"). |
64 * @param {Array.<string>} methodNames names of methods to disable. | 64 * @param {Array<string>} methodNames names of methods to disable. |
65 * @param {Boolean} useThrowingStubs if true, the replaced methods will throw | 65 * @param {Boolean} useThrowingStubs if true, the replaced methods will throw |
66 * an error instead of silently returning undefined | 66 * an error instead of silently returning undefined |
67 */ | 67 */ |
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 * Replaces the given properties of the passed in object with stubs that log | 78 * 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 | 79 * '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 | 80 * a property's setter is later invoked, the getter and setter are restored to |
81 * default behaviors. | 81 * default behaviors. |
82 * | 82 * |
83 * @param {Object} object The object with properties to disable. The prototype | 83 * @param {Object} object The object with properties to disable. The prototype |
84 * is preferred. | 84 * is preferred. |
85 * @param {string} objectName The display name to use in the error message | 85 * @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 | 86 * 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 | 87 * referred to by web developers, e.g. "document" instead of |
88 * "HTMLDocument"). | 88 * "HTMLDocument"). |
89 * @param {Array.<string>} propertyNames names of properties to disable. | 89 * @param {Array<string>} propertyNames names of properties to disable. |
90 */ | 90 */ |
91 function disableGetters(object, objectName, propertyNames, opt_messageSuffix) { | 91 function disableGetters(object, objectName, propertyNames, opt_messageSuffix) { |
92 $Array.forEach(propertyNames, function(propertyName) { | 92 $Array.forEach(propertyNames, function(propertyName) { |
93 var stub = generateDisabledMethodStub(objectName + '.' + propertyName, | 93 var stub = generateDisabledMethodStub(objectName + '.' + propertyName, |
94 opt_messageSuffix); | 94 opt_messageSuffix); |
95 stub._is_platform_app_disabled_getter = true; | 95 stub._is_platform_app_disabled_getter = true; |
96 $Object.defineProperty(object, propertyName, { | 96 $Object.defineProperty(object, propertyName, { |
97 configurable: true, | 97 configurable: true, |
98 enumerable: false, | 98 enumerable: false, |
99 get: stub, | 99 get: stub, |
(...skipping 21 matching lines...) Expand all Loading... |
121 /** | 121 /** |
122 * Replaces the given properties of the passed in object with stubs that log | 122 * Replaces the given properties of the passed in object with stubs that log |
123 * 'not available' warnings to the console when set. | 123 * 'not available' warnings to the console when set. |
124 * | 124 * |
125 * @param {Object} object The object with properties to disable. The prototype | 125 * @param {Object} object The object with properties to disable. The prototype |
126 * is preferred. | 126 * is preferred. |
127 * @param {string} objectName The display name to use in the error message | 127 * @param {string} objectName The display name to use in the error message |
128 * thrown by the setter stub (this is the name that the object is commonly | 128 * thrown by the setter stub (this is the name that the object is commonly |
129 * referred to by web developers, e.g. "document" instead of | 129 * referred to by web developers, e.g. "document" instead of |
130 * "HTMLDocument"). | 130 * "HTMLDocument"). |
131 * @param {Array.<string>} propertyNames names of properties to disable. | 131 * @param {Array<string>} propertyNames names of properties to disable. |
132 */ | 132 */ |
133 function disableSetters(object, objectName, propertyNames, opt_messageSuffix) { | 133 function disableSetters(object, objectName, propertyNames, opt_messageSuffix) { |
134 $Array.forEach(propertyNames, function(propertyName) { | 134 $Array.forEach(propertyNames, function(propertyName) { |
135 var stub = generateDisabledMethodStub(objectName + '.' + propertyName, | 135 var stub = generateDisabledMethodStub(objectName + '.' + propertyName, |
136 opt_messageSuffix); | 136 opt_messageSuffix); |
137 $Object.defineProperty(object, propertyName, { | 137 $Object.defineProperty(object, propertyName, { |
138 configurable: true, | 138 configurable: true, |
139 enumerable: false, | 139 enumerable: false, |
140 get: function() { | 140 get: function() { |
141 return; | 141 return; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 198 |
199 // Disable onunload, onbeforeunload. | 199 // Disable onunload, onbeforeunload. |
200 disableSetters(Window.prototype, 'window', ['onbeforeunload', 'onunload']); | 200 disableSetters(Window.prototype, 'window', ['onbeforeunload', 'onunload']); |
201 var windowAddEventListener = Window.prototype.addEventListener; | 201 var windowAddEventListener = Window.prototype.addEventListener; |
202 Window.prototype.addEventListener = function(type) { | 202 Window.prototype.addEventListener = function(type) { |
203 if (type === 'unload' || type === 'beforeunload') | 203 if (type === 'unload' || type === 'beforeunload') |
204 generateDisabledMethodStub(type)(); | 204 generateDisabledMethodStub(type)(); |
205 else | 205 else |
206 return $Function.apply(windowAddEventListener, window, arguments); | 206 return $Function.apply(windowAddEventListener, window, arguments); |
207 }; | 207 }; |
OLD | NEW |