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..37a73ca216d6fabd995c9aef0cab2cad4efe5a41 100644 |
| --- a/Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js |
| +++ b/Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js |
| @@ -11,27 +11,41 @@ WebInspector.ServiceWorkerCacheModel = function(target) |
| { |
| WebInspector.SDKModel.call(this, WebInspector.ServiceWorkerCacheModel, target); |
| - /** @type {!Set.<string>} */ |
| - this._cacheNames = new Set(); |
| + /** @type {!Map<string, !Set.<string>>} */ |
| + this._cacheNamesBySecurityOrigin = new Map(); |
| - 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) { |
| + this._loadCacheNames(securityOrigin); |
| + } |
| }, |
| /** |
| @@ -39,6 +53,7 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| */ |
| deleteCache: function(cacheId) |
| { |
| + var securityOrigin = cacheId.securityOrigin; |
| /** |
| * @this {WebInspector.ServiceWorkerCacheModel} |
| */ |
| @@ -48,9 +63,13 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| console.error("ServiceWorkerCacheAgent error: ", error); |
| return; |
| } |
| - this._cacheRemoved(cacheId.name); |
| + if (!this._cacheNamesBySecurityOrigin.has(securityOrigin)) |
| + return; |
| + var caches = this._cacheNamesBySecurityOrigin.get(securityOrigin); |
|
pfeldman
2015/04/03 09:15:57
get it right away, then bail out if none.
dmurph
2015/04/08 19:35:35
Done.
|
| + caches.delete(cacheId.name); |
| + this._cacheRemoved(securityOrigin, cacheId.name); |
| } |
| - this._agent.deleteCache(cacheId.name, callback.bind(this)); |
| + this._agent.deleteCache(securityOrigin, cacheId.name, callback.bind(this)); |
| }, |
| /** |
| @@ -70,18 +89,50 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| caches: function() |
| { |
| var result = []; |
| - for (var cacheName of this._cacheNames) { |
| - result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName)); |
| - } |
| + this._cacheNamesBySecurityOrigin.forEach(function(caches, securityOrigin) { |
|
pfeldman
2015/04/03 09:15:57
We don't use anonymous functions, please declare n
dmurph
2015/04/08 19:35:35
I feel like that is normal and readable for forEac
pfeldman
2015/04/09 10:31:12
That's JS coding guidelines for Blink, otherwise i
|
| + for (var cacheName of caches) { |
|
pfeldman
2015/04/03 09:15:57
no {} around single line blocks
dmurph
2015/04/08 19:35:35
Done.
|
| + result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(securityOrigin, cacheName)); |
| + } |
| + }, this); |
| return result; |
| }, |
| dispose: function() |
| { |
| - this._updateCacheNames([]); |
| + for (var securityOrigin of this._cacheNamesBySecurityOrigin.keys()) { |
|
pfeldman
2015/04/03 09:15:58
no {}
dmurph
2015/04/08 19:35:35
Done.
|
| + this._updateCacheNames(securityOrigin, []); |
| + } |
| + this._cacheNamesBySecurityOrigin.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); |
| + } |
| + }, |
| + |
| + _addOrigin: function(securityOrigin) { |
|
pfeldman
2015/04/03 09:15:57
{ on the next line.
dmurph
2015/04/08 19:35:35
Done.
|
| + console.assert(!this._cacheNamesBySecurityOrigin.has(securityOrigin)); |
| + this._cacheNamesBySecurityOrigin.set(securityOrigin, new Set()); |
| + this._loadCacheNames(securityOrigin); |
| + }, |
| + |
| + /** |
| + * @param {string} securityOrigin |
| + */ |
| + _removeOrigin: function(securityOrigin) |
| + { |
| + console.assert(this._cacheNamesBySecurityOrigin.has(securityOrigin)); |
| + var cacheNames = this._cacheNamesBySecurityOrigin.get(securityOrigin); |
| + if (cacheNames) { |
| + for (var cacheName of cacheNames) |
| + this._cacheRemoved(securityOrigin, cacheName); |
| + } |
| + this._cacheNamesBySecurityOrigin.delete(securityOrigin); |
| }, |
| - _loadCacheNames: function() |
| + /** |
| + * @param {string} securityOrigin |
| + */ |
| + _loadCacheNames: function(securityOrigin) |
| { |
| /** |
| * @param {?Protocol.Error} error |
| @@ -94,52 +145,78 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| console.error("ServiceWorkerCacheAgent error: ", error); |
| return; |
| } |
| - |
| - if (!this._cacheNames) |
| + if (!this._cacheNamesBySecurityOrigin.has(securityOrigin)) |
| return; |
| - this._updateCacheNames(cacheNames); |
| + this._updateCacheNames(securityOrigin, cacheNames); |
| } |
| - |
| - this._agent.requestCacheNames(callback.bind(this)); |
| + this._agent.requestCacheNames(securityOrigin, callback.bind(this)); |
| }, |
| /** |
| + * @param {string} securityOrigin |
| * @param {!Array.<string>} cacheNames |
| */ |
| - _updateCacheNames: function(cacheNames) |
| + _updateCacheNames: function(securityOrigin, cacheNames) |
| { |
| - /** @type {!Set.<string>} */ |
| + /** @type {!Set<string>} */ |
| var newCacheNames = new Set(cacheNames); |
| - /** @type {!Set.<string>} */ |
| - var oldCacheNames = this._cacheNames; |
| + /** @type {!Set<string>} */ |
| + var oldCacheNames; |
| + /** @type {!Set<string>|undefined} */ |
| + var tempOldCacheNames = this._cacheNamesBySecurityOrigin.get(securityOrigin); |
| + if (tempOldCacheNames) { |
|
pfeldman
2015/04/03 09:15:57
no {}
dmurph
2015/04/08 19:35:35
For an if-else statement? Isn't that super awful?
pfeldman
2015/04/09 10:31:12
https://www.chromium.org/blink/coding-style
|
| + oldCacheNames = tempOldCacheNames; |
| + } else { |
| + oldCacheNames = new Set(); |
| + } |
| - this._cacheNames = new Set(cacheNames); |
| + this._cacheNamesBySecurityOrigin.set(securityOrigin, newCacheNames); |
| for (var oldCacheName of oldCacheNames) { |
| - if (!newCacheNames[oldCacheName]) |
| - this._cacheRemoved(oldCacheName); |
| + if (!newCacheNames.has(oldCacheName)) |
| + this._cacheRemoved(securityOrigin, oldCacheName); |
| } |
| for (var newCacheName of newCacheNames) { |
| - if (!oldCacheNames[newCacheName]) |
| - this._cacheAdded(newCacheName); |
| + if (!oldCacheNames.has(newCacheName)) |
| + this._cacheAdded(securityOrigin, newCacheName); |
| } |
| }, |
| /** |
| + * @param {!WebInspector.Event} event |
| + */ |
| + _securityOriginAdded: function(event) |
| + { |
| + var securityOrigin = /** @type {string} */ (event.data); |
| + this._addOrigin(securityOrigin); |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.Event} event |
| + */ |
| + _securityOriginRemoved: function(event) |
| + { |
| + var securityOrigin = /** @type {string} */ (event.data); |
| + this._removeOrigin(securityOrigin); |
| + }, |
| + |
| + /** |
| + * @param {string} securityOrigin |
| * @param {string} cacheName |
| */ |
| - _cacheAdded: function(cacheName) |
| + _cacheAdded: function(securityOrigin, cacheName) |
| { |
| - var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName); |
| + var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(securityOrigin, cacheName); |
| this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.EventTypes.CacheAdded, cacheId); |
| }, |
| /** |
| + * @param {string} securityOrigin |
| * @param {string} cacheName |
| */ |
| - _cacheRemoved: function(cacheName) |
| + _cacheRemoved: function(securityOrigin, cacheName) |
| { |
| - var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName); |
| + var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(securityOrigin, cacheName); |
| this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.EventTypes.CacheRemoved, cacheId); |
| }, |
| @@ -164,8 +241,7 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| console.error("ServiceWorkerCacheAgent error: ", error); |
| return; |
| } |
| - |
| - if (!this._cacheNames) |
| + if (!this._cacheNamesBySecurityOrigin.has(cacheId.securityOrigin)) |
| return; |
| var entries = []; |
| for (var i = 0; i < dataEntries.length; ++i) { |
| @@ -175,8 +251,7 @@ WebInspector.ServiceWorkerCacheModel.prototype = { |
| } |
| callback(entries, hasMore); |
| } |
| - |
| - this._agent.requestEntries(cacheName, skipCount, pageSize, innerCallback.bind(this)); |
| + this._agent.requestEntries(cacheId.securityOrigin, cacheName, skipCount, pageSize, innerCallback.bind(this)); |
| }, |
| __proto__: WebInspector.SDKModel.prototype |
| @@ -195,10 +270,12 @@ WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) |
| /** |
| * @constructor |
| + * @param {string} securityOrigin |
| * @param {string} name |
| */ |
| -WebInspector.ServiceWorkerCacheModel.CacheId = function(name) |
| +WebInspector.ServiceWorkerCacheModel.CacheId = function(securityOrigin, name) |
| { |
| + this.securityOrigin = securityOrigin; |
| this.name = name; |
| } |
| @@ -209,7 +286,7 @@ WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { |
| */ |
| equals: function(cacheId) |
| { |
| - return this.name === cacheId.name; |
| + return this.name === cacheId.name && this.securityOrigin === cacheId.securityOrigin; |
| } |
| } |
| @@ -220,4 +297,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]; |
| } |