Skip to content

Commit

Permalink
Handle case if localStorage is not supported by the browser
Browse files Browse the repository at this point in the history
For example, if LS is disabled by browser
  • Loading branch information
Hengjie committed Aug 24, 2015
1 parent 84d5fc7 commit a1d0018
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
12 changes: 9 additions & 3 deletions app/scripts/ng-xdLocalStorage.js
Expand Up @@ -29,9 +29,15 @@ angular.module('xdLocalStorage', [])
return {
init: function (options) {
var defer = $q.defer();
options.initCallback = function () {
apiReady.resolve();
defer.resolve();
options.initCallback = function (localStorageSupported) {
if (localStorageSupported) {
apiReady.resolve();
defer.resolve();
} else {
apiReady.reject();
defer.reject();
console.warn('localStorage not supported in iframe');
}
};
xdLocalStorage.init(options);
return defer.promise;
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/xdLocalStorage.js
Expand Up @@ -8,7 +8,7 @@ window.xdLocalStorage = window.xdLocalStorage || (function () {
var options = {
iframeId: 'cross-domain-iframe',
iframeUrl: undefined,
initCallback: function () {}
initCallback: function (localStorageSupported) {}
};
var requestId = -1;
var iframe;
Expand All @@ -33,7 +33,7 @@ window.xdLocalStorage = window.xdLocalStorage || (function () {
if (data && data.namespace === MESSAGE_NAMESPACE) {
if (data.id === 'iframe-ready') {
iframeReady = true;
options.initCallback();
options.initCallback(data.localStorageSupported);
} else {
applyCallback(data);
}
Expand Down
25 changes: 24 additions & 1 deletion app/scripts/xdLocalStoragePostMessageApi.js
Expand Up @@ -10,6 +10,28 @@
var allowedOriginsAttr = (postApi && postApi.getAttribute("accepted-origins")) || '';
var allowedOrigins = allowedOriginsAttr.split(',');

// Checks the browser to see if local storage is supported
var browserSupportsLocalStorage = (function () {
try {
var supported = (localStorage in window && window[localStorage] !== null);

// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
// is available, but trying to call .setItem throws an exception.
//
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
// that exceeded the quota."
if (supported) {
var key = '__' + Math.round(Math.random() * 1e7);
localStorage.setItem(key, '');
localStorage.removeItem(key);
}

return supported;
} catch (e) {
return false;
}
}());

// Verify the sender's origin has been whitelisted
function isOriginAllowed(origin) {
if (allowedOrigins.length === 1 && (allowedOrigins[0] === '*' || allowedOrigins[0] === '')) {
Expand Down Expand Up @@ -103,7 +125,8 @@
function sendOnLoad() {
var data = {
namespace: MESSAGE_NAMESPACE,
id: 'iframe-ready'
id: 'iframe-ready',
localStorageSupported: browserSupportsLocalStorage
};
parent.postMessage(JSON.stringify(data), '*');
}
Expand Down

0 comments on commit a1d0018

Please sign in to comment.