[WTF] Import std::optional reference implementation as WTF::Optional
[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 #import "RemoteInspectorXPCConnection.h"
31 #import <wtf/Forward.h>
32 #import <wtf/HashMap.h>
33 #import <wtf/Lock.h>
34 #import <wtf/RetainPtr.h>
35
36 OBJC_CLASS NSDictionary;
37 OBJC_CLASS NSString;
38
39 namespace Inspector {
40
41 class RemoteAutomationTarget;
42 class RemoteConnectionToTarget;
43 class RemoteControllableTarget;
44 class RemoteInspectionTarget;
45 class RemoteInspectorClient;
46
47 class JS_EXPORT_PRIVATE RemoteInspector final : public RemoteInspectorXPCConnection::Client {
48 public:
49     class Client {
50     public:
51         struct Capabilities {
52             bool remoteAutomationAllowed : 1;
53         };
54
55         virtual ~Client() { }
56         virtual bool remoteAutomationAllowed() const = 0;
57         virtual void requestAutomationSession(const String& sessionIdentifier) = 0;
58     };
59
60     static void startDisabled();
61     static RemoteInspector& singleton();
62     friend class NeverDestroyed<RemoteInspector>;
63
64     void registerTarget(RemoteControllableTarget*);
65     void unregisterTarget(RemoteControllableTarget*);
66     void updateTarget(RemoteControllableTarget*);
67     void sendMessageToRemote(unsigned targetIdentifier, const String& message);
68
69     void setRemoteInspectorClient(RemoteInspector::Client*);
70     void clientCapabilitiesDidChange();
71
72     void setupFailed(unsigned targetIdentifier);
73     void setupCompleted(unsigned targetIdentifier);
74     bool waitingForAutomaticInspection(unsigned targetIdentifier);
75     void updateAutomaticInspectionCandidate(RemoteInspectionTarget*);
76
77     bool enabled() const { return m_enabled; }
78     bool hasActiveDebugSession() const { return m_hasActiveDebugSession; }
79
80     void start();
81     void stop();
82
83     bool hasParentProcessInformation() const { return m_parentProcessIdentifier != 0; }
84     pid_t parentProcessIdentifier() const { return m_parentProcessIdentifier; }
85     RetainPtr<CFDataRef> parentProcessAuditData() const { return m_parentProcessAuditData; }
86     void setParentProcessInformation(pid_t, RetainPtr<CFDataRef> auditData);
87     void setParentProcessInfomationIsDelayed();
88
89 private:
90     RemoteInspector();
91
92     unsigned nextAvailableTargetIdentifier();
93
94     enum class StopSource { API, XPCMessage };
95     void stopInternal(StopSource);
96
97     void setupXPCConnectionIfNeeded();
98
99     RetainPtr<NSDictionary> listingForTarget(const RemoteControllableTarget&) const;
100     RetainPtr<NSDictionary> listingForInspectionTarget(const RemoteInspectionTarget&) const;
101     RetainPtr<NSDictionary> listingForAutomationTarget(const RemoteAutomationTarget&) const;
102     void pushListingsNow();
103     void pushListingsSoon();
104
105     void updateHasActiveDebugSession();
106     void updateClientCapabilities();
107
108     void sendAutomaticInspectionCandidateMessage();
109
110     void xpcConnectionReceivedMessage(RemoteInspectorXPCConnection*, NSString *messageName, NSDictionary *userInfo) override;
111     void xpcConnectionFailed(RemoteInspectorXPCConnection*) override;
112     void xpcConnectionUnhandledMessage(RemoteInspectorXPCConnection*, xpc_object_t) override;
113
114     void receivedSetupMessage(NSDictionary *userInfo);
115     void receivedDataMessage(NSDictionary *userInfo);
116     void receivedDidCloseMessage(NSDictionary *userInfo);
117     void receivedGetListingMessage(NSDictionary *userInfo);
118     void receivedIndicateMessage(NSDictionary *userInfo);
119     void receivedProxyApplicationSetupMessage(NSDictionary *userInfo);
120     void receivedConnectionDiedMessage(NSDictionary *userInfo);
121     void receivedAutomaticInspectionConfigurationMessage(NSDictionary *userInfo);
122     void receivedAutomaticInspectionRejectMessage(NSDictionary *userInfo);
123     void receivedAutomationSessionRequestMessage(NSDictionary *userInfo);
124
125     static bool startEnabled;
126
127     // Targets can be registered from any thread at any time.
128     // Any target can send messages over the XPC connection.
129     // So lock access to all maps and state as they can change
130     // from any thread.
131     Lock m_mutex;
132
133     HashMap<unsigned, RemoteControllableTarget*> m_targetMap;
134     HashMap<unsigned, RetainPtr<NSDictionary>> m_targetListingMap;
135     HashMap<unsigned, RefPtr<RemoteConnectionToTarget>> m_targetConnectionMap;
136
137     RefPtr<RemoteInspectorXPCConnection> m_relayConnection;
138
139     RemoteInspector::Client* m_client { nullptr };
140     std::optional<RemoteInspector::Client::Capabilities> m_clientCapabilities;
141
142     dispatch_queue_t m_xpcQueue;
143     unsigned m_nextAvailableTargetIdentifier { 1 };
144     int m_notifyToken { 0 };
145     bool m_enabled { false };
146     bool m_hasActiveDebugSession { false };
147     bool m_pushScheduled { false };
148
149     pid_t m_parentProcessIdentifier { 0 };
150     RetainPtr<CFDataRef> m_parentProcessAuditData;
151     bool m_shouldSendParentProcessInformation { false };
152     bool m_automaticInspectionEnabled { false };
153     bool m_automaticInspectionPaused { false };
154     unsigned m_automaticInspectionCandidateTargetIdentifier { 0 };
155 };
156
157 } // namespace Inspector
158
159 #endif // ENABLE(REMOTE_INSPECTOR)