OLD | NEW |
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 /** @type {!Map<string, !WebInspector.ServiceWorkerCacheModel.Cache>} */ |
15 this._cacheNames = new Set(); | 15 this._caches = new Map(); |
16 | 16 |
17 this._agent = target.serviceWorkerCacheAgent(); | 17 this._agent = target.cacheStorageAgent(); |
| 18 |
| 19 /** @type {boolean} */ |
| 20 this._enabled = false; |
18 } | 21 } |
19 | 22 |
20 WebInspector.ServiceWorkerCacheModel.EventTypes = { | 23 WebInspector.ServiceWorkerCacheModel.EventTypes = { |
21 CacheAdded: "CacheAdded", | 24 CacheAdded: "CacheAdded", |
22 CacheRemoved: "CacheRemoved", | 25 CacheRemoved: "CacheRemoved" |
23 } | 26 } |
24 | 27 |
25 WebInspector.ServiceWorkerCacheModel.prototype = { | 28 WebInspector.ServiceWorkerCacheModel.prototype = { |
26 _reset: function() | 29 enable: function() |
27 { | 30 { |
28 this._updateCacheNames([]); | 31 if (this._enabled) |
29 this._loadCacheNames(); | 32 return; |
| 33 |
| 34 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTr
eeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); |
| 35 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTr
eeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this); |
| 36 |
| 37 var securityOrigins = this.target().resourceTreeModel.securityOrigins(); |
| 38 for (var i = 0; i < securityOrigins.length; ++i) |
| 39 this._addOrigin(securityOrigins[i]); |
| 40 this._enabled = true; |
30 }, | 41 }, |
31 | 42 |
32 refreshCacheNames: function() | 43 refreshCacheNames: function() |
33 { | 44 { |
34 this._loadCacheNames(); | 45 var securityOrigins = this.target().resourceTreeModel.securityOrigins(); |
| 46 for (var securityOrigin of securityOrigins) |
| 47 this._loadCacheNames(securityOrigin); |
35 }, | 48 }, |
36 | 49 |
37 /** | 50 /** |
38 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 51 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache |
39 */ | 52 */ |
40 deleteCache: function(cacheId) | 53 deleteCache: function(cache) |
41 { | 54 { |
42 /** | 55 /** |
43 * @this {WebInspector.ServiceWorkerCacheModel} | 56 * @this {WebInspector.ServiceWorkerCacheModel} |
44 */ | 57 */ |
45 function callback(error) | 58 function callback(error) |
46 { | 59 { |
47 if (error) { | 60 if (error) { |
48 console.error("ServiceWorkerCacheAgent error: ", error); | 61 console.error("ServiceWorkerCacheAgent error deleting cache ", c
ache.toString(), ": ", error); |
49 return; | 62 return; |
50 } | 63 } |
51 this._cacheRemoved(cacheId.name); | 64 this._caches.delete(cache.cacheId); |
| 65 this._cacheRemoved(cache); |
52 } | 66 } |
53 this._agent.deleteCache(cacheId.name, callback.bind(this)); | 67 this._agent.deleteCache(cache.cacheId, callback.bind(this)); |
54 }, | 68 }, |
55 | 69 |
56 /** | 70 /** |
57 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 71 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache |
58 * @param {number} skipCount | 72 * @param {number} skipCount |
59 * @param {number} pageSize | 73 * @param {number} pageSize |
60 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo
olean)} callback | 74 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo
olean)} callback |
61 */ | 75 */ |
62 loadCacheData: function(cacheId, skipCount, pageSize, callback) | 76 loadCacheData: function(cache, skipCount, pageSize, callback) |
63 { | 77 { |
64 this._requestEntries(cacheId, cacheId.name, skipCount, pageSize, callbac
k); | 78 this._requestEntries(cache, skipCount, pageSize, callback); |
65 }, | 79 }, |
66 | 80 |
67 /** | 81 /** |
68 * @return {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>} | 82 * @return {!Array.<!WebInspector.ServiceWorkerCacheModel.Cache>} |
69 */ | 83 */ |
70 caches: function() | 84 caches: function() |
71 { | 85 { |
72 var result = []; | 86 var caches = new Array(); |
73 for (var cacheName of this._cacheNames) { | 87 for (var cache of this._caches.values()) |
74 result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName
)); | 88 caches.push(cache); |
75 } | 89 return caches; |
76 return result; | |
77 }, | 90 }, |
78 | 91 |
79 dispose: function() | 92 dispose: function() |
80 { | 93 { |
81 this._updateCacheNames([]); | 94 for (var cache of this._caches.values()) |
| 95 this._cacheRemoved(cache); |
| 96 this._caches.clear(); |
| 97 if (this._enabled) { |
| 98 this.target().resourceTreeModel.removeEventListener(WebInspector.Res
ourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); |
| 99 this.target().resourceTreeModel.removeEventListener(WebInspector.Res
ourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, th
is); |
| 100 } |
82 }, | 101 }, |
83 | 102 |
84 _loadCacheNames: function() | 103 _addOrigin: function(securityOrigin) |
85 { | 104 { |
86 /** | 105 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 }, | 106 }, |
105 | 107 |
106 /** | 108 /** |
107 * @param {!Array.<string>} cacheNames | 109 * @param {string} securityOrigin |
108 */ | 110 */ |
109 _updateCacheNames: function(cacheNames) | 111 _removeOrigin: function(securityOrigin) |
110 { | 112 { |
111 /** @type {!Set.<string>} */ | 113 for (var opaqueId of this._caches.keys()) { |
112 var newCacheNames = new Set(cacheNames); | 114 var cache = this._caches.get(opaqueId); |
113 /** @type {!Set.<string>} */ | 115 if (cache.securityOrigin == securityOrigin) { |
114 var oldCacheNames = this._cacheNames; | 116 this._caches.delete(opaqueId); |
115 | 117 this._cacheRemoved(cache); |
116 this._cacheNames = new Set(cacheNames); | 118 } |
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 } | 119 } |
126 }, | 120 }, |
127 | 121 |
128 /** | 122 /** |
129 * @param {string} cacheName | 123 * @param {string} securityOrigin |
130 */ | 124 */ |
131 _cacheAdded: function(cacheName) | 125 _loadCacheNames: function(securityOrigin) |
132 { | 126 { |
133 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName
); | 127 /** |
134 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event
Types.CacheAdded, cacheId); | 128 * @param {?Protocol.Error} error |
| 129 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Cache>} caches |
| 130 * @this {WebInspector.ServiceWorkerCacheModel} |
| 131 */ |
| 132 function callback(error, caches) |
| 133 { |
| 134 if (error) { |
| 135 console.error("ServiceWorkerCacheAgent error while loading cache
s: ", error); |
| 136 return; |
| 137 } |
| 138 this._updateCacheNames(securityOrigin, caches); |
| 139 } |
| 140 this._agent.requestCacheNames(securityOrigin, callback.bind(this)); |
135 }, | 141 }, |
136 | 142 |
137 /** | 143 /** |
138 * @param {string} cacheName | 144 * @param {string} securityOrigin |
| 145 * @param {!Array} cachesJson |
139 */ | 146 */ |
140 _cacheRemoved: function(cacheName) | 147 _updateCacheNames: function(securityOrigin, cachesJson) |
141 { | 148 { |
142 var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName
); | 149 /** |
143 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event
Types.CacheRemoved, cacheId); | 150 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache |
| 151 * @this {WebInspector.ServiceWorkerCacheModel} |
| 152 */ |
| 153 function deleteAndSaveOldCaches(cache) |
| 154 { |
| 155 if (cache.securityOrigin == securityOrigin && !updatingCachesIds.has
(cache.cacheId)) { |
| 156 oldCaches.set(cache.cacheId, cache); |
| 157 this._caches.delete(cache.cacheId); |
| 158 } |
| 159 } |
| 160 |
| 161 /** @type {!Set<string>} */ |
| 162 var updatingCachesIds = new Set(); |
| 163 /** @type {!Map<string, !WebInspector.ServiceWorkerCacheModel.Cache>} */ |
| 164 var newCaches = new Map(); |
| 165 /** @type {!Map<string, !WebInspector.ServiceWorkerCacheModel.Cache>} */ |
| 166 var oldCaches = new Map(); |
| 167 |
| 168 for (var cacheJson of cachesJson) { |
| 169 var cache = new WebInspector.ServiceWorkerCacheModel.Cache(cacheJson
.securityOrigin, cacheJson.cacheName, cacheJson.cacheId); |
| 170 updatingCachesIds.add(cache.cacheId); |
| 171 if (this._caches.has(cache.cacheId)) |
| 172 continue; |
| 173 newCaches.set(cache.cacheId, cache); |
| 174 this._caches.set(cache.cacheId, cache); |
| 175 } |
| 176 this._caches.forEach(deleteAndSaveOldCaches, this); |
| 177 newCaches.forEach(this._cacheAdded, this); |
| 178 oldCaches.forEach(this._cacheRemoved, this); |
144 }, | 179 }, |
145 | 180 |
146 /** | 181 /** |
147 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 182 * @param {!WebInspector.Event} event |
148 * @param {string} cacheName | 183 */ |
| 184 _securityOriginAdded: function(event) |
| 185 { |
| 186 var securityOrigin = /** @type {string} */ (event.data); |
| 187 this._addOrigin(securityOrigin); |
| 188 }, |
| 189 |
| 190 /** |
| 191 * @param {!WebInspector.Event} event |
| 192 */ |
| 193 _securityOriginRemoved: function(event) |
| 194 { |
| 195 var securityOrigin = /** @type {string} */ (event.data); |
| 196 this._removeOrigin(securityOrigin); |
| 197 }, |
| 198 |
| 199 /** |
| 200 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache |
| 201 */ |
| 202 _cacheAdded: function(cache) |
| 203 { |
| 204 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event
Types.CacheAdded, cache); |
| 205 }, |
| 206 |
| 207 /** |
| 208 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache |
| 209 */ |
| 210 _cacheRemoved: function(cache) |
| 211 { |
| 212 this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.Event
Types.CacheRemoved, cache); |
| 213 }, |
| 214 |
| 215 /** |
| 216 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache |
149 * @param {number} skipCount | 217 * @param {number} skipCount |
150 * @param {number} pageSize | 218 * @param {number} pageSize |
151 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo
olean)} callback | 219 * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, bo
olean)} callback |
152 */ | 220 */ |
153 _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback) | 221 _requestEntries: function(cache, skipCount, pageSize, callback) |
154 { | 222 { |
155 /** | 223 /** |
156 * @param {?Protocol.Error} error | 224 * @param {?Protocol.Error} error |
157 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt
ries | 225 * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEnt
ries |
158 * @param {boolean} hasMore | 226 * @param {boolean} hasMore |
159 * @this {WebInspector.ServiceWorkerCacheModel} | |
160 */ | 227 */ |
161 function innerCallback(error, dataEntries, hasMore) | 228 function innerCallback(error, dataEntries, hasMore) |
162 { | 229 { |
163 if (error) { | 230 if (error) { |
164 console.error("ServiceWorkerCacheAgent error: ", error); | 231 console.error("ServiceWorkerCacheAgent error while requesting en
tries: ", error); |
165 return; | 232 return; |
166 } | 233 } |
167 | |
168 if (!this._cacheNames) | |
169 return; | |
170 var entries = []; | 234 var entries = []; |
171 for (var i = 0; i < dataEntries.length; ++i) { | 235 for (var i = 0; i < dataEntries.length; ++i) { |
172 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par
se(dataEntries[i].request)); | 236 var request = WebInspector.RemoteObject.fromLocalObject(JSON.par
se(dataEntries[i].request)); |
173 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa
rse(dataEntries[i].response)); | 237 var response = WebInspector.RemoteObject.fromLocalObject(JSON.pa
rse(dataEntries[i].response)); |
174 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ
est, response)); | 238 entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(requ
est, response)); |
175 } | 239 } |
176 callback(entries, hasMore); | 240 callback(entries, hasMore); |
177 } | 241 } |
178 | 242 this._agent.requestEntries(cache.cacheId, skipCount, pageSize, innerCall
back); |
179 this._agent.requestEntries(cacheName, skipCount, pageSize, innerCallback
.bind(this)); | |
180 }, | 243 }, |
181 | 244 |
182 __proto__: WebInspector.SDKModel.prototype | 245 __proto__: WebInspector.SDKModel.prototype |
183 } | 246 } |
184 | 247 |
185 /** | 248 /** |
186 * @constructor | 249 * @constructor |
187 * @param {!WebInspector.RemoteObject} request | 250 * @param {!WebInspector.RemoteObject} request |
188 * @param {!WebInspector.RemoteObject} response | 251 * @param {!WebInspector.RemoteObject} response |
189 */ | 252 */ |
190 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) | 253 WebInspector.ServiceWorkerCacheModel.Entry = function(request, response) |
191 { | 254 { |
192 this.request = request; | 255 this.request = request; |
193 this.response = response; | 256 this.response = response; |
194 } | 257 } |
195 | 258 |
196 /** | 259 /** |
197 * @constructor | 260 * @constructor |
198 * @param {string} name | 261 * @param {string} securityOrigin |
| 262 * @param {string} cacheName |
| 263 * @param {string} cacheId |
199 */ | 264 */ |
200 WebInspector.ServiceWorkerCacheModel.CacheId = function(name) | 265 WebInspector.ServiceWorkerCacheModel.Cache = function(securityOrigin, cacheName,
cacheId) |
201 { | 266 { |
202 this.name = name; | 267 this.securityOrigin = securityOrigin; |
| 268 this.cacheName = cacheName; |
| 269 this.cacheId = cacheId; |
203 } | 270 } |
204 | 271 |
205 WebInspector.ServiceWorkerCacheModel.CacheId.prototype = { | 272 WebInspector.ServiceWorkerCacheModel.Cache.prototype = { |
206 /** | 273 /** |
207 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 274 * @param {!WebInspector.ServiceWorkerCacheModel.Cache} cache |
208 * @return {boolean} | 275 * @return {boolean} |
209 */ | 276 */ |
210 equals: function(cacheId) | 277 equals: function(cache) |
211 { | 278 { |
212 return this.name === cacheId.name; | 279 return this.cacheId == cache.cacheId; |
| 280 }, |
| 281 |
| 282 /** |
| 283 * @override |
| 284 * @return {string} |
| 285 */ |
| 286 toString: function() |
| 287 { |
| 288 return this.securityOrigin + this.cacheName; |
213 } | 289 } |
214 } | 290 } |
215 | 291 |
| 292 |
| 293 WebInspector.ServiceWorkerCacheModel._symbol = Symbol("CacheStorageModel"); |
216 /** | 294 /** |
217 * @constructor | 295 * @param {!WebInspector.Target} target |
218 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId | 296 * @return {!WebInspector.ServiceWorkerCacheModel} |
219 */ | 297 */ |
220 WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId) | 298 WebInspector.ServiceWorkerCacheModel.fromTarget = function(target) |
221 { | 299 { |
222 this.cacheId = cacheId; | 300 if (!target[WebInspector.ServiceWorkerCacheModel._symbol]) |
| 301 target[WebInspector.ServiceWorkerCacheModel._symbol] = new WebInspector.
ServiceWorkerCacheModel(target); |
| 302 |
| 303 return target[WebInspector.ServiceWorkerCacheModel._symbol]; |
223 } | 304 } |
OLD | NEW |