<https://webkit.org/b/119859> Frame::loader() should return a reference
[WebKit-https.git] / Source / WebCore / inspector / InspectorApplicationCacheAgent.cpp
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
28 #if ENABLE(INSPECTOR)
29
30 #include "InspectorApplicationCacheAgent.h"
31
32 #include "ApplicationCacheHost.h"
33 #include "DocumentLoader.h"
34 #include "Frame.h"
35 #include "FrameLoader.h"
36 #include "InspectorAgent.h"
37 #include "InspectorFrontend.h"
38 #include "InspectorPageAgent.h"
39 #include "InspectorState.h"
40 #include "InspectorValues.h"
41 #include "InstrumentingAgents.h"
42 #include "NetworkStateNotifier.h"
43 #include "Page.h"
44 #include "ResourceResponse.h"
45
46 namespace WebCore {
47
48 namespace ApplicationCacheAgentState {
49 static const char applicationCacheAgentEnabled[] = "applicationCacheAgentEnabled";
50 }
51
52 InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InspectorPageAgent* pageAgent)
53     : InspectorBaseAgent<InspectorApplicationCacheAgent>(ASCIILiteral("ApplicationCache"), instrumentingAgents, state)
54     , m_pageAgent(pageAgent)
55     , m_frontend(0)
56 {
57 }
58
59 void InspectorApplicationCacheAgent::setFrontend(InspectorFrontend* frontend)
60 {
61     m_frontend = frontend->applicationcache();
62 }
63
64 void InspectorApplicationCacheAgent::clearFrontend()
65 {
66     m_instrumentingAgents->setInspectorApplicationCacheAgent(0);
67     m_frontend = 0;
68 }
69
70 void InspectorApplicationCacheAgent::restore()
71 {
72     if (m_state->getBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled)) {
73         ErrorString error;
74         enable(&error);
75     }
76 }
77
78 void InspectorApplicationCacheAgent::enable(ErrorString*)
79 {
80     m_state->setBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled, true);
81     m_instrumentingAgents->setInspectorApplicationCacheAgent(this);
82
83     // We need to pass initial navigator.onOnline.
84     networkStateChanged();
85 }
86
87 void InspectorApplicationCacheAgent::updateApplicationCacheStatus(Frame* frame)
88 {
89     DocumentLoader* documentLoader = frame->loader().documentLoader();
90     if (!documentLoader)
91         return;
92
93     ApplicationCacheHost* host = documentLoader->applicationCacheHost();
94     ApplicationCacheHost::Status status = host->status();
95     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
96
97     String manifestURL = info.m_manifest.string();
98     m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status));
99 }
100
101 void InspectorApplicationCacheAgent::networkStateChanged()
102 {
103     bool isNowOnline = networkStateNotifier().onLine();
104     m_frontend->networkStateUpdated(isNowOnline);
105 }
106
107 void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest> >& result)
108 {
109     result = TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest>::create();
110
111     Frame* mainFrame = m_pageAgent->mainFrame();
112     for (Frame* frame = mainFrame; frame; frame = frame->tree()->traverseNext(mainFrame)) {
113         DocumentLoader* documentLoader = frame->loader().documentLoader();
114         if (!documentLoader)
115             continue;
116
117         ApplicationCacheHost* host = documentLoader->applicationCacheHost();
118         ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
119         String manifestURL = info.m_manifest.string();
120         if (!manifestURL.isEmpty()) {
121             RefPtr<TypeBuilder::ApplicationCache::FrameWithManifest> value = TypeBuilder::ApplicationCache::FrameWithManifest::create()
122                 .setFrameId(m_pageAgent->frameId(frame))
123                 .setManifestURL(manifestURL)
124                 .setStatus(static_cast<int>(host->status()));
125             result->addItem(value);
126         }
127     }
128 }
129
130 DocumentLoader* InspectorApplicationCacheAgent::assertFrameWithDocumentLoader(ErrorString* errorString, String frameId)
131 {
132     Frame* frame = m_pageAgent->assertFrame(errorString, frameId);
133     if (!frame)
134         return 0;
135
136     return InspectorPageAgent::assertDocumentLoader(errorString, frame);
137 }
138
139 void InspectorApplicationCacheAgent::getManifestForFrame(ErrorString* errorString, const String& frameId, String* manifestURL)
140 {
141     DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
142     if (!documentLoader)
143         return;
144
145     ApplicationCacheHost::CacheInfo info = documentLoader->applicationCacheHost()->applicationCacheInfo();
146     *manifestURL = info.m_manifest.string();
147 }
148
149 void InspectorApplicationCacheAgent::getApplicationCacheForFrame(ErrorString* errorString, const String& frameId, RefPtr<TypeBuilder::ApplicationCache::ApplicationCache>& applicationCache)
150 {
151     DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
152     if (!documentLoader)
153         return;
154
155     ApplicationCacheHost* host = documentLoader->applicationCacheHost();
156     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
157
158     ApplicationCacheHost::ResourceInfoList resources;
159     host->fillResourceList(&resources);
160
161     applicationCache = buildObjectForApplicationCache(resources, info);
162 }
163
164 PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCache> InspectorApplicationCacheAgent::buildObjectForApplicationCache(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources, const ApplicationCacheHost::CacheInfo& applicationCacheInfo)
165 {
166     return TypeBuilder::ApplicationCache::ApplicationCache::create()
167         .setManifestURL(applicationCacheInfo.m_manifest.string())
168         .setSize(applicationCacheInfo.m_size)
169         .setCreationTime(applicationCacheInfo.m_creationTime)
170         .setUpdateTime(applicationCacheInfo.m_updateTime)
171         .setResources(buildArrayForApplicationCacheResources(applicationCacheResources))
172         .release();
173 }
174
175 PassRefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > InspectorApplicationCacheAgent::buildArrayForApplicationCacheResources(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources)
176 {
177     RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource> > resources = TypeBuilder::Array<TypeBuilder::ApplicationCache::ApplicationCacheResource>::create();
178
179     ApplicationCacheHost::ResourceInfoList::const_iterator end = applicationCacheResources.end();
180     ApplicationCacheHost::ResourceInfoList::const_iterator it = applicationCacheResources.begin();
181     for (int i = 0; it != end; ++it, i++)
182         resources->addItem(buildObjectForApplicationCacheResource(*it));
183
184     return resources;
185 }
186
187 PassRefPtr<TypeBuilder::ApplicationCache::ApplicationCacheResource> InspectorApplicationCacheAgent::buildObjectForApplicationCacheResource(const ApplicationCacheHost::ResourceInfo& resourceInfo)
188 {
189     String types;
190     if (resourceInfo.m_isMaster)
191         types.append("Master ");
192
193     if (resourceInfo.m_isManifest)
194         types.append("Manifest ");
195
196     if (resourceInfo.m_isFallback)
197         types.append("Fallback ");
198
199     if (resourceInfo.m_isForeign)
200         types.append("Foreign ");
201
202     if (resourceInfo.m_isExplicit)
203         types.append("Explicit ");
204
205     RefPtr<TypeBuilder::ApplicationCache::ApplicationCacheResource> value = TypeBuilder::ApplicationCache::ApplicationCacheResource::create()
206         .setUrl(resourceInfo.m_resource.string())
207         .setSize(static_cast<int>(resourceInfo.m_size))
208         .setType(types);
209     return value;
210 }
211
212 } // namespace WebCore
213
214 #endif // ENABLE(INSPECTOR)