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 {!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 var caches = this._cacheNamesBySecurityOrigin.get(securityOrigin); |
| 67 if (!caches) | |
| 68 return; | |
| 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 /** @suppressReceiverCheck because you can never win. */ |
| 74 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName )); | 93 var mapEntriesToResults = function(caches, securityOrigin) { |
|
pfeldman
2015/04/10 12:35:34
Your patch does not apply to ToT. But you should:
dmurph
2015/04/13 18:23:03
Ended up being unneeded.
| |
| 94 for (var cacheName of caches) | |
| 95 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(sec urityOrigin, cacheName)); | |
| 75 } | 96 } |
| 97 this._cacheNamesBySecurityOrigin.forEach(mapEntriesToResults, this); | |
| 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 of this._cacheNamesBySecurityOrigin.keys()) |
| 104 this._updateCacheNames(securityOrigin, []); | |
| 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) |
| 113 { | |
| 114 console.assert(!this._cacheNamesBySecurityOrigin.has(securityOrigin)); | |
| 115 this._cacheNamesBySecurityOrigin.set(securityOrigin, new Set()); | |
| 116 this._loadCacheNames(securityOrigin); | |
| 117 }, | |
| 118 | |
| 119 /** | |
| 120 * @param {string} securityOrigin | |
| 121 */ | |
| 122 _removeOrigin: function(securityOrigin) | |
| 123 { | |
| 124 var cacheNames = this._cacheNamesBySecurityOrigin.get(securityOrigin); | |
| 125 console.assert(cacheNames); | |
| 126 if (cacheNames) { | |
| 127 for (var cacheName of cacheNames) | |
| 128 this._cacheRemoved(securityOrigin, cacheName); | |
| 129 } | |
| 130 this._cacheNamesBySecurityOrigin.delete(securityOrigin); | |
| 131 }, | |
| 132 | |
| 133 /** | |
| 134 * @param {string} securityOrigin | |
| 135 */ | |
| 136 _loadCacheNames: function(securityOrigin) | |
| 85 { | 137 { |
| 86 /** | 138 /** |
| 87 * @param {?Protocol.Error} error | 139 * @param {?Protocol.Error} error |
| 88 * @param {!Array.<string>} cacheNames | 140 * @param {!Array.<string>} cacheNames |
| 89 * @this {WebInspector.ServiceWorkerCacheModel} | 141 * @this {WebInspector.ServiceWorkerCacheModel} |
| 90 */ | 142 */ |
| 91 function callback(error, cacheNames) | 143 function callback(error, cacheNames) |
| 92 { | 144 { |
| 93 if (error) { | 145 if (error) { |
| 94 console.error("ServiceWorkerCacheAgent error: ", error); | 146 console.error("ServiceWorkerCacheAgent error: ", error); |
| 95 return; | 147 return; |
| 96 } | 148 } |
| 97 | 149 if (!this._cacheNamesBySecurityOrigin.has(securityOrigin)) |
| 98 if (!this._cacheNames) | |
| 99 return; | 150 return; |
| 100 this._updateCacheNames(cacheNames); | 151 this._updateCacheNames(securityOrigin, cacheNames); |
| 101 } | 152 } |
| 102 | 153 this._agent.requestCacheNames(securityOrigin, callback.bind(this)); |
| 103 this._agent.requestCacheNames(callback.bind(this)); | |
| 104 }, | 154 }, |
| 105 | 155 |
| 106 /** | 156 /** |
| 157 * @param {string} securityOrigin | |
| 107 * @param {!Array.<string>} cacheNames | 158 * @param {!Array.<string>} cacheNames |
| 108 */ | 159 */ |
| 109 _updateCacheNames: function(cacheNames) | 160 _updateCacheNames: function(securityOrigin, cacheNames) |
| 110 { | 161 { |
| 111 /** @type {!Set.<string>} */ | 162 /** @type {!Set<string>} */ |
| 112 var newCacheNames = new Set(cacheNames); | 163 var newCacheNames = new Set(cacheNames); |
| 113 /** @type {!Set.<string>} */ | 164 /** @type {!Set<string>} */ |
| 114 var oldCacheNames = this._cacheNames; | 165 var oldCacheNames; |
| 166 /** @type {!Set<string>|undefined} */ | |
| 167 var tempOldCacheNames = this._cacheNamesBySecurityOrigin.get(securityOri gin); | |
| 168 if (tempOldCacheNames) | |
| 169 oldCacheNames = tempOldCacheNames; | |
| 170 else | |
| 171 oldCacheNames = new Set(); | |
| 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 |