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

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

Issue 2493373002: DevTools: rename WebInspector into modules. (Closed)
Patch Set: for bots Created 4 years, 1 month 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 * @unrestricted 5 * @unrestricted
6 */ 6 */
7 WebInspector.ServiceWorkerCacheModel = class extends WebInspector.SDKModel { 7 SDK.ServiceWorkerCacheModel = class extends SDK.SDKModel {
8 /** 8 /**
9 * Invariant: This model can only be constructed on a ServiceWorker target. 9 * Invariant: This model can only be constructed on a ServiceWorker target.
10 * @param {!WebInspector.Target} target 10 * @param {!SDK.Target} target
11 * @param {!WebInspector.SecurityOriginManager} securityOriginManager 11 * @param {!SDK.SecurityOriginManager} securityOriginManager
12 */ 12 */
13 constructor(target, securityOriginManager) { 13 constructor(target, securityOriginManager) {
14 super(WebInspector.ServiceWorkerCacheModel, target); 14 super(SDK.ServiceWorkerCacheModel, target);
15 15
16 /** @type {!Map<string, !WebInspector.ServiceWorkerCacheModel.Cache>} */ 16 /** @type {!Map<string, !SDK.ServiceWorkerCacheModel.Cache>} */
17 this._caches = new Map(); 17 this._caches = new Map();
18 18
19 this._agent = target.cacheStorageAgent(); 19 this._agent = target.cacheStorageAgent();
20 20
21 this._securityOriginManager = securityOriginManager; 21 this._securityOriginManager = securityOriginManager;
22 22
23 /** @type {boolean} */ 23 /** @type {boolean} */
24 this._enabled = false; 24 this._enabled = false;
25 } 25 }
26 26
27 /** 27 /**
28 * @param {!WebInspector.Target} target 28 * @param {!SDK.Target} target
29 * @return {?WebInspector.ServiceWorkerCacheModel} 29 * @return {?SDK.ServiceWorkerCacheModel}
30 */ 30 */
31 static fromTarget(target) { 31 static fromTarget(target) {
32 if (!target.hasBrowserCapability()) 32 if (!target.hasBrowserCapability())
33 return null; 33 return null;
34 var instance = 34 var instance =
35 /** @type {?WebInspector.ServiceWorkerCacheModel} */ (target.model(WebIn spector.ServiceWorkerCacheModel)); 35 /** @type {?SDK.ServiceWorkerCacheModel} */ (target.model(SDK.ServiceWor kerCacheModel));
36 if (!instance) 36 if (!instance)
37 instance = 37 instance =
38 new WebInspector.ServiceWorkerCacheModel(target, WebInspector.Security OriginManager.fromTarget(target)); 38 new SDK.ServiceWorkerCacheModel(target, SDK.SecurityOriginManager.from Target(target));
39 return instance; 39 return instance;
40 } 40 }
41 41
42 enable() { 42 enable() {
43 if (this._enabled) 43 if (this._enabled)
44 return; 44 return;
45 45
46 this._securityOriginManager.addEventListener( 46 this._securityOriginManager.addEventListener(
47 WebInspector.SecurityOriginManager.Events.SecurityOriginAdded, this._sec urityOriginAdded, this); 47 SDK.SecurityOriginManager.Events.SecurityOriginAdded, this._securityOrig inAdded, this);
48 this._securityOriginManager.addEventListener( 48 this._securityOriginManager.addEventListener(
49 WebInspector.SecurityOriginManager.Events.SecurityOriginRemoved, this._s ecurityOriginRemoved, this); 49 SDK.SecurityOriginManager.Events.SecurityOriginRemoved, this._securityOr iginRemoved, this);
50 50
51 for (var securityOrigin of this._securityOriginManager.securityOrigins()) 51 for (var securityOrigin of this._securityOriginManager.securityOrigins())
52 this._addOrigin(securityOrigin); 52 this._addOrigin(securityOrigin);
53 this._enabled = true; 53 this._enabled = true;
54 } 54 }
55 55
56 /** 56 /**
57 * @param {string} origin 57 * @param {string} origin
58 */ 58 */
59 clearForOrigin(origin) { 59 clearForOrigin(origin) {
60 this._removeOrigin(origin); 60 this._removeOrigin(origin);
61 this._addOrigin(origin); 61 this._addOrigin(origin);
62 } 62 }
63 63
64 refreshCacheNames() { 64 refreshCacheNames() {
65 for (var cache of this._caches.values()) 65 for (var cache of this._caches.values())
66 this._cacheRemoved(cache); 66 this._cacheRemoved(cache);
67 this._caches.clear(); 67 this._caches.clear();
68 var securityOrigins = this._securityOriginManager.securityOrigins(); 68 var securityOrigins = this._securityOriginManager.securityOrigins();
69 for (var securityOrigin of securityOrigins) 69 for (var securityOrigin of securityOrigins)
70 this._loadCacheNames(securityOrigin); 70 this._loadCacheNames(securityOrigin);
71 } 71 }
72 72
73 /** 73 /**
74 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 74 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
75 */ 75 */
76 deleteCache(cache) { 76 deleteCache(cache) {
77 /** 77 /**
78 * @this {WebInspector.ServiceWorkerCacheModel} 78 * @this {SDK.ServiceWorkerCacheModel}
79 */ 79 */
80 function callback(error) { 80 function callback(error) {
81 if (error) { 81 if (error) {
82 console.error('ServiceWorkerCacheAgent error deleting cache ', cache.toS tring(), ': ', error); 82 console.error('ServiceWorkerCacheAgent error deleting cache ', cache.toS tring(), ': ', error);
83 return; 83 return;
84 } 84 }
85 this._caches.delete(cache.cacheId); 85 this._caches.delete(cache.cacheId);
86 this._cacheRemoved(cache); 86 this._cacheRemoved(cache);
87 } 87 }
88 this._agent.deleteCache(cache.cacheId, callback.bind(this)); 88 this._agent.deleteCache(cache.cacheId, callback.bind(this));
89 } 89 }
90 90
91 /** 91 /**
92 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 92 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
93 * @param {string} request 93 * @param {string} request
94 * @param {function()} callback 94 * @param {function()} callback
95 */ 95 */
96 deleteCacheEntry(cache, request, callback) { 96 deleteCacheEntry(cache, request, callback) {
97 /** 97 /**
98 * @param {?Protocol.Error} error 98 * @param {?Protocol.Error} error
99 */ 99 */
100 function myCallback(error) { 100 function myCallback(error) {
101 if (error) { 101 if (error) {
102 WebInspector.console.error(WebInspector.UIString( 102 Common.console.error(Common.UIString(
103 'ServiceWorkerCacheAgent error deleting cache entry %s in cache: %s' , cache.toString(), error)); 103 'ServiceWorkerCacheAgent error deleting cache entry %s in cache: %s' , cache.toString(), error));
104 return; 104 return;
105 } 105 }
106 callback(); 106 callback();
107 } 107 }
108 this._agent.deleteEntry(cache.cacheId, request, myCallback); 108 this._agent.deleteEntry(cache.cacheId, request, myCallback);
109 } 109 }
110 110
111 /** 111 /**
112 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 112 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
113 * @param {number} skipCount 113 * @param {number} skipCount
114 * @param {number} pageSize 114 * @param {number} pageSize
115 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bool ean)} callback 115 * @param {function(!Array.<!SDK.ServiceWorkerCacheModel.Entry>, boolean)} cal lback
116 */ 116 */
117 loadCacheData(cache, skipCount, pageSize, callback) { 117 loadCacheData(cache, skipCount, pageSize, callback) {
118 this._requestEntries(cache, skipCount, pageSize, callback); 118 this._requestEntries(cache, skipCount, pageSize, callback);
119 } 119 }
120 120
121 /** 121 /**
122 * @return {!Array.<!WebInspector.ServiceWorkerCacheModel.Cache>} 122 * @return {!Array.<!SDK.ServiceWorkerCacheModel.Cache>}
123 */ 123 */
124 caches() { 124 caches() {
125 var caches = new Array(); 125 var caches = new Array();
126 for (var cache of this._caches.values()) 126 for (var cache of this._caches.values())
127 caches.push(cache); 127 caches.push(cache);
128 return caches; 128 return caches;
129 } 129 }
130 130
131 /** 131 /**
132 * @override 132 * @override
133 */ 133 */
134 dispose() { 134 dispose() {
135 for (var cache of this._caches.values()) 135 for (var cache of this._caches.values())
136 this._cacheRemoved(cache); 136 this._cacheRemoved(cache);
137 this._caches.clear(); 137 this._caches.clear();
138 if (this._enabled) { 138 if (this._enabled) {
139 this._securityOriginManager.removeEventListener( 139 this._securityOriginManager.removeEventListener(
140 WebInspector.SecurityOriginManager.Events.SecurityOriginAdded, this._s ecurityOriginAdded, this); 140 SDK.SecurityOriginManager.Events.SecurityOriginAdded, this._securityOr iginAdded, this);
141 this._securityOriginManager.removeEventListener( 141 this._securityOriginManager.removeEventListener(
142 WebInspector.SecurityOriginManager.Events.SecurityOriginRemoved, this. _securityOriginRemoved, this); 142 SDK.SecurityOriginManager.Events.SecurityOriginRemoved, this._security OriginRemoved, this);
143 } 143 }
144 } 144 }
145 145
146 _addOrigin(securityOrigin) { 146 _addOrigin(securityOrigin) {
147 this._loadCacheNames(securityOrigin); 147 this._loadCacheNames(securityOrigin);
148 } 148 }
149 149
150 /** 150 /**
151 * @param {string} securityOrigin 151 * @param {string} securityOrigin
152 */ 152 */
153 _removeOrigin(securityOrigin) { 153 _removeOrigin(securityOrigin) {
154 for (var opaqueId of this._caches.keys()) { 154 for (var opaqueId of this._caches.keys()) {
155 var cache = this._caches.get(opaqueId); 155 var cache = this._caches.get(opaqueId);
156 if (cache.securityOrigin === securityOrigin) { 156 if (cache.securityOrigin === securityOrigin) {
157 this._caches.delete(opaqueId); 157 this._caches.delete(opaqueId);
158 this._cacheRemoved(cache); 158 this._cacheRemoved(cache);
159 } 159 }
160 } 160 }
161 } 161 }
162 162
163 /** 163 /**
164 * @param {string} securityOrigin 164 * @param {string} securityOrigin
165 */ 165 */
166 _loadCacheNames(securityOrigin) { 166 _loadCacheNames(securityOrigin) {
167 /** 167 /**
168 * @param {?Protocol.Error} error 168 * @param {?Protocol.Error} error
169 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Cache>} caches 169 * @param {!Array.<!SDK.ServiceWorkerCacheModel.Cache>} caches
170 * @this {WebInspector.ServiceWorkerCacheModel} 170 * @this {SDK.ServiceWorkerCacheModel}
171 */ 171 */
172 function callback(error, caches) { 172 function callback(error, caches) {
173 if (error) { 173 if (error) {
174 console.error('ServiceWorkerCacheAgent error while loading caches: ', er ror); 174 console.error('ServiceWorkerCacheAgent error while loading caches: ', er ror);
175 return; 175 return;
176 } 176 }
177 this._updateCacheNames(securityOrigin, caches); 177 this._updateCacheNames(securityOrigin, caches);
178 } 178 }
179 this._agent.requestCacheNames(securityOrigin, callback.bind(this)); 179 this._agent.requestCacheNames(securityOrigin, callback.bind(this));
180 } 180 }
181 181
182 /** 182 /**
183 * @param {string} securityOrigin 183 * @param {string} securityOrigin
184 * @param {!Array} cachesJson 184 * @param {!Array} cachesJson
185 */ 185 */
186 _updateCacheNames(securityOrigin, cachesJson) { 186 _updateCacheNames(securityOrigin, cachesJson) {
187 /** 187 /**
188 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 188 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
189 * @this {WebInspector.ServiceWorkerCacheModel} 189 * @this {SDK.ServiceWorkerCacheModel}
190 */ 190 */
191 function deleteAndSaveOldCaches(cache) { 191 function deleteAndSaveOldCaches(cache) {
192 if (cache.securityOrigin === securityOrigin && !updatingCachesIds.has(cach e.cacheId)) { 192 if (cache.securityOrigin === securityOrigin && !updatingCachesIds.has(cach e.cacheId)) {
193 oldCaches.set(cache.cacheId, cache); 193 oldCaches.set(cache.cacheId, cache);
194 this._caches.delete(cache.cacheId); 194 this._caches.delete(cache.cacheId);
195 } 195 }
196 } 196 }
197 197
198 /** @type {!Set<string>} */ 198 /** @type {!Set<string>} */
199 var updatingCachesIds = new Set(); 199 var updatingCachesIds = new Set();
200 /** @type {!Map<string, !WebInspector.ServiceWorkerCacheModel.Cache>} */ 200 /** @type {!Map<string, !SDK.ServiceWorkerCacheModel.Cache>} */
201 var newCaches = new Map(); 201 var newCaches = new Map();
202 /** @type {!Map<string, !WebInspector.ServiceWorkerCacheModel.Cache>} */ 202 /** @type {!Map<string, !SDK.ServiceWorkerCacheModel.Cache>} */
203 var oldCaches = new Map(); 203 var oldCaches = new Map();
204 204
205 for (var cacheJson of cachesJson) { 205 for (var cacheJson of cachesJson) {
206 var cache = new WebInspector.ServiceWorkerCacheModel.Cache( 206 var cache = new SDK.ServiceWorkerCacheModel.Cache(
207 cacheJson.securityOrigin, cacheJson.cacheName, cacheJson.cacheId); 207 cacheJson.securityOrigin, cacheJson.cacheName, cacheJson.cacheId);
208 updatingCachesIds.add(cache.cacheId); 208 updatingCachesIds.add(cache.cacheId);
209 if (this._caches.has(cache.cacheId)) 209 if (this._caches.has(cache.cacheId))
210 continue; 210 continue;
211 newCaches.set(cache.cacheId, cache); 211 newCaches.set(cache.cacheId, cache);
212 this._caches.set(cache.cacheId, cache); 212 this._caches.set(cache.cacheId, cache);
213 } 213 }
214 this._caches.forEach(deleteAndSaveOldCaches, this); 214 this._caches.forEach(deleteAndSaveOldCaches, this);
215 newCaches.forEach(this._cacheAdded, this); 215 newCaches.forEach(this._cacheAdded, this);
216 oldCaches.forEach(this._cacheRemoved, this); 216 oldCaches.forEach(this._cacheRemoved, this);
217 } 217 }
218 218
219 /** 219 /**
220 * @param {!WebInspector.Event} event 220 * @param {!Common.Event} event
221 */ 221 */
222 _securityOriginAdded(event) { 222 _securityOriginAdded(event) {
223 var securityOrigin = /** @type {string} */ (event.data); 223 var securityOrigin = /** @type {string} */ (event.data);
224 this._addOrigin(securityOrigin); 224 this._addOrigin(securityOrigin);
225 } 225 }
226 226
227 /** 227 /**
228 * @param {!WebInspector.Event} event 228 * @param {!Common.Event} event
229 */ 229 */
230 _securityOriginRemoved(event) { 230 _securityOriginRemoved(event) {
231 var securityOrigin = /** @type {string} */ (event.data); 231 var securityOrigin = /** @type {string} */ (event.data);
232 this._removeOrigin(securityOrigin); 232 this._removeOrigin(securityOrigin);
233 } 233 }
234 234
235 /** 235 /**
236 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 236 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
237 */ 237 */
238 _cacheAdded(cache) { 238 _cacheAdded(cache) {
239 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Events.Ca cheAdded, cache); 239 this.dispatchEventToListeners(SDK.ServiceWorkerCacheModel.Events.CacheAdded, cache);
240 } 240 }
241 241
242 /** 242 /**
243 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 243 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
244 */ 244 */
245 _cacheRemoved(cache) { 245 _cacheRemoved(cache) {
246 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Events.Ca cheRemoved, cache); 246 this.dispatchEventToListeners(SDK.ServiceWorkerCacheModel.Events.CacheRemove d, cache);
247 } 247 }
248 248
249 /** 249 /**
250 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 250 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
251 * @param {number} skipCount 251 * @param {number} skipCount
252 * @param {number} pageSize 252 * @param {number} pageSize
253 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bool ean)} callback 253 * @param {function(!Array.<!SDK.ServiceWorkerCacheModel.Entry>, boolean)} cal lback
254 */ 254 */
255 _requestEntries(cache, skipCount, pageSize, callback) { 255 _requestEntries(cache, skipCount, pageSize, callback) {
256 /** 256 /**
257 * @param {?Protocol.Error} error 257 * @param {?Protocol.Error} error
258 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEntries 258 * @param {!Array.<!SDK.ServiceWorkerCacheModel.Entry>} dataEntries
259 * @param {boolean} hasMore 259 * @param {boolean} hasMore
260 */ 260 */
261 function innerCallback(error, dataEntries, hasMore) { 261 function innerCallback(error, dataEntries, hasMore) {
262 if (error) { 262 if (error) {
263 console.error('ServiceWorkerCacheAgent error while requesting entries: ' , error); 263 console.error('ServiceWorkerCacheAgent error while requesting entries: ' , error);
264 return; 264 return;
265 } 265 }
266 var entries = []; 266 var entries = [];
267 for (var i = 0; i < dataEntries.length; ++i) { 267 for (var i = 0; i < dataEntries.length; ++i) {
268 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(dataEntries[ i].request, dataEntries[i].response)); 268 entries.push(new SDK.ServiceWorkerCacheModel.Entry(dataEntries[i].reques t, dataEntries[i].response));
269 } 269 }
270 callback(entries, hasMore); 270 callback(entries, hasMore);
271 } 271 }
272 this._agent.requestEntries(cache.cacheId, skipCount, pageSize, innerCallback ); 272 this._agent.requestEntries(cache.cacheId, skipCount, pageSize, innerCallback );
273 } 273 }
274 }; 274 };
275 275
276 /** @enum {symbol} */ 276 /** @enum {symbol} */
277 WebInspector.ServiceWorkerCacheModel.Events = { 277 SDK.ServiceWorkerCacheModel.Events = {
278 CacheAdded: Symbol('CacheAdded'), 278 CacheAdded: Symbol('CacheAdded'),
279 CacheRemoved: Symbol('CacheRemoved') 279 CacheRemoved: Symbol('CacheRemoved')
280 }; 280 };
281 281
282 /** 282 /**
283 * @unrestricted 283 * @unrestricted
284 */ 284 */
285 WebInspector.ServiceWorkerCacheModel.Entry = class { 285 SDK.ServiceWorkerCacheModel.Entry = class {
286 /** 286 /**
287 * @param {string} request 287 * @param {string} request
288 * @param {string} response 288 * @param {string} response
289 */ 289 */
290 constructor(request, response) { 290 constructor(request, response) {
291 this.request = request; 291 this.request = request;
292 this.response = response; 292 this.response = response;
293 } 293 }
294 }; 294 };
295 295
296 /** 296 /**
297 * @unrestricted 297 * @unrestricted
298 */ 298 */
299 WebInspector.ServiceWorkerCacheModel.Cache = class { 299 SDK.ServiceWorkerCacheModel.Cache = class {
300 /** 300 /**
301 * @param {string} securityOrigin 301 * @param {string} securityOrigin
302 * @param {string} cacheName 302 * @param {string} cacheName
303 * @param {string} cacheId 303 * @param {string} cacheId
304 */ 304 */
305 constructor(securityOrigin, cacheName, cacheId) { 305 constructor(securityOrigin, cacheName, cacheId) {
306 this.securityOrigin = securityOrigin; 306 this.securityOrigin = securityOrigin;
307 this.cacheName = cacheName; 307 this.cacheName = cacheName;
308 this.cacheId = cacheId; 308 this.cacheId = cacheId;
309 } 309 }
310 310
311 /** 311 /**
312 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache 312 * @param {!SDK.ServiceWorkerCacheModel.Cache} cache
313 * @return {boolean} 313 * @return {boolean}
314 */ 314 */
315 equals(cache) { 315 equals(cache) {
316 return this.cacheId === cache.cacheId; 316 return this.cacheId === cache.cacheId;
317 } 317 }
318 318
319 /** 319 /**
320 * @override 320 * @override
321 * @return {string} 321 * @return {string}
322 */ 322 */
323 toString() { 323 toString() {
324 return this.securityOrigin + this.cacheName; 324 return this.securityOrigin + this.cacheName;
325 } 325 }
326 }; 326 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698