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