4 // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode
5 // which throws when accessing the localStorage property itself, as opposed
6 // to the standard behaviour of throwing on getItem/setItem. (T148998)
8 localStorage
= ( function () {
10 return window
.localStorage
;
13 sessionStorage
= ( function () {
15 return window
.sessionStorage
;
20 * A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`)
21 * that is safe to call on all browsers.
23 * @class mw.SafeStorage
29 * @param {Object|undefined} store The Storage instance to wrap around
31 function SafeStorage( store
) {
36 * Retrieve value from device storage.
38 * @param {string} key Key of item to retrieve
39 * @return {string|boolean} False when localStorage not available, otherwise string
41 SafeStorage
.prototype.get = function ( key
) {
43 return this.store
.getItem( key
);
49 * Set a value in device storage.
51 * @param {string} key Key name to store under
52 * @param {string} value Value to be stored
53 * @return {boolean} Whether the save succeeded or not
55 SafeStorage
.prototype.set = function ( key
, value
) {
57 this.store
.setItem( key
, value
);
64 * Remove a value from device storage.
66 * @param {string} key Key of item to remove
67 * @return {boolean} Whether the save succeeded or not
69 SafeStorage
.prototype.remove = function ( key
) {
71 this.store
.removeItem( key
);
80 * @extends mw.SafeStorage
82 mw
.storage
= new SafeStorage( localStorage
);
87 * @extends mw.SafeStorage
89 mw
.storage
.session
= new SafeStorage( sessionStorage
);