1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Wrap an instance of the browser's local or session storage to handle
|
|
|
18 |
* cache expiry, key namespacing and other helpful things.
|
|
|
19 |
*
|
|
|
20 |
* @module core/storagewrapper
|
|
|
21 |
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
define(['core/config'], function(config) {
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Constructor.
|
|
|
28 |
*
|
|
|
29 |
* @param {object} storage window.localStorage or window.sessionStorage
|
|
|
30 |
*/
|
|
|
31 |
var Wrapper = function(storage) {
|
|
|
32 |
this.storage = storage;
|
|
|
33 |
this.supported = this.detectSupport();
|
|
|
34 |
this.hashSource = config.wwwroot + '/' + config.jsrev;
|
|
|
35 |
this.hash = this.hashString(this.hashSource);
|
|
|
36 |
this.prefix = this.hash + '/';
|
|
|
37 |
this.jsrevPrefix = this.hashString(config.wwwroot) + '/jsrev';
|
|
|
38 |
this.validateCache();
|
|
|
39 |
};
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Check if the browser supports the type of storage.
|
|
|
43 |
*
|
|
|
44 |
* @method detectSupport
|
|
|
45 |
* @return {boolean} True if the browser supports storage.
|
|
|
46 |
*/
|
|
|
47 |
Wrapper.prototype.detectSupport = function() {
|
|
|
48 |
if (config.jsrev == -1) {
|
|
|
49 |
// Disable cache if debugging.
|
|
|
50 |
return false;
|
|
|
51 |
}
|
|
|
52 |
if (typeof (this.storage) === "undefined") {
|
|
|
53 |
return false;
|
|
|
54 |
}
|
|
|
55 |
var testKey = 'test';
|
|
|
56 |
try {
|
|
|
57 |
if (this.storage === null) {
|
|
|
58 |
return false;
|
|
|
59 |
}
|
|
|
60 |
// MDL-51461 - Some browsers misreport availability of the storage
|
|
|
61 |
// so check it is actually usable.
|
|
|
62 |
this.storage.setItem(testKey, '1');
|
|
|
63 |
this.storage.removeItem(testKey);
|
|
|
64 |
return true;
|
|
|
65 |
} catch (ex) {
|
|
|
66 |
return false;
|
|
|
67 |
}
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Add a unique prefix to all keys so multiple moodle sites do not share caches.
|
|
|
72 |
*
|
|
|
73 |
* @method prefixKey
|
|
|
74 |
* @param {string} key The cache key to prefix.
|
|
|
75 |
* @return {string} The new key
|
|
|
76 |
*/
|
|
|
77 |
Wrapper.prototype.prefixKey = function(key) {
|
|
|
78 |
return this.prefix + key;
|
|
|
79 |
};
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Check the current jsrev version and clear the cache if it has been bumped.
|
|
|
83 |
*
|
|
|
84 |
* @method validateCache
|
|
|
85 |
*/
|
|
|
86 |
Wrapper.prototype.validateCache = function() {
|
|
|
87 |
if (!this.supported) {
|
|
|
88 |
return;
|
|
|
89 |
}
|
|
|
90 |
var cacheVersion = this.storage.getItem(this.jsrevPrefix);
|
|
|
91 |
if (cacheVersion === null) {
|
|
|
92 |
this.storage.setItem(this.jsrevPrefix, config.jsrev);
|
|
|
93 |
return;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
var moodleVersion = config.jsrev;
|
|
|
97 |
if (moodleVersion != cacheVersion) {
|
|
|
98 |
this.storage.clear();
|
|
|
99 |
this.storage.setItem(this.jsrevPrefix, config.jsrev);
|
|
|
100 |
}
|
|
|
101 |
};
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Hash a string, used to make shorter key prefixes.
|
|
|
105 |
*
|
|
|
106 |
* @method hashString
|
|
|
107 |
* @param {String} source The string to hash
|
|
|
108 |
* @return {Number}
|
|
|
109 |
*/
|
|
|
110 |
Wrapper.prototype.hashString = function(source) {
|
|
|
111 |
// From http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery.
|
|
|
112 |
/* jshint bitwise: false */
|
|
|
113 |
/* eslint no-bitwise: "off" */
|
|
|
114 |
var hash = 0;
|
|
|
115 |
var i, chr, len;
|
|
|
116 |
if (source.length === 0) {
|
|
|
117 |
return hash;
|
|
|
118 |
}
|
|
|
119 |
for (i = 0, len = source.length; i < len; i++) {
|
|
|
120 |
chr = source.charCodeAt(i);
|
|
|
121 |
hash = ((hash << 5) - hash) + chr;
|
|
|
122 |
hash |= 0; // Convert to 32bit integer
|
|
|
123 |
}
|
|
|
124 |
return hash;
|
|
|
125 |
};
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Get a value from local storage. Remember - all values must be strings.
|
|
|
129 |
*
|
|
|
130 |
* @method get
|
|
|
131 |
* @param {string} key The cache key to check.
|
|
|
132 |
* @return {boolean|string} False if the value is not in the cache, or some other error - a string otherwise.
|
|
|
133 |
*/
|
|
|
134 |
Wrapper.prototype.get = function(key) {
|
|
|
135 |
if (!this.supported) {
|
|
|
136 |
return false;
|
|
|
137 |
}
|
|
|
138 |
key = this.prefixKey(key);
|
|
|
139 |
|
|
|
140 |
return this.storage.getItem(key);
|
|
|
141 |
};
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Set a value to local storage. Remember - all values must be strings.
|
|
|
145 |
*
|
|
|
146 |
* @method set
|
|
|
147 |
* @param {string} key The cache key to set.
|
|
|
148 |
* @param {string} value The value to set.
|
|
|
149 |
* @return {boolean} False if the value can't be saved in the cache, or some other error - true otherwise.
|
|
|
150 |
*/
|
|
|
151 |
Wrapper.prototype.set = function(key, value) {
|
|
|
152 |
if (!this.supported) {
|
|
|
153 |
return false;
|
|
|
154 |
}
|
|
|
155 |
key = this.prefixKey(key);
|
|
|
156 |
// This can throw exceptions when the storage limit is reached.
|
|
|
157 |
try {
|
|
|
158 |
this.storage.setItem(key, value);
|
|
|
159 |
} catch (e) {
|
|
|
160 |
return false;
|
|
|
161 |
}
|
|
|
162 |
return true;
|
|
|
163 |
};
|
|
|
164 |
|
11 |
efrain |
165 |
/**
|
|
|
166 |
* Clean local storage.
|
|
|
167 |
*
|
|
|
168 |
* @method clean
|
|
|
169 |
*/
|
|
|
170 |
Wrapper.prototype.clean = function() {
|
|
|
171 |
this.storage.clear();
|
|
|
172 |
};
|
|
|
173 |
|
1 |
efrain |
174 |
return Wrapper;
|
|
|
175 |
});
|