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 {!Object.<string, !Array.<string>>} */ |
15 this._cacheNames = new Set(); | 15 this._cacheNamesBySecurityOrigin = {}; |
pfeldman
2015/04/01 07:01:09
Use Map.
Also, please extract the Cache-per-Origi
dmurph
2015/04/02 20:59:03
It's basically exactly the same patch, but without
| |
16 | 16 |
17 this._agent = target.serviceWorkerCacheAgent(); | 17 this._agent = target.cacheStorageAgent(); |
18 } | 18 } |
19 | 19 |
20 WebInspector.ServiceWorkerCacheModel.EventTypes = { | 20 WebInspector.ServiceWorkerCacheModel.EventTypes = { |
21 CacheAdded: "CacheAdded", | 21 CacheAdded: "CacheAdded", |
22 CacheRemoved: "CacheRemoved", | 22 CacheRemoved: "CacheRemoved" |
23 } | 23 } |
24 | 24 |
25 WebInspector.ServiceWorkerCacheModel.prototype = { | 25 WebInspector.ServiceWorkerCacheModel.prototype = { |
26 _reset: function() | 26 enable: function() |
27 { | 27 { |
28 this._updateCacheNames([]); | 28 if (this._enabled) |
29 this._loadCacheNames(); | 29 return; |
30 | |
31 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTr eeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); | |
32 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTr eeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this); | |
33 | |
34 var securityOrigins = this.target().resourceTreeModel.securityOrigins(); | |
35 for (var i = 0; i < securityOrigins.length; ++i) | |
36 this._addOrigin(securityOrigins[i]); | |
37 this._enabled = true; | |
30 }, | 38 }, |
31 | 39 |
32 refreshCacheNames: function() | 40 refreshCacheNames: function() |
33 { | 41 { |
34 this._loadCacheNames(); | 42 var securityOrigins = this.target().resourceTreeModel.securityOrigins(); |
43 for (var securityOrigin of securityOrigins) { | |
44 this._loadCacheNames(securityOrigin); | |
45 } | |
35 }, | 46 }, |
36 | 47 |
37 /** | 48 /** |
38 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 49 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
39 */ | 50 */ |
40 deleteCache: function(cacheId) | 51 deleteCache: function(cacheId) |
41 { | 52 { |
53 var securityOrigin = cacheId.securityOrigin; | |
42 /** | 54 /** |
43 * @this {WebInspector.ServiceWorkerCacheModel} | 55 * @this {WebInspector.ServiceWorkerCacheModel} |
44 */ | 56 */ |
45 function callback(error) | 57 function callback(error) |
46 { | 58 { |
47 if (error) { | 59 if (error) { |
48 console.error("ServiceWorkerCacheAgent error: ", error); | 60 console.error("ServiceWorkerCacheAgent error: ", error); |
49 return; | 61 return; |
50 } | 62 } |
51 this._cacheRemoved(cacheId.name); | 63 if (!this._cacheNamesBySecurityOrigin[securityOrigin]) |
64 return; | |
65 var caches = this._cacheNamesBySecurityOrigin[securityOrigin]; | |
66 var index = caches.indexOf(cacheId.name); | |
67 if (index != -1) { | |
68 caches.splice(index, 1); | |
69 } | |
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 for (var securityOrigin in this._cacheNamesBySecurityOrigin) { |
74 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName )); | 93 var caches = this._cacheNamesBySecurityOrigin[securityOrigin]; |
94 for (var cacheName of caches) { | |
95 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(sec urityOrigin, cacheName)); | |
96 } | |
75 } | 97 } |
76 return result; | 98 return result; |
77 }, | 99 }, |
78 | 100 |
79 dispose: function() | 101 dispose: function() |
80 { | 102 { |
81 this._updateCacheNames([]); | 103 for (var securityOrigin in this._cacheNamesBySecurityOrigin) { |
104 this._updateCacheNames(securityOrigin, []); | |
105 } | |
106 this._cacheNamesBySecurityOrigin = {}; | |
82 }, | 107 }, |
83 | 108 |
84 _loadCacheNames: function() | 109 _addOrigin: function(securityOrigin) { |
110 console.assert(!this._cacheNamesBySecurityOrigin[securityOrigin]); | |
111 this._cacheNamesBySecurityOrigin[securityOrigin] = []; | |
112 this._loadCacheNames(securityOrigin); | |
113 }, | |
114 | |
115 /** | |
116 * @param {string} securityOrigin | |
117 */ | |
118 _removeOrigin: function(securityOrigin) | |
119 { | |
120 console.assert(this._cacheNamesBySecurityOrigin[securityOrigin]); | |
121 for (var cacheName of this._cacheNamesBySecurityOrigin[securityOrigin]) | |
122 this._cacheRemoved(securityOrigin, cacheName); | |
123 delete this._cacheNamesBySecurityOrigin[securityOrigin]; | |
124 }, | |
125 | |
126 /** | |
127 * @param {string} securityOrigin | |
128 */ | |
129 _loadCacheNames: function(securityOrigin) | |
85 { | 130 { |
86 /** | 131 /** |
87 * @param {?Protocol.Error} error | 132 * @param {?Protocol.Error} error |
88 * @param {!Array.<string>} cacheNames | 133 * @param {!Array.<string>} cacheNames |
89 * @this {WebInspector.ServiceWorkerCacheModel} | 134 * @this {WebInspector.ServiceWorkerCacheModel} |
90 */ | 135 */ |
91 function callback(error, cacheNames) | 136 function callback(error, cacheNames) |
92 { | 137 { |
93 if (error) { | 138 if (error) { |
94 console.error("ServiceWorkerCacheAgent error: ", error); | 139 console.error("ServiceWorkerCacheAgent error: ", error); |
95 return; | 140 return; |
96 } | 141 } |
97 | 142 if (!this._cacheNamesBySecurityOrigin[securityOrigin]) |
98 if (!this._cacheNames) | |
99 return; | 143 return; |
100 this._updateCacheNames(cacheNames); | 144 this._updateCacheNames(securityOrigin, cacheNames); |
101 } | 145 } |
102 | 146 this._agent.requestCacheNames(securityOrigin, callback.bind(this)); |
103 this._agent.requestCacheNames(callback.bind(this)); | |
104 }, | 147 }, |
105 | 148 |
106 /** | 149 /** |
150 * @param {string} securityOrigin | |
107 * @param {!Array.<string>} cacheNames | 151 * @param {!Array.<string>} cacheNames |
108 */ | 152 */ |
109 _updateCacheNames: function(cacheNames) | 153 _updateCacheNames: function(securityOrigin, cacheNames) |
110 { | 154 { |
111 /** @type {!Set.<string>} */ | 155 /** @type {!Object<string,boolean>} */ |
112 var newCacheNames = new Set(cacheNames); | 156 var newCacheNames = cacheNames.keySet(); |
113 /** @type {!Set.<string>} */ | 157 /** @type {!Object<string,boolean>} */ |
114 var oldCacheNames = this._cacheNames; | 158 var oldCacheNames = this._cacheNamesBySecurityOrigin[securityOrigin].key Set(); |
115 | 159 |
116 this._cacheNames = new Set(cacheNames); | 160 this._cacheNamesBySecurityOrigin[securityOrigin] = cacheNames; |
117 | 161 |
118 for (var oldCacheName of oldCacheNames) { | 162 for (var oldCacheName in oldCacheNames) { |
119 if (!newCacheNames[oldCacheName]) | 163 if (!newCacheNames[oldCacheName]) |
120 this._cacheRemoved(oldCacheName); | 164 this._cacheRemoved(securityOrigin, oldCacheName); |
121 } | 165 } |
122 for (var newCacheName of newCacheNames) { | 166 for (var newCacheName in newCacheNames) { |
123 if (!oldCacheNames[newCacheName]) | 167 if (!oldCacheNames[newCacheName]) |
124 this._cacheAdded(newCacheName); | 168 this._cacheAdded(securityOrigin, newCacheName); |
125 } | 169 } |
126 }, | 170 }, |
127 | 171 |
128 /** | 172 /** |
173 * @param {!WebInspector.Event} event | |
174 */ | |
175 _securityOriginAdded: function(event) | |
176 { | |
177 var securityOrigin = /** @type {string} */ (event.data); | |
178 this._addOrigin(securityOrigin); | |
179 }, | |
180 | |
181 /** | |
182 * @param {!WebInspector.Event} event | |
183 */ | |
184 _securityOriginRemoved: function(event) | |
185 { | |
186 var securityOrigin = /** @type {string} */ (event.data); | |
187 this._removeOrigin(securityOrigin); | |
188 }, | |
189 | |
190 /** | |
191 * @param {string} securityOrigin | |
129 * @param {string} cacheName | 192 * @param {string} cacheName |
130 */ | 193 */ |
131 _cacheAdded: function(cacheName) | 194 _cacheAdded: function(securityOrigin, cacheName) |
132 { | 195 { |
133 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName ); | 196 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(securityO rigin, cacheName); |
134 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheAdded, cacheId); | 197 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheAdded, cacheId); |
135 }, | 198 }, |
136 | 199 |
137 /** | 200 /** |
201 * @param {string} securityOrigin | |
138 * @param {string} cacheName | 202 * @param {string} cacheName |
139 */ | 203 */ |
140 _cacheRemoved: function(cacheName) | 204 _cacheRemoved: function(securityOrigin, cacheName) |
141 { | 205 { |
142 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName ); | 206 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(securityO rigin, cacheName); |
143 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheRemoved, cacheId); | 207 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheRemoved, cacheId); |
144 }, | 208 }, |
145 | 209 |
146 /** | 210 /** |
147 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 211 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
148 * @param {string} cacheName | 212 * @param {string} cacheName |
149 * @param {number} skipCount | 213 * @param {number} skipCount |
150 * @param {number} pageSize | 214 * @param {number} pageSize |
151 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback | 215 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback |
152 */ | 216 */ |
153 _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback) | 217 _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback) |
154 { | 218 { |
155 /** | 219 /** |
156 * @param {?Protocol.Error} error | 220 * @param {?Protocol.Error} error |
157 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt ries | 221 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt ries |
158 * @param {boolean} hasMore | 222 * @param {boolean} hasMore |
159 * @this {WebInspector.ServiceWorkerCacheModel} | 223 * @this {WebInspector.ServiceWorkerCacheModel} |
160 */ | 224 */ |
161 function innerCallback(error, dataEntries, hasMore) | 225 function innerCallback(error, dataEntries, hasMore) |
162 { | 226 { |
163 if (error) { | 227 if (error) { |
164 console.error("ServiceWorkerCacheAgent error: ", error); | 228 console.error("ServiceWorkerCacheAgent error: ", error); |
165 return; | 229 return; |
166 } | 230 } |
167 | 231 if (!this._cacheNamesBySecurityOrigin[cacheId.securityOrigin]) |
168 if (!this._cacheNames) | |
169 return; | 232 return; |
170 var entries = []; | 233 var entries = []; |
171 for (var i = 0; i < dataEntries.length; ++i) { | 234 for (var i = 0; i < dataEntries.length; ++i) { |
172 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par se(dataEntries[i].request)); | 235 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par se(dataEntries[i].request)); |
173 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa rse(dataEntries[i].response)); | 236 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa rse(dataEntries[i].response)); |
174 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ est, response)); | 237 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ est, response)); |
175 } | 238 } |
176 callback(entries, hasMore); | 239 callback(entries, hasMore); |
177 } | 240 } |
178 | 241 this._agent.requestEntries(cacheId.securityOrigin, cacheName, skipCount, pageSize, innerCallback.bind(this)); |
179 this._agent.requestEntries(cacheName, skipCount, pageSize, innerCallback .bind(this)); | |
180 }, | 242 }, |
181 | 243 |
182 __proto__: WebInspector.SDKModel.prototype | 244 __proto__: WebInspector.SDKModel.prototype |
183 } | 245 } |
184 | 246 |
185 /** | 247 /** |
186 * @constructor | 248 * @constructor |
187 * @param {!WebInspector.RemoteObject} request | 249 * @param {!WebInspector.RemoteObject} request |
188 * @param {!WebInspector.RemoteObject} response | 250 * @param {!WebInspector.RemoteObject} response |
189 */ | 251 */ |
190 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) | 252 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) |
191 { | 253 { |
192 this.request = request; | 254 this.request = request; |
193 this.response = response; | 255 this.response = response; |
194 } | 256 } |
195 | 257 |
196 /** | 258 /** |
197 * @constructor | 259 * @constructor |
260 * @param {string} securityOrigin | |
198 * @param {string} name | 261 * @param {string} name |
199 */ | 262 */ |
200 WebInspector.ServiceWorkerCacheModel.CacheId = function(name) | 263 WebInspector.ServiceWorkerCacheModel.CacheId = function(securityOrigin, name) |
201 { | 264 { |
265 this.securityOrigin = securityOrigin; | |
202 this.name = name; | 266 this.name = name; |
203 } | 267 } |
204 | 268 |
205 WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { | 269 WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { |
206 /** | 270 /** |
207 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 271 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
208 * @return {boolean} | 272 * @return {boolean} |
209 */ | 273 */ |
210 equals: function(cacheId) | 274 equals: function(cacheId) |
211 { | 275 { |
212 return this.name === cacheId.name; | 276 return this.name === cacheId.name && this.securityOrigin === cacheId.sec urityOrigin; |
213 } | 277 } |
214 } | 278 } |
215 | 279 |
216 /** | 280 /** |
217 * @constructor | 281 * @constructor |
218 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 282 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
219 */ | 283 */ |
220 WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId) | 284 WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId) |
221 { | 285 { |
222 this.cacheId = cacheId; | 286 this.cacheId = cacheId; |
287 } | |
288 | |
289 WebInspector.ServiceWorkerCacheModel._symbol = Symbol("CacheStorageModel"); | |
290 /** | |
291 * @param {!WebInspector.Target} target | |
292 * @return {!WebInspector.ServiceWorkerCacheModel} | |
293 */ | |
294 WebInspector.ServiceWorkerCacheModel.fromTarget = function(target) | |
295 { | |
296 if (!target[WebInspector.ServiceWorkerCacheModel._symbol]) | |
297 target[WebInspector.ServiceWorkerCacheModel._symbol] = new WebInspector. ServiceWorkerCacheModel(target); | |
298 | |
299 return target[WebInspector.ServiceWorkerCacheModel._symbol]; | |
223 } | 300 } |
OLD | NEW |