[Win] WTF: Add alias for process id to use in place of direct uses of pid_t
[WebKit-https.git] / Source / JavaScriptCore / inspector / remote / RemoteInspector.h
1 /*
2  * Copyright (C) 2013, 2015, 2016 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 #pragma once
27
28 #if ENABLE(REMOTE_INSPECTOR)
29
30 #include <wtf/Forward.h>
31 #include <wtf/HashMap.h>
32 #include <wtf/Lock.h>
33 #include <wtf/ProcessID.h>
34 #include <wtf/text/WTFString.h>
35
36 #if PLATFORM(COCOA)
37 #include "RemoteInspectorXPCConnection.h"
38 #include <wtf/RetainPtr.h>
39
40 OBJC_CLASS NSDictionary;
41 OBJC_CLASS NSString;
42 typedef RetainPtr<NSDictionary> TargetListing;
43 #endif
44
45 #if USE(GLIB)
46 #include <wtf/glib/GRefPtr.h>
47 typedef GRefPtr<GVariant> TargetListing;
48 typedef struct _GCancellable GCancellable;
49 typedef struct _GDBusConnection GDBusConnection;
50 typedef struct _GDBusInterfaceVTable GDBusInterfaceVTable;
51 #endif
52
53 namespace Inspector {
54
55 class RemoteAutomationTarget;
56 class RemoteConnectionToTarget;
57 class RemoteControllableTarget;
58 class RemoteInspectionTarget;
59 class RemoteInspectorClient;
60
61 class JS_EXPORT_PRIVATE RemoteInspector final
62 #if PLATFORM(COCOA)
63     : public RemoteInspectorXPCConnection::Client
64 #endif
65 {
66 public:
67     class Client {
68     public:
69         struct Capabilities {
70             bool remoteAutomationAllowed : 1;
71             String browserName;
72             String browserVersion;
73         };
74
75         virtual ~Client() { }
76         virtual bool remoteAutomationAllowed() const = 0;
77         virtual String browserName() const { return { }; }
78         virtual String browserVersion() const { return { }; }
79         virtual void requestAutomationSession(const String& sessionIdentifier) = 0;
80 #if PLATFORM(COCOA)
81         virtual void requestAutomationSessionWithCapabilities(NSString *sessionIdentifier, NSDictionary *forwardedCapabilities) = 0;
82 #endif
83     };
84
85     static void startDisabled();
86     static RemoteInspector& singleton();
87     friend class NeverDestroyed<RemoteInspector>;
88
89     void registerTarget(RemoteControllableTarget*);
90     void unregisterTarget(RemoteControllableTarget*);
91     void updateTarget(RemoteControllableTarget*);
92     void sendMessageToRemote(unsigned targetIdentifier, const String& message);
93
94     RemoteInspector::Client* client() const { return m_client; }
95     void setClient(RemoteInspector::Client*);
96     void clientCapabilitiesDidChange();
97     std::optional<RemoteInspector::Client::Capabilities> clientCapabilities() const { return m_clientCapabilities; }
98
99     void setupFailed(unsigned targetIdentifier);
100     void setupCompleted(unsigned targetIdentifier);
101     bool waitingForAutomaticInspection(unsigned targetIdentifier);
102     void updateAutomaticInspectionCandidate(RemoteInspectionTarget*);
103
104     bool enabled() const { return m_enabled; }
105     bool hasActiveDebugSession() const { return m_hasActiveDebugSession; }
106
107     void start();
108     void stop();
109
110 #if PLATFORM(COCOA)
111     bool hasParentProcessInformation() const { return m_parentProcessIdentifier != 0; }
112     ProcessID parentProcessIdentifier() const { return m_parentProcessIdentifier; }
113     RetainPtr<CFDataRef> parentProcessAuditData() const { return m_parentProcessAuditData; }
114     void setParentProcessInformation(ProcessID, RetainPtr<CFDataRef> auditData);
115     void setParentProcessInfomationIsDelayed();
116 #endif
117
118     void updateTargetListing(unsigned targetIdentifier);
119
120 #if USE(GLIB)
121     void requestAutomationSession(const char* sessionID);
122     void setup(unsigned targetIdentifier);
123     void sendMessageToTarget(unsigned targetIdentifier, const char* message);
124 #endif
125
126 private:
127     RemoteInspector();
128
129     unsigned nextAvailableTargetIdentifier();
130
131     enum class StopSource { API, XPCMessage };
132     void stopInternal(StopSource);
133
134 #if PLATFORM(COCOA)
135     void setupXPCConnectionIfNeeded();
136 #endif
137 #if USE(GLIB)
138     void setupConnection(GRefPtr<GDBusConnection>&&);
139     static const GDBusInterfaceVTable s_interfaceVTable;
140
141     void receivedGetTargetListMessage();
142     void receivedSetupMessage(unsigned targetIdentifier);
143     void receivedDataMessage(unsigned targetIdentifier, const char* message);
144     void receivedCloseMessage(unsigned targetIdentifier);
145     void receivedAutomationSessionRequestMessage(const char* sessionID);
146 #endif
147
148     TargetListing listingForTarget(const RemoteControllableTarget&) const;
149     TargetListing listingForInspectionTarget(const RemoteInspectionTarget&) const;
150     TargetListing listingForAutomationTarget(const RemoteAutomationTarget&) const;
151
152     void pushListingsNow();
153     void pushListingsSoon();
154
155     void updateTargetListing(const RemoteControllableTarget&);
156
157     void updateHasActiveDebugSession();
158     void updateClientCapabilities();
159
160     void sendAutomaticInspectionCandidateMessage();
161
162 #if PLATFORM(COCOA)
163     void xpcConnectionReceivedMessage(RemoteInspectorXPCConnection*, NSString *messageName, NSDictionary *userInfo) override;
164     void xpcConnectionFailed(RemoteInspectorXPCConnection*) override;
165     void xpcConnectionUnhandledMessage(RemoteInspectorXPCConnection*, xpc_object_t) override;
166
167     void receivedSetupMessage(NSDictionary *userInfo);
168     void receivedDataMessage(NSDictionary *userInfo);
169     void receivedDidCloseMessage(NSDictionary *userInfo);
170     void receivedGetListingMessage(NSDictionary *userInfo);
171     void receivedIndicateMessage(NSDictionary *userInfo);
172     void receivedProxyApplicationSetupMessage(NSDictionary *userInfo);
173     void receivedConnectionDiedMessage(NSDictionary *userInfo);
174     void receivedAutomaticInspectionConfigurationMessage(NSDictionary *userInfo);
175     void receivedAutomaticInspectionRejectMessage(NSDictionary *userInfo);
176     void receivedAutomationSessionRequestMessage(NSDictionary *userInfo);
177 #endif
178
179     static bool startEnabled;
180
181     // Targets can be registered from any thread at any time.
182     // Any target can send messages over the XPC connection.
183     // So lock access to all maps and state as they can change
184     // from any thread.
185     Lock m_mutex;
186
187     HashMap<unsigned, RemoteControllableTarget*> m_targetMap;
188     HashMap<unsigned, RefPtr<RemoteConnectionToTarget>> m_targetConnectionMap;
189     HashMap<unsigned, TargetListing> m_targetListingMap;
190
191 #if PLATFORM(COCOA)
192     RefPtr<RemoteInspectorXPCConnection> m_relayConnection;
193 #endif
194 #if USE(GLIB)
195     GRefPtr<GDBusConnection> m_dbusConnection;
196     GRefPtr<GCancellable> m_cancellable;
197 #endif
198
199     RemoteInspector::Client* m_client { nullptr };
200     std::optional<RemoteInspector::Client::Capabilities> m_clientCapabilities;
201
202 #if PLATFORM(COCOA)
203     dispatch_queue_t m_xpcQueue;
204 #endif
205     unsigned m_nextAvailableTargetIdentifier { 1 };
206     int m_notifyToken { 0 };
207     bool m_enabled { false };
208     bool m_hasActiveDebugSession { false };
209     bool m_pushScheduled { false };
210
211     ProcessID m_parentProcessIdentifier { 0 };
212 #if PLATFORM(COCOA)
213     RetainPtr<CFDataRef> m_parentProcessAuditData;
214 #endif
215     bool m_shouldSendParentProcessInformation { false };
216     bool m_automaticInspectionEnabled { false };
217     bool m_automaticInspectionPaused { false };
218     unsigned m_automaticInspectionCandidateTargetIdentifier { 0 };
219 };
220
221 } // namespace Inspector
222
223 #endif // ENABLE(REMOTE_INSPECTOR)