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