Web Inspector: agents should send messages through FrontendRouter instead of Frontend...
[WebKit-https.git] / Source / WebCore / inspector / InspectorApplicationCacheAgent.cpp
1 /*
2  * Copyright (C) 2010, 2015 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 #include "ApplicationCacheHost.h"
30 #include "DocumentLoader.h"
31 #include "Frame.h"
32 #include "FrameLoader.h"
33 #include "InspectorPageAgent.h"
34 #include "InstrumentingAgents.h"
35 #include "NetworkStateNotifier.h"
36 #include "Page.h"
37 #include "ResourceResponse.h"
38 #include <inspector/InspectorFrontendDispatchers.h>
39 #include <inspector/InspectorValues.h>
40 #include <wtf/text/StringBuilder.h>
41
42 using namespace Inspector;
43
44 namespace WebCore {
45
46 InspectorApplicationCacheAgent::InspectorApplicationCacheAgent(InstrumentingAgents& instrumentingAgents, InspectorPageAgent* pageAgent)
47     : InspectorAgentBase(ASCIILiteral("ApplicationCache"), instrumentingAgents)
48     , m_pageAgent(pageAgent)
49 {
50 }
51
52 void InspectorApplicationCacheAgent::didCreateFrontendAndBackend(FrontendRouter* frontendRouter, BackendDispatcher* backendDispatcher)
53 {
54     m_frontendDispatcher = std::make_unique<Inspector::ApplicationCacheFrontendDispatcher>(frontendRouter);
55     m_backendDispatcher = Inspector::ApplicationCacheBackendDispatcher::create(backendDispatcher, this);
56 }
57
58 void InspectorApplicationCacheAgent::willDestroyFrontendAndBackend(Inspector::DisconnectReason)
59 {
60     m_frontendDispatcher = nullptr;
61     m_backendDispatcher = nullptr;
62
63     m_instrumentingAgents.setInspectorApplicationCacheAgent(nullptr);
64 }
65
66 void InspectorApplicationCacheAgent::enable(ErrorString&)
67 {
68     m_instrumentingAgents.setInspectorApplicationCacheAgent(this);
69
70     // We need to pass initial navigator.onOnline.
71     networkStateChanged();
72 }
73
74 void InspectorApplicationCacheAgent::updateApplicationCacheStatus(Frame* frame)
75 {
76     DocumentLoader* documentLoader = frame->loader().documentLoader();
77     if (!documentLoader)
78         return;
79
80     ApplicationCacheHost* host = documentLoader->applicationCacheHost();
81     ApplicationCacheHost::Status status = host->status();
82     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
83
84     String manifestURL = info.m_manifest.string();
85     m_frontendDispatcher->applicationCacheStatusUpdated(m_pageAgent->frameId(frame), manifestURL, static_cast<int>(status));
86 }
87
88 void InspectorApplicationCacheAgent::networkStateChanged()
89 {
90     bool isNowOnline = networkStateNotifier().onLine();
91     m_frontendDispatcher->networkStateUpdated(isNowOnline);
92 }
93
94 void InspectorApplicationCacheAgent::getFramesWithManifests(ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::ApplicationCache::FrameWithManifest>>& result)
95 {
96     result = Inspector::Protocol::Array<Inspector::Protocol::ApplicationCache::FrameWithManifest>::create();
97
98     for (Frame* frame = m_pageAgent->mainFrame(); frame; frame = frame->tree().traverseNext()) {
99         DocumentLoader* documentLoader = frame->loader().documentLoader();
100         if (!documentLoader)
101             continue;
102
103         ApplicationCacheHost* host = documentLoader->applicationCacheHost();
104         ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
105         String manifestURL = info.m_manifest.string();
106         if (!manifestURL.isEmpty()) {
107             Ref<Inspector::Protocol::ApplicationCache::FrameWithManifest> value = Inspector::Protocol::ApplicationCache::FrameWithManifest::create()
108                 .setFrameId(m_pageAgent->frameId(frame))
109                 .setManifestURL(manifestURL)
110                 .setStatus(static_cast<int>(host->status()))
111                 .release();
112             result->addItem(WTF::move(value));
113         }
114     }
115 }
116
117 DocumentLoader* InspectorApplicationCacheAgent::assertFrameWithDocumentLoader(ErrorString& errorString, const String& frameId)
118 {
119     Frame* frame = m_pageAgent->assertFrame(errorString, frameId);
120     if (!frame)
121         return nullptr;
122
123     return InspectorPageAgent::assertDocumentLoader(errorString, frame);
124 }
125
126 void InspectorApplicationCacheAgent::getManifestForFrame(ErrorString& errorString, const String& frameId, String* manifestURL)
127 {
128     DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
129     if (!documentLoader)
130         return;
131
132     ApplicationCacheHost::CacheInfo info = documentLoader->applicationCacheHost()->applicationCacheInfo();
133     *manifestURL = info.m_manifest.string();
134 }
135
136 void InspectorApplicationCacheAgent::getApplicationCacheForFrame(ErrorString& errorString, const String& frameId, RefPtr<Inspector::Protocol::ApplicationCache::ApplicationCache>& applicationCache)
137 {
138     DocumentLoader* documentLoader = assertFrameWithDocumentLoader(errorString, frameId);
139     if (!documentLoader)
140         return;
141
142     ApplicationCacheHost* host = documentLoader->applicationCacheHost();
143     ApplicationCacheHost::CacheInfo info = host->applicationCacheInfo();
144
145     ApplicationCacheHost::ResourceInfoList resources;
146     host->fillResourceList(&resources);
147
148     applicationCache = buildObjectForApplicationCache(resources, info);
149 }
150
151 Ref<Inspector::Protocol::ApplicationCache::ApplicationCache> InspectorApplicationCacheAgent::buildObjectForApplicationCache(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources, const ApplicationCacheHost::CacheInfo& applicationCacheInfo)
152 {
153     return Inspector::Protocol::ApplicationCache::ApplicationCache::create()
154         .setManifestURL(applicationCacheInfo.m_manifest.string())
155         .setSize(applicationCacheInfo.m_size)
156         .setCreationTime(applicationCacheInfo.m_creationTime)
157         .setUpdateTime(applicationCacheInfo.m_updateTime)
158         .setResources(buildArrayForApplicationCacheResources(applicationCacheResources))
159         .release();
160 }
161
162 Ref<Inspector::Protocol::Array<Inspector::Protocol::ApplicationCache::ApplicationCacheResource>> InspectorApplicationCacheAgent::buildArrayForApplicationCacheResources(const ApplicationCacheHost::ResourceInfoList& applicationCacheResources)
163 {
164     auto resources = Inspector::Protocol::Array<Inspector::Protocol::ApplicationCache::ApplicationCacheResource>::create();
165
166     for (const auto& resourceInfo : applicationCacheResources)
167         resources->addItem(buildObjectForApplicationCacheResource(resourceInfo));
168
169     return WTF::move(resources);
170 }
171
172 Ref<Inspector::Protocol::ApplicationCache::ApplicationCacheResource> InspectorApplicationCacheAgent::buildObjectForApplicationCacheResource(const ApplicationCacheHost::ResourceInfo& resourceInfo)
173 {
174     StringBuilder types;
175
176     if (resourceInfo.m_isMaster)
177         types.appendLiteral("Master ");
178
179     if (resourceInfo.m_isManifest)
180         types.appendLiteral("Manifest ");
181
182     if (resourceInfo.m_isFallback)
183         types.appendLiteral("Fallback ");
184
185     if (resourceInfo.m_isForeign)
186         types.appendLiteral("Foreign ");
187
188     if (resourceInfo.m_isExplicit)
189         types.appendLiteral("Explicit ");
190
191     return Inspector::Protocol::ApplicationCache::ApplicationCacheResource::create()
192         .setUrl(resourceInfo.m_resource.string())
193         .setSize(static_cast<int>(resourceInfo.m_size))
194         .setType(types.toString())
195         .release();
196 }
197
198 } // namespace WebCore