Chromium Code Reviews| Index: Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js |
| diff --git a/Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js b/Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js |
| index 52af59c9decd71f00a6306b0d78371a188fb7b5e..06498579bde082266a947974997d02c6101f9de4 100644 |
| --- a/Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js |
| +++ b/Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js |
| @@ -11,27 +11,43 @@ WebInspector.ServiceWorkerCacheModel = function(target) |
| { |
| WebInspector.SDKModel.call(this, WebInspector.ServiceWorkerCacheModel, target); |
| - /** @type {!Set.<string>} */ |
| - this._cacheNames = new Set(); |
| + // We use JSON.stringify of the CacheId to form our key. This is because |
| + // we cannot specify our own equality function for Sets yet. |
| + /** @type {!Map<string, !WebInspector.ServiceWorkerCacheModel.CacheId>} */ |
| + this._caches = new Map(); |
|
pfeldman
2015/04/16 10:22:52
Sorry, I did not notice you migrated to cacheIds,
pfeldman
2015/04/17 07:57:36
This would actually be totally fine if the key is
dmurph
2015/04/18 00:59:31
Awesome, that's what I was thinking. Done.
|
| - this._agent = target.serviceWorkerCacheAgent(); |
| + this._agent = target.cacheStorageAgent(); |
| + |
| + /** @type {boolean} */ |
| + this._enabled = false; |
| } |
| WebInspector.ServiceWorkerCacheModel.EventTypes = { |
| CacheAdded: "CacheAdded", |
| - CacheRemoved: "CacheRemoved", |
| + CacheRemoved: "CacheRemoved" |
| } |
| WebInspector.ServiceWorkerCacheModel.prototype = { |
| - _reset: function() |
| + enable: function() |
| { |
| - this._updateCacheNames([]); |
| - this._loadCacheNames(); |
| + if (this._enabled) |
| + return; |
| + |
| + this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); |
| + this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this); |
| + |
| + var securityOrigins = this.target().resourceTreeModel.securityOrigins(); |
| + for (var i = 0; i < securityOrigins.length; ++i) |
| + this._addOrigin(securityOrigins[i]); |
| + this._enabled = true; |
| }, |
| refreshCacheNames: function() |
| { |
| - this._loadCacheNames(); |
| + var securityOrigins = this.target().resourceTreeModel.securityOrigins(); |
| + for (var securityOrigin of securityOrigins) { |
|
pfeldman
2015/04/16 10:22:52
drop {}
dmurph
2015/04/18 00:59:31
Done.
|
| + this._loadCacheNames(securityOrigin); |
| + } |
| }, |
| /** |
| @@ -45,12 +61,13 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| function callback(error) |
| { |
| if (error) { |
| - console.error("ServiceWorkerCacheAgent error: ", error); |
| + console.error("ServiceWorkerCacheAgent error deleting cache ", cacheId.toString(), ": ", error); |
| return; |
| } |
| - this._cacheRemoved(cacheId.name); |
| + this._caches.delete(JSON.stringify(cacheId)); |
| + this._cacheRemoved(cacheId); |
| } |
| - this._agent.deleteCache(cacheId.name, callback.bind(this)); |
| + this._agent.deleteCache(cacheId, callback.bind(this)); |
| }, |
| /** |
| @@ -61,7 +78,7 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| */ |
| loadCacheData: function(cacheId, skipCount, pageSize, callback) |
| { |
| - this._requestEntries(cacheId, cacheId.name, skipCount, pageSize, callback); |
| + this._requestEntries(cacheId, skipCount, pageSize, callback); |
| }, |
| /** |
| @@ -69,104 +86,164 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| */ |
| caches: function() |
| { |
| - var result = []; |
| - for (var cacheName of this._cacheNames) { |
| - result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName)); |
| + var caches = new Array(this._caches.size); |
| + var i = 0; |
| + for (var cache of this._caches.values()) { |
|
pfeldman
2015/04/16 10:22:52
drop {}
dmurph
2015/04/18 00:59:31
Done.
|
| + caches[i++] = cache; |
|
pfeldman
2015/04/16 10:22:53
We typically just .push for simplicity.
dmurph
2015/04/18 00:59:31
Done.
|
| } |
| - return result; |
| + return caches; |
| }, |
| dispose: function() |
| { |
| - this._updateCacheNames([]); |
| + for (var cacheId of this._caches.values()) |
| + this._cacheRemoved(cacheId); |
| + this._caches.clear(); |
| + if (this._enabled) { |
| + this.target().resourceTreeModel.removeEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); |
| + this.target().resourceTreeModel.removeEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this); |
| + } |
| }, |
| - _loadCacheNames: function() |
| + _addOrigin: function(securityOrigin) |
| + { |
| + this._loadCacheNames(securityOrigin); |
| + }, |
| + |
| + /** |
| + * @param {string} securityOrigin |
| + */ |
| + _removeOrigin: function(securityOrigin) |
| + { |
| + for (var serializedId of this._caches.keys()) { |
| + var cacheId = this._caches.get(serializedId); |
| + if (cacheId.securityOrigin == securityOrigin) { |
| + this._caches.delete(serializedId); |
| + this._cacheRemoved(cacheId); |
| + } |
| + } |
| + }, |
| + |
| + /** |
| + * @param {string} securityOrigin |
| + */ |
| + _loadCacheNames: function(securityOrigin) |
| { |
| /** |
| * @param {?Protocol.Error} error |
| - * @param {!Array.<string>} cacheNames |
| + * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>} cacheIds |
| * @this {WebInspector.ServiceWorkerCacheModel} |
| */ |
| - function callback(error, cacheNames) |
| + function callback(error, cacheIds) |
| { |
| if (error) { |
| - console.error("ServiceWorkerCacheAgent error: ", error); |
| + console.error("ServiceWorkerCacheAgent error while loading caches: ", error); |
| return; |
| } |
| - |
| - if (!this._cacheNames) |
| - return; |
| - this._updateCacheNames(cacheNames); |
| + this._updateCacheNames(securityOrigin, cacheIds); |
| } |
| - |
| - this._agent.requestCacheNames(callback.bind(this)); |
| + this._agent.requestCacheNames(securityOrigin, callback.bind(this)); |
| }, |
| /** |
| - * @param {!Array.<string>} cacheNames |
| + * @param {string} securityOrigin |
| + * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>} cacheIds |
| */ |
| - _updateCacheNames: function(cacheNames) |
| + _updateCacheNames: function(securityOrigin, cacheIds) |
| { |
| - /** @type {!Set.<string>} */ |
| - var newCacheNames = new Set(cacheNames); |
| - /** @type {!Set.<string>} */ |
| - var oldCacheNames = this._cacheNames; |
| + /** |
| + * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
| + * @return {string} |
| + */ |
| + function cacheIdToName(cacheId) |
| + { |
| + return cacheId.cacheName; |
| + } |
| + /** |
| + * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
| + * @param {string} serializedId |
| + * @this {WebInspector.ServiceWorkerCacheModel} |
| + */ |
| + function deleteAndSaveForSecurityOrigin(cacheId, serializedId) |
| + { |
| + if (cacheId.securityOrigin == securityOrigin) { |
| + oldCacheNames.add(cacheId.cacheName); |
| + this._caches.delete(serializedId); |
| + } |
| + } |
| + |
| + /** @type {!Set<string>} */ |
| + var newCacheNames = new Set(cacheIds.map(cacheIdToName)); |
| + /** @type {!Set<string>} */ |
| + var oldCacheNames = new Set(); |
| + this._caches.forEach(deleteAndSaveForSecurityOrigin, this); |
| - this._cacheNames = new Set(cacheNames); |
| + for (var cacheId of cacheIds) |
| + this._caches.set(JSON.stringify(cacheId), cacheId); |
| for (var oldCacheName of oldCacheNames) { |
| - if (!newCacheNames[oldCacheName]) |
| - this._cacheRemoved(oldCacheName); |
| + if (!newCacheNames.has(oldCacheName)) |
| + this._cacheRemoved(new WebInspector.ServiceWorkerCacheModel.CacheId(securityOrigin, oldCacheName)); |
| } |
| for (var newCacheName of newCacheNames) { |
| - if (!oldCacheNames[newCacheName]) |
| - this._cacheAdded(newCacheName); |
| + if (!oldCacheNames.has(newCacheName)) |
| + this._cacheAdded(new WebInspector.ServiceWorkerCacheModel.CacheId(securityOrigin, newCacheName)); |
| } |
| }, |
| /** |
| - * @param {string} cacheName |
| + * @param {!WebInspector.Event} event |
| + */ |
| + _securityOriginAdded: function(event) |
| + { |
| + var securityOrigin = /** @type {string} */ (event.data); |
| + this._addOrigin(securityOrigin); |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.Event} event |
| */ |
| - _cacheAdded: function(cacheName) |
| + _securityOriginRemoved: function(event) |
| + { |
| + var securityOrigin = /** @type {string} */ (event.data); |
| + this._removeOrigin(securityOrigin); |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
| + */ |
| + _cacheAdded: function(cacheId) |
| { |
| - var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName); |
| this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.EventTypes.CacheAdded, cacheId); |
| }, |
| /** |
| - * @param {string} cacheName |
| + * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
| */ |
| - _cacheRemoved: function(cacheName) |
| + _cacheRemoved: function(cacheId) |
| { |
| - var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName); |
| this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.EventTypes.CacheRemoved, cacheId); |
| }, |
| /** |
| * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId |
| - * @param {string} cacheName |
| * @param {number} skipCount |
| * @param {number} pageSize |
| * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, boolean)} callback |
| */ |
| - _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback) |
| + _requestEntries: function(cacheId, skipCount, pageSize, callback) |
| { |
| /** |
| * @param {?Protocol.Error} error |
| * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEntries |
| * @param {boolean} hasMore |
| - * @this {WebInspector.ServiceWorkerCacheModel} |
| */ |
| function innerCallback(error, dataEntries, hasMore) |
| { |
| if (error) { |
| - console.error("ServiceWorkerCacheAgent error: ", error); |
| + console.error("ServiceWorkerCacheAgent error while requesting entries: ", error); |
| return; |
| } |
| - |
| - if (!this._cacheNames) |
| - return; |
| var entries = []; |
| for (var i = 0; i < dataEntries.length; ++i) { |
| var request = WebInspector.RemoteObject.fromLocalObject(JSON.parse(dataEntries[i].request)); |
| @@ -175,8 +252,7 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| } |
| callback(entries, hasMore); |
| } |
| - |
| - this._agent.requestEntries(cacheName, skipCount, pageSize, innerCallback.bind(this)); |
| + this._agent.requestEntries(cacheId, skipCount, pageSize, innerCallback); |
| }, |
| __proto__: WebInspector.SDKModel.prototype |
| @@ -195,11 +271,13 @@ WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) |
| /** |
| * @constructor |
| - * @param {string} name |
| + * @param {string} securityOrigin |
| + * @param {string} cacheName |
| */ |
| -WebInspector.ServiceWorkerCacheModel.CacheId = function(name) |
| +WebInspector.ServiceWorkerCacheModel.CacheId = function(securityOrigin, cacheName) |
| { |
| - this.name = name; |
| + this.securityOrigin = securityOrigin; |
| + this.cacheName = cacheName; |
| } |
| WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { |
| @@ -209,7 +287,16 @@ WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { |
| */ |
| equals: function(cacheId) |
| { |
| - return this.name === cacheId.name; |
| + return this.cacheName === cacheId.cacheName && this.securityOrigin === cacheId.securityOrigin; |
| + }, |
| + |
| + /** |
| + * @override |
| + * @return {string} |
| + */ |
| + toString: function() |
| + { |
| + return this.securityOrigin + this.cacheName; |
| } |
| } |
| @@ -220,4 +307,17 @@ WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { |
| WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId) |
| { |
| this.cacheId = cacheId; |
| +} |
| + |
| +WebInspector.ServiceWorkerCacheModel._symbol = Symbol("CacheStorageModel"); |
| +/** |
| + * @param {!WebInspector.Target} target |
| + * @return {!WebInspector.ServiceWorkerCacheModel} |
| + */ |
| +WebInspector.ServiceWorkerCacheModel.fromTarget = function(target) |
| +{ |
| + if (!target[WebInspector.ServiceWorkerCacheModel._symbol]) |
| + target[WebInspector.ServiceWorkerCacheModel._symbol] = new WebInspector.ServiceWorkerCacheModel(target); |
| + |
| + return target[WebInspector.ServiceWorkerCacheModel._symbol]; |
| } |