1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "PageNetworkAgent.h"
28
29#include "Document.h"
30#include "DocumentLoader.h"
31#include "Frame.h"
32#include "InstrumentingAgents.h"
33#include "Page.h"
34#include "ScriptState.h"
35#include "WebSocket.h"
36#include "WebSocketChannel.h"
37
38namespace WebCore {
39
40using namespace Inspector;
41
42PageNetworkAgent::PageNetworkAgent(PageAgentContext& context)
43 : InspectorNetworkAgent(context)
44 , m_inspectedPage(context.inspectedPage)
45{
46}
47
48String PageNetworkAgent::loaderIdentifier(DocumentLoader* loader)
49{
50 if (loader) {
51 if (auto* pageAgent = m_instrumentingAgents.inspectorPageAgent())
52 return pageAgent->loaderId(loader);
53 }
54 return { };
55}
56
57String PageNetworkAgent::frameIdentifier(DocumentLoader* loader)
58{
59 if (loader) {
60 if (auto* pageAgent = m_instrumentingAgents.inspectorPageAgent())
61 return pageAgent->frameId(loader->frame());
62 }
63 return { };
64}
65
66Vector<WebSocket*> PageNetworkAgent::activeWebSockets(const LockHolder& lock)
67{
68 Vector<WebSocket*> webSockets;
69
70 for (WebSocket* webSocket : WebSocket::allActiveWebSockets(lock)) {
71 if (!is<WebSocketChannel>(webSocket->channel().get()))
72 continue;
73
74 auto* channel = downcast<WebSocketChannel>(webSocket->channel().get());
75 if (!channel)
76 continue;
77
78 if (!channel->hasCreatedHandshake())
79 continue;
80
81 if (!is<Document>(webSocket->scriptExecutionContext()))
82 continue;
83
84 // FIXME: <https://webkit.org/b/168475> Web Inspector: Correctly display iframe's WebSockets
85 auto* document = downcast<Document>(webSocket->scriptExecutionContext());
86 if (document->page() != &m_inspectedPage)
87 continue;
88
89 webSockets.append(webSocket);
90 }
91
92 return webSockets;
93}
94
95void PageNetworkAgent::setResourceCachingDisabled(bool disabled)
96{
97 m_inspectedPage.setResourceCachingDisabledOverride(disabled);
98}
99
100ScriptExecutionContext* PageNetworkAgent::scriptExecutionContext(ErrorString& errorString, const String& frameId)
101{
102 auto* pageAgent = m_instrumentingAgents.inspectorPageAgent();
103 if (!pageAgent) {
104 errorString = "Missing Page agent"_s;
105 return nullptr;
106 }
107
108 auto* frame = pageAgent->assertFrame(errorString, frameId);
109 if (!frame)
110 return nullptr;
111
112 auto* document = frame->document();
113 if (!document) {
114 errorString = "No Document instance for the specified frame"_s;
115 return nullptr;
116 }
117
118 return document;
119}
120
121} // namespace WebCore
122