Throw exceptions when attempts are made to use localStorage in packaged apps.

BUG=130224


Review URL: https://chromiumcodereview.appspot.com/10832352

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152232 0039d316-1c4b-4281-b951-d872f2087c98
parent 94974227
......@@ -7,9 +7,11 @@
*
* @param {string} messagePrefix text to prepend to the exception message.
*/
function generateStub(messagePrefix) {
function generateStub(messagePrefix, opt_messageSuffix) {
return function() {
throw messagePrefix + ' is not available in packaged apps.';
var message = messagePrefix + ' is not available in packaged apps.';
if (opt_messageSuffix) message = message + ' ' + opt_messageSuffix;
throw message;
};
}
......@@ -41,10 +43,11 @@ function stubOutMethods(object, objectName, methodNames) {
* to by web developers, e.g. "document" instead of "HTMLDocument").
* @param {Array.<string>} propertyNames property names
*/
function stubOutGetters(object, objectName, propertyNames) {
function stubOutGetters(object, objectName, propertyNames, opt_messageSuffix) {
propertyNames.forEach(function(propertyName) {
object.__defineGetter__(
propertyName, generateStub(objectName + '.' + propertyName));
propertyName, generateStub(
objectName + '.' + propertyName, opt_messageSuffix));
});
}
......@@ -75,6 +78,11 @@ stubOutGetters(window, 'window',
['locationbar', 'menubar', 'personalbar', 'scrollbars', 'statusbar',
'toolbar']);
// Disable window.localStorage.
stubOutGetters(window, 'window',
['localStorage'],
'Use chrome.storage.local instead.');
// Disable onunload, onbeforeunload.
Window.prototype.__defineSetter__(
'onbeforeunload', generateStub('onbeforeunload'));
......
......@@ -2,6 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function assertContains(string, substring, error) {
chrome.test.assertTrue(string.indexOf(substring) != -1, error);
}
chrome.test.runTests([
function testOpenDatabase() {
chrome.test.assertTrue(!window.openDatabase);
......@@ -14,7 +18,15 @@ chrome.test.runTests([
},
function testLocalStorage() {
chrome.test.assertTrue(!window.localStorage);
chrome.test.succeed();
try {
window.localStorage;
chrome.test.fail('error not thrown');
} catch (e) {
var message = e.message || e;
var expected = 'is not available in packaged apps. ' +
'Use chrome.storage.local instead.';
assertContains(message, expected, 'Unexpected message ' + message);
chrome.test.succeed();
}
}
]);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment