207c18664b4e3b3fcb93091a0a8e87b5a5566052
[WebKit.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 "RemoteControllableTarget.h"
31
32 #include <utility>
33 #include <wtf/Forward.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/Lock.h>
36 #include <wtf/ProcessID.h>
37 #include <wtf/text/WTFString.h>
38
39 #if PLATFORM(COCOA)
40 #include "RemoteInspectorXPCConnection.h"
41 #include <wtf/RetainPtr.h>
42
43 OBJC_CLASS NSDictionary;
44 OBJC_CLASS NSString;
45 typedef RetainPtr<NSDictionary> TargetListing;
46 #endif
47
48 #if USE(GLIB)
49 #include <wtf/glib/GRefPtr.h>
50 typedef GRefPtr<GVariant> TargetListing;
51 typedef struct _GCancellable GCancellable;
52 typedef struct _GDBusConnection GDBusConnection;
53 typedef struct _GDBusInterfaceVTable GDBusInterfaceVTable;
54 #endif
55
56 #if USE(INSPECTOR_SOCKET_SERVER)
57 #include "RemoteConnectionToTarget.h"
58 #include "RemoteInspectorConnectionClient.h"
59 #include <wtf/JSONValues.h>
60 #include <wtf/RefCounted.h>
61 #include <wtf/RefPtr.h>
62
63 namespace Inspector {
64 using TargetListing = RefPtr<JSON::Object>;
65 }
66 #endif
67
68 namespace Inspector {
69
70 class RemoteAutomationTarget;
71 class RemoteConnectionToTarget;
72 class RemoteControllableTarget;
73 class RemoteInspectionTarget;
74 class RemoteInspectorClient;
75
76 class JS_EXPORT_PRIVATE RemoteInspector final
77 #if PLATFORM(COCOA)
78     : public RemoteInspectorXPCConnection::Client
79 #elif USE(INSPECTOR_SOCKET_SERVER)
80     : public RemoteInspectorConnectionClient
81 #endif
82 {
83 public:
84     class Client {
85     public:
86         struct Capabilities {
87             bool remoteAutomationAllowed : 1;
88             String browserName;
89             String browserVersion;
90         };
91
92         struct SessionCapabilities {
93             bool acceptInsecureCertificates { false };
94 #if USE(GLIB)
95             Vector<std::pair<String, String>> certificates;
96 #endif
97 #if PLATFORM(COCOA)
98             Optional<bool> allowInsecureMediaCapture;
99             Optional<bool> suppressICECandidateFiltering;
100 #endif
101         };
102
103         virtual ~Client();
104         virtual bool remoteAutomationAllowed() const = 0;
105         virtual String browserName() const { return { }; }
106         virtual String browserVersion() const { return { }; }
107         virtual void requestAutomationSession(const String& sessionIdentifier, const SessionCapabilities&) = 0;
108     };
109
110     static void startDisabled();
111     static RemoteInspector& singleton();
112     friend class NeverDestroyed<RemoteInspector>;
113
114     void registerTarget(RemoteControllableTarget*);
115     void unregisterTarget(RemoteControllableTarget*);
116     void updateTarget(RemoteControllableTarget*);
117     void sendMessageToRemote(TargetID, const String& message);
118
119     RemoteInspector::Client* client() const { return m_client; }
120     void setClient(RemoteInspector::Client*);
121     void clientCapabilitiesDidChange();
122     Optional<RemoteInspector::Client::Capabilities> clientCapabilities() const { return m_clientCapabilities; }
123
124     void setupFailed(TargetID);
125     void setupCompleted(TargetID);
126     bool waitingForAutomaticInspection(TargetID);
127     void updateAutomaticInspectionCandidate(RemoteInspectionTarget*);
128
129     bool enabled() const { return m_enabled; }
130     bool hasActiveDebugSession() const { return m_hasActiveDebugSession; }
131
132     void start();
133     void stop();
134
135 #if PLATFORM(COCOA)
136     bool hasParentProcessInformation() const { return m_parentProcessIdentifier != 0; }
137     ProcessID parentProcessIdentifier() const { return m_parentProcessIdentifier; }
138     RetainPtr<CFDataRef> parentProcessAuditData() const { return m_parentProcessAuditData; }
139     void setParentProcessInformation(ProcessID, RetainPtr<CFDataRef> auditData);
140     void setParentProcessInfomationIsDelayed();
141 #endif
142
143     void updateTargetListing(TargetID);
144
145 #if USE(GLIB)
146     void requestAutomationSession(const char* sessionID, const Client::SessionCapabilities&);
147 #endif
148 #if USE(GLIB) || USE(INSPECTOR_SOCKET_SERVER)
149     void setup(TargetID);
150     void sendMessageToTarget(TargetID, const char* message);
151 #endif
152 #if USE(INSPECTOR_SOCKET_SERVER)
153     static void setConnectionIdentifier(PlatformSocketType);
154     static void setServerPort(uint16_t);
155 #endif
156
157 private:
158     RemoteInspector();
159
160     TargetID nextAvailableTargetIdentifier();
161
162     enum class StopSource { API, XPCMessage };
163     void stopInternal(StopSource);
164
165 #if PLATFORM(COCOA)
166     void setupXPCConnectionIfNeeded();
167 #endif
168 #if USE(GLIB)
169     void setupConnection(GRefPtr<GDBusConnection>&&);
170     static const GDBusInterfaceVTable s_interfaceVTable;
171
172     void receivedGetTargetListMessage();
173     void receivedSetupMessage(TargetID);
174     void receivedDataMessage(TargetID, const char* message);
175     void receivedCloseMessage(TargetID);
176     void receivedAutomationSessionRequestMessage(const char* sessionID);
177 #endif
178
179     TargetListing listingForTarget(const RemoteControllableTarget&) const;
180     TargetListing listingForInspectionTarget(const RemoteInspectionTarget&) const;
181     TargetListing listingForAutomationTarget(const RemoteAutomationTarget&) const;
182
183     bool updateTargetMap(RemoteControllableTarget*);
184
185     void pushListingsNow();
186     void pushListingsSoon();
187
188     void updateTargetListing(const RemoteControllableTarget&);
189
190     void updateHasActiveDebugSession();
191     void updateClientCapabilities();
192
193     void sendAutomaticInspectionCandidateMessage();
194
195 #if PLATFORM(COCOA)
196     void xpcConnectionReceivedMessage(RemoteInspectorXPCConnection*, NSString *messageName, NSDictionary *userInfo) override;
197     void xpcConnectionFailed(RemoteInspectorXPCConnection*) override;
198     void xpcConnectionUnhandledMessage(RemoteInspectorXPCConnection*, xpc_object_t) override;
199
200     void receivedSetupMessage(NSDictionary *userInfo);
201     void receivedDataMessage(NSDictionary *userInfo);
202     void receivedDidCloseMessage(NSDictionary *userInfo);
203     void receivedGetListingMessage(NSDictionary *userInfo);
204     void receivedIndicateMessage(NSDictionary *userInfo);
205     void receivedProxyApplicationSetupMessage(NSDictionary *userInfo);
206     void receivedConnectionDiedMessage(NSDictionary *userInfo);
207     void receivedAutomaticInspectionConfigurationMessage(NSDictionary *userInfo);
208     void receivedAutomaticInspectionRejectMessage(NSDictionary *userInfo);
209     void receivedAutomationSessionRequestMessage(NSDictionary *userInfo);
210 #endif
211 #if USE(INSPECTOR_SOCKET_SERVER)
212     HashMap<String, CallHandler>& dispatchMap() override;
213     void didClose(ConnectionID) override;
214
215     void sendWebInspectorEvent(const String&);
216
217     void receivedGetTargetListMessage(const Event&);
218     void receivedSetupMessage(const Event&);
219     void receivedDataMessage(const Event&);
220     void receivedCloseMessage(const Event&);
221 #endif
222     static bool startEnabled;
223
224     // Targets can be registered from any thread at any time.
225     // Any target can send messages over the XPC connection.
226     // So lock access to all maps and state as they can change
227     // from any thread.
228     Lock m_mutex;
229
230     HashMap<TargetID, RemoteControllableTarget*> m_targetMap;
231     HashMap<TargetID, RefPtr<RemoteConnectionToTarget>> m_targetConnectionMap;
232     HashMap<TargetID, TargetListing> m_targetListingMap;
233
234 #if PLATFORM(COCOA)
235     RefPtr<RemoteInspectorXPCConnection> m_relayConnection;
236 #endif
237 #if USE(GLIB)
238     GRefPtr<GDBusConnection> m_dbusConnection;
239     GRefPtr<GCancellable> m_cancellable;
240 #endif
241
242 #if USE(INSPECTOR_SOCKET_SERVER)
243     static PlatformSocketType s_connectionIdentifier;
244     static uint16_t s_serverPort;
245     Optional<ConnectionID> m_clientID;
246 #endif
247
248     RemoteInspector::Client* m_client { nullptr };
249     Optional<RemoteInspector::Client::Capabilities> m_clientCapabilities;
250
251 #if PLATFORM(COCOA)
252     dispatch_queue_t m_xpcQueue;
253 #endif
254     TargetID m_nextAvailableTargetIdentifier { 1 };
255     int m_notifyToken { 0 };
256     bool m_enabled { false };
257     bool m_hasActiveDebugSession { false };
258     bool m_pushScheduled { false };
259
260     ProcessID m_parentProcessIdentifier { 0 };
261 #if PLATFORM(COCOA)
262     RetainPtr<CFDataRef> m_parentProcessAuditData;
263 #endif
264     bool m_shouldSendParentProcessInformation { false };
265     bool m_automaticInspectionEnabled { false };
266     bool m_automaticInspectionPaused { false };
267     TargetID m_automaticInspectionCandidateTargetIdentifier { 0 };
268 };
269
270 } // namespace Inspector
271
272 #endif // ENABLE(REMOTE_INSPECTOR)