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