/
storageManager.js
270 lines (250 loc) · 8.42 KB
/
storageManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import {hook} from './hook.js';
import {checkCookieSupport, hasDeviceAccess, logError, logInfo} from './utils.js';
import {bidderSettings as defaultBidderSettings} from './bidderSettings.js';
import {MODULE_TYPE_BIDDER, MODULE_TYPE_CORE} from './activities/modules.js';
export const STORAGE_TYPE_LOCALSTORAGE = 'html5';
export const STORAGE_TYPE_COOKIES = 'cookie';
export let storageCallbacks = [];
/*
* Storage manager constructor. Consumers should prefer one of `getStorageManager` or `getCoreStorageManager`.
*/
export function newStorageManager({moduleName, moduleType} = {}, {bidderSettings = defaultBidderSettings} = {}) {
function isBidderAllowed(storageType) {
if (moduleType !== MODULE_TYPE_BIDDER) {
return true;
}
const storageAllowed = bidderSettings.get(moduleName, 'storageAllowed');
if (!storageAllowed || storageAllowed === true) return !!storageAllowed;
if (Array.isArray(storageAllowed)) return storageAllowed.some((e) => e === storageType);
return storageAllowed === storageType;
}
function isValid(cb, storageType) {
if (!isBidderAllowed(storageType)) {
logInfo(`bidderSettings denied access to device storage for bidder '${moduleName}'`);
const result = {valid: false};
return cb(result);
} else {
let value;
let hookDetails = {
hasEnforcementHook: false
}
validateStorageEnforcement(moduleType, moduleName, hookDetails, function(result) {
if (result && result.hasEnforcementHook) {
value = cb(result);
} else {
let result = {
hasEnforcementHook: false,
valid: hasDeviceAccess()
}
value = cb(result);
}
});
return value;
}
}
function schedule(operation, storageType, done) {
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(operation, storageType);
done(result);
});
} else {
return isValid(operation, storageType);
}
}
/**
* @param {string} key
* @param {string} value
* @param {string} [expires='']
* @param {string} [sameSite='/']
* @param {string} [domain] domain (e.g., 'example.com' or 'subdomain.example.com').
* If not specified, defaults to the host portion of the current document location.
* If a domain is specified, subdomains are always included.
* Domain must match the domain of the JavaScript origin. Setting cookies to foreign domains will be silently ignored.
*/
const setCookie = function (key, value, expires, sameSite, domain, done) {
let cb = function (result) {
if (result && result.valid) {
const domainPortion = (domain && domain !== '') ? ` ;domain=${encodeURIComponent(domain)}` : '';
const expiresPortion = (expires && expires !== '') ? ` ;expires=${expires}` : '';
const isNone = (sameSite != null && sameSite.toLowerCase() == 'none')
const secure = (isNone) ? '; Secure' : '';
document.cookie = `${key}=${encodeURIComponent(value)}${expiresPortion}; path=/${domainPortion}${sameSite ? `; SameSite=${sameSite}` : ''}${secure}`;
}
}
return schedule(cb, STORAGE_TYPE_COOKIES, done);
};
/**
* @param {string} name
* @returns {(string|null)}
*/
const getCookie = function(name, done) {
let cb = function (result) {
if (result && result.valid) {
let m = window.document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]*)\\s*(;|$)');
return m ? decodeURIComponent(m[2]) : null;
}
return null;
}
return schedule(cb, STORAGE_TYPE_COOKIES, done);
};
/**
* @returns {boolean}
*/
const localStorageIsEnabled = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
localStorage.setItem('prebid.cookieTest', '1');
return localStorage.getItem('prebid.cookieTest') === '1';
} catch (error) {
} finally {
try {
localStorage.removeItem('prebid.cookieTest');
} catch (error) {}
}
}
return false;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
/**
* @returns {boolean}
*/
const cookiesAreEnabled = function (done) {
let cb = function (result) {
if (result && result.valid) {
return checkCookieSupport();
}
return false;
}
return schedule(cb, STORAGE_TYPE_COOKIES, done);
}
/**
* @param {string} key
* @param {string} value
*/
const setDataInLocalStorage = function (key, value, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
window.localStorage.setItem(key, value);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
/**
* @param {string} key
* @returns {(string|null)}
*/
const getDataFromLocalStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
return window.localStorage.getItem(key);
}
return null;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
/**
* @param {string} key
*/
const removeDataFromLocalStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
window.localStorage.removeItem(key);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
/**
* @returns {boolean}
*/
const hasLocalStorage = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
return !!window.localStorage;
} catch (e) {
logError('Local storage api disabled');
}
}
return false;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
/**
* Returns all cookie values from the jar whose names contain the `keyLike`
* Needs to exist in `utils.js` as it follows the StorageHandler interface defined in live-connect-js. If that module were to be removed, this function can go as well.
* @param {string} keyLike
* @return {[]}
*/
const findSimilarCookies = function(keyLike, done) {
let cb = function (result) {
if (result && result.valid) {
const all = [];
if (hasDeviceAccess()) {
const cookies = document.cookie.split(';');
while (cookies.length) {
const cookie = cookies.pop();
let separatorIndex = cookie.indexOf('=');
separatorIndex = separatorIndex < 0 ? cookie.length : separatorIndex;
const cookieName = decodeURIComponent(cookie.slice(0, separatorIndex).replace(/^\s+/, ''));
if (cookieName.indexOf(keyLike) >= 0) {
all.push(decodeURIComponent(cookie.slice(separatorIndex + 1)));
}
}
}
return all;
}
}
return schedule(cb, STORAGE_TYPE_COOKIES, done);
}
return {
setCookie,
getCookie,
localStorageIsEnabled,
cookiesAreEnabled,
setDataInLocalStorage,
getDataFromLocalStorage,
removeDataFromLocalStorage,
hasLocalStorage,
findSimilarCookies
}
}
/**
* This hook validates the storage enforcement if gdprEnforcement module is included
*/
export const validateStorageEnforcement = hook('async', function(moduleType, moduleName, hookDetails, callback) {
callback(hookDetails);
}, 'validateStorageEnforcement');
/**
* Get a storage manager for a particular module.
*
* Either bidderCode or a combination of moduleType + moduleName must be provided. The former is a shorthand
* for `{moduleType: 'bidder', moduleName: bidderCode}`.
*
*/
export function getStorageManager({moduleType, moduleName, bidderCode} = {}) {
function err() {
throw new Error(`Invalid invocation for getStorageManager: must set either bidderCode, or moduleType + moduleName`)
}
if (bidderCode) {
if ((moduleType && moduleType !== MODULE_TYPE_BIDDER) || moduleName) err()
moduleType = MODULE_TYPE_BIDDER;
moduleName = bidderCode;
} else if (!moduleName || !moduleType) {
err()
}
return newStorageManager({moduleType, moduleName});
}
/**
* Get a storage manager for "core" (vendorless, or first-party) modules. Shorthand for `getStorageManager({moduleName, moduleType: 'core'})`.
*
* @param {string} moduleName Module name
*/
export function getCoreStorageManager(moduleName) {
return newStorageManager({moduleName: moduleName, moduleType: MODULE_TYPE_CORE});
}
export function resetData() {
storageCallbacks = [];
}