OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Invariant: This model can only be constructed on a ServiceWorker target. | 6 * Invariant: This model can only be constructed on a ServiceWorker target. |
7 * @constructor | 7 * @constructor |
8 * @extends {WebInspector.SDKModel} | 8 * @extends {WebInspector.SDKModel} |
9 */ | 9 */ |
10 WebInspector.ServiceWorkerCacheModel = function(target) | 10 WebInspector.ServiceWorkerCacheModel = function(target) |
11 { | 11 { |
12 WebInspector.SDKModel.call(this, WebInspector.ServiceWorkerCacheModel, targe t); | 12 WebInspector.SDKModel.call(this, WebInspector.ServiceWorkerCacheModel, targe t); |
13 | 13 |
14 /** @type {!Set.<string>} */ | 14 /** @type {!Map<string, !Set.<string>>} */ |
15 this._cacheNames = new Set(); | 15 this._cacheNamesBySecurityOrigin = new Map(); |
16 | 16 |
17 this._agent = target.serviceWorkerCacheAgent(); | 17 this._agent = target.cacheStorageAgent(); |
18 | |
19 /** @type {boolean} */ | |
20 this._enabled = false; | |
18 } | 21 } |
19 | 22 |
20 WebInspector.ServiceWorkerCacheModel.EventTypes = { | 23 WebInspector.ServiceWorkerCacheModel.EventTypes = { |
21 CacheAdded: "CacheAdded", | 24 CacheAdded: "CacheAdded", |
22 CacheRemoved: "CacheRemoved", | 25 CacheRemoved: "CacheRemoved" |
23 } | 26 } |
24 | 27 |
25 WebInspector.ServiceWorkerCacheModel.prototype = { | 28 WebInspector.ServiceWorkerCacheModel.prototype = { |
26 _reset: function() | 29 enable: function() |
27 { | 30 { |
28 this._updateCacheNames([]); | 31 if (this._enabled) |
29 this._loadCacheNames(); | 32 return; |
33 | |
34 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTr eeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); | |
35 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTr eeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this); | |
36 | |
37 var securityOrigins = this.target().resourceTreeModel.securityOrigins(); | |
38 for (var i = 0; i < securityOrigins.length; ++i) | |
39 this._addOrigin(securityOrigins[i]); | |
40 this._enabled = true; | |
30 }, | 41 }, |
31 | 42 |
32 refreshCacheNames: function() | 43 refreshCacheNames: function() |
33 { | 44 { |
34 this._loadCacheNames(); | 45 var securityOrigins = this.target().resourceTreeModel.securityOrigins(); |
46 for (var securityOrigin of securityOrigins) { | |
47 this._loadCacheNames(securityOrigin); | |
48 } | |
35 }, | 49 }, |
36 | 50 |
37 /** | 51 /** |
38 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 52 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
39 */ | 53 */ |
40 deleteCache: function(cacheId) | 54 deleteCache: function(cacheId) |
41 { | 55 { |
56 var securityOrigin = cacheId.securityOrigin; | |
42 /** | 57 /** |
43 * @this {WebInspector.ServiceWorkerCacheModel} | 58 * @this {WebInspector.ServiceWorkerCacheModel} |
44 */ | 59 */ |
45 function callback(error) | 60 function callback(error) |
46 { | 61 { |
47 if (error) { | 62 if (error) { |
48 console.error("ServiceWorkerCacheAgent error: ", error); | 63 console.error("ServiceWorkerCacheAgent error: ", error); |
49 return; | 64 return; |
50 } | 65 } |
51 this._cacheRemoved(cacheId.name); | 66 if (!this._cacheNamesBySecurityOrigin.has(securityOrigin)) |
67 return; | |
68 var caches = this._cacheNamesBySecurityOrigin.get(securityOrigin); | |
pfeldman
2015/04/03 09:15:57
get it right away, then bail out if none.
dmurph
2015/04/08 19:35:35
Done.
| |
69 caches.delete(cacheId.name); | |
70 this._cacheRemoved(securityOrigin, cacheId.name); | |
52 } | 71 } |
53 this._agent.deleteCache(cacheId.name, callback.bind(this)); | 72 this._agent.deleteCache(securityOrigin, cacheId.name, callback.bind(this )); |
54 }, | 73 }, |
55 | 74 |
56 /** | 75 /** |
57 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 76 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
58 * @param {number} skipCount | 77 * @param {number} skipCount |
59 * @param {number} pageSize | 78 * @param {number} pageSize |
60 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback | 79 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback |
61 */ | 80 */ |
62 loadCacheData: function(cacheId, skipCount, pageSize, callback) | 81 loadCacheData: function(cacheId, skipCount, pageSize, callback) |
63 { | 82 { |
64 this._requestEntries(cacheId, cacheId.name, skipCount, pageSize, callbac k); | 83 this._requestEntries(cacheId, cacheId.name, skipCount, pageSize, callbac k); |
65 }, | 84 }, |
66 | 85 |
67 /** | 86 /** |
68 * @return {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>} | 87 * @return {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>} |
69 */ | 88 */ |
70 caches: function() | 89 caches: function() |
71 { | 90 { |
72 var result = []; | 91 var result = []; |
73 for (var cacheName of this._cacheNames) { | 92 this._cacheNamesBySecurityOrigin.forEach(function(caches, securityOrigin ) { |
pfeldman
2015/04/03 09:15:57
We don't use anonymous functions, please declare n
dmurph
2015/04/08 19:35:35
I feel like that is normal and readable for forEac
pfeldman
2015/04/09 10:31:12
That's JS coding guidelines for Blink, otherwise i
| |
74 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName )); | 93 for (var cacheName of caches) { |
pfeldman
2015/04/03 09:15:57
no {} around single line blocks
dmurph
2015/04/08 19:35:35
Done.
| |
75 } | 94 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(sec urityOrigin, cacheName)); |
95 } | |
96 }, this); | |
76 return result; | 97 return result; |
77 }, | 98 }, |
78 | 99 |
79 dispose: function() | 100 dispose: function() |
80 { | 101 { |
81 this._updateCacheNames([]); | 102 for (var securityOrigin of this._cacheNamesBySecurityOrigin.keys()) { |
pfeldman
2015/04/03 09:15:58
no {}
dmurph
2015/04/08 19:35:35
Done.
| |
103 this._updateCacheNames(securityOrigin, []); | |
104 } | |
105 this._cacheNamesBySecurityOrigin.clear(); | |
106 if (this._enabled) { | |
107 this.target().resourceTreeModel.removeEventListener(WebInspector.Res ourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); | |
108 this.target().resourceTreeModel.removeEventListener(WebInspector.Res ourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, th is); | |
109 } | |
82 }, | 110 }, |
83 | 111 |
84 _loadCacheNames: function() | 112 _addOrigin: function(securityOrigin) { |
pfeldman
2015/04/03 09:15:57
{ on the next line.
dmurph
2015/04/08 19:35:35
Done.
| |
113 console.assert(!this._cacheNamesBySecurityOrigin.has(securityOrigin)); | |
114 this._cacheNamesBySecurityOrigin.set(securityOrigin, new Set()); | |
115 this._loadCacheNames(securityOrigin); | |
116 }, | |
117 | |
118 /** | |
119 * @param {string} securityOrigin | |
120 */ | |
121 _removeOrigin: function(securityOrigin) | |
122 { | |
123 console.assert(this._cacheNamesBySecurityOrigin.has(securityOrigin)); | |
124 var cacheNames = this._cacheNamesBySecurityOrigin.get(securityOrigin); | |
125 if (cacheNames) { | |
126 for (var cacheName of cacheNames) | |
127 this._cacheRemoved(securityOrigin, cacheName); | |
128 } | |
129 this._cacheNamesBySecurityOrigin.delete(securityOrigin); | |
130 }, | |
131 | |
132 /** | |
133 * @param {string} securityOrigin | |
134 */ | |
135 _loadCacheNames: function(securityOrigin) | |
85 { | 136 { |
86 /** | 137 /** |
87 * @param {?Protocol.Error} error | 138 * @param {?Protocol.Error} error |
88 * @param {!Array.<string>} cacheNames | 139 * @param {!Array.<string>} cacheNames |
89 * @this {WebInspector.ServiceWorkerCacheModel} | 140 * @this {WebInspector.ServiceWorkerCacheModel} |
90 */ | 141 */ |
91 function callback(error, cacheNames) | 142 function callback(error, cacheNames) |
92 { | 143 { |
93 if (error) { | 144 if (error) { |
94 console.error("ServiceWorkerCacheAgent error: ", error); | 145 console.error("ServiceWorkerCacheAgent error: ", error); |
95 return; | 146 return; |
96 } | 147 } |
97 | 148 if (!this._cacheNamesBySecurityOrigin.has(securityOrigin)) |
98 if (!this._cacheNames) | |
99 return; | 149 return; |
100 this._updateCacheNames(cacheNames); | 150 this._updateCacheNames(securityOrigin, cacheNames); |
101 } | 151 } |
102 | 152 this._agent.requestCacheNames(securityOrigin, callback.bind(this)); |
103 this._agent.requestCacheNames(callback.bind(this)); | |
104 }, | 153 }, |
105 | 154 |
106 /** | 155 /** |
156 * @param {string} securityOrigin | |
107 * @param {!Array.<string>} cacheNames | 157 * @param {!Array.<string>} cacheNames |
108 */ | 158 */ |
109 _updateCacheNames: function(cacheNames) | 159 _updateCacheNames: function(securityOrigin, cacheNames) |
110 { | 160 { |
111 /** @type {!Set.<string>} */ | 161 /** @type {!Set<string>} */ |
112 var newCacheNames = new Set(cacheNames); | 162 var newCacheNames = new Set(cacheNames); |
113 /** @type {!Set.<string>} */ | 163 /** @type {!Set<string>} */ |
114 var oldCacheNames = this._cacheNames; | 164 var oldCacheNames; |
165 /** @type {!Set<string>|undefined} */ | |
166 var tempOldCacheNames = this._cacheNamesBySecurityOrigin.get(securityOri gin); | |
167 if (tempOldCacheNames) { | |
pfeldman
2015/04/03 09:15:57
no {}
dmurph
2015/04/08 19:35:35
For an if-else statement? Isn't that super awful?
pfeldman
2015/04/09 10:31:12
https://www.chromium.org/blink/coding-style
| |
168 oldCacheNames = tempOldCacheNames; | |
169 } else { | |
170 oldCacheNames = new Set(); | |
171 } | |
115 | 172 |
116 this._cacheNames = new Set(cacheNames); | 173 this._cacheNamesBySecurityOrigin.set(securityOrigin, newCacheNames); |
117 | 174 |
118 for (var oldCacheName of oldCacheNames) { | 175 for (var oldCacheName of oldCacheNames) { |
119 if (!newCacheNames[oldCacheName]) | 176 if (!newCacheNames.has(oldCacheName)) |
120 this._cacheRemoved(oldCacheName); | 177 this._cacheRemoved(securityOrigin, oldCacheName); |
121 } | 178 } |
122 for (var newCacheName of newCacheNames) { | 179 for (var newCacheName of newCacheNames) { |
123 if (!oldCacheNames[newCacheName]) | 180 if (!oldCacheNames.has(newCacheName)) |
124 this._cacheAdded(newCacheName); | 181 this._cacheAdded(securityOrigin, newCacheName); |
125 } | 182 } |
126 }, | 183 }, |
127 | 184 |
128 /** | 185 /** |
186 * @param {!WebInspector.Event} event | |
187 */ | |
188 _securityOriginAdded: function(event) | |
189 { | |
190 var securityOrigin = /** @type {string} */ (event.data); | |
191 this._addOrigin(securityOrigin); | |
192 }, | |
193 | |
194 /** | |
195 * @param {!WebInspector.Event} event | |
196 */ | |
197 _securityOriginRemoved: function(event) | |
198 { | |
199 var securityOrigin = /** @type {string} */ (event.data); | |
200 this._removeOrigin(securityOrigin); | |
201 }, | |
202 | |
203 /** | |
204 * @param {string} securityOrigin | |
129 * @param {string} cacheName | 205 * @param {string} cacheName |
130 */ | 206 */ |
131 _cacheAdded: function(cacheName) | 207 _cacheAdded: function(securityOrigin, cacheName) |
132 { | 208 { |
133 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName ); | 209 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(securityO rigin, cacheName); |
134 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheAdded, cacheId); | 210 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheAdded, cacheId); |
135 }, | 211 }, |
136 | 212 |
137 /** | 213 /** |
214 * @param {string} securityOrigin | |
138 * @param {string} cacheName | 215 * @param {string} cacheName |
139 */ | 216 */ |
140 _cacheRemoved: function(cacheName) | 217 _cacheRemoved: function(securityOrigin, cacheName) |
141 { | 218 { |
142 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName ); | 219 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(securityO rigin, cacheName); |
143 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheRemoved, cacheId); | 220 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheRemoved, cacheId); |
144 }, | 221 }, |
145 | 222 |
146 /** | 223 /** |
147 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 224 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
148 * @param {string} cacheName | 225 * @param {string} cacheName |
149 * @param {number} skipCount | 226 * @param {number} skipCount |
150 * @param {number} pageSize | 227 * @param {number} pageSize |
151 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback | 228 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback |
152 */ | 229 */ |
153 _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback) | 230 _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback) |
154 { | 231 { |
155 /** | 232 /** |
156 * @param {?Protocol.Error} error | 233 * @param {?Protocol.Error} error |
157 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt ries | 234 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt ries |
158 * @param {boolean} hasMore | 235 * @param {boolean} hasMore |
159 * @this {WebInspector.ServiceWorkerCacheModel} | 236 * @this {WebInspector.ServiceWorkerCacheModel} |
160 */ | 237 */ |
161 function innerCallback(error, dataEntries, hasMore) | 238 function innerCallback(error, dataEntries, hasMore) |
162 { | 239 { |
163 if (error) { | 240 if (error) { |
164 console.error("ServiceWorkerCacheAgent error: ", error); | 241 console.error("ServiceWorkerCacheAgent error: ", error); |
165 return; | 242 return; |
166 } | 243 } |
167 | 244 if (!this._cacheNamesBySecurityOrigin.has(cacheId.securityOrigin)) |
168 if (!this._cacheNames) | |
169 return; | 245 return; |
170 var entries = []; | 246 var entries = []; |
171 for (var i = 0; i < dataEntries.length; ++i) { | 247 for (var i = 0; i < dataEntries.length; ++i) { |
172 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par se(dataEntries[i].request)); | 248 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par se(dataEntries[i].request)); |
173 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa rse(dataEntries[i].response)); | 249 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa rse(dataEntries[i].response)); |
174 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ est, response)); | 250 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ est, response)); |
175 } | 251 } |
176 callback(entries, hasMore); | 252 callback(entries, hasMore); |
177 } | 253 } |
178 | 254 this._agent.requestEntries(cacheId.securityOrigin, cacheName, skipCount, pageSize, innerCallback.bind(this)); |
179 this._agent.requestEntries(cacheName, skipCount, pageSize, innerCallback .bind(this)); | |
180 }, | 255 }, |
181 | 256 |
182 __proto__: WebInspector.SDKModel.prototype | 257 __proto__: WebInspector.SDKModel.prototype |
183 } | 258 } |
184 | 259 |
185 /** | 260 /** |
186 * @constructor | 261 * @constructor |
187 * @param {!WebInspector.RemoteObject} request | 262 * @param {!WebInspector.RemoteObject} request |
188 * @param {!WebInspector.RemoteObject} response | 263 * @param {!WebInspector.RemoteObject} response |
189 */ | 264 */ |
190 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) | 265 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) |
191 { | 266 { |
192 this.request = request; | 267 this.request = request; |
193 this.response = response; | 268 this.response = response; |
194 } | 269 } |
195 | 270 |
196 /** | 271 /** |
197 * @constructor | 272 * @constructor |
273 * @param {string} securityOrigin | |
198 * @param {string} name | 274 * @param {string} name |
199 */ | 275 */ |
200 WebInspector.ServiceWorkerCacheModel.CacheId = function(name) | 276 WebInspector.ServiceWorkerCacheModel.CacheId = function(securityOrigin, name) |
201 { | 277 { |
278 this.securityOrigin = securityOrigin; | |
202 this.name = name; | 279 this.name = name; |
203 } | 280 } |
204 | 281 |
205 WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { | 282 WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { |
206 /** | 283 /** |
207 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 284 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
208 * @return {boolean} | 285 * @return {boolean} |
209 */ | 286 */ |
210 equals: function(cacheId) | 287 equals: function(cacheId) |
211 { | 288 { |
212 return this.name === cacheId.name; | 289 return this.name === cacheId.name && this.securityOrigin === cacheId.sec urityOrigin; |
213 } | 290 } |
214 } | 291 } |
215 | 292 |
216 /** | 293 /** |
217 * @constructor | 294 * @constructor |
218 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 295 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
219 */ | 296 */ |
220 WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId) | 297 WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId) |
221 { | 298 { |
222 this.cacheId = cacheId; | 299 this.cacheId = cacheId; |
300 } | |
301 | |
302 WebInspector.ServiceWorkerCacheModel._symbol = Symbol("CacheStorageModel"); | |
303 /** | |
304 * @param {!WebInspector.Target} target | |
305 * @return {!WebInspector.ServiceWorkerCacheModel} | |
306 */ | |
307 WebInspector.ServiceWorkerCacheModel.fromTarget = function(target) | |
308 { | |
309 if (!target[WebInspector.ServiceWorkerCacheModel._symbol]) | |
310 target[WebInspector.ServiceWorkerCacheModel._symbol] = new WebInspector. ServiceWorkerCacheModel(target); | |
311 | |
312 return target[WebInspector.ServiceWorkerCacheModel._symbol]; | |
223 } | 313 } |
OLD | NEW |