- add third_party src.
[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 "InspectorFrontend.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/loader/appcache/ApplicationCacheHost.h"
36 #include "core/frame/Frame.h"
37 #include "core/page/NetworkStateNotifier.h"
38 #include "wtf/text/StringBuilder.h"
39
40 namespace WebCore {
41
42 namespace ApplicationCacheAgentState {
43 static const char applicationCacheAgentEnabled[] = "applicationCacheAgentEnabled";
44 }
45
46 InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InspectorPageAgent* pageAgent)
47     : InspectorBaseAgent<InspectorApplicationCacheAgent>("ApplicationCache", instrumentingAgents, state)
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::restore()
65 {
66     if (m_state->getBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled)) {
67         ErrorString error;
68         enable(&error);
69     }
70 }
71
72 void InspectorApplicationCacheAgent::enable(ErrorString*)
73 {
74     m_state->setBoolean(ApplicationCacheAgentState::applicationCacheAgentEnabled, true);
75     m_instrumentingAgents->setInspectorApplicationCacheAgent(this);
76
77     // We need to pass initial navigator.onOnline.
78     networkStateChanged(networkStateNotifier().onLine());
79 }
80
81 void InspectorApplicationCacheAgent::updateApplicationCacheStatus(Frame* frame)
82 {
83     DocumentLoader* documentLoader = frame->loader().documentLoader();
84     if (!documentLoader)
85         return;
86
87     ApplicationCacheHost* host = documentLoader->applicationCacheHost();
88     ApplicationCacheHost::Status status = host->status();
89     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
90
91     String manifestURL = info.m_manifest.string();
92     m_frontend->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status));
93 }
94
95 void InspectorApplicationCacheAgent::networkStateChanged(bool online)
96 {
97     m_frontend->networkStateUpdated(online);
98 }
99
100 void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest> >& result)
101 {
102     result = TypeBuilder::Array<TypeBuilder::ApplicationCache::FrameWithManifest>::create();
103
104     Frame* mainFrame = m_pageAgent->mainFrame();
105     for (Frame* frame = mainFrame; frame; frame = frame->tree().traverseNext(mainFrame)) {
106         DocumentLoader* documentLoader = 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(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     Frame* 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