API: Migrate Title::userCan() calls to PermissionManager
[lhc/web/wiklou.git] / resources / src / mediawiki.storage.js
1 ( function () {
2 'use strict';
3
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)
7 var
8 localStorage = ( function () {
9 try {
10 return window.localStorage;
11 } catch ( e ) {}
12 }() ),
13 sessionStorage = ( function () {
14 try {
15 return window.sessionStorage;
16 } catch ( e ) {}
17 }() );
18
19 /**
20 * A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`)
21 * that is safe to call on all browsers.
22 *
23 * @class mw.SafeStorage
24 * @private
25 * @param {Object|undefined} store The Storage instance to wrap around
26 */
27 function SafeStorage( store ) {
28 this.store = store;
29 }
30
31 /**
32 * Retrieve value from device storage.
33 *
34 * @param {string} key Key of item to retrieve
35 * @return {string|null|boolean} String value, null if no value exists, or false
36 * if storage is not available.
37 */
38 SafeStorage.prototype.get = function ( key ) {
39 try {
40 return this.store.getItem( key );
41 } catch ( e ) {}
42 return false;
43 };
44
45 /**
46 * Set a value in device storage.
47 *
48 * @param {string} key Key name to store under
49 * @param {string} value Value to be stored
50 * @return {boolean} The value was set
51 */
52 SafeStorage.prototype.set = function ( key, value ) {
53 try {
54 this.store.setItem( key, value );
55 return true;
56 } catch ( e ) {}
57 return false;
58 };
59
60 /**
61 * Remove a value from device storage.
62 *
63 * @param {string} key Key of item to remove
64 * @return {boolean} Whether the key was removed
65 */
66 SafeStorage.prototype.remove = function ( key ) {
67 try {
68 this.store.removeItem( key );
69 return true;
70 } catch ( e ) {}
71 return false;
72 };
73
74 /**
75 * Retrieve JSON object from device storage.
76 *
77 * @param {string} key Key of item to retrieve
78 * @return {Object|null|boolean} Object, null if no value exists or value
79 * is not JSON-parseable, or false if storage is not available.
80 */
81 SafeStorage.prototype.getObject = function ( key ) {
82 var json = this.get( key );
83
84 if ( json === false ) {
85 return false;
86 }
87
88 try {
89 return JSON.parse( json );
90 } catch ( e ) {}
91
92 return null;
93 };
94
95 /**
96 * Set an object value in device storage by JSON encoding
97 *
98 * @param {string} key Key name to store under
99 * @param {Object} value Object value to be stored
100 * @return {boolean} The value was set
101 */
102 SafeStorage.prototype.setObject = function ( key, value ) {
103 var json;
104 try {
105 json = JSON.stringify( value );
106 return this.set( key, json );
107 } catch ( e ) {}
108 return false;
109 };
110
111 /**
112 * A wrapper for the HTML5 `localStorage` interface
113 * that is safe to call on all browsers.
114 *
115 * @class
116 * @singleton
117 * @extends mw.SafeStorage
118 */
119 mw.storage = new SafeStorage( localStorage );
120
121 /**
122 * A wrapper for the HTML5 `sessionStorage` interface
123 * that is safe to call on all browsers.
124 *
125 * @class
126 * @singleton
127 * @extends mw.SafeStorage
128 */
129 mw.storage.session = new SafeStorage( sessionStorage );
130
131 }() );