tizen beta release
[profile/ivi/webkit-efl.git] / debian / libwebkit-engine / usr / share / ewebkit-0 / webinspector / ApplicationCacheModel.js
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
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
14  * distribution.
15  *
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.
27  */
28
29 /**
30  * @constructor
31  * @extends {WebInspector.Object}
32  */
33 WebInspector.ApplicationCacheModel = function()
34 {
35     ApplicationCacheAgent.enable();
36     InspectorBackend.registerApplicationCacheDispatcher(new WebInspector.ApplicationCacheDispatcher(this));
37     
38     WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameNavigated, this._frameNavigated, this);
39     WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameDetached, this._frameDetached, this);
40     
41     this._statuses = {};
42     this._manifestURLsByFrame = {};
43
44     this._mainFrameNavigated();
45     
46     this._onLine = true;
47 }
48
49 WebInspector.ApplicationCacheModel.EventTypes = {
50     FrameManifestStatusUpdated: "FrameManifestStatusUpdated",
51     FrameManifestAdded: "FrameManifestAdded",
52     FrameManifestRemoved: "FrameManifestRemoved",
53     NetworkStateChanged: "NetworkStateChanged"
54 }
55
56 WebInspector.ApplicationCacheModel.prototype = {
57     _frameNavigated: function(event)
58     {
59         var frame = /** @type {WebInspector.ResourceTreeFrame} */ event.data;
60         if (frame.isMainFrame()) {
61             this._mainFrameNavigated();
62             return;
63         }
64
65         ApplicationCacheAgent.getManifestForFrame(frame.id, this._manifestForFrameLoaded.bind(this, frame.id));
66     },
67     
68     _frameDetached: function(event)
69     {
70         var frame = /** @type {WebInspector.ResourceTreeFrame} */ event.data;
71         this._frameManifestRemoved(frame.id);
72     },
73     
74     _mainFrameNavigated: function()
75     {
76         ApplicationCacheAgent.getFramesWithManifests(this._framesWithManifestsLoaded.bind(this));
77     },
78
79     /**
80      * @param {string} frameId
81      * @param {string} error
82      * @param {string} manifestURL
83      */
84     _manifestForFrameLoaded: function(frameId, error, manifestURL)
85     {
86         if (error) {
87             console.error(error);
88             return;
89         }
90         
91         if (!manifestURL)
92             this._frameManifestRemoved(frameId);
93     },
94     
95     /**
96      * @param {string} error
97      * @param {Array.<ApplicationCacheAgent.FrameWithManifest>} framesWithManifests
98      */
99     _framesWithManifestsLoaded: function(error, framesWithManifests)
100     {
101         if (error) {
102             console.error(error);
103             return;
104         }
105
106         for (var i = 0; i < framesWithManifests.length; ++i)
107             this._frameManifestUpdated(framesWithManifests[i].frameId, framesWithManifests[i].manifestURL, framesWithManifests[i].status);
108     },
109     
110     /**
111      * @param {string} frameId
112      * @param {string} manifestURL
113      * @param {number} status
114      */
115     _frameManifestUpdated: function(frameId, manifestURL, status)
116     {
117         if (status === applicationCache.UNCACHED) {
118             this._frameManifestRemoved(frameId);
119             return;
120         }
121             
122         if (!manifestURL)
123             return;
124             
125         if (this._manifestURLsByFrame[frameId] && manifestURL !== this._manifestURLsByFrame[frameId])
126             this._frameManifestRemoved(frameId);
127         
128         var statusChanged = this._statuses[frameId] !== status;
129         this._statuses[frameId] = status;
130         
131         if (!this._manifestURLsByFrame[frameId]) {
132             this._manifestURLsByFrame[frameId] = manifestURL;
133             this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestAdded, frameId);
134         }
135             
136         if (statusChanged)
137             this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestStatusUpdated, frameId);
138     },
139     
140     /**
141      * @param {string} frameId
142      */
143     _frameManifestRemoved: function(frameId)
144     {
145         if (!this._manifestURLsByFrame[frameId])
146             return;
147
148         var manifestURL = this._manifestURLsByFrame[frameId];
149         delete this._manifestURLsByFrame[frameId];
150         delete this._statuses[frameId];
151         
152         this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestRemoved, frameId);
153     },
154     
155     /**
156      * @param {string} frameId
157      * @return {string}
158      */
159     frameManifestURL: function(frameId)
160     {
161         return this._manifestURLsByFrame[frameId] || "";
162     },
163     
164     /**
165      * @param {string} frameId
166      * @return {number}
167      */
168     frameManifestStatus: function(frameId)
169     {
170         return this._statuses[frameId] || applicationCache.UNCACHED;
171     },
172     
173     /**
174      * @return {boolean}
175      */
176     get onLine()
177     {
178         return this._onLine;
179     },
180     
181     /**
182      * @param {string} frameId
183      * @param {string} manifestURL
184      * @param {number} status
185      */
186     _statusUpdated: function(frameId, manifestURL, status)
187     {
188         this._frameManifestUpdated(frameId, manifestURL, status);
189     },
190     
191     /**
192      * @param {string} frameId
193      * @param {function(Object)} callback
194      */
195     requestApplicationCache: function(frameId, callback)
196     {
197         function callbackWrapper(error, applicationCache)
198         {
199             if (error) {
200                 console.error(error);
201                 callback(null);
202                 return;
203             }
204             
205             callback(applicationCache);
206         }
207         
208         ApplicationCacheAgent.getApplicationCacheForFrame(frameId, callbackWrapper.bind(this));
209     },
210     
211     /**
212      * @param {boolean} isNowOnline
213      */
214     _networkStateUpdated: function(isNowOnline)
215     {
216         this._onLine = isNowOnline;
217         this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.NetworkStateChanged, isNowOnline);
218     }
219 }
220
221 WebInspector.ApplicationCacheModel.prototype.__proto__ = WebInspector.Object.prototype;
222
223 /**
224  * @constructor
225  * @implements {ApplicationCacheAgent.Dispatcher}
226  */
227 WebInspector.ApplicationCacheDispatcher = function(applicationCacheModel)
228 {
229     this._applicationCacheModel = applicationCacheModel;
230 }
231
232 WebInspector.ApplicationCacheDispatcher.prototype = {
233     /**
234      * @param {string} frameId
235      * @param {string} manifestURL
236      * @param {number} status
237      */
238     applicationCacheStatusUpdated: function(frameId, manifestURL, status)
239     {
240         this._applicationCacheModel._statusUpdated(frameId, manifestURL, status);
241     },
242     
243     /**
244      * @param {boolean} isNowOnline
245      */
246     networkStateUpdated: function(isNowOnline)
247     {
248         this._applicationCacheModel._networkStateUpdated(isNowOnline);
249     },
250 }