1 | dojo.provide("dojox.storage.AirEncryptedLocalStorageProvider"); |
---|
2 | dojo.require("dojox.storage.manager"); |
---|
3 | dojo.require("dojox.storage.Provider"); |
---|
4 | |
---|
5 | if (dojo.isAIR) { |
---|
6 | (function(){ |
---|
7 | |
---|
8 | if (!air) { |
---|
9 | var air = {}; |
---|
10 | } |
---|
11 | air.ByteArray = window.runtime.flash.utils.ByteArray; |
---|
12 | air.EncryptedLocalStore = window.runtime.flash.data.EncryptedLocalStore, |
---|
13 | |
---|
14 | // summary: |
---|
15 | // Storage provider that uses features in the Adobe AIR runtime to achieve |
---|
16 | // permanent storage |
---|
17 | dojo.declare("dojox.storage.AirEncryptedLocalStorageProvider", [ dojox.storage.Provider ], { |
---|
18 | initialize: function(){ |
---|
19 | // indicate that this storage provider is now loaded |
---|
20 | dojox.storage.manager.loaded(); |
---|
21 | }, |
---|
22 | |
---|
23 | isAvailable: function(){ |
---|
24 | return true; |
---|
25 | }, |
---|
26 | |
---|
27 | _getItem: function(key){ |
---|
28 | var storedValue = air.EncryptedLocalStore.getItem("__dojo_" + key); |
---|
29 | return storedValue ? storedValue.readUTFBytes(storedValue.length) : ""; |
---|
30 | }, |
---|
31 | |
---|
32 | _setItem: function(key, value){ |
---|
33 | var bytes = new air.ByteArray(); |
---|
34 | bytes.writeUTFBytes(value); |
---|
35 | air.EncryptedLocalStore.setItem("__dojo_" + key, bytes); |
---|
36 | }, |
---|
37 | |
---|
38 | _removeItem: function(key){ |
---|
39 | air.EncryptedLocalStore.removeItem("__dojo_" + key); |
---|
40 | }, |
---|
41 | |
---|
42 | put: function(key, value, resultsHandler, namespace){ |
---|
43 | if(this.isValidKey(key) == false){ |
---|
44 | throw new Error("Invalid key given: " + key); |
---|
45 | } |
---|
46 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
47 | if(this.isValidKey(namespace) == false){ |
---|
48 | throw new Error("Invalid namespace given: " + namespace); |
---|
49 | } |
---|
50 | |
---|
51 | // try to store the value |
---|
52 | try{ |
---|
53 | var namespaces = this._getItem("namespaces")||'|'; |
---|
54 | if(namespaces.indexOf('|'+namespace+'|')==-1){ |
---|
55 | this._setItem("namespaces", namespaces + namespace + '|'); |
---|
56 | } |
---|
57 | var keys = this._getItem(namespace + "_keys")||'|'; |
---|
58 | if(keys.indexOf('|'+key+'|')==-1){ |
---|
59 | this._setItem(namespace + "_keys", keys + key + '|'); |
---|
60 | } |
---|
61 | this._setItem('_' + namespace + '_' + key, value); |
---|
62 | }catch(e){ |
---|
63 | // indicate we failed |
---|
64 | console.debug("dojox.storage.AirEncryptedLocalStorageProvider.put:", e); |
---|
65 | resultsHandler(this.FAILED, key, e.toString(), namespace); |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | if(resultsHandler){ |
---|
70 | resultsHandler(this.SUCCESS, key, null, namespace); |
---|
71 | } |
---|
72 | }, |
---|
73 | |
---|
74 | get: function(key, namespace){ |
---|
75 | if(this.isValidKey(key) == false){ |
---|
76 | throw new Error("Invalid key given: " + key); |
---|
77 | } |
---|
78 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
79 | return this._getItem('_' + namespace + '_' + key); |
---|
80 | }, |
---|
81 | |
---|
82 | getNamespaces: function(){ |
---|
83 | var results = [ this.DEFAULT_NAMESPACE ]; |
---|
84 | var namespaces = (this._getItem("namespaces")||'|').split('|'); |
---|
85 | for (var i=0;i<namespaces.length;i++){ |
---|
86 | if(namespaces[i].length && namespaces[i] != this.DEFAULT_NAMESPACE){ |
---|
87 | results.push(namespaces[i]); |
---|
88 | } |
---|
89 | } |
---|
90 | return results; |
---|
91 | }, |
---|
92 | |
---|
93 | getKeys: function(namespace){ |
---|
94 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
95 | if(this.isValidKey(namespace) == false){ |
---|
96 | throw new Error("Invalid namespace given: " + namespace); |
---|
97 | } |
---|
98 | |
---|
99 | var results = []; |
---|
100 | var keys = (this._getItem(namespace + "_keys")||'|').split('|'); |
---|
101 | for (var i=0;i<keys.length;i++){ |
---|
102 | if (keys[i].length){ |
---|
103 | results.push(keys[i]); |
---|
104 | } |
---|
105 | } |
---|
106 | return results; |
---|
107 | }, |
---|
108 | |
---|
109 | clear: function(namespace){ |
---|
110 | if(this.isValidKey(namespace) == false){ |
---|
111 | throw new Error("Invalid namespace given: " + namespace); |
---|
112 | } |
---|
113 | var namespaces = this._getItem("namespaces")||'|'; |
---|
114 | if(namespaces.indexOf('|'+namespace+'|')!=-1){ |
---|
115 | this._setItem("namespaces", namespaces.replace('|' + namespace + '|', '|')); |
---|
116 | } |
---|
117 | var keys = (this._getItem(namespace + "_keys")||'|').split('|'); |
---|
118 | for (var i=0;i<keys.length;i++){ |
---|
119 | if (keys[i].length){ |
---|
120 | this._removeItem(namespace + "_" + keys[i]); |
---|
121 | } |
---|
122 | } |
---|
123 | this._removeItem(namespace + "_keys"); |
---|
124 | }, |
---|
125 | |
---|
126 | remove: function(key, namespace){ |
---|
127 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
128 | |
---|
129 | var keys = this._getItem(namespace + "_keys")||'|'; |
---|
130 | if(keys.indexOf('|'+key+'|')!=-1){ |
---|
131 | this._setItem(namespace + "_keys", keys.replace('|' + key + '|', '|')); |
---|
132 | } |
---|
133 | this._removeItem('_' + namespace + '_' + key); |
---|
134 | }, |
---|
135 | |
---|
136 | putMultiple: function(keys, values, resultsHandler, namespace) { |
---|
137 | if(this.isValidKeyArray(keys) === false |
---|
138 | || ! values instanceof Array |
---|
139 | || keys.length != values.length){ |
---|
140 | throw new Error("Invalid arguments: keys = [" + keys + "], values = [" + values + "]"); |
---|
141 | } |
---|
142 | |
---|
143 | if(namespace == null || typeof namespace == "undefined"){ |
---|
144 | namespace = this.DEFAULT_NAMESPACE; |
---|
145 | } |
---|
146 | |
---|
147 | if(this.isValidKey(namespace) == false){ |
---|
148 | throw new Error("Invalid namespace given: " + namespace); |
---|
149 | } |
---|
150 | |
---|
151 | this._statusHandler = resultsHandler; |
---|
152 | |
---|
153 | // try to store the value |
---|
154 | try{ |
---|
155 | for(var i=0;i<keys.length;i++) { |
---|
156 | this.put(keys[i], values[i], null, namespace); |
---|
157 | } |
---|
158 | }catch(e){ |
---|
159 | // indicate we failed |
---|
160 | console.debug("dojox.storage.AirEncryptedLocalStorageProvider.putMultiple:", e); |
---|
161 | if(resultsHandler){ |
---|
162 | resultsHandler(this.FAILED, keys, e.toString(), namespace); |
---|
163 | } |
---|
164 | return; |
---|
165 | } |
---|
166 | |
---|
167 | if(resultsHandler){ |
---|
168 | resultsHandler(this.SUCCESS, keys, null); |
---|
169 | } |
---|
170 | }, |
---|
171 | |
---|
172 | getMultiple: function(keys, namespace){ |
---|
173 | if(this.isValidKeyArray(keys) === false){ |
---|
174 | throw new Error("Invalid key array given: " + keys); |
---|
175 | } |
---|
176 | |
---|
177 | if(namespace == null || typeof namespace == "undefined"){ |
---|
178 | namespace = this.DEFAULT_NAMESPACE; |
---|
179 | } |
---|
180 | |
---|
181 | if(this.isValidKey(namespace) == false){ |
---|
182 | throw new Error("Invalid namespace given: " + namespace); |
---|
183 | } |
---|
184 | |
---|
185 | var results = []; |
---|
186 | for(var i=0;i<keys.length;i++){ |
---|
187 | results[i] = this.get(keys[i], namespace); |
---|
188 | } |
---|
189 | return results; |
---|
190 | }, |
---|
191 | |
---|
192 | removeMultiple: function(keys, namespace){ |
---|
193 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
194 | for(var i=0;i<keys.length;i++){ |
---|
195 | this.remove(keys[i], namespace); |
---|
196 | } |
---|
197 | }, |
---|
198 | |
---|
199 | isPermanent: function(){ return true; }, |
---|
200 | |
---|
201 | getMaximumSize: function(){ return this.SIZE_NO_LIMIT; }, |
---|
202 | |
---|
203 | hasSettingsUI: function(){ return false; }, |
---|
204 | |
---|
205 | showSettingsUI: function(){ |
---|
206 | throw new Error(this.declaredClass + " does not support a storage settings user-interface"); |
---|
207 | }, |
---|
208 | |
---|
209 | hideSettingsUI: function(){ |
---|
210 | throw new Error(this.declaredClass + " does not support a storage settings user-interface"); |
---|
211 | } |
---|
212 | }); |
---|
213 | |
---|
214 | dojox.storage.manager.register("dojox.storage.AirEncryptedLocalStorageProvider", new dojox.storage.AirEncryptedLocalStorageProvider()); |
---|
215 | dojox.storage.manager.initialize(); |
---|
216 | })(); |
---|
217 | } |
---|