1 /* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2010 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "core/inspector/InspectorApplicationCacheAgent.h" 28 29 #include "InspectorFrontend.h" 30 #include "core/inspector/InspectorPageAgent.h" 31 #include "core/inspector/InspectorState.h" 32 #include "core/inspector/InstrumentingAgents.h" 33 #include "core/loader/DocumentLoader.h" 34 #include "core/loader/FrameLoader.h" 35 #include "core/loader/appcache/ApplicationCacheHost.h" 36 #include "core/page/Frame.h" 37 #include "core/platform/network/NetworkStateNotifier.h" 38 39 namespace WebCore { 40 41 namespace ApplicationCacheAgentState { 42 static const char applicationCacheAgentEnabled[] = "applicationCacheAgentEnabled"; 43 } 44 45 InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InspectorPageAgent* pageAgent) 46 : InspectorBaseAgent<InspectorApplicationCacheAgent>("ApplicationCache", instrumentingAgents, state) 47 , m_pageAgent(pageAgent) 48 , m_frontend(0) 49 { 50 } 51 52 void InspectorApplicationCacheAgent::setFrontend(InspectorFrontend* frontend) 53 { 54 m_frontend = frontend->applicationcache(); 55 } 56 57 void InspectorApplicationCacheAgent::clearFrontend() 58 { 59 m_instrumentingAgents->setInspectorApplicationCacheAgent(0); 60 m_frontend = 0; 61 } 62 63 void InspectorApplicationCacheAgent::restore() 64 { 65 if (m_state->getBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled)) { 66 ErrorString error; 67 enable(&error); 68 } 69 } 70 71 void InspectorApplicationCacheAgent::enable(ErrorString*) 72 { 73 m_state->setBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled, true); 74 m_instrumentingAgents->setInspectorApplicationCacheAgent(this); 75 76 // We need to pass initial navigator.onOnline. 77 networkStateChanged(); 78 } 79 80 void InspectorApplicationCacheAgent::updateApplicationCacheStatus(Frame* frame) 81 { 82 DocumentLoader* documentLoader = frame->loader()->documentLoader(); 83 if (!documentLoader) 84 return; 85 86 ApplicationCacheHost* host = documentLoader->applicationCacheHost(); 87 ApplicationCacheHost::Status status = host->status(); 88 ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo(); 89 90 String manifestURL = info.m_manifest.string(); 91 m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status)); 92 } 93 94 void InspectorApplicationCacheAgent::networkStateChanged() 95 { 96 bool isNowOnline = networkStateNotifier().onLine(); 97 m_frontend->networkStateUpdated(isNowOnline); 98 } 99 100 void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest> >& result) 101 { 102 result = TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest>::create(); 103 104 Frame* mainFrame = m_pageAgent->mainFrame(); 105 for (Frame* frame = mainFrame; frame; frame = frame->tree()->traverseNext(mainFrame)) { 106 DocumentLoader* documentLoader = frame->loader()->documentLoader(); 107 if (!documentLoader) 108 continue; 109 110 ApplicationCacheHost* host = documentLoader->applicationCacheHost(); 111 ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo(); 112 String manifestURL = info.m_manifest.string(); 113 if (!manifestURL.isEmpty()) { 114 RefPtr<TypeBuilder::ApplicationCache::FrameWithManifest> value = TypeBuilder::ApplicationCache::FrameWithManifest::create() 115 .setFrameId(m_pageAgent->frameId(frame)) 116 .setManifestURL(manifestURL) 117 .setStatus(static_cast<int>(host->status())); 118 result->addItem(value); 119 } 120 } 121 } 122 123 DocumentLoader* InspectorApplicationCacheAgent::assertFrameWithDocumentLoader(ErrorString* errorString, String frameId) 124 { 125 Frame* frame = m_pageAgent->assertFrame(errorString, frameId); 126 if (!frame) 127 return 0; 128 129 return InspectorPageAgent::assertDocumentLoader(errorString, frame); 130 } 131 132 void InspectorApplicationCacheAgent::getManifestForFrame(ErrorString* errorString, const String& frameId, String* manifestURL) 133 { 134 DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId); 135 if (!documentLoader) 136 return; 137 138 ApplicationCacheHost::CacheInfo info = documentLoader->applicationCacheHost()->applicationCacheInfo(); 139 *manifestURL = info.m_manifest.string(); 140 } 141 142 void InspectorApplicationCacheAgent::getApplicationCacheForFrame(ErrorString* errorString, const String& frameId, RefPtr<TypeBuilder::ApplicationCache::ApplicationCache>& applicationCache) 143 { 144 DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId); 145 if (!documentLoader) 146 return; 147 148 ApplicationCacheHost* host = documentLoader->applicationCacheHost(); 149 ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo(); 150 151 ApplicationCacheHost::ResourceInfoList resources; 152 host->fillResourceList(&resources); 153 154 applicationCache = buildObjectForApplicationCache(resources, info); 155 } 156 157 PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCache> InspectorApplicationCacheAgent::buildObjectForApplicationCache(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources, const ApplicationCacheHost::CacheInfo& applicationCacheInfo) 158 { 159 return TypeBuilder::ApplicationCache::ApplicationCache::create() 160 .setManifestURL(applicationCacheInfo.m_manifest.string()) 161 .setSize(applicationCacheInfo.m_size) 162 .setCreationTime(applicationCacheInfo.m_creationTime) 163 .setUpdateTime(applicationCacheInfo.m_updateTime) 164 .setResources(buildArrayForApplicationCacheResources(applicationCacheResources)) 165 .release(); 166 } 167 168 PassRefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > InspectorApplicationCacheAgent::buildArrayForApplicationCacheResources(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources) 169 { 170 RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > resources = TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource>::create(); 171 172 ApplicationCacheHost::ResourceInfoList::const_iterator end = applicationCacheResources.end(); 173 ApplicationCacheHost::ResourceInfoList::const_iterator it = applicationCacheResources.begin(); 174 for (int i = 0; it != end; ++it, i++) 175 resources->addItem(buildObjectForApplicationCacheResource(*it)); 176 177 return resources; 178 } 179 180 PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCacheResource> InspectorApplicationCacheAgent::buildObjectForApplicationCacheResource(const ApplicationCacheHost::ResourceInfo& resourceInfo) 181 { 182 String types; 183 if (resourceInfo.m_isMaster) 184 types.append("Master "); 185 186 if (resourceInfo.m_isManifest) 187 types.append("Manifest "); 188 189 if (resourceInfo.m_isFallback) 190 types.append("Fallback "); 191 192 if (resourceInfo.m_isForeign) 193 types.append("Foreign "); 194 195 if (resourceInfo.m_isExplicit) 196 types.append("Explicit "); 197 198 RefPtr<TypeBuilder::ApplicationCache::ApplicationCacheResource> value = TypeBuilder::ApplicationCache::ApplicationCacheResource::create() 199 .setUrl(resourceInfo.m_resource.string()) 200 .setSize(static_cast<int>(resourceInfo.m_size)) 201 .setType(types); 202 return value; 203 } 204 205 } // namespace WebCore 206 207