From 063b05d9643c35b303568a28809e4eb3224f71ec Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Mon, 24 Oct 2011 01:10:04 +0100 Subject: [PATCH] Fixes #1384. Avoid user defaults QUOTA_EXCEEDED_ERR crash. Switch to cookie based user defaults if HTML 5 local storage exists but is disabled, as is the case when Safari is in 'private browsing' mode. Don't crash if the local storage quota is reached. Instead log a warning and silently fail to store the new user default. --- Foundation/CPUserDefaults.j | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Foundation/CPUserDefaults.j b/Foundation/CPUserDefaults.j index e3358ddd4..88685aa4a 100644 --- a/Foundation/CPUserDefaults.j +++ b/Foundation/CPUserDefaults.j @@ -618,13 +618,31 @@ var StandardUserDefaults; @end +var CPUserDefaultsLocalStoreTestKey = "9961800812587769-Cappuccino-Storage-Test"; + @implementation CPUserDefaultsLocalStore : CPUserDefaultsStore { } + (BOOL)supportsLocalStorage { - return !!window.localStorage; + if (!window.localStorage) + return NO; + + try + { + // Just because localStorage exists does not mean it works. In particular it might be disabled + // as it is when Safari's private browsing mode is active. + localStorage.setItem(CPUserDefaultsLocalStoreTestKey, "1"); + if (localStorage.getItem(CPUserDefaultsLocalStoreTestKey) != "1") + return NO; + localStorage.removeItem(CPUserDefaultsLocalStoreTestKey); + } + catch (e) + { + return NO; + } + return YES; } - (id)init @@ -649,7 +667,14 @@ var StandardUserDefaults; - (void)setData:(CPData)aData { - localStorage.setItem(_domain, encodeURIComponent([aData rawString])); + try + { + localStorage.setItem(_domain, encodeURIComponent([aData rawString])); + } + catch (e) + { + CPLog.warn("Unable to write to local storage: " + e); + } } @end