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