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 /** | 5 /** |
6 * Returns a function that throws a 'not available' exception when called. | 6 * Returns a function that throws a 'not available' exception when called. |
7 * | 7 * |
8 * @param {string} messagePrefix text to prepend to the exception message. | 8 * @param {string} messagePrefix text to prepend to the exception message. |
9 */ | 9 */ |
10 function generateStub(messagePrefix) { | 10 function generateStub(messagePrefix, opt_messageSuffix) { |
11 return function() { | 11 return function() { |
12 throw messagePrefix + ' is not available in packaged apps.'; | 12 var message = messagePrefix + ' is not available in packaged apps.'; |
| 13 if (opt_messageSuffix) message = message + ' ' + opt_messageSuffix; |
| 14 throw message; |
13 }; | 15 }; |
14 } | 16 } |
15 | 17 |
16 /** | 18 /** |
17 * Replaces the given methods of the passed in object with stubs that throw | 19 * Replaces the given methods of the passed in object with stubs that throw |
18 * 'not available' exceptions when called. | 20 * 'not available' exceptions when called. |
19 * | 21 * |
20 * @param {Object} object The object whose methods to stub out. The prototype | 22 * @param {Object} object The object whose methods to stub out. The prototype |
21 * is preferred. | 23 * is preferred. |
22 * @param {string} objectName The display name to use in the error message | 24 * @param {string} objectName The display name to use in the error message |
(...skipping 11 matching lines...) Expand all Loading... |
34 * Replaces the given properties of the passed in object with stubs that throw | 36 * Replaces the given properties of the passed in object with stubs that throw |
35 * 'not available' exceptions when gotten. | 37 * 'not available' exceptions when gotten. |
36 * | 38 * |
37 * @param {Object} object The object whose properties to stub out. The prototype | 39 * @param {Object} object The object whose properties to stub out. The prototype |
38 * is preferred. | 40 * is preferred. |
39 * @param {string} objectName The display name to use in the error message | 41 * @param {string} objectName The display name to use in the error message |
40 * thrown by the stub (this is the name that the object is commonly referred | 42 * thrown by the stub (this is the name that the object is commonly referred |
41 * to by web developers, e.g. "document" instead of "HTMLDocument"). | 43 * to by web developers, e.g. "document" instead of "HTMLDocument"). |
42 * @param {Array.<string>} propertyNames property names | 44 * @param {Array.<string>} propertyNames property names |
43 */ | 45 */ |
44 function stubOutGetters(object, objectName, propertyNames) { | 46 function stubOutGetters(object, objectName, propertyNames, opt_messageSuffix) { |
45 propertyNames.forEach(function(propertyName) { | 47 propertyNames.forEach(function(propertyName) { |
46 object.__defineGetter__( | 48 object.__defineGetter__( |
47 propertyName, generateStub(objectName + '.' + propertyName)); | 49 propertyName, generateStub( |
| 50 objectName + '.' + propertyName, opt_messageSuffix)); |
48 }); | 51 }); |
49 } | 52 } |
50 | 53 |
51 // Disable document.open|close|write|etc. | 54 // Disable document.open|close|write|etc. |
52 stubOutMethods(HTMLDocument.prototype, 'document', | 55 stubOutMethods(HTMLDocument.prototype, 'document', |
53 ['open', 'clear', 'close', 'write', 'writeln']); | 56 ['open', 'clear', 'close', 'write', 'writeln']); |
54 | 57 |
55 // Deprecated document properties from | 58 // Deprecated document properties from |
56 // https://developer.mozilla.org/en/DOM/document. | 59 // https://developer.mozilla.org/en/DOM/document. |
57 stubOutGetters(document, 'document', | 60 stubOutGetters(document, 'document', |
(...skipping 10 matching lines...) Expand all Loading... |
68 | 71 |
69 // Disable modal dialogs. Shell windows disable these anyway, but it's nice to | 72 // Disable modal dialogs. Shell windows disable these anyway, but it's nice to |
70 // warn. | 73 // warn. |
71 stubOutMethods(Window.prototype, 'window', ['alert', 'confirm', 'prompt']); | 74 stubOutMethods(Window.prototype, 'window', ['alert', 'confirm', 'prompt']); |
72 | 75 |
73 // Disable window.*bar. | 76 // Disable window.*bar. |
74 stubOutGetters(window, 'window', | 77 stubOutGetters(window, 'window', |
75 ['locationbar', 'menubar', 'personalbar', 'scrollbars', 'statusbar', | 78 ['locationbar', 'menubar', 'personalbar', 'scrollbars', 'statusbar', |
76 'toolbar']); | 79 'toolbar']); |
77 | 80 |
| 81 // Disable window.localStorage. |
| 82 stubOutGetters(window, 'window', |
| 83 ['localStorage'], |
| 84 'Use chrome.storage.local instead.'); |
| 85 |
78 // Disable onunload, onbeforeunload. | 86 // Disable onunload, onbeforeunload. |
79 Window.prototype.__defineSetter__( | 87 Window.prototype.__defineSetter__( |
80 'onbeforeunload', generateStub('onbeforeunload')); | 88 'onbeforeunload', generateStub('onbeforeunload')); |
81 Window.prototype.__defineSetter__('onunload', generateStub('onunload')); | 89 Window.prototype.__defineSetter__('onunload', generateStub('onunload')); |
82 var windowAddEventListener = Window.prototype.addEventListener; | 90 var windowAddEventListener = Window.prototype.addEventListener; |
83 Window.prototype.addEventListener = function(type) { | 91 Window.prototype.addEventListener = function(type) { |
84 if (type === 'unload' || type === 'beforeunload') | 92 if (type === 'unload' || type === 'beforeunload') |
85 generateStub(type)(); | 93 generateStub(type)(); |
86 else | 94 else |
87 return windowAddEventListener.apply(window, arguments); | 95 return windowAddEventListener.apply(window, arguments); |
88 }; | 96 }; |
OLD | NEW |