Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: Source/devtools/front_end/sdk/ServiceWorkerCacheModel.js

Issue 1044203004: [Storage] Cache storage inspection on all the frames! (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Comments and fix Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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();
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.
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) {
pfeldman 2015/04/16 10:22:52 drop {}
dmurph 2015/04/18 00:59:31 Done.
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 deleting cache ", c acheId.toString(), ": ", 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 var caches = new Array(this._caches.size);
73 for (var cacheName of this._cacheNames) { 90 var i = 0;
74 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName )); 91 for (var cache of this._caches.values()) {
pfeldman 2015/04/16 10:22:52 drop {}
dmurph 2015/04/18 00:59:31 Done.
92 caches[i++] = cache;
pfeldman 2015/04/16 10:22:53 We typically just .push for simplicity.
dmurph 2015/04/18 00:59:31 Done.
75 } 93 }
76 return result; 94 return caches;
77 }, 95 },
78 96
79 dispose: function() 97 dispose: function()
80 { 98 {
81 this._updateCacheNames([]); 99 for (var cacheId of this._caches.values())
100 this._cacheRemoved(cacheId);
101 this._caches.clear();
102 if (this._enabled) {
103 this.target().resourceTreeModel.removeEventListener(WebInspector.Res ourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this);
104 this.target().resourceTreeModel.removeEventListener(WebInspector.Res ourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, th is);
105 }
82 }, 106 },
83 107
84 _loadCacheNames: function() 108 _addOrigin: function(securityOrigin)
85 { 109 {
86 /** 110 this._loadCacheNames(securityOrigin);
87 * @param {?Protocol.Error} error
88 * @param {!Array.<string>} cacheNames
89 * @this {WebInspector.ServiceWorkerCacheModel}
90 */
91 function callback(error, cacheNames)
92 {
93 if (error) {
94 console.error("ServiceWorkerCacheAgent error: ", error);
95 return;
96 }
97
98 if (!this._cacheNames)
99 return;
100 this._updateCacheNames(cacheNames);
101 }
102
103 this._agent.requestCacheNames(callback.bind(this));
104 }, 111 },
105 112
106 /** 113 /**
107 * @param {!Array.<string>} cacheNames 114 * @param {string} securityOrigin
108 */ 115 */
109 _updateCacheNames: function(cacheNames) 116 _removeOrigin: function(securityOrigin)
110 { 117 {
111 /** @type {!Set.<string>} */ 118 for (var serializedId of this._caches.keys()) {
112 var newCacheNames = new Set(cacheNames); 119 var cacheId = this._caches.get(serializedId);
113 /** @type {!Set.<string>} */ 120 if (cacheId.securityOrigin == securityOrigin) {
114 var oldCacheNames = this._cacheNames; 121 this._caches.delete(serializedId);
115 122 this._cacheRemoved(cacheId);
116 this._cacheNames = new Set(cacheNames); 123 }
117
118 for (var oldCacheName of oldCacheNames) {
119 if (!newCacheNames[oldCacheName])
120 this._cacheRemoved(oldCacheName);
121 }
122 for (var newCacheName of newCacheNames) {
123 if (!oldCacheNames[newCacheName])
124 this._cacheAdded(newCacheName);
125 } 124 }
126 }, 125 },
127 126
128 /** 127 /**
129 * @param {string} cacheName 128 * @param {string} securityOrigin
130 */ 129 */
131 _cacheAdded: function(cacheName) 130 _loadCacheNames: function(securityOrigin)
132 { 131 {
133 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName ); 132 /**
133 * @param {?Protocol.Error} error
134 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>} cache Ids
135 * @this {WebInspector.ServiceWorkerCacheModel}
136 */
137 function callback(error, cacheIds)
138 {
139 if (error) {
140 console.error("ServiceWorkerCacheAgent error while loading cache s: ", error);
141 return;
142 }
143 this._updateCacheNames(securityOrigin, cacheIds);
144 }
145 this._agent.requestCacheNames(securityOrigin, callback.bind(this));
146 },
147
148 /**
149 * @param {string} securityOrigin
150 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>} cacheIds
151 */
152 _updateCacheNames: function(securityOrigin, cacheIds)
153 {
154 /**
155 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
156 * @return {string}
157 */
158 function cacheIdToName(cacheId)
159 {
160 return cacheId.cacheName;
161 }
162 /**
163 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
164 * @param {string} serializedId
165 * @this {WebInspector.ServiceWorkerCacheModel}
166 */
167 function deleteAndSaveForSecurityOrigin(cacheId, serializedId)
168 {
169 if (cacheId.securityOrigin == securityOrigin) {
170 oldCacheNames.add(cacheId.cacheName);
171 this._caches.delete(serializedId);
172 }
173 }
174
175 /** @type {!Set<string>} */
176 var newCacheNames = new Set(cacheIds.map(cacheIdToName));
177 /** @type {!Set<string>} */
178 var oldCacheNames = new Set();
179 this._caches.forEach(deleteAndSaveForSecurityOrigin, this);
180
181 for (var cacheId of cacheIds)
182 this._caches.set(JSON.stringify(cacheId), cacheId);
183
184 for (var oldCacheName of oldCacheNames) {
185 if (!newCacheNames.has(oldCacheName))
186 this._cacheRemoved(new WebInspector.ServiceWorkerCacheModel.Cach eId(securityOrigin, oldCacheName));
187 }
188 for (var newCacheName of newCacheNames) {
189 if (!oldCacheNames.has(newCacheName))
190 this._cacheAdded(new WebInspector.ServiceWorkerCacheModel.CacheI d(securityOrigin, newCacheName));
191 }
192 },
193
194 /**
195 * @param {!WebInspector.Event} event
196 */
197 _securityOriginAdded: function(event)
198 {
199 var securityOrigin = /** @type {string} */ (event.data);
200 this._addOrigin(securityOrigin);
201 },
202
203 /**
204 * @param {!WebInspector.Event} event
205 */
206 _securityOriginRemoved: function(event)
207 {
208 var securityOrigin = /** @type {string} */ (event.data);
209 this._removeOrigin(securityOrigin);
210 },
211
212 /**
213 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
214 */
215 _cacheAdded: function(cacheId)
216 {
134 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheAdded, cacheId); 217 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheAdded, cacheId);
135 }, 218 },
136 219
137 /** 220 /**
138 * @param {string} cacheName 221 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
139 */ 222 */
140 _cacheRemoved: function(cacheName) 223 _cacheRemoved: function(cacheId)
141 { 224 {
142 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName );
143 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheRemoved, cacheId); 225 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event Types.CacheRemoved, cacheId);
144 }, 226 },
145 227
146 /** 228 /**
147 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId 229 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
148 * @param {string} cacheName
149 * @param {number} skipCount 230 * @param {number} skipCount
150 * @param {number} pageSize 231 * @param {number} pageSize
151 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback 232 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo olean)} callback
152 */ 233 */
153 _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback) 234 _requestEntries: function(cacheId, skipCount, pageSize, callback)
154 { 235 {
155 /** 236 /**
156 * @param {?Protocol.Error} error 237 * @param {?Protocol.Error} error
157 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt ries 238 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt ries
158 * @param {boolean} hasMore 239 * @param {boolean} hasMore
159 * @this {WebInspector.ServiceWorkerCacheModel}
160 */ 240 */
161 function innerCallback(error, dataEntries, hasMore) 241 function innerCallback(error, dataEntries, hasMore)
162 { 242 {
163 if (error) { 243 if (error) {
164 console.error("ServiceWorkerCacheAgent error: ", error); 244 console.error("ServiceWorkerCacheAgent error while requesting en tries: ", error);
165 return; 245 return;
166 } 246 }
167
168 if (!this._cacheNames)
169 return;
170 var entries = []; 247 var entries = [];
171 for (var i = 0; i < dataEntries.length; ++i) { 248 for (var i = 0; i < dataEntries.length; ++i) {
172 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par se(dataEntries[i].request)); 249 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par se(dataEntries[i].request));
173 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa rse(dataEntries[i].response)); 250 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa rse(dataEntries[i].response));
174 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ est, response)); 251 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ est, response));
175 } 252 }
176 callback(entries, hasMore); 253 callback(entries, hasMore);
177 } 254 }
178 255 this._agent.requestEntries(cacheId, skipCount, pageSize, innerCallback);
179 this._agent.requestEntries(cacheName, skipCount, pageSize, innerCallback .bind(this));
180 }, 256 },
181 257
182 __proto__: WebInspector.SDKModel.prototype 258 __proto__: WebInspector.SDKModel.prototype
183 } 259 }
184 260
185 /** 261 /**
186 * @constructor 262 * @constructor
187 * @param {!WebInspector.RemoteObject} request 263 * @param {!WebInspector.RemoteObject} request
188 * @param {!WebInspector.RemoteObject} response 264 * @param {!WebInspector.RemoteObject} response
189 */ 265 */
190 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) 266 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response)
191 { 267 {
192 this.request = request; 268 this.request = request;
193 this.response = response; 269 this.response = response;
194 } 270 }
195 271
196 /** 272 /**
197 * @constructor 273 * @constructor
198 * @param {string} name 274 * @param {string} securityOrigin
275 * @param {string} cacheName
199 */ 276 */
200 WebInspector.ServiceWorkerCacheModel.CacheId = function(name) 277 WebInspector.ServiceWorkerCacheModel.CacheId = function(securityOrigin, cacheNam e)
201 { 278 {
202 this.name = name; 279 this.securityOrigin = securityOrigin;
280 this.cacheName = cacheName;
203 } 281 }
204 282
205 WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { 283 WebInspector.ServiceWorkerCacheModel.CacheId.prototype = {
206 /** 284 /**
207 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId 285 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
208 * @return {boolean} 286 * @return {boolean}
209 */ 287 */
210 equals: function(cacheId) 288 equals: function(cacheId)
211 { 289 {
212 return this.name === cacheId.name; 290 return this.cacheName === cacheId.cacheName && this.securityOrigin === c acheId.securityOrigin;
291 },
292
293 /**
294 * @override
295 * @return {string}
296 */
297 toString: function()
298 {
299 return this.securityOrigin + this.cacheName;
213 } 300 }
214 } 301 }
215 302
216 /** 303 /**
217 * @constructor 304 * @constructor
218 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId 305 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
219 */ 306 */
220 WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId) 307 WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId)
221 { 308 {
222 this.cacheId = cacheId; 309 this.cacheId = cacheId;
310 }
311
312 WebInspector.ServiceWorkerCacheModel._symbol = Symbol("CacheStorageModel");
313 /**
314 * @param {!WebInspector.Target} target
315 * @return {!WebInspector.ServiceWorkerCacheModel}
316 */
317 WebInspector.ServiceWorkerCacheModel.fromTarget = function(target)
318 {
319 if (!target[WebInspector.ServiceWorkerCacheModel._symbol])
320 target[WebInspector.ServiceWorkerCacheModel._symbol] = new WebInspector. ServiceWorkerCacheModel(target);
321
322 return target[WebInspector.ServiceWorkerCacheModel._symbol];
223 } 323 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698