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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
* Mock class for Chrome's storage API to aid with the transition to Apps v2.
*
* The local storage API is not supported in Apps v2, so we'll have to use
* the new storage API instead. This doesn't require Apps v2, but it turns
* out that migrating our OAuth2 code to the new API is non-trivial; since
* that code will need to be rewritten to be Apps-v2-compatible anyway, a
* mock seems like a good temporary solution.
*
* TODO(jamiewalch): Delete this file when we migrate to apps v2
* (http://crbug.com/ 134213).
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/** @type {Object} */
remoting.storage = {};
remoting.initMockStorage = function() {
if (typeof(chrome.storage) != 'undefined') {
console.warn('If storage.js is no longer needed, consider removing it.');
remoting.storage.local = chrome.storage.local;
} else {
remoting.storage.local = new remoting.MockStorage();
}
};
/**
* @constructor
*/
remoting.MockStorage = function() {
};
/** @type {number} Delay for |get|, in ms, to simulate slow local storage
* and test correct asynchronous behavior. A value of 0 results in
* synchronous behavior.
*/
remoting.MockStorage.GET_DELAY = 0;
/**
* @param {string|Array.<string>|Object.<string>} items
* @param {function(Object.<string>):void} callback
* @return {void}
*/
remoting.MockStorage.prototype.get = function(items, callback) {
var result = {};
if (items == null) {
// Return all items for null input.
result = window.localStorage;
} else if (typeof(items) == 'string') {
// If the input is a string, return one or zero items, depending on
// whether or not the value is stored in localStorage.
if (window.localStorage.hasOwnProperty(items)) {
result[items] = window.localStorage.getItem(items);
}
} else if (items.constructor == Array) {
// If the input is an array, return those items that have an entry
// in localStorage.
for (var index in items) {
/** @type {string} */
var item = items[index];
if (window.localStorage.hasOwnProperty(item)) {
result[item] = window.localStorage.getItem(item);
}
}
} else {
// If the input is a dictionary, return the localStorage value for
// items that are stored, and the dictionary value otherwise.
for (var item in items) {
if (window.localStorage.hasOwnProperty(item)) {
result[item] = window.localStorage.getItem(item);
} else {
result[item] = items[item];
}
}
}
if (remoting.MockStorage.GET_DELAY == 0) {
callback(result);
} else {
window.setTimeout(callback.bind(null, result),
remoting.MockStorage.GET_DELAY);
}
};
/**
* @param {Object.<string>} items
* @param {function():void=} opt_callback
* @return {void}
*/
remoting.MockStorage.prototype.set = function(items, opt_callback) {
for (var item in items) {
window.localStorage.setItem(item, items[item]);
}
if (opt_callback) {
opt_callback();
}
};
/**
* @param {string|Array.<string>} items
* @param {function():void=} opt_callback
* @return {void}
*/
remoting.MockStorage.prototype.remove = function(items, opt_callback) {
if (typeof(items) == 'string') {
window.localStorage.removeItem(items);
} else if (items.constructor == Array) {
for (var index in items) {
window.localStorage.removeItem(items[index]);
}
}
if (opt_callback) {
opt_callback();
}
};
/**
* @param {function():void=} opt_callback
* @return {void}
*/
remoting.MockStorage.prototype.clear = function(opt_callback) {
window.localStorage.clear();
if (opt_callback) {
opt_callback();
}
};
|