1/* 2 * Copyright (C) 2013 Apple 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26WebInspector.ApplicationCacheManager = function() 27{ 28 WebInspector.Object.call(this); 29 30 if (window.ApplicationCacheAgent) 31 ApplicationCacheAgent.enable(); 32 33 WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this); 34 WebInspector.Frame.addEventListener(WebInspector.Frame.Event.ChildFrameWasRemoved, this._childFrameWasRemoved, this); 35 36 this._online = true; 37 38 this.initialize(); 39}; 40 41WebInspector.ApplicationCacheManager.Event = { 42 Cleared: "application-cache-manager-cleared", 43 FrameManifestAdded: "application-cache-manager-frame-manifest-added", 44 FrameManifestRemoved: "application-cache-manager-frame-manifest-removed", 45 FrameManifestStatusChanged: "application-cache-manager-frame-manifest-status-changed", 46 NetworkStateUpdated: "application-cache-manager-network-state-updated" 47}; 48 49WebInspector.ApplicationCacheManager.Status = { 50 Uncached: 0, 51 Idle: 1, 52 Checking: 2, 53 Downloading: 3, 54 UpdateReady: 4, 55 Obsolete: 5 56}; 57 58WebInspector.ApplicationCacheManager.prototype = { 59 constructor: WebInspector.ApplicationCacheManager, 60 61 // Public 62 63 initialize: function() 64 { 65 this._applicationCacheObjects = []; 66 67 if (window.ApplicationCacheAgent) 68 ApplicationCacheAgent.getFramesWithManifests(this._framesWithManifestsLoaded.bind(this)); 69 }, 70 71 networkStateUpdated: function(isNowOnline) 72 { 73 this._online = isNowOnline; 74 75 this.dispatchEventToListeners(WebInspector.ApplicationCacheManager.Event.NetworkStateUpdated, {online: this._online}); 76 }, 77 78 get online() 79 { 80 return this._online; 81 }, 82 83 applicationCacheStatusUpdated: function(frameId, manifestURL, status) 84 { 85 var frame = WebInspector.frameResourceManager.frameForIdentifier(frameId); 86 if (!frame) 87 return; 88 89 this._frameManifestUpdated(frame, manifestURL, status); 90 }, 91 92 requestApplicationCache: function(frame, callback) 93 { 94 function callbackWrapper(error, applicationCache) 95 { 96 if (error) { 97 callback(null); 98 return; 99 } 100 101 callback(applicationCache); 102 } 103 104 ApplicationCacheAgent.getApplicationCacheForFrame(frame.id, callbackWrapper); 105 }, 106 107 // Private 108 109 _mainResourceDidChange: function(event) 110 { 111 console.assert(event.target instanceof WebInspector.Frame); 112 113 if (event.target.isMainFrame()) { 114 // If we are dealing with the main frame, we want to clear our list of objects, because we are navigating to a new page. 115 this.initialize(); 116 117 this.dispatchEventToListeners(WebInspector.ApplicationCacheManager.Event.Cleared); 118 119 return; 120 } 121 122 ApplicationCacheAgent.getManifestForFrame(event.target.id, this._manifestForFrameLoaded.bind(this, event.target.id)); 123 }, 124 125 _childFrameWasRemoved: function(event) 126 { 127 this._frameManifestRemoved(event.data.childFrame); 128 }, 129 130 _manifestForFrameLoaded: function(frameId, error, manifestURL) 131 { 132 if (error) 133 return; 134 135 var frame = WebInspector.frameResourceManager.frameForIdentifier(frameId); 136 if (!frame) 137 return; 138 139 if (!manifestURL) 140 this._frameManifestRemoved(frame); 141 }, 142 143 _framesWithManifestsLoaded: function(error, framesWithManifests) 144 { 145 if (error) 146 return; 147 148 for (var i = 0; i < framesWithManifests.length; ++i) { 149 var frame = WebInspector.frameResourceManager.frameForIdentifier(framesWithManifests[i].frameId); 150 if (!frame) 151 continue; 152 153 this._frameManifestUpdated(frame, framesWithManifests[i].manifestURL, framesWithManifests[i].status); 154 } 155 }, 156 157 _frameManifestUpdated: function(frame, manifestURL, status) 158 { 159 if (status === WebInspector.ApplicationCacheManager.Status.Uncached) { 160 this._frameManifestRemoved(frame); 161 return; 162 } 163 164 if (!manifestURL) 165 return; 166 167 var manifestFrame = this._applicationCacheObjects[frame.id]; 168 if (manifestFrame && manifestURL !== manifestFrame.manifest.manifestURL) 169 this._frameManifestRemoved(frame); 170 171 var oldStatus = manifestFrame ? manifestFrame.status : -1; 172 var statusChanged = manifestFrame && status !== oldStatus; 173 if (manifestFrame) 174 manifestFrame.status = status; 175 176 if (!this._applicationCacheObjects[frame.id]) { 177 var cacheManifest = new WebInspector.ApplicationCacheManifest(manifestURL); 178 this._applicationCacheObjects[frame.id] = new WebInspector.ApplicationCacheFrame(frame, cacheManifest, status); 179 180 this.dispatchEventToListeners(WebInspector.ApplicationCacheManager.Event.FrameManifestAdded, {frameManifest: this._applicationCacheObjects[frame.id]}); 181 } 182 183 if (statusChanged) 184 this.dispatchEventToListeners(WebInspector.ApplicationCacheManager.Event.FrameManifestStatusChanged, {frameManifest: this._applicationCacheObjects[frame.id]}); 185 }, 186 187 _frameManifestRemoved: function(frame) 188 { 189 if (!this._applicationCacheObjects[frame.id]) 190 return; 191 192 delete this._applicationCacheObjects[frame.id]; 193 194 this.dispatchEventToListeners(WebInspector.ApplicationCacheManager.Event.FrameManifestRemoved, {frame: frame}); 195 } 196}; 197 198WebInspector.ApplicationCacheManager.prototype.__proto__ = WebInspector.Object.prototype; 199