1/*
2 * Copyright (C) 2018 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "InspectorTargetAgent.h"
28
29#include "InspectorTarget.h"
30
31namespace Inspector {
32
33InspectorTargetAgent::InspectorTargetAgent(FrontendRouter& frontendRouter, BackendDispatcher& backendDispatcher)
34 : InspectorAgentBase("Target"_s)
35 , m_frontendDispatcher(std::make_unique<TargetFrontendDispatcher>(frontendRouter))
36 , m_backendDispatcher(TargetBackendDispatcher::create(backendDispatcher, this))
37{
38}
39
40void InspectorTargetAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
41{
42 m_isConnected = true;
43
44 connectToTargets();
45}
46
47void InspectorTargetAgent::willDestroyFrontendAndBackend(DisconnectReason)
48{
49 disconnectFromTargets();
50
51 m_isConnected = false;
52}
53
54void InspectorTargetAgent::exists(ErrorString&)
55{
56 // Intentionally do nothing to return success.
57 // FIXME: Remove this when the local inspector has switched over to the modern path.
58}
59
60void InspectorTargetAgent::sendMessageToTarget(ErrorString& errorString, const String& targetId, const String& message)
61{
62 InspectorTarget* target = m_targets.get(targetId);
63 if (!target) {
64 errorString = "Target not found."_s;
65 return;
66 }
67
68 target->sendMessageToTargetBackend(message);
69}
70
71void InspectorTargetAgent::sendMessageFromTargetToFrontend(const String& targetId, const String& message)
72{
73 ASSERT_WITH_MESSAGE(m_targets.get(targetId), "Sending a message from an untracked target to the frontend.");
74
75 m_frontendDispatcher->dispatchMessageFromTarget(targetId, message);
76}
77
78static Protocol::Target::TargetInfo::Type targetTypeToProtocolType(InspectorTargetType type)
79{
80 switch (type) {
81 case InspectorTargetType::JavaScriptContext:
82 return Protocol::Target::TargetInfo::Type::JavaScript;
83 case InspectorTargetType::Page:
84 return Protocol::Target::TargetInfo::Type::Page;
85 case InspectorTargetType::DedicatedWorker:
86 return Protocol::Target::TargetInfo::Type::Worker;
87 case InspectorTargetType::ServiceWorker:
88 return Protocol::Target::TargetInfo::Type::ServiceWorker;
89 }
90
91 ASSERT_NOT_REACHED();
92 return Protocol::Target::TargetInfo::Type::JavaScript;
93}
94
95static Ref<Protocol::Target::TargetInfo> buildTargetInfoObject(const InspectorTarget& target)
96{
97 return Protocol::Target::TargetInfo::create()
98 .setTargetId(target.identifier())
99 .setType(targetTypeToProtocolType(target.type()))
100 .release();
101}
102
103void InspectorTargetAgent::targetCreated(InspectorTarget& target)
104{
105 auto addResult = m_targets.set(target.identifier(), &target);
106 ASSERT_UNUSED(addResult, addResult.isNewEntry);
107
108 if (!m_isConnected)
109 return;
110
111 target.connect(frontendChannel());
112
113 m_frontendDispatcher->targetCreated(buildTargetInfoObject(target));
114}
115
116void InspectorTargetAgent::targetDestroyed(InspectorTarget& target)
117{
118 m_targets.remove(target.identifier());
119
120 if (!m_isConnected)
121 return;
122
123 target.disconnect(frontendChannel());
124
125 m_frontendDispatcher->targetDestroyed(target.identifier());
126}
127
128void InspectorTargetAgent::connectToTargets()
129{
130 auto& channel = frontendChannel();
131
132 for (InspectorTarget* target : m_targets.values()) {
133 target->connect(channel);
134 m_frontendDispatcher->targetCreated(buildTargetInfoObject(*target));
135 }
136}
137
138void InspectorTargetAgent::disconnectFromTargets()
139{
140 auto& channel = frontendChannel();
141
142 for (InspectorTarget* target : m_targets.values())
143 target->disconnect(channel);
144}
145
146} // namespace Inspector
147