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