ServiceWorkerCacheModel.js 6.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export default class ServiceWorkerCacheModel extends SDK.SDKModel{constructor(target){super(target);target.registerStorageDispatcher(this);this._caches=new Map();this._cacheAgent=target.cacheStorageAgent();this._storageAgent=target.storageAgent();this._securityOriginManager=target.model(SDK.SecurityOriginManager);this._originsUpdated=new Set();this._throttler=new Common.Throttler(2000);this._enabled=false;}
  2. enable(){if(this._enabled){return;}
  3. this._securityOriginManager.addEventListener(SDK.SecurityOriginManager.Events.SecurityOriginAdded,this._securityOriginAdded,this);this._securityOriginManager.addEventListener(SDK.SecurityOriginManager.Events.SecurityOriginRemoved,this._securityOriginRemoved,this);for(const securityOrigin of this._securityOriginManager.securityOrigins()){this._addOrigin(securityOrigin);}
  4. this._enabled=true;}
  5. clearForOrigin(origin){this._removeOrigin(origin);this._addOrigin(origin);}
  6. refreshCacheNames(){for(const cache of this._caches.values()){this._cacheRemoved(cache);}
  7. this._caches.clear();const securityOrigins=this._securityOriginManager.securityOrigins();for(const securityOrigin of securityOrigins){this._loadCacheNames(securityOrigin);}}
  8. async deleteCache(cache){const response=await this._cacheAgent.invoke_deleteCache({cacheId:cache.cacheId});if(response[Protocol.Error]){console.error(`ServiceWorkerCacheAgent error deleting cache ${cache.toString()}: ${response[Protocol.Error]}`);return;}
  9. this._caches.delete(cache.cacheId);this._cacheRemoved(cache);}
  10. async deleteCacheEntry(cache,request){const response=await this._cacheAgent.invoke_deleteEntry({cacheId:cache.cacheId,request});if(!response[Protocol.Error]){return;}
  11. Common.console.error(Common.UIString('ServiceWorkerCacheAgent error deleting cache entry %s in cache: %s',cache.toString(),response[Protocol.Error]));}
  12. loadCacheData(cache,skipCount,pageSize,pathFilter,callback){this._requestEntries(cache,skipCount,pageSize,pathFilter,callback);}
  13. loadAllCacheData(cache,pathFilter,callback){this._requestAllEntries(cache,pathFilter,callback);}
  14. caches(){const caches=new Array();for(const cache of this._caches.values()){caches.push(cache);}
  15. return caches;}
  16. dispose(){for(const cache of this._caches.values()){this._cacheRemoved(cache);}
  17. this._caches.clear();if(this._enabled){this._securityOriginManager.removeEventListener(SDK.SecurityOriginManager.Events.SecurityOriginAdded,this._securityOriginAdded,this);this._securityOriginManager.removeEventListener(SDK.SecurityOriginManager.Events.SecurityOriginRemoved,this._securityOriginRemoved,this);}}
  18. _addOrigin(securityOrigin){this._loadCacheNames(securityOrigin);if(this._isValidSecurityOrigin(securityOrigin)){this._storageAgent.trackCacheStorageForOrigin(securityOrigin);}}
  19. _removeOrigin(securityOrigin){for(const opaqueId of this._caches.keys()){const cache=this._caches.get(opaqueId);if(cache.securityOrigin===securityOrigin){this._caches.delete(opaqueId);this._cacheRemoved(cache);}}
  20. if(this._isValidSecurityOrigin(securityOrigin)){this._storageAgent.untrackCacheStorageForOrigin(securityOrigin);}}
  21. _isValidSecurityOrigin(securityOrigin){const parsedURL=securityOrigin.asParsedURL();return!!parsedURL&&parsedURL.scheme.startsWith('http');}
  22. async _loadCacheNames(securityOrigin){const caches=await this._cacheAgent.requestCacheNames(securityOrigin);if(!caches){return;}
  23. this._updateCacheNames(securityOrigin,caches);}
  24. _updateCacheNames(securityOrigin,cachesJson){function deleteAndSaveOldCaches(cache){if(cache.securityOrigin===securityOrigin&&!updatingCachesIds.has(cache.cacheId)){oldCaches.set(cache.cacheId,cache);this._caches.delete(cache.cacheId);}}
  25. const updatingCachesIds=new Set();const newCaches=new Map();const oldCaches=new Map();for(const cacheJson of cachesJson){const cache=new Cache(this,cacheJson.securityOrigin,cacheJson.cacheName,cacheJson.cacheId);updatingCachesIds.add(cache.cacheId);if(this._caches.has(cache.cacheId)){continue;}
  26. newCaches.set(cache.cacheId,cache);this._caches.set(cache.cacheId,cache);}
  27. this._caches.forEach(deleteAndSaveOldCaches,this);newCaches.forEach(this._cacheAdded,this);oldCaches.forEach(this._cacheRemoved,this);}
  28. _securityOriginAdded(event){const securityOrigin=(event.data);this._addOrigin(securityOrigin);}
  29. _securityOriginRemoved(event){const securityOrigin=(event.data);this._removeOrigin(securityOrigin);}
  30. _cacheAdded(cache){this.dispatchEventToListeners(Events.CacheAdded,{model:this,cache:cache});}
  31. _cacheRemoved(cache){this.dispatchEventToListeners(Events.CacheRemoved,{model:this,cache:cache});}
  32. async _requestEntries(cache,skipCount,pageSize,pathFilter,callback){const response=await this._cacheAgent.invoke_requestEntries({cacheId:cache.cacheId,skipCount,pageSize,pathFilter});if(response[Protocol.Error]){console.error('ServiceWorkerCacheAgent error while requesting entries: ',response[Protocol.Error]);return;}
  33. callback(response.cacheDataEntries,response.returnCount);}
  34. async _requestAllEntries(cache,pathFilter,callback){const response=await this._cacheAgent.invoke_requestEntries({cacheId:cache.cacheId,pathFilter});if(response[Protocol.Error]){console.error('ServiceWorkerCacheAgent error while requesting entries: ',response[Protocol.Error]);return;}
  35. callback(response.cacheDataEntries,response.returnCount);}
  36. cacheStorageListUpdated(origin){this._originsUpdated.add(origin);this._throttler.schedule(()=>{const promises=Array.from(this._originsUpdated,origin=>this._loadCacheNames(origin));this._originsUpdated.clear();return Promise.all(promises);});}
  37. cacheStorageContentUpdated(origin,cacheName){this.dispatchEventToListeners(Events.CacheStorageContentUpdated,{origin:origin,cacheName:cacheName});}
  38. indexedDBListUpdated(origin){}
  39. indexedDBContentUpdated(origin,databaseName,objectStoreName){}}
  40. export const Events={CacheAdded:Symbol('CacheAdded'),CacheRemoved:Symbol('CacheRemoved'),CacheStorageContentUpdated:Symbol('CacheStorageContentUpdated')};export class Cache{constructor(model,securityOrigin,cacheName,cacheId){this._model=model;this.securityOrigin=securityOrigin;this.cacheName=cacheName;this.cacheId=cacheId;}
  41. equals(cache){return this.cacheId===cache.cacheId;}
  42. toString(){return this.securityOrigin+this.cacheName;}
  43. requestCachedResponse(url,requestHeaders){return this._model._cacheAgent.requestCachedResponse(this.cacheId,url,requestHeaders);}}
  44. self.SDK=self.SDK||{};SDK=SDK||{};SDK.ServiceWorkerCacheModel=ServiceWorkerCacheModel;SDK.ServiceWorkerCacheModel.Events=Events;SDK.ServiceWorkerCacheModel.Cache=Cache;SDK.SDKModel.register(SDK.ServiceWorkerCacheModel,SDK.Target.Capability.Storage,false);