| 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 forEach = require('utils').forEach; |
| 6 |
| 5 /** | 7 /** |
| 6 * Returns a function that throws a 'not available' exception when called. | 8 * Returns a function that throws a 'not available' exception when called. |
| 7 * | 9 * |
| 8 * @param {string} messagePrefix text to prepend to the exception message. | 10 * @param {string} messagePrefix text to prepend to the exception message. |
| 9 */ | 11 */ |
| 10 function generateDisabledMethodStub(messagePrefix, opt_messageSuffix) { | 12 function generateDisabledMethodStub(messagePrefix, opt_messageSuffix) { |
| 11 return function() { | 13 return function() { |
| 12 var message = messagePrefix + ' is not available in packaged apps.'; | 14 var message = messagePrefix + ' is not available in packaged apps.'; |
| 13 if (opt_messageSuffix) message = message + ' ' + opt_messageSuffix; | 15 if (opt_messageSuffix) message = message + ' ' + opt_messageSuffix; |
| 14 throw message; | 16 throw message; |
| 15 }; | 17 }; |
| 16 } | 18 } |
| 17 | 19 |
| 18 /** | 20 /** |
| 19 * Replaces the given methods of the passed in object with stubs that throw | 21 * Replaces the given methods of the passed in object with stubs that throw |
| 20 * 'not available' exceptions when called. | 22 * 'not available' exceptions when called. |
| 21 * | 23 * |
| 22 * @param {Object} object The object with methods to disable. The prototype is | 24 * @param {Object} object The object with methods to disable. The prototype is |
| 23 * preferred. | 25 * preferred. |
| 24 * @param {string} objectName The display name to use in the error message | 26 * @param {string} objectName The display name to use in the error message |
| 25 * thrown by the stub (this is the name that the object is commonly referred | 27 * thrown by the stub (this is the name that the object is commonly referred |
| 26 * to by web developers, e.g. "document" instead of "HTMLDocument"). | 28 * to by web developers, e.g. "document" instead of "HTMLDocument"). |
| 27 * @param {Array.<string>} methodNames names of methods to disable. | 29 * @param {Array.<string>} methodNames names of methods to disable. |
| 28 */ | 30 */ |
| 29 function disableMethods(object, objectName, methodNames) { | 31 function disableMethods(object, objectName, methodNames) { |
| 30 methodNames.forEach(function(methodName) { | 32 forEach(methodNames, function(i, methodName) { |
| 31 object[methodName] = | 33 object[methodName] = |
| 32 generateDisabledMethodStub(objectName + '.' + methodName + '()'); | 34 generateDisabledMethodStub(objectName + '.' + methodName + '()'); |
| 33 }); | 35 }); |
| 34 } | 36 } |
| 35 | 37 |
| 36 /** | 38 /** |
| 37 * Replaces the given properties of the passed in object with stubs that throw | 39 * Replaces the given properties of the passed in object with stubs that throw |
| 38 * 'not available' exceptions when gotten. If a property's setter is later | 40 * 'not available' exceptions when gotten. If a property's setter is later |
| 39 * invoked, the getter and setter are restored to default behaviors. | 41 * invoked, the getter and setter are restored to default behaviors. |
| 40 * | 42 * |
| 41 * @param {Object} object The object with properties to disable. The prototype | 43 * @param {Object} object The object with properties to disable. The prototype |
| 42 * is preferred. | 44 * is preferred. |
| 43 * @param {string} objectName The display name to use in the error message | 45 * @param {string} objectName The display name to use in the error message |
| 44 * thrown by the getter stub (this is the name that the object is commonly | 46 * thrown by the getter stub (this is the name that the object is commonly |
| 45 * referred to by web developers, e.g. "document" instead of | 47 * referred to by web developers, e.g. "document" instead of |
| 46 * "HTMLDocument"). | 48 * "HTMLDocument"). |
| 47 * @param {Array.<string>} propertyNames names of properties to disable. | 49 * @param {Array.<string>} propertyNames names of properties to disable. |
| 48 */ | 50 */ |
| 49 function disableGetters(object, objectName, propertyNames, opt_messageSuffix) { | 51 function disableGetters(object, objectName, propertyNames, opt_messageSuffix) { |
| 50 propertyNames.forEach(function(propertyName) { | 52 forEach(propertyNames, function(i, propertyName) { |
| 51 var stub = generateDisabledMethodStub(objectName + '.' + propertyName, | 53 var stub = generateDisabledMethodStub(objectName + '.' + propertyName, |
| 52 opt_messageSuffix); | 54 opt_messageSuffix); |
| 53 stub._is_platform_app_disabled_getter = true; | 55 stub._is_platform_app_disabled_getter = true; |
| 54 object.__defineGetter__(propertyName, stub); | 56 object.__defineGetter__(propertyName, stub); |
| 55 | 57 |
| 56 object.__defineSetter__(propertyName, function(value) { | 58 object.__defineSetter__(propertyName, function(value) { |
| 57 var getter = this.__lookupGetter__(propertyName); | 59 var getter = this.__lookupGetter__(propertyName); |
| 58 if (!getter || getter._is_platform_app_disabled_getter) { | 60 if (!getter || getter._is_platform_app_disabled_getter) { |
| 59 // The stub getter is still defined. Blow-away the property to restore | 61 // The stub getter is still defined. Blow-away the property to restore |
| 60 // default getter/setter behaviors and re-create it with the given | 62 // default getter/setter behaviors and re-create it with the given |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 'onbeforeunload', generateDisabledMethodStub('onbeforeunload')); | 123 'onbeforeunload', generateDisabledMethodStub('onbeforeunload')); |
| 122 Window.prototype.__defineSetter__( | 124 Window.prototype.__defineSetter__( |
| 123 'onunload', generateDisabledMethodStub('onunload')); | 125 'onunload', generateDisabledMethodStub('onunload')); |
| 124 var windowAddEventListener = Window.prototype.addEventListener; | 126 var windowAddEventListener = Window.prototype.addEventListener; |
| 125 Window.prototype.addEventListener = function(type) { | 127 Window.prototype.addEventListener = function(type) { |
| 126 if (type === 'unload' || type === 'beforeunload') | 128 if (type === 'unload' || type === 'beforeunload') |
| 127 generateDisabledMethodStub(type)(); | 129 generateDisabledMethodStub(type)(); |
| 128 else | 130 else |
| 129 return windowAddEventListener.apply(window, arguments); | 131 return windowAddEventListener.apply(window, arguments); |
| 130 }; | 132 }; |
| OLD | NEW |