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) && ENABLE(OFFLINE_WEB_APPLICATIONS)
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 "InspectorValues.h"
38 #include "InstrumentingAgents.h"
39 #include "NetworkStateNotifier.h"
40 #include "Page.h"
41 #include "ResourceResponse.h"
42 
43 namespace WebCore {
44 
InspectorApplicationCacheAgent(InstrumentingAgents * instrumentingAgents,Page * inspectedPage)45 InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InstrumentingAgents* instrumentingAgents, Page* inspectedPage)
46     : m_instrumentingAgents(instrumentingAgents)
47     , m_inspectedPage(inspectedPage)
48     , m_frontend(0)
49 {
50 }
51 
setFrontend(InspectorFrontend * frontend)52 void InspectorApplicationCacheAgent::setFrontend(InspectorFrontend* frontend)
53 {
54     m_frontend = frontend->applicationcache();
55     m_instrumentingAgents->setInspectorApplicationCacheAgent(this);
56 }
57 
clearFrontend()58 void InspectorApplicationCacheAgent::clearFrontend()
59 {
60     m_instrumentingAgents->setInspectorApplicationCacheAgent(0);
61     m_frontend = 0;
62 }
63 
updateApplicationCacheStatus(Frame * frame)64 void InspectorApplicationCacheAgent::updateApplicationCacheStatus(Frame* frame)
65 {
66     ApplicationCacheHost::Status status = frame->loader()->documentLoader()->applicationCacheHost()->status();
67     m_frontend->updateApplicationCacheStatus(status);
68 }
69 
networkStateChanged()70 void InspectorApplicationCacheAgent::networkStateChanged()
71 {
72     bool isNowOnline = networkStateNotifier().onLine();
73     m_frontend->updateNetworkState(isNowOnline);
74 }
75 
getApplicationCaches(ErrorString *,RefPtr<InspectorObject> * applicationCaches)76 void InspectorApplicationCacheAgent::getApplicationCaches(ErrorString*, RefPtr<InspectorObject>* applicationCaches)
77 {
78     DocumentLoader* documentLoader = m_inspectedPage->mainFrame()->loader()->documentLoader();
79     if (!documentLoader)
80         return;
81     ApplicationCacheHost* host = documentLoader->applicationCacheHost();
82     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
83 
84     ApplicationCacheHost::ResourceInfoList resources;
85     host->fillResourceList(&resources);
86     *applicationCaches = buildObjectForApplicationCache(resources, info);
87 }
88 
buildObjectForApplicationCache(const ApplicationCacheHost::ResourceInfoList & applicationCacheResources,const ApplicationCacheHost::CacheInfo & applicationCacheInfo)89 PassRefPtr<InspectorObject> InspectorApplicationCacheAgent::buildObjectForApplicationCache(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources, const ApplicationCacheHost::CacheInfo& applicationCacheInfo)
90 {
91     RefPtr<InspectorObject> value = InspectorObject::create();
92     value->setNumber("size", applicationCacheInfo.m_size);
93     value->setString("manifest", applicationCacheInfo.m_manifest.string());
94     value->setString("lastPathComponent", applicationCacheInfo.m_manifest.lastPathComponent());
95     value->setNumber("creationTime", applicationCacheInfo.m_creationTime);
96     value->setNumber("updateTime", applicationCacheInfo.m_updateTime);
97     value->setArray("resources", buildArrayForApplicationCacheResources(applicationCacheResources));
98     return value;
99 }
100 
buildArrayForApplicationCacheResources(const ApplicationCacheHost::ResourceInfoList & applicationCacheResources)101 PassRefPtr<InspectorArray> InspectorApplicationCacheAgent::buildArrayForApplicationCacheResources(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources)
102 {
103     RefPtr<InspectorArray> resources = InspectorArray::create();
104 
105     ApplicationCacheHost::ResourceInfoList::const_iterator end = applicationCacheResources.end();
106     ApplicationCacheHost::ResourceInfoList::const_iterator it = applicationCacheResources.begin();
107     for (int i = 0; it != end; ++it, i++)
108         resources->pushObject(buildObjectForApplicationCacheResource(*it));
109 
110     return resources;
111 }
112 
buildObjectForApplicationCacheResource(const ApplicationCacheHost::ResourceInfo & resourceInfo)113 PassRefPtr<InspectorObject> InspectorApplicationCacheAgent::buildObjectForApplicationCacheResource(const ApplicationCacheHost::ResourceInfo& resourceInfo)
114 {
115     RefPtr<InspectorObject> value = InspectorObject::create();
116     value->setString("name", resourceInfo.m_resource.string());
117     value->setNumber("size", resourceInfo.m_size);
118 
119     String types;
120     if (resourceInfo.m_isMaster)
121         types.append("Master ");
122 
123     if (resourceInfo.m_isManifest)
124         types.append("Manifest ");
125 
126     if (resourceInfo.m_isFallback)
127         types.append("Fallback ");
128 
129     if (resourceInfo.m_isForeign)
130         types.append("Foreign ");
131 
132     if (resourceInfo.m_isExplicit)
133         types.append("Explicit ");
134 
135     value->setString("type", types);
136     return value;
137 }
138 
139 } // namespace WebCore
140 
141 #endif // ENABLE(INSPECTOR) && ENABLE(OFFLINE_WEB_APPLICATIONS)
142