/**
 * @fileoverview Simple wrapper for localStorage
 */

goog.provide('treesaver.storage');

goog.require('treesaver.debug');

goog.scope(function() {
  var storage = treesaver.storage,
      debug = treesaver.debug,
      localStore = window.localStorage,
      sessionStore = window.sessionStorage;

  /**
   * @param {!string} key
   * @param {!*} value
   * @param {boolean=} persist
   */
  storage.set = function set(key, value, persist) {
    var store = persist ? localStore : sessionStore;

    // iPad throws QUOTA_EXCEEDED_ERR frequently here, even though we're not
    // using that much storage
    // Clear the storage first in order to avoid this error
    store.removeItem(key);

    try {
      store.setItem(key, treesaver.json.stringify(value));
    }
    catch (ex) {
      // Still happened, not much we can do about it
      // TODO: Do something about it? :)
    }
  };

  /**
   * @param {!string} key
   * @return {*} Previously stored value, if any.
   */
  storage.get = function(key) {
    // Session take precedence over local
    var val = sessionStore.getItem(key) || localStore.getItem(key);

    if (val) {
      return treesaver.json.parse( /** @type {string} */ (val));
    }
    else {
      return null;
    }
  };

  /**
   * @param {!string} key
   */
  storage.clear = function(key) {
    // IE9 goes against spec here and throws an exception
    // if the key doesn't exist. Be defensive
    if (!SUPPORT_IE || sessionStore.getItem(key)) {
      sessionStore.removeItem(key);
    }
    if (!SUPPORT_IE || localStore.getItem(key)) {
      localStore.removeItem(key);
    }
  };

  /**
   * Returns a list of keys currently used in storage
   *
   * @param {string=} prefix
   * @return {!Array.<string>}
   */
  storage.getKeys_ = function(prefix) {
    var all_keys = [],
        i, len, key,
        prefix_len;

    prefix = prefix || '';
    prefix_len = prefix.length;

    for (i = 0, len = localStore.length; i < len; i += 1) {
      key = localStore.key(i);
      if (key && (!prefix || prefix === key.substr(0, prefix_len))) {
        all_keys.push(localStore.key(i));
      }
    }

    for (i = 0, len = sessionStore.length; i < len; i += 1) {
      key = sessionStore.key(i);
      if (all_keys.indexOf(key) === -1 &&
          (!prefix || prefix === key.substr(0, prefix_len))) {
        all_keys.push(key);
      }
    }

    return all_keys;
  };

  /**
   * Cleans up space in storage
   * @param {string=} prefix
   * @param {!Array.<string>=} whitelist
   */
  storage.clean = function(prefix, whitelist) {
    if (!whitelist) {
      localStore.clear();
      sessionStore.clear();
    }
    else {
      storage.getKeys_(prefix).forEach(function(key) {
        if (!whitelist || whitelist.indexOf(key) === -1) {
          storage.clear(key);
        }
      });
    }
  };
});

@@ -1327,254 +1327,38 @@
     
-// IE9 goes against spec here and throws an exception%0A    // if the key doesn't exist. Be defensive%0A    if (!SUPPORT_IE %7C%7C sessionStore.getItem(key)) %7B%0A      sessionStore.removeItem(key);%0A    %7D%0A    if (!SUPPORT_IE %7C%7C localStore.getItem(key)) %7B%0A  
+sessionStore.removeItem(key);%0A
     
@@ -1385,22 +1385,16 @@
 m(key);%0A
-    %7D%0A
   %7D;%0A%0A  

Remove IE_only code from storage