2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * @extends {WebInspector.Object}
33 WebInspector.ApplicationCacheModel = function()
35 ApplicationCacheAgent.enable();
36 InspectorBackend.registerApplicationCacheDispatcher(new WebInspector.ApplicationCacheDispatcher(this));
38 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameNavigated, this._frameNavigated, this);
39 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameDetached, this._frameDetached, this);
42 this._manifestURLsByFrame = {};
44 this._mainFrameNavigated();
49 WebInspector.ApplicationCacheModel.EventTypes = {
50 FrameManifestStatusUpdated: "FrameManifestStatusUpdated",
51 FrameManifestAdded: "FrameManifestAdded",
52 FrameManifestRemoved: "FrameManifestRemoved",
53 NetworkStateChanged: "NetworkStateChanged"
56 WebInspector.ApplicationCacheModel.prototype = {
57 _frameNavigated: function(event)
59 var frame = /** @type {WebInspector.ResourceTreeFrame} */ event.data;
60 if (frame.isMainFrame()) {
61 this._mainFrameNavigated();
65 ApplicationCacheAgent.getManifestForFrame(frame.id, this._manifestForFrameLoaded.bind(this, frame.id));
68 _frameDetached: function(event)
70 var frame = /** @type {WebInspector.ResourceTreeFrame} */ event.data;
71 this._frameManifestRemoved(frame.id);
74 _mainFrameNavigated: function()
76 ApplicationCacheAgent.getFramesWithManifests(this._framesWithManifestsLoaded.bind(this));
80 * @param {string} frameId
81 * @param {string} error
82 * @param {string} manifestURL
84 _manifestForFrameLoaded: function(frameId, error, manifestURL)
92 this._frameManifestRemoved(frameId);
96 * @param {string} error
97 * @param {Array.<ApplicationCacheAgent.FrameWithManifest>} framesWithManifests
99 _framesWithManifestsLoaded: function(error, framesWithManifests)
102 console.error(error);
106 for (var i = 0; i < framesWithManifests.length; ++i)
107 this._frameManifestUpdated(framesWithManifests[i].frameId, framesWithManifests[i].manifestURL, framesWithManifests[i].status);
111 * @param {string} frameId
112 * @param {string} manifestURL
113 * @param {number} status
115 _frameManifestUpdated: function(frameId, manifestURL, status)
117 if (status === applicationCache.UNCACHED) {
118 this._frameManifestRemoved(frameId);
125 if (this._manifestURLsByFrame[frameId] && manifestURL !== this._manifestURLsByFrame[frameId])
126 this._frameManifestRemoved(frameId);
128 var statusChanged = this._statuses[frameId] !== status;
129 this._statuses[frameId] = status;
131 if (!this._manifestURLsByFrame[frameId]) {
132 this._manifestURLsByFrame[frameId] = manifestURL;
133 this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestAdded, frameId);
137 this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestStatusUpdated, frameId);
141 * @param {string} frameId
143 _frameManifestRemoved: function(frameId)
145 if (!this._manifestURLsByFrame[frameId])
148 var manifestURL = this._manifestURLsByFrame[frameId];
149 delete this._manifestURLsByFrame[frameId];
150 delete this._statuses[frameId];
152 this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestRemoved, frameId);
156 * @param {string} frameId
159 frameManifestURL: function(frameId)
161 return this._manifestURLsByFrame[frameId] || "";
165 * @param {string} frameId
168 frameManifestStatus: function(frameId)
170 return this._statuses[frameId] || applicationCache.UNCACHED;
182 * @param {string} frameId
183 * @param {string} manifestURL
184 * @param {number} status
186 _statusUpdated: function(frameId, manifestURL, status)
188 this._frameManifestUpdated(frameId, manifestURL, status);
192 * @param {string} frameId
193 * @param {function(Object)} callback
195 requestApplicationCache: function(frameId, callback)
197 function callbackWrapper(error, applicationCache)
200 console.error(error);
205 callback(applicationCache);
208 ApplicationCacheAgent.getApplicationCacheForFrame(frameId, callbackWrapper.bind(this));
212 * @param {boolean} isNowOnline
214 _networkStateUpdated: function(isNowOnline)
216 this._onLine = isNowOnline;
217 this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.NetworkStateChanged, isNowOnline);
221 WebInspector.ApplicationCacheModel.prototype.__proto__ = WebInspector.Object.prototype;
225 * @implements {ApplicationCacheAgent.Dispatcher}
227 WebInspector.ApplicationCacheDispatcher = function(applicationCacheModel)
229 this._applicationCacheModel = applicationCacheModel;
232 WebInspector.ApplicationCacheDispatcher.prototype = {
234 * @param {string} frameId
235 * @param {string} manifestURL
236 * @param {number} status
238 applicationCacheStatusUpdated: function(frameId, manifestURL, status)
240 this._applicationCacheModel._statusUpdated(frameId, manifestURL, status);
244 * @param {boolean} isNowOnline
246 networkStateUpdated: function(isNowOnline)
248 this._applicationCacheModel._networkStateUpdated(isNowOnline);