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