Fix errors when using localStorage in Safari's private browsing mode (#1489996)

pull/208/head
Aleksander Machniak 10 years ago
parent 83316e175d
commit b0b9cf00d2

@ -38,6 +38,7 @@ CHANGELOG Roundcube Webmail
- Don't remove links when html signature is converted to text (#1489621) - Don't remove links when html signature is converted to text (#1489621)
- Fix mbox files import - Fix mbox files import
- Fix some mime-type to extension mapping checks in Installer (#1489983) - Fix some mime-type to extension mapping checks in Installer (#1489983)
- Fix errors when using localStorage in Safari's private browsing mode (#1489996)
RELEASE 1.0.2 RELEASE 1.0.2
------------- -------------

@ -3374,9 +3374,7 @@ function rcube_webmail()
} }
// check for locally stored compose data // check for locally stored compose data
if (window.localStorage) {
this.compose_restore_dialog(0, html_mode) this.compose_restore_dialog(0, html_mode)
}
if (input_to.val() == '') if (input_to.val() == '')
input_to.focus(); input_to.focus();
@ -3900,13 +3898,14 @@ function rcube_webmail()
} }
}); });
if (window.localStorage && !empty) { if (!empty) {
var index = this.local_storage_get_item('compose.index', []), var index = this.local_storage_get_item('compose.index', []),
key = this.env.compose_id; key = this.env.compose_id;
if ($.inArray(key, index) < 0) { if ($.inArray(key, index) < 0) {
index.push(key); index.push(key);
} }
this.local_storage_set_item('compose.' + key, formdata, true); this.local_storage_set_item('compose.' + key, formdata, true);
this.local_storage_set_item('compose.index', index); this.local_storage_set_item('compose.index', index);
} }
@ -3940,27 +3939,24 @@ function rcube_webmail()
// remove stored compose data from localStorage // remove stored compose data from localStorage
this.remove_compose_data = function(key) this.remove_compose_data = function(key)
{ {
if (window.localStorage) {
var index = this.local_storage_get_item('compose.index', []); var index = this.local_storage_get_item('compose.index', []);
if ($.inArray(key, index) >= 0) { if ($.inArray(key, index) >= 0) {
this.local_storage_remove_item('compose.' + key); this.local_storage_remove_item('compose.' + key);
this.local_storage_set_item('compose.index', $.grep(index, function(val,i) { return val != key; })); this.local_storage_set_item('compose.index', $.grep(index, function(val,i) { return val != key; }));
} }
}
}; };
// clear all stored compose data of this user // clear all stored compose data of this user
this.clear_compose_data = function() this.clear_compose_data = function()
{ {
if (window.localStorage) {
var i, index = this.local_storage_get_item('compose.index', []); var i, index = this.local_storage_get_item('compose.index', []);
for (i=0; i < index.length; i++) { for (i=0; i < index.length; i++) {
this.local_storage_remove_item('compose.' + index[i]); this.local_storage_remove_item('compose.' + index[i]);
} }
this.local_storage_remove_item('compose.index'); this.local_storage_remove_item('compose.index');
}
}; };
@ -8030,22 +8026,43 @@ function rcube_webmail()
// wrapper for localStorage.getItem(key) // wrapper for localStorage.getItem(key)
this.local_storage_get_item = function(key, deflt, encrypted) this.local_storage_get_item = function(key, deflt, encrypted)
{ {
var item;
// TODO: add encryption // TODO: add encryption
var item = localStorage.getItem(this.get_local_storage_prefix() + key); try {
item = localStorage.getItem(this.get_local_storage_prefix() + key);
}
catch (e) { }
return item !== null ? JSON.parse(item) : (deflt || null); return item !== null ? JSON.parse(item) : (deflt || null);
}; };
// wrapper for localStorage.setItem(key, data) // wrapper for localStorage.setItem(key, data)
this.local_storage_set_item = function(key, data, encrypted) this.local_storage_set_item = function(key, data, encrypted)
{ {
// try/catch to handle no localStorage support, but also error
// in Safari-in-private-browsing-mode where localStorage exists
// but can't be used (#1489996)
try {
// TODO: add encryption // TODO: add encryption
return localStorage.setItem(this.get_local_storage_prefix() + key, JSON.stringify(data)); localStorage.setItem(this.get_local_storage_prefix() + key, JSON.stringify(data));
return true;
}
catch (e) {
return false;
}
}; };
// wrapper for localStorage.removeItem(key) // wrapper for localStorage.removeItem(key)
this.local_storage_remove_item = function(key) this.local_storage_remove_item = function(key)
{ {
return localStorage.removeItem(this.get_local_storage_prefix() + key); try {
localStorage.removeItem(this.get_local_storage_prefix() + key);
return true;
}
catch (e) {
return false;
}
}; };
} // end object rcube_webmail } // end object rcube_webmail

@ -76,7 +76,7 @@ function rcube_mail_ui()
function get_pref(key) function get_pref(key)
{ {
if (!prefs) { if (!prefs) {
prefs = window.localStorage ? rcmail.local_storage_get_item('prefs.larry', {}) : {}; prefs = rcmail.local_storage_get_item('prefs.larry', {});
} }
// fall-back to cookies // fall-back to cookies
@ -85,9 +85,8 @@ function rcube_mail_ui()
if (cookie != null) { if (cookie != null) {
prefs[key] = cookie; prefs[key] = cookie;
// copy value to local storage and remove cookie // copy value to local storage and remove cookie (if localStorage is supported)
if (window.localStorage) { if (rcmail.local_storage_set_item('prefs.larry', prefs)) {
rcmail.local_storage_set_item('prefs.larry', prefs);
rcmail.set_cookie(key, cookie, new Date()); // expire cookie rcmail.set_cookie(key, cookie, new Date()); // expire cookie
} }
} }
@ -103,11 +102,8 @@ function rcube_mail_ui()
{ {
prefs[key] = val; prefs[key] = val;
// write prefs to local storage // write prefs to local storage (if supported)
if (window.localStorage) { if (!rcmail.local_storage_set_item('prefs.larry', prefs)) {
rcmail.local_storage_set_item('prefs.larry', prefs);
}
else {
// store value in cookie // store value in cookie
var exp = new Date(); var exp = new Date();
exp.setYear(exp.getFullYear() + 1); exp.setYear(exp.getFullYear() + 1);

Loading…
Cancel
Save