2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/inspector/InspectorApplicationCacheAgent.h"
29 #include "core/frame/LocalFrame.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/page/NetworkStateNotifier.h"
36 #include "wtf/text/StringBuilder.h"
40 namespace ApplicationCacheAgentState {
41 static const char applicationCacheAgentEnabled[] = "applicationCacheAgentEnabled";
44 InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InspectorPageAgent* pageAgent)
45 : InspectorBaseAgent<InspectorApplicationCacheAgent>("ApplicationCache")
46 , m_pageAgent(pageAgent)
51 void InspectorApplicationCacheAgent::setFrontend(InspectorFrontend* frontend)
53 m_frontend = frontend->applicationcache();
56 void InspectorApplicationCacheAgent::clearFrontend()
58 m_instrumentingAgents->setInspectorApplicationCacheAgent(0);
62 void InspectorApplicationCacheAgent::restore()
64 if (m_state->getBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled)) {
70 void InspectorApplicationCacheAgent::enable(ErrorString*)
72 m_state->setBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled, true);
73 m_instrumentingAgents->setInspectorApplicationCacheAgent(this);
75 // We need to pass initial navigator.onOnline.
76 networkStateChanged(networkStateNotifier().onLine());
79 void InspectorApplicationCacheAgent::updateApplicationCacheStatus(LocalFrame* frame)
81 DocumentLoader* documentLoader = frame->loader().documentLoader();
85 ApplicationCacheHost* host = documentLoader->applicationCacheHost();
86 ApplicationCacheHost::Status status = host->status();
87 ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
89 String manifestURL = info.m_manifest.string();
90 m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status));
93 void InspectorApplicationCacheAgent::networkStateChanged(bool online)
95 m_frontend->networkStateUpdated(online);
98 void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest> >& result)
100 result = TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest>::create();
102 LocalFrame* mainFrame = m_pageAgent->mainFrame();
103 for (Frame* frame = mainFrame; frame; frame = frame->tree().traverseNext(mainFrame)) {
104 if (!frame->isLocalFrame())
106 DocumentLoader* documentLoader = toLocalFrame(frame)->loader().documentLoader();
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(toLocalFrame(frame)))
116 .setManifestURL(manifestURL)
117 .setStatus(static_cast<int>(host->status()));
118 result->addItem(value);
123 DocumentLoader* InspectorApplicationCacheAgent::assertFrameWithDocumentLoader(ErrorString* errorString, String frameId)
125 LocalFrame* frame = m_pageAgent->assertFrame(errorString, frameId);
129 return InspectorPageAgent::assertDocumentLoader(errorString, frame);
132 void InspectorApplicationCacheAgent::getManifestForFrame(ErrorString* errorString, const String& frameId, String* manifestURL)
134 DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
138 ApplicationCacheHost::CacheInfo info = documentLoader->applicationCacheHost()->applicationCacheInfo();
139 *manifestURL = info.m_manifest.string();
142 void InspectorApplicationCacheAgent::getApplicationCacheForFrame(ErrorString* errorString, const String& frameId, RefPtr<TypeBuilder::ApplicationCache::ApplicationCache>& applicationCache)
144 DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
148 ApplicationCacheHost* host = documentLoader->applicationCacheHost();
149 ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
151 ApplicationCacheHost::ResourceInfoList resources;
152 host->fillResourceList(&resources);
154 applicationCache = buildObjectForApplicationCache(resources, info);
157 PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCache> InspectorApplicationCacheAgent::buildObjectForApplicationCache(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources, const ApplicationCacheHost::CacheInfo& applicationCacheInfo)
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))
168 PassRefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > InspectorApplicationCacheAgent::buildArrayForApplicationCacheResources(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources)
170 RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > resources = TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource>::create();
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));
180 PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCacheResource> InspectorApplicationCacheAgent::buildObjectForApplicationCacheResource(const ApplicationCacheHost::ResourceInfo& resourceInfo)
182 StringBuilder builder;
183 if (resourceInfo.m_isMaster)
184 builder.appendLiteral("Master ");
186 if (resourceInfo.m_isManifest)
187 builder.appendLiteral("Manifest ");
189 if (resourceInfo.m_isFallback)
190 builder.appendLiteral("Fallback ");
192 if (resourceInfo.m_isForeign)
193 builder.appendLiteral("Foreign ");
195 if (resourceInfo.m_isExplicit)
196 builder.appendLiteral("Explicit ");
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(builder.toString());
205 void InspectorApplicationCacheAgent::trace(Visitor* visitor)
207 visitor->trace(m_pageAgent);
208 InspectorBaseAgent::trace(visitor);