mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-10-27 18:38:49 +00:00
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.
This commit is contained in:
1 parent
ac90a0c0fb
commit
063b05d964
1 file changed
+27
-2
@@ -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
|
||||
Reference in new issue
Block a user