Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/inspector/InspectorApplicationCacheAgent.h"
28
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"
37
38 namespace WebCore {
39
40 namespace ApplicationCacheAgentState {
41 static const char applicationCacheAgentEnabled[] = "applicationCacheAgentEnabled";
42 }
43
44 InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InspectorPageAgent* pageAgent)
45     : InspectorBaseAgent<InspectorApplicationCacheAgent>("ApplicationCache")
46     , m_pageAgent(pageAgent)
47     , m_frontend(0)
48 {
49 }
50
51 void InspectorApplicationCacheAgent::setFrontend(InspectorFrontend* frontend)
52 {
53     m_frontend = frontend->applicationcache();
54 }
55
56 void InspectorApplicationCacheAgent::clearFrontend()
57 {
58     m_instrumentingAgents->setInspectorApplicationCacheAgent(0);
59     m_frontend = 0;
60 }
61
62 void InspectorApplicationCacheAgent::restore()
63 {
64     if (m_state->getBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled)) {
65         ErrorString error;
66         enable(&error);
67     }
68 }
69
70 void InspectorApplicationCacheAgent::enable(ErrorString*)
71 {
72     m_state->setBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled, true);
73     m_instrumentingAgents->setInspectorApplicationCacheAgent(this);
74
75     // We need to pass initial navigator.onOnline.
76     networkStateChanged(networkStateNotifier().onLine());
77 }
78
79 void InspectorApplicationCacheAgent::updateApplicationCacheStatus(LocalFrame* frame)
80 {
81     DocumentLoader* documentLoader = frame->loader().documentLoader();
82     if (!documentLoader)
83         return;
84
85     ApplicationCacheHost* host = documentLoader->applicationCacheHost();
86     ApplicationCacheHost::Status status = host->status();
87     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
88
89     String manifestURL = info.m_manifest.string();
90     m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status));
91 }
92
93 void InspectorApplicationCacheAgent::networkStateChanged(bool online)
94 {
95     m_frontend->networkStateUpdated(online);
96 }
97
98 void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest> >& result)
99 {
100     result = TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest>::create();
101
102     LocalFrame* mainFrame = m_pageAgent->mainFrame();
103     for (Frame* frame = mainFrame; frame; frame = frame->tree().traverseNext(mainFrame)) {
104         if (!frame->isLocalFrame())
105             continue;
106         DocumentLoader* documentLoader = toLocalFrame(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(toLocalFrame(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     LocalFrame* 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     StringBuilder builder;
183     if (resourceInfo.m_isMaster)
184         builder.append("Master ");
185
186     if (resourceInfo.m_isManifest)
187         builder.append("Manifest ");
188
189     if (resourceInfo.m_isFallback)
190         builder.append("Fallback ");
191
192     if (resourceInfo.m_isForeign)
193         builder.append("Foreign ");
194
195     if (resourceInfo.m_isExplicit)
196         builder.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(builder.toString());
202     return value;
203 }
204
205 } // namespace WebCore
206